If I execute the following SQL Command I get a Fortify finding with SQL Injection (Notice the AddCommand method accepts a string)
internal void AddCommand()
{
string cmtTxt = "SELECT * from Users WHERE @ID = 1";
if (m_dbCon != null && !string.IsNullOrEmpty(cmdTxt))
{
m_dbCmd = new SqlCommand(cmdTxt, m_dbCon);
}
}
If I execute the following method the SQL Injection finding goes away. What's the difference between passing the SQL string in as a parameter vs. a hardcoded string?
internal void AddCommand()
{
if (m_dbCon != null)
{
m_dbCmd = new SqlCommand("SELECT * from Users WHERE @ID = 1", m_dbCon);
}
}
I build up the parameters like so:
internal void AddCmdParam(string param, string value)
{
if (m_dbCmd != null && !string.IsNullOrEmpty(param) && value != null && Utilities.ValidParameter(param))
{
m_dbCmd.Parameters.Clear();
m_dbCmd.Parameters.Add(param, SqlDbType.NVarChar, 100);
m_dbCmd.Parameters[param].Value = value;
}
}