Skip to content

Commit f80e2f1

Browse files
Explain the data type SQLi bypass
This is hard to explain, but important. Signed-off-by: David A. Wheeler <[email protected]>
1 parent b3f27cd commit f80e2f1

File tree

1 file changed

+57
-13
lines changed

1 file changed

+57
-13
lines changed

secure_software_development_fundamentals.md

Lines changed: 57 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2499,18 +2499,19 @@ From a security point-of-view it's best if the parameters of
24992499
parameterized statements are processed directly
25002500
within the database management system (DBMS),
25012501
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.
25042505
There are many advantages to DBMS-side parameter processing.
25052506
The DBMS has the current information on escaping rules
25062507
(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.
25092510
Perhaps most importantly, the DBMS developers will typically have
25102511
security experts review this part of the DBMS system.
25112512
However, DBMS-side parameter processing can require more effort to
25122513
within implement in DBMS libraries, so many DBMS libraries use
2513-
"application-side" parameter processing.
2514+
"application-side" parameter processing instead.
25142515

25152516
"Application-side" parameter processing occurs when the parameter escaping
25162517
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.
25212522
Application-side parameter processing is often easier to implement, so
25222523
several DBMS libraries use this approach.
25232524
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.
25252526

25262527
Unfortunately, application-side parameter processing has a weakness:
25272528
the application side may interpret information differently than the DBMS.
@@ -2532,19 +2533,58 @@ This weakness can lead to vulnerabilities. For example:
25322533
or situations that need to be escaped.
25332534
2. The application-side library may interpret multi-byte characters
25342535
differently or not escape multi-byte characters correctly for the
2535-
circumstance. (See
2536+
circumstance that actually exists on the DBMS side. (See
25362537
[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.
25402549
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.)
25432581

25442582
It's often hard to determine if a library uses DBMS-side or
25452583
application-side parameterization, and in some circumstances
25462584
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.
25482588

25492589
##### Stored Procedures
25502590

@@ -5712,10 +5752,14 @@ Delaitre, Aurelien; Stivalet, Bertrand; Black, Paul E.; Okun, Vadim; Ribeiro, At
57125752

57135753
Di Paola, Stefano, and Arshan Dabirsiaghi. "Expression Language Injection", 2011-09-12, (<https://www.mindedsecurity.com/fileshare/ExpressionLanguageInjection.pdf>)
57145754

5755+
Dulin, Maxwell (ꓘ), Finding an Authorization Bypass on my Own Website, 2022-03-03, (<https://maxwelldulin.com/BlogPost?post=9185867776>)
5756+
57155757
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))
57165758

57175759
Enable Cross-Origin Resource Sharing ([https://enable-cors.org/](https://enable-cors.org/))
57185760

5761+
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+
57195763
Forum of Incident Response and Security Teams (FIRST), *FIRST Services Framework* ([https://www.first.org/standards/frameworks/](https://www.first.org/standards/frameworks/))
57205764

57215765
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

Comments
 (0)