Skip to content

Commit b81cf5b

Browse files
A few small tweaks
In the process of getting this text ready to transfer to the "real" course I found these nits in the SQL injection section, so let's get them fixed now. Signed-off-by: David A. Wheeler <[email protected]>
1 parent 5d7e0ea commit b81cf5b

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

secure_software_development_fundamentals.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2372,7 +2372,7 @@ The intent is clear; if **search_lastname** has the value **Fred**, then the dat
23722372

23732373
There are many ways to trigger SQL injection attacks; attackers can insert single quotes (used to surround constant character data), semicolons (which act as command separators), “**--**” which is a comment token, and so on. This is not a complete list; different database systems interpret different characters differently. For example, double quotes are often metacharacters, but with different meanings. Even different versions of the *same* database system, or different configurations, can cause changes to how the characters are interpreted. We already know we should not create a list of “bad” characters, because that is a denylist. We could create an allowlist of characters we know are not metacharacters and then escape the rest, but this process is hard to do correctly for SQL.
23742374

2375-
Don't concatenate strings to create a DBMS query, because that is insecure by default. That includes using format strings, string interpolations, string templates, and all other mechanism that simply concatenate text. For example, the same vulnerabilities happen if you use Python formatted string literals (f-strings such as <tt>f'{year}-{month}'</tt>), Python's `.format` method, JavaScript's template strings (<tt>`${year}-${month}`</tt>), PHP's string interpolations (<tt>"${year}-${month}"</tt>), Ruby string interpolation (<tt>"#{year}-#{month}"</tt>), Go (string) templates, or any other string-based template or formatting language, Remember, we want to try to use a routine that is easy to use securely, and all of these are dangerous by default when used to create commands like SQL comamnds.
2375+
Don't concatenate strings to create a DBMS query, because that is insecure by default. That includes using format strings, string interpolations, string templates, and all other mechanism that simply concatenate text. For example, the same vulnerabilities happen if you use Python formatted string literals (f-strings such as <tt>f'{year}-{month}'</tt>), Python's `.format` method, JavaScript's template strings (<tt>`${year}-${month}`</tt>), PHP's string interpolations (<tt>"${year}-${month}"</tt>), Ruby string interpolation (<tt>"#{year}-#{month}"</tt>), Go (string) templates, or any other string-based template or formatting language. Remember, we want to try to use a routine that is easy to use securely, and all of these are dangerous by default when used to create commands like SQL commands.
23762376

23772377
Many developers try to fix this in an unwise way by calling an escape routine on every value, e.g., like this:
23782378

@@ -2393,7 +2393,7 @@ but experience shows that the mistake *will* happen.
23932393

23942394
🔔 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’)*.
23952395

2396-
Remember, we want to try to use an approach that is easy to use correctly - it needs to be secure by default.
2396+
Again, we want to try to use an approach that is easy to use correctly - it needs to be secure by default.
23972397

23982398
For databases, there are well-known solutions that are far easier to use securely.
23992399

@@ -2459,7 +2459,7 @@ APIs and placeholder syntax vary by programming language, library, and database.
24592459
Here we'll see some examples.
24602460

24612461
In Python there are several libraries that interface to databases.
2462-
Many of them implement the Python Database API Specification v2.0,
2462+
Many of them implement the Python Database API Specification v2.0
24632463
([PEP 249](https://peps.python.org/pep-0249/)),
24642464
whose `execute` and `executemany` methods implement parameterized statements.
24652465
The library's placeholder syntax is reported by its `paramstyle` attribute.
@@ -2519,7 +2519,7 @@ parameterized statements are processed directly
25192519
within the database management system (DBMS),
25202520
aka "DBMS-side" parameter processing.
25212521
This approach is often called "server-side" since many DBMSs use a
2522-
client/server architecture where the client connect over a network
2522+
client/server architecture where the client connects over a network
25232523
to a server-side DBMS.
25242524
There are many advantages to DBMS-side parameter processing.
25252525
The DBMS has the current information on escaping rules
@@ -2529,6 +2529,7 @@ and expected data types.
25292529
Perhaps most importantly, the DBMS developers will typically have
25302530
security experts review this part of the DBMS system.
25312531
However, DBMS-side parameter processing can require more effort to
2532+
implement, so some libraries use
25322533
"application-side" parameter processing instead.
25332534

25342535
"Application-side" parameter processing occurs when the parameter escaping
@@ -2569,8 +2570,8 @@ This weakness can lead to vulnerabilities. For example:
25692570
complex types into a library that cannot always handle them securely.
25702571
For example, the widely-used Node.js MySQL library
25712572
[mysqljs/mysql](https://github.com/mysqljs/mysql)
2572-
as of early 2022 is exploitable through its parameterized library
2573-
if a JavaScript object can be sent as a parameter to it.
2573+
as of early 2022 is often exploitable through its parameterized library
2574+
*if* a JavaScript object can be sent as a parameter to it
25742575
(see
25752576
[Finding an Authorization Bypass on my Own Website](https://maxwelldulin.com/BlogPost?post=9185867776) by Maxwell Dulin (ꓘ)).
25762577

@@ -2586,7 +2587,7 @@ The library will internally expand the expression after `AND`
25862587
into `password = ``password`` = 1`.
25872588
The MYSQL DBMS will interpret `password = ``password``` as 1 (true),
25882589
and then determine that `1 = 1` is true.
2589-
Thus result: this expression will *always* true.
2590+
The result: this expression will *always* be true.
25902591
This incorrect escaping of a complex data type
25912592
is enough to completely bypass authentication in some situations.
25922593

0 commit comments

Comments
 (0)