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
+57-13Lines changed: 57 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2499,18 +2499,19 @@ From a security point-of-view it's best if the parameters of
2499
2499
parameterized statements are processed directly
2500
2500
within the database management system (DBMS),
2501
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.
2502
+
This approach is often called "server-side" since many DBMSs use a
2503
+
client/server architecture where the client connect over a network
2504
+
to a server-side DBMS.
2504
2505
There are many advantages to DBMS-side parameter processing.
2505
2506
The DBMS has the current information on escaping rules
2506
2507
(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.
2508
+
and it also has other information such the relevant character encodings
2509
+
and expected data types.
2509
2510
Perhaps most importantly, the DBMS developers will typically have
2510
2511
security experts review this part of the DBMS system.
2511
2512
However, DBMS-side parameter processing can require more effort to
2512
2513
within implement in DBMS libraries, so many DBMS libraries use
2513
-
"application-side" parameter processing.
2514
+
"application-side" parameter processing instead.
2514
2515
2515
2516
"Application-side" parameter processing occurs when the parameter escaping
2516
2517
occurs within a library *not* in the DBMS, but instead in the application's
@@ -2521,7 +2522,7 @@ by directly inserting escape characters where they are needed.
2521
2522
Application-side parameter processing is often easier to implement, so
2522
2523
several DBMS libraries use this approach.
2523
2524
Application-side libraries are better than directly calling an escape
2524
-
mechanism "by hand" since the escapes are automatically added.
2525
+
mechanism "by hand" on every use since the escapes are automatically added.
2525
2526
2526
2527
Unfortunately, application-side parameter processing has a weakness:
2527
2528
the application side may interpret information differently than the DBMS.
@@ -2532,19 +2533,58 @@ This weakness can lead to vulnerabilities. For example:
2532
2533
or situations that need to be escaped.
2533
2534
2. The application-side library may interpret multi-byte characters
2534
2535
differently or not escape multi-byte characters correctly for the
2535
-
circumstance. (See
2536
+
circumstance that actually exists on the DBMS side. (See
2536
2537
[Multibyte character exploits PHP/MySQL](https://security.stackexchange.com/questions/9908/multibyte-character-exploits-php-mysql).)
2537
-
3. The application-side library may not correctly implement parameterization of
2538
-
more complex data types (in particular arrays and
2539
-
associative arrays/dictionaries).
2538
+
3. If the application-side library implements parameterization of
2539
+
data types more complex than numbers and strings (such as arrays,
2540
+
objects, associative arrays, and/or dictionaries), then there
2541
+
is a significant risk of a vulnerability.
2542
+
The fundamental problem is that the application-side library isn't
2543
+
parsing the query language the same way that the DBMS would -
2544
+
it is doing simple text substitutions. So if the library implements this
2545
+
functionality, it must typically make *guesses* of what types are expected.
2546
+
For example, it may guess that associative arrays are only provided sent
2547
+
to the library when that is sensible in the parameterized SQL query.
2548
+
That guess, sadly, may be exploitable.
2540
2549
This is especially a risk in languages that don't require static types
2541
-
(compile-time knowledge of types), as it's much easier to get complex
2542
-
types to a library that cannot correctly handle them.
2550
+
(compile-time knowledge of types), as it's much easier to get unexpected
2551
+
complex types into a library that cannot always handle them securely.
2552
+
For example, the widely-used Node.js MySQL library
2553
+
[mysqljs/mysql](https://github.com/mysqljs/mysql)
2554
+
as of early 2022 is exploitable through its parameterized library
2555
+
if a JavaScript object can be sent as a parameter to it
2556
+
(see
2557
+
[Finding an Authorization Bypass on my Own Website](https://maxwelldulin.com/BlogPost?post=9185867776) by Maxwell Dulin (ꓘ)).
2558
+
2559
+
That last issue for application-side processing (that
2560
+
complex data types may not always be escaped properly)
2561
+
can be a *huge* challenge to solve:
2562
+
2563
+
1. The safe solution is to disable processing of complex types
2564
+
(types other than numbers and strings) by the library.
2565
+
This may be impractical if the application already depends on this,
2566
+
and there may not be a way to fully disable it.
2567
+
For example, mysqljs/mysql allows setting `stringifyObjects` to true
2568
+
when calling `mysql.createConnection`, but while this can help,
2569
+
this only disables
2570
+
escaping generic Objects - it does not
2571
+
disable other complex data types such as arrays.
2572
+
2. The general solution is to verify every type before calling the library.
2573
+
For example, require that all data expected to be strings must be strings.
2574
+
This is typically easy to do in a statically typed language
2575
+
if the language enforces the types - just declare the required type.
2576
+
This can take a lot of time to implement in languages that don't
2577
+
enforce static types, and there is also the
2578
+
risk of missing a check when creating or modifying the code.
2579
+
However, this approach is more flexible.
2580
+
(See ["Finding an unseen SQL Injection by bypassing escape functions in mysqljs/mysql"](https://flattsecurity.medium.com/finding-an-unseen-sql-injection-by-bypassing-escape-functions-in-mysqljs-mysql-90b27f6542b4) by Flatt Security Inc., 2022-02-21.)
2543
2581
2544
2582
It's often hard to determine if a library uses DBMS-side or
2545
2583
application-side parameterization, and in some circumstances
2546
2584
only an application-side approach is available.
2547
-
Still, if you have a practical choice, prefer a DBMS-side implementation.
2585
+
In some cases requesting a prepared statement forces the library to
2586
+
use DBMS-side processing, but don't assume it - check the documentation.
2587
+
If you have a practical choice, prefer a DBMS-side implementation.
2548
2588
2549
2589
##### Stored Procedures
2550
2590
@@ -5712,10 +5752,14 @@ Delaitre, Aurelien; Stivalet, Bertrand; Black, Paul E.; Okun, Vadim; Ribeiro, At
5712
5752
5713
5753
Di Paola, Stefano, and Arshan Dabirsiaghi. "Expression Language Injection", 2011-09-12, (<https://www.mindedsecurity.com/fileshare/ExpressionLanguageInjection.pdf>)
5714
5754
5755
+
Dulin, Maxwell (ꓘ), Finding an Authorization Bypass on my Own Website, 2022-03-03, (<https://maxwelldulin.com/BlogPost?post=9185867776>)
5756
+
5715
5757
ECMA, ECMA-262, 12th edition, June 2021, ECMAScript® 2021 Language Specification, “The Number Type” ([https://www.ecma-international.org/ecma-262/11.0/index.html#sec-ecmascript-language-types-number-type]((https://www.ecma-international.org/ecma-262/11.0/index.html#sec-ecmascript-language-types-number-type))
Flatt Security Inc,, "Finding an unseen SQL Injection by bypassing escape functions in mysqljs/mysql", 2022-02-21, (<https://flattsecurity.medium.com/finding-an-unseen-sql-injection-by-bypassing-escape-functions-in-mysqljs-mysql-90b27f6542b4>)
5762
+
5719
5763
Forum of Incident Response and Security Teams (FIRST), *FIRST Services Framework* ([https://www.first.org/standards/frameworks/](https://www.first.org/standards/frameworks/))
5720
5764
5721
5765
Forum of Incident Response and Security Teams (FIRST), *Product Security Incident Response Team (PSIRT) Services Framework* ([https://www.first.org/standards/frameworks/](https://www.first.org/standards/frameworks/))
0 commit comments