You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: secure_software_development_fundamentals.md
+61-18Lines changed: 61 additions & 18 deletions
Original file line number
Diff line number
Diff line change
@@ -2376,9 +2376,7 @@ This is false. Clearly, if you pick known *insecure* software, you will have a p
2376
2376
2377
2377
## Calling Other Programs: Injection and Filenames
2378
2378
2379
-
### SQL Injection
2380
-
2381
-
#### SQL Injection Vulnerability
2379
+
### SQL Injection Vulnerability
2382
2380
2383
2381

2384
2382
@@ -2428,7 +2426,17 @@ Again, we want to try to use an approach that is easy to use correctly - it need
2428
2426
2429
2427
For databases, there are well-known solutions that are far easier to use securely.
2430
2428
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
2432
2440
2433
2441
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.
2434
2442
@@ -2438,7 +2446,7 @@ For our purposes, a *prepared statement* compiles the statement with the databas
2438
2446
2439
2447
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.
2440
2448
2441
-
##### Advantages of parameterized/prepared statements
2449
+
#### Advantages of parameterized/prepared statements
2442
2450
2443
2451
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:
2444
2452
@@ -2448,7 +2456,7 @@ Most programming languages have at least one library that implements parameteriz
2448
2456
2449
2457
3. Many can handle variation in different SQL engines (which is important because different systems often have different syntax rules).
2450
2458
2451
-
##### Example: Prepared statements in Java
2459
+
#### Example: Prepared statements in Java
2452
2460
2453
2461
Here is an example of using prepared statements in Java
2454
2462
using its JDBC interface:
@@ -2483,7 +2491,7 @@ Of course, like any technique, if you use it wrongly then it won’t be secure.
2483
2491
2484
2492
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.
2485
2493
2486
-
##### Examples: Parameterized and Prepared Statements in some Other Languages
2494
+
#### Examples: Parameterized and Prepared Statements in some Other Languages
2487
2495
2488
2496
Parameterized and prepared statements are widely available, though the
2489
2497
APIs and placeholder syntax vary by programming language, library, and database.
@@ -2538,12 +2546,20 @@ explained in the [PostgreSQL (Command Execution Functions) documentation](https:
2538
2546
2539
2547
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.
2540
2548
2541
-
##### Subtle issues: DBMS (Server) side vs. Application (client) side
\>\>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
2542
2558
2543
-
An important security issue is *where* the
2559
+
An important yet subtle security issue when using
2560
+
SQL parameterized statements is *where* the
2544
2561
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.
2547
2563
2548
2564
From a security point-of-view it's best if the parameters of
This approach is often called "server-side" since many DBMSs use a
2553
2569
client/server architecture where the client connects over a network
2554
2570
to a server-side DBMS.
2571
+
2555
2572
There are many advantages to DBMS-side parameter processing.
2556
2573
The DBMS has the current information on escaping rules
2557
2574
(and can often use more efficient mechanisms than adding escape characters),
@@ -2590,8 +2607,9 @@ This weakness can lead to vulnerabilities. For example:
2590
2607
objects, associative arrays, and/or dictionaries), then there
2591
2608
is a significant risk of a vulnerability.
2592
2609
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
2595
2613
functionality, it must typically make *guesses* of what types are expected.
2596
2614
For example, it may guess that associative arrays are only sent
2597
2615
to the library when that is sensible in the parameterized SQL query.
@@ -2655,8 +2673,33 @@ only an application-side approach is available.
2655
2673
In some cases requesting a prepared statement forces the library to
2656
2674
use DBMS-side processing, but don't assume it - check the documentation.
2657
2675
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.
2658
2690
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
2660
2703
2661
2704
Many database systems support "stored procedures", that is,
2662
2705
procedures embedded in the database itself.
@@ -2674,7 +2717,7 @@ in stored procedures, see your library's documentation, the
2674
2717
[OWASP Query Parameterization Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Query_Parameterization_Cheat_Sheet.html#stored-procedure-examples), and the
In some situations parameterized statements (including
2680
2723
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
2710
2753
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
2711
2754
~~~~
2712
2755
2713
-
##### Other Approaches for Countering SQL Injection
2756
+
#### Other Approaches for Countering SQL Injection
2714
2757
2715
2758
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.
2716
2759
@@ -2720,9 +2763,9 @@ There are other approaches, of course. You can write your own escape code, but t
2720
2763
2721
2764
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.
2722
2765
2723
-
#### Quiz 3.2: SQL Injection
2766
+
#### Quiz: SQL Injection: Alternatives to Parameterized Statements
2724
2767
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.<<
0 commit comments