liveBook · Manning

SQL injection

As explained in Building Web APIs with ASP.NET Core

SQL injection is a security vulnerability in which untrusted input changes the structure or meaning of a SQL statement.

SQL injection occurs when untrusted input changes a SQL statement’s structure or meaning. Building dynamic SQL through string interpolation increases this risk because input becomes part of the command itself. section 4.3.1

SQL parameters isolate values from SQL execution. ORMs such as EF Core can apply these protections automatically while generating the required database commands. section 4.3.1

Prefer these protections over ad hoc string handling:

  • Use SQL parameters when writing dynamic SQL. section 4.3.1
  • Use configured EF Core operations where appropriate; it generates SQL and applies anti-injection countermeasures. section 4.3.1
  • Treat manual escaping and centralized helper workarounds cautiously because they can become outdated or insecure. section 4.3.1
var notes = "this user has been disabled";

// ... other code

$"UPDATE users SET notes = "{notes}" WHERE id = 4;";

Compare unsafe string interpolation with parameterized database commands. section 4.3.1

SqlDataAdapter myCommand = new SqlDataAdapter(  
"UPDATE users SET notes = @notes", conn);  
SQLParameter parm = 
   myCommand.SelectCommand.Parameters.Add("@notes",
       SqlDbType.VarChar, 11);  
Parm.Value = notes;

Inspect a parameterized update that keeps the value separate from the SQL command. section 4.3.1

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Compares interpolation, parameters, stored procedures, escaping, and EF Core, but does not detail exploit payloads or database internals.
Examples
Uses a user-notes UPDATE statement, unsafe C# interpolation, parameterized commands, and an EF Core update.
What it assumes
Assumes basic familiarity with SQL UPDATE statements and C# while explaining parameters and ORM protection.
What you can do after
Apply it via SQL parameters or EF Core to keep input values separate from generated SQL commands.
How it connects
Connects SQL injection with dynamic SQL, SQL parameters, stored procedures, ORM, and EF Core.

Also explained in

Compare how 9 books explain SQL injection →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Web APIs with ASP.NET Core. The section links point to the parts of the book it draws on.