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
+14-8Lines changed: 14 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2597,20 +2597,26 @@ can be confusing, so an example may help.
2597
2597
In the Node.js mysqljs/mysql library,
2598
2598
imagine that an attacker manages to provide
2599
2599
the JavaScript *object* `{password = 1}` as the password parameter
2600
-
and it's used in the SQL query
2601
-
`SELECT * FROM accounts WHERE username = ? AND password = ?`.
2600
+
(this is not just a string, but an actual JavaScript object).
2601
+
Now imagine that this object is used in the SQL query
2602
+
<tt>SELECT * FROM accounts WHERE username = ? AND password = ?</tt>
2603
+
(note that this is parameterized).
2602
2604
The library will internally expand the expression after `AND`
2603
-
into `password = ``password`` = 1`.
2604
-
The MYSQL DBMS will interpret `password = ``password``` as 1 (true),
2605
-
and then determine that `1 = 1` is true.
2605
+
into <tt>password = `password` = 1</tt> because the library does simple
2606
+
text replacement of the second `?`, without noticing that a JavaScript object
2607
+
doesn't make sense in the context of this query (a string or number would
2608
+
be expected here).
2609
+
The MYSQL DBMS will interpret <tt>password = `password`</tt>
2610
+
as 1 (true), and then determine that `1 = 1` is true.
2606
2611
The result: this expression will *always* be true.
2607
2612
This incorrect escaping of a complex data type
2608
2613
is enough to completely bypass authentication in some situations.
2609
2614
2610
2615
Unfortunately, this last issue can be a challenge to solve:
2611
2616
2612
2617
1. The safe solution is to make sure that complex data types
2613
-
(types other than numbers and strings) are not expanded by the library
2618
+
(types other than numbers and strings) are not expanded by
2619
+
application-side libraries
2614
2620
unless the developer specifically marks them as allowed.
2615
2621
This may be impractical if the application already depends on this,
2616
2622
and the library might not provide a way to fully disable the functionality.
@@ -3207,7 +3213,7 @@ In that case, where possible, use libraries *already designed* to allow only wha
3207
3213
3208
3214
We have focused on escaping HTML, because that is the biggest problem in web applications. But HTML can embed other kinds of data, and of those, perhaps the most common are URLs.
3209
3215
3210
-
Embedded URLs must also be escaped, and the rules for escaping URLs are different. The URL syntax is generally **scheme:[//authority]path[?query][#fragment]**. For example, in the URL **<https://www.linuxfoundation.org/about/>**, the scheme is “**https**”, authority “<b>www.linuxfoundation.org</b>”, path is “**/about/**”, and this example has no query or fragment part. Sometimes you need special characters in the path, query, or fragment. The conventional way to escape those parts of the URLs is to first ensure the data is encoded with UTF-8, and escape as “**%hh**” (where **hh** is the hexadecimal representation) all bytes except for “safe” bytes, which are typically **A-Z**, **a-z**, **0-9**, “**.**”, “**-**”, “*****”, and “**_**”. The Java routine **java.net.URLEncoder.encode()** turns all spaces into “**+**” instead of “**%20**”; both the “**+**” and “**%20**” conventions are in wide use.
3216
+
Embedded URLs must also be escaped, and the rules for escaping URLs are different. The URL syntax is generally **scheme:[//authority]path[?query] [#fragment]**. For example, in the URL **<https://www.linuxfoundation.org/about/>**, the scheme is “**https**”, authority “<b>www.linuxfoundation.org</b>”, path is “**/about/**”, and this example has no query or fragment part. Sometimes you need special characters in the path, query, or fragment. The conventional way to escape those parts of the URLs is to first ensure the data is encoded with UTF-8, and escape as “**%hh**” (where **hh** is the hexadecimal representation) all bytes except for “safe” bytes, which are typically **A-Z**, **a-z**, **0-9**, “**.**”, “**-**”, “*****”, and “**_**”. The Java routine **java.net.URLEncoder.encode()** turns all spaces into “**+**” instead of “**%20**”; both the “**+**” and “**%20**” conventions are in wide use.
3211
3217
3212
3218
#### XSS Alternatives
3213
3219
@@ -3497,7 +3503,7 @@ This is true! Yes, this is a weird and subtle point. There is reason to hope tha
3497
3503
3498
3504
A Uniform Resource Locator (URL) is a way to refer to a specific web resource by location. Technically, a URL is a specific type of Uniform Resource Identifier (URI), but for our purposes we will use the terms interchangeably. As specified in [IETF RFC 3986](https://tools.ietf.org/html/rfc3986), a generic URI has this syntax:
0 commit comments