SonarQube 报告 SQL 对存储过程名称的注入

SonarQube reports SQL injection on stored procedure name

我已经开始使用SonarQube进行静态分析了。它向我报告了很多 SQL 注入漏洞,但看起来是 false positive 发现的。

假设数据库需要 运行 过程。过程名称取自配置。

SonarQube 正在报告:

Make sure to sanitize the parameters of this SQL command.

示例代码:

using (SqlCommand cmd = new SqlCommand(procName, Connection))
{
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add(new SqlParameter("@param", SqlDbType.NVarChar, 32)).Value = record;
    using (SqlDataReader dr = cmd.ExecuteReader())
    {
    }
}

是否可以仅在过程名称上进行 sql 注入?名称需要清理吗?

我承认 S3649 的实现不是最聪明的,如果你将 non-constant 字符串传递给 CommandText [= SqlCommand 的 27=] 或相应的 ctor 参数。

如果您确定 CommandText 的值不是来自可能被利用的来源,例如例如查询字符串参数,处理这个特定问题的最佳方法是在 SonarQube 中将其标记为 Won't Fix。如果您还在该项目的连接模式下使用 SonarLint,它将自动抑制问题出现在 IDE.

另一方面,如果该值可能来自可利用的来源,例如查询字符串参数、请求 body、cookie、header 等,则设置 CommandType = StoredProcedure 可能仍然不足以阻止攻击者在您的数据库上执行与您预期不同的存储过程......在这种情况下,如果您为您拥有的存储过程创建单独的包装方法可能会更好,从而防止潜在的攻击者从选择不同的 SP 执行。