
Escaping Quotes Won't Stop SQL Injection: Parameterized Queries Will
- كاتب موثّق
- OWASP Top 10 2025
محتويات المقال
O'Brien works, and ' OR '1'='1 matches nothing. It gets merged. A week later, a curious customer changes the category in the URL to 0 OR 1=1, and the page lists every product in the table, including a draft price list that was never published. There isn't a single quote in that input.
The common misconception
' OR '1'='1, is made of quotes, and escaping really does defuse it inside a string literal: in our test, the condition became Name = ''' OR ''1''=''1' and matched nothing. Escape functions have been around for a long time, and OWASP's own Top 10 still lists escaping as a fallback: "For any residual dynamic queries, escape special characters using the specific escape syntax for that interpreter." So a team that escapes everything feels covered. The problem is everything that isn't a quoted string.
What actually happens
Injection is code and data sharing one string
Escaping only protects quoted strings
CategoryId = {input} became CategoryId = 0 OR 1=1, and because AND binds tighter than OR, the filter on published products disappeared. Structure is worse: the A05:2025 Injection page warns that "SQL structures such as table names, column names, and so on cannot be escaped." And escaping isn't even stable for strings: OWASP notes that it "depends on the SQL dialect and configuration", which is why it calls escaping "STRONGLY DISCOURAGED" as a substitute for parameters.
Escaped data comes back unescaped
O'Brien with its real quote, and the next piece of code that reads it back and glues it into another query is working with raw input again. That's why OWASP says its controls apply "regardless of whether data comes from an HTTP request, another service, or previously stored data."
Parameters keep values out of the SQL text
Escaped vs parameterized, on a real database
using Microsoft.Data.SqlClient;public static class ProductQueries{ static string Escape(string s) => s.Replace("'", "''"); public static SqlCommand ByCategory(SqlConnection db, string categoryId) => new($""" SELECT Name, Price FROM Products WHERE IsPublished = 1 AND CategoryId = {Escape(categoryId)} """, db);}using System.Data;using Microsoft.Data.SqlClient;public static class ProductQueries{ public static SqlCommand ByCategory(SqlConnection db, int categoryId) { var cmd = new SqlCommand(""" SELECT Name, Price FROM Products WHERE IsPublished = 1 AND CategoryId = @categoryId """, db); cmd.Parameters.Add("@categoryId", SqlDbType.Int).Value = categoryId; return cmd; }}using Microsoft.EntityFrameworkCore;public static class ProductQueries{ // FromSql turns {categoryId} into a parameter; FromSqlRaw would not public static IQueryable<Product> ByCategory(ShopDb db, int categoryId) => db.Products.FromSql($""" SELECT * FROM Products WHERE IsPublished = 1 AND CategoryId = {categoryId} """);}-- Escaped, categoryId = "0 OR 1=1": 4 rows, the draft includedSELECT Name, Price FROM ProductsWHERE IsPublished = 1 AND CategoryId = 0 OR 1=1-- Parameterized, categoryId = 2: 2 rows(@categoryId int)SELECT Name, Price FROM ProductsWHERE IsPublished = 1 AND CategoryId = @categoryIdint. An int can't even hold 0 OR 1=1, so that request fails at parsing, before any SQL is built. The EF Core version produced (@p0 int)SELECT * FROM Products ...: Microsoft documents that FromSql and FromSqlInterpolated "always integrate parameter data as a separate SQL parameter", while FromSqlRaw "can be vulnerable to SQL injection attacks, if improperly used."
ASC/DESC) | ||
How to apply it in your project
Find every query built from strings Search for SqlCommandandExecuteSqlRaw/FromSqlRawcalls that use+or$", and for helpers like.Replace("'", "''"). Each one is a place where input can become code.Bind every value as a typed parameter Use SqlParameterwith aSqlDbType, EF Core LINQ, orFromSql/ExecuteSqlwith interpolation. Prefer real types such asintandDateTimeover strings.Turn structure into allow-lists Column names, table names and sort directions come from a fixed map in your code. Unknown values are rejected, not cleaned. Check inside stored procedures OWASP warns that binding arguments "protects the call, not dynamic SQL constructed inside the procedure." Inside T-SQL, pass values to sp_executesqlas parameters too.Delete the escape helper Once the queries are parameterized, remove the helper, so nobody reuses it for the next query.
Key takeaways
SQL injection happens when input becomes part of the SQL syntax, not just when it contains quotes. Escaping protects only quoted strings; numbers, identifiers and stored data slip past it. Parameters send SQL text and values separately, so the database never parses input as code. In EF Core, FromSqlparameterizes interpolated values andFromSqlRawdoesn't.Map column names and sort directions to an allow-list, because parameters can't hold structure.
التعليقات (0)