SQL text is built by concatenating or interpolating values

tx0/csharp:sql-from-stringsrecommendedwarningsecurity/injectiondepth fastsecurity, cwe-89

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.

Example from the rule's tests
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";    }}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

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:

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.