Why it matters
The caller controls part of the statement, so they can change what it selects, updates or deletes.
How to fix it
Keep the statement text constant and add the value as a parameter (SqlCommand.Parameters.AddWithValue, Dapper's anonymous parameter object, or EF Core's FromSqlInterpolated / FromSql, which parameterise the interpolated values).
Example
The rule's own test fixtures, verbatim: the test suite requires the rule to report the first and not the second.
using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.SqlClient;
public class OrdersController : Controller
{
private readonly SqlConnection _conn;
public IActionResult Index(string status)
{
var cmd = new SqlCommand("SELECT id FROM orders WHERE status = '" + status + "'", _conn);
return Ok(cmd.ExecuteScalar());
}
}using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.SqlClient;
public class OrdersController : Controller
{
private readonly SqlConnection _conn;
public IActionResult Index(string status)
{
var cmd = new SqlCommand("SELECT id FROM orders WHERE status = @status", _conn);
cmd.Parameters.AddWithValue("@status", status);
return Ok(cmd.ExecuteScalar());
}
}What TrustIvan reads
Runs on repository scans of C# source once static analysis is turned on (SAST_RULES and the separately built analyzer image); it is off by default.
Weakness classes and references
Related checks
- Command injection from request input (C#)
C#Severity: High
trustivan-csharp-taint-command-injection - Path traversal from request input (C#)
C#Severity: High
trustivan-csharp-taint-path-traversal - SSRF from request input (C#)
C#Severity: High
trustivan-csharp-taint-ssrf - XSS from request input (C#)
C#Severity: High
trustivan-csharp-taint-xss - Deserialization from request input (C#)
C#Severity: High
trustivan-csharp-taint-deserialization - Open redirect from request input (C#)
C#Severity: Medium
trustivan-csharp-taint-open-redirect
Terms: SAST (static application security testing), Taint analysis, CWE (Common Weakness Enumeration), OWASP Top 10
All static analysis (SAST) checks · The whole catalogue · How this works in TrustIvan · Documentation