SQL text is built by concatenating or interpolating values
What it finds
A command whose text is put together from strings, as in new SqlCommand("... WHERE name = '" + name + "'"), runs whatever SQL the values contain once one of them comes from outside. Parameters keep values out of the SQL. The places checked are the text argument of new SqlCommand, OleDbCommand, NpgsqlCommand and the like, assignments to CommandText, and EF Core's FromSqlRaw and ExecuteSqlRaw.
From the rule's source, sql_from_strings.rs.
using System.Data.SqlClient; class Users{ const string TABLE = "users"; void Find(SqlConnection connection, string name, int id) { var a = new SqlCommand("SELECT * FROM users WHERE name = '" + name + "'", connection);tx0 reports: SQL text is built from strings; values in it can change the statement var b = new SqlCommand("SELECT * FROM " + TABLE, connection); a.CommandText = $"DELETE FROM users WHERE id = {id}";tx0 reports: SQL text is built from strings; values in it can change the statement b.CommandText = "SELECT 1"; }} Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/csharp:sql-from-strings" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/csharp:sql-from-strings] reason="..."tx0 explain tx0/csharp:sql-from-strings shows the same in the terminal. Found a false positive? Report it.