Skip to content

Commit 2a6b252

Browse files
Add DBMS vs Application side parameterization
Signed-off-by: David A. Wheeler <[email protected]>
1 parent 595bb8c commit 2a6b252

File tree

1 file changed

+58
-2
lines changed

1 file changed

+58
-2
lines changed

secure_software_development_fundamentals.md

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2386,7 +2386,7 @@ SQL injection vulnerabilities are one of the most common and devastating vulnera
23862386

23872387
For our purposes, a *prepared statement* compiles the statement with the database system ahead-of-time so that a later request with specific data can be executed more efficiently. Preparing a statement with a database ahead-of-time can improve performance if the statement will be executed multiple times. Prepared statement APIs generally include support for parameterized statements, and many people (and APIs) use the terms "prepared statement" and "parameterized statement" as synonyms.
23882388

2389-
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 into a request.
2389+
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.
23902390

23912391
##### Advantages of parameterized/prepared statements
23922392

@@ -2488,6 +2488,62 @@ explained in the [PostgreSQL (Command Execution Functions) documentation](https:
24882488

24892489
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.
24902490

2491+
##### Subtle issues: DBMS (Server) side vs. Application (client) side
2492+
2493+
An important security issue is *where* the
2494+
parameters of a parameterized statement are processed.
2495+
There are two options, DBMS-side and application-side, and
2496+
DBMS-side is better from a security point of view.
2497+
2498+
From a security point-of-view it's best if the parameters of
2499+
parameterized statements are be processed directly
2500+
within the database management system (DBMS),
2501+
aka "DBMS-side" parameter processing.
2502+
This approach is often called "server-side" since many DBMSs use
2503+
client/server architectures connecting over a network.
2504+
There are many advantages to DBMS-side parameter processing.
2505+
The DBMS has the current information on escaping rules
2506+
(and can often use more efficient mechanisms than adding escape characters),
2507+
and it also has other information such as all relevant character
2508+
encodings.
2509+
Perhaps most importantly, the DBMS developers will typically have
2510+
security experts review this part of the DBMS system.
2511+
However, DBMS-side parameter processing often requires using special
2512+
effort, so many DBMS libraries use "application-side" parameter processing.
2513+
2514+
"Application-side" parameter processing occurs when the parameter escaping
2515+
occurs within a library *not* in the DBMS, but instead in the application's
2516+
processing space.
2517+
This is also called "client-side" parameter processing.
2518+
Application-side parameter processing systems generally are implemented
2519+
by directly inserting escape characters where they are needed.
2520+
Application-side parameter processing is often easier to implement, so
2521+
several DBMS libraries use this approach.
2522+
Application-side libraries are better than directly calling an escape
2523+
mechanism "by hand" since the escapes are automatically added.
2524+
2525+
Unfortunately, application-side parameter processing has a weakness:
2526+
the application side may interpret information differently than the DBMS.
2527+
This weakness can lead to vulnerabilities. For example:
2528+
2529+
1. The application-side library may be intended for a different
2530+
version of the DBMS. The DBMS may have a different list of characters
2531+
or situations that need to be escaped.
2532+
2. The application-side library may interpret multi-byte characters
2533+
differently or not escape multi-byte characters correctly for the
2534+
circumstance. (See
2535+
[Multibyte character exploits PHP/MySQL](https://security.stackexchange.com/questions/9908/multibyte-character-exploits-php-mysql).)
2536+
3. The application-side library may not correctly implement parameterization of
2537+
more complex data types (in particular arrays and
2538+
associative arrays/dictionaries).
2539+
This is especially a risk in languages that don't require static types
2540+
(compile-time knowledge of types), as it's much easier to get complex
2541+
types to a library that cannot correctly handle them.
2542+
2543+
It's often hard to determine if a library uses DBMS-side or
2544+
application-side parameterization, and in some circumstances
2545+
only an application-side approach is available.
2546+
Still, if you have a practical choice, prefer a DBMS-side implementation.
24912547

24922548
##### Stored Procedures
24932549

@@ -2576,7 +2632,7 @@ If all you want to do is call another program and pass it some parameters, try t
25762632

25772633
* In JavaScript Node.js, prefer using **shell=False** (the default) with **child_process.spawn()** or **child_process.execFile()** instead of using **shell=True** or **child_process.exec()**
25782634

2579-
In short: if you see code that concatenates strings for execution by a shell, and that concatenation includes untrusted input, be extremely concerned. While it is possible to do this securely, it is better avoided when you reasonably can.
2635+
In short: if you see code that concatenates strings (including formatted strings) for execution by a shell, and that concatenation includes untrusted input, be extremely concerned. While it is possible to do this securely, it is better avoided when you reasonably can.
25802636

25812637
If you must call a program through a shell, and also include some data that might be provided by an attacker, you need to use it securely. That is actually rather tricky. As always, *do not use a denylist*. There are many “lists of shell metacharacters” that are wrong because they miss some. So if you are sending data through a shell, you need to escape every character except for ones on an allowlist (characters you know are *not* metacharacters). Generally, A-Z, a-z, and 0-9 are not metacharacters, and after that, check very carefully. Make sure you quote everything as needed.
25822638

0 commit comments

Comments
 (0)