Skip to content

Commit 687d9c2

Browse files
Merge pull request #150 from ossf/split_sql
Split SQL injection unit
2 parents ec06964 + 463b089 commit 687d9c2

File tree

1 file changed

+61
-18
lines changed

1 file changed

+61
-18
lines changed

secure_software_development_fundamentals.md

Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2376,9 +2376,7 @@ This is false. Clearly, if you pick known *insecure* software, you will have a p
23762376

23772377
## Calling Other Programs: Injection and Filenames
23782378

2379-
### SQL Injection
2380-
2381-
#### SQL Injection Vulnerability
2379+
### SQL Injection Vulnerability
23822380

23832381
![image alt text](exploits_of_a_mom.png)
23842382

@@ -2428,7 +2426,17 @@ Again, we want to try to use an approach that is easy to use correctly - it need
24282426

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

2431-
#### SQL Injection Solutions
2429+
#### Quiz - SQL Injection Vulnerability
2430+
2431+
\>\>Select all the warning signs suggesting that a SQL injection is especially likely:<<
2432+
2433+
[x] A SQL statement is being created via string concatenation.
2434+
2435+
[x] At least one part of the SQL statement is data that may be from an attacker.
2436+
2437+
[x] The SQL statement is executed.
2438+
2439+
### SQL Injection: Parameterized Statements
24322440

24332441
SQL injection vulnerabilities are one of the most common and devastating vulnerabilities, especially in web applications. They are also easy to counter, once you know how to do it.
24342442

@@ -2438,7 +2446,7 @@ For our purposes, a *prepared statement* compiles the statement with the databas
24382446

24392447
For security, the key is to use an API with parameterized statements (including a prepared statement API) and ensure that every untrusted input is sent as a separate parameter. Make sure that you do *not* normally include untrusted input by concatenating untrusted data as a string (including a formatted string) into a request.
24402448

2441-
##### Advantages of parameterized/prepared statements
2449+
#### Advantages of parameterized/prepared statements
24422450

24432451
Most programming languages have at least one library that implements parameterized statements and/or prepared statements. Using parameterized statements, including by using prepared statements, has many advantages:
24442452

@@ -2448,7 +2456,7 @@ Most programming languages have at least one library that implements parameteriz
24482456

24492457
3. Many can handle variation in different SQL engines (which is important because different systems often have different syntax rules).
24502458

2451-
##### Example: Prepared statements in Java
2459+
#### Example: Prepared statements in Java
24522460

24532461
Here is an example of using prepared statements in Java
24542462
using its JDBC interface:
@@ -2483,7 +2491,7 @@ Of course, like any technique, if you use it wrongly then it won’t be secure.
24832491

24842492
This insecure program uses a prepared statement, but instead of correctly using “**?**” as a value placeholder (which will then be properly escaped), this code directly concatenates data into the query. Unless the data is properly escaped (and it almost certainly is not), this code can quickly lead to a serious vulnerability if this data can be controlled by an attacker.
24852493

2486-
##### Examples: Parameterized and Prepared Statements in some Other Languages
2494+
#### Examples: Parameterized and Prepared Statements in some Other Languages
24872495

24882496
Parameterized and prepared statements are widely available, though the
24892497
APIs and placeholder syntax vary by programming language, library, and database.
@@ -2538,12 +2546,20 @@ explained in the [PostgreSQL (Command Execution Functions) documentation](https:
25382546

25392547
The [OWASP Query Parameterization Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Query_Parameterization_Cheat_Sheet.html) and [Bobby Tables website](https://bobby-tables.com/) provide examples for a variety of ecosystems.
25402548

2541-
##### Subtle issues: DBMS (Server) side vs. Application (client) side
2549+
#### Quiz 3.2: SQL Injection: Parameterized Statements
2550+
2551+
\>\>Parameterized statements (including prepared statements) are a valuable countermeasure against SQL injection, but you have to use placeholders for every data value that might possibly be controllable by an attacker. True or False?<<
2552+
2553+
(x) True
2554+
2555+
( ) False
2556+
2557+
### SQL Injection: DBMS (Server) side vs. Application (client) side
25422558

2543-
An important security issue is *where* the
2559+
An important yet subtle security issue when using
2560+
SQL parameterized statements is *where* the
25442561
parameters of a parameterized statement are processed.
2545-
There are two options, DBMS-side and application-side, and
2546-
DBMS-side is better from a security point of view.
2562+
There are two options, DBMS-side and application-side.
25472563

25482564
From a security point-of-view it's best if the parameters of
25492565
parameterized statements are processed directly
@@ -2552,6 +2568,7 @@ aka "DBMS-side" parameter processing.
25522568
This approach is often called "server-side" since many DBMSs use a
25532569
client/server architecture where the client connects over a network
25542570
to a server-side DBMS.
2571+
25552572
There are many advantages to DBMS-side parameter processing.
25562573
The DBMS has the current information on escaping rules
25572574
(and can often use more efficient mechanisms than adding escape characters),
@@ -2590,8 +2607,9 @@ This weakness can lead to vulnerabilities. For example:
25902607
objects, associative arrays, and/or dictionaries), then there
25912608
is a significant risk of a vulnerability.
25922609
The fundamental problem is that the application-side library isn't
2593-
parsing the query language the same way that the DBMS would -
2594-
it is doing simple text substitutions. So if the library implements this
2610+
necessarily parsing the query language the same way that the DBMS would -
2611+
it is usually doing simple text substitutions.
2612+
So if the library implements this
25952613
functionality, it must typically make *guesses* of what types are expected.
25962614
For example, it may guess that associative arrays are only sent
25972615
to the library when that is sensible in the parameterized SQL query.
@@ -2655,8 +2673,33 @@ only an application-side approach is available.
26552673
In some cases requesting a prepared statement forces the library to
26562674
use DBMS-side processing, but don't assume it - check the documentation.
26572675
If you have a practical choice, prefer a DBMS-side implementation.
2676+
Otherwise, carefully validate input and data going to the library
2677+
to ensure they are the appropriate types.
2678+
2679+
This is a good example of a general kind of security issue,
2680+
namely, that different components may interpret the same input differently.
2681+
In some cases, an attacker can exploit this, by making their input
2682+
appear benign to one part while performing something malicious in another.
2683+
2684+
#### Quiz: SQL Injection: DBMS (Server) side vs. Application (client) side
2685+
2686+
\>\>Which of these is an advantage of libraries that implement application-side parameterization?<<
2687+
2688+
( ) The library necessarily has the current information on the
2689+
DBMS' current escaping rules for its particular version and configuration.
26582690

2659-
##### Stored Procedures
2691+
( ) The library has the information on this DBMS session's character encodings
2692+
and expected data types.
2693+
2694+
(x) An application-side library is often easier to implement.
2695+
2696+
### SQL Injection: Alternatives to Parameterized Statements
2697+
2698+
Database systems are widely used, so there are many ways to
2699+
interact with them.
2700+
Here we discuss some alternatives.
2701+
2702+
#### Stored Procedures
26602703

26612704
Many database systems support "stored procedures", that is,
26622705
procedures embedded in the database itself.
@@ -2674,7 +2717,7 @@ in stored procedures, see your library's documentation, the
26742717
[OWASP Query Parameterization Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Query_Parameterization_Cheat_Sheet.html#stored-procedure-examples), and the
26752718
[OWASP SQL Injection Prevention Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/SQL_Injection_Prevention_Cheat_Sheet.html)
26762719

2677-
##### When Parameterized Statements Won't Work
2720+
#### When Parameterized Statements Won't Work
26782721

26792722
In some situations parameterized statements (including
26802723
prepared statements) will *not* work.
@@ -2710,7 +2753,7 @@ For example, in Python, if you need to write to a user-provided table name, you
27102753
cur.execute(f"insert into {table_name}(d, ts) values (?, ?)", (today, now)) # This is safe because we know that table_name can only take trusted values from table_name_map
27112754
~~~~
27122755

2713-
##### Other Approaches for Countering SQL Injection
2756+
#### Other Approaches for Countering SQL Injection
27142757

27152758
Many programs use object-relational mapping (ORM). This is just a technique to automatically convert data in a relational database into an object in an object-oriented programming language and back; lots of libraries and frameworks will do this for you. This is fine, as long as the ORM is implemented using parameterized statements or something equivalent to them. In practice, any good ORM implementation will do so. So if you are using a respected ORM, you are already doing this. That said, it is common in systems that use ORMs to occasionally need to use SQL queries directly… and when you do, use parameterized statements or prepared statements.
27162759

@@ -2720,9 +2763,9 @@ There are other approaches, of course. You can write your own escape code, but t
27202763

27212764
In summary, properly using parameterized statement libraries makes it much easier to write secure code. In addition, they typically make code easier to read, automatically handle the variations between how databases escape things, and sometimes they are faster than doing metacharacter escapes yourself.
27222765

2723-
#### Quiz 3.2: SQL Injection
2766+
#### Quiz: SQL Injection: Alternatives to Parameterized Statements
27242767

2725-
\>\>Parameterized statements (including prepared statements) are a valuable countermeasure against SQL injection, but you have to use placeholders for every data value that might possibly be controllable by an attacker. True or False?<<
2768+
\>\>True or false: A good object-relational mapping (ORM) system should be implemented using parameterized statements or something equivalent to them.<<
27262769

27272770
(x) True
27282771

0 commit comments

Comments
 (0)