在我们的代码的sonarqube扫描中,我有许多违反csharpsquid的行为:S3649 - 用户提供的值应该在SQL语句中使用之前进行清理。我认为我的代码是合规的,是否有其他人可以解释为什么这被标记为不合规?
string connectionString = DatabaseContext.GetiXDataConnectionString();
string sql = "SELECT UserID FROM SystemUsers " +
"Where WindowsLogonName = @WindowsLogon and DomainName = @WindowsDomain and " +
"[Disabled] = 0";
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.Add(new SqlParameter("@WindowsLogon", windowsUsername));
cmd.Parameters.Add(new SqlParameter("@WindowsDomain", userDomain));
object queryResult = cmd.ExecuteScalar();
if (queryResult != null)
return queryResult.ToString();
}
}
答案 0 :(得分:1)
它抱怨,因为sql
变量不是常量。如果将代码更改为(通常不是坏事),问题就会消失:
const string sql = "..." + "..." + "...";
S3649是一个非常简单的规则,当执行的SQL不是常量字符串时会引发该规则。它远非真正的污点分析检查,但它可以捕获最简单和最明显的错误。