Skip to content

Commit f4486d1

Browse files
Expressly show why calling escape routines every time is bad
Signed-off-by: David A. Wheeler <[email protected]>
1 parent f80e2f1 commit f4486d1

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

secure_software_development_fundamentals.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2374,8 +2374,25 @@ There are many ways to trigger SQL injection attacks; attackers can insert singl
23742374

23752375
If you are using a database, you shouldn’t ever be concatenating strings to create a query, because that is easy to get wrong. That includes using format strings and other mechanisms that concatenate simple text. Remember, we want to try to use a routine that is easy to use correctly.
23762376

2377+
Many developers try to fix this in an unwise way by calling an escape routine on every value, e.g., like this:
2378+
2379+
~~~~java
2380+
String QueryString = "select * from authors where lastname = ' " +
2381+
sql_escape(search_lastname) + " '; "; // BAD IDEA
2382+
~~~~
2383+
2384+
This approach (calling an escape routine every time you use untrusted input)
2385+
has a fundamental flaw: the *default* is insecure.
2386+
If an escape routine must be called every time untrusted data is used,
2387+
and there are many uses of untrusted data,
2388+
eventually someone will forget to call the escape.
2389+
The mistake can happen at the beginning, or later when the code is modified,
2390+
but experience shows that the mistake *will* happen.
2391+
23772392
🔔 SQL injection is a special case of injection attacks, and we have already noted that injection attacks are so common and dangerous that they are 2017 OWASP Top 10 #1. SQL injection specifically is such a common cause of security vulnerabilities that just SQL injection is 2021 CWE Top 25 #6 and 2019 CWE Top 25 #6. SQL injection is also identified as [CWE-89](https://cwe.mitre.org/data/definitions/89.html), *Improper Neutralization of Special Elements used in an SQL Command (‘SQL Injection’)*.
23782393

2394+
Remember, we want to try to use an approach that is easy to use correctly - it needs to be secure by default.
2395+
23792396
For databases, there are well-known solutions that are far easier to use securely.
23802397

23812398
#### SQL Injection Solutions

0 commit comments

Comments
 (0)