|
| 1 | +<!DOCTYPE qhelp PUBLIC |
| 2 | +"-//Semmle//qhelp//EN" |
| 3 | +"qhelp.dtd"> |
| 4 | +<qhelp> |
| 5 | + |
| 6 | + <overview> |
| 7 | + <p> |
| 8 | + |
| 9 | + Sanitizing untrusted input for HTML meta-characters is an |
| 10 | + important technique for preventing cross-site scripting attacks. But |
| 11 | + even a sanitized input can be dangerous to use if it is modified |
| 12 | + further before it is parsed as HTML. |
| 13 | + |
| 14 | + A seemingly innocent transformation that expands a |
| 15 | + self-closing HTML tag from <code>>div attr="{sanitized}"/<</code> |
| 16 | + to <code>>div attr="{sanitized}"></div></code> may |
| 17 | + in fact cause cross-site scripting vulnerabilities. |
| 18 | + |
| 19 | + </p> |
| 20 | + |
| 21 | + </overview> |
| 22 | + |
| 23 | + <recommendation> |
| 24 | + <p> |
| 25 | + |
| 26 | + Use a (well-tested) sanitization library if at all |
| 27 | + possible, and avoid modifying sanitized values further before parsing |
| 28 | + them as HTML. |
| 29 | + |
| 30 | + </p> |
| 31 | + </recommendation> |
| 32 | + |
| 33 | + <example> |
| 34 | + |
| 35 | + <p> |
| 36 | + |
| 37 | + The following function transforms a self-closing HTML tag |
| 38 | + to a pair of open/close tags. It does so for all non-<code>img</code> |
| 39 | + and non-<code>area</code> tags using a regular expression with two |
| 40 | + capture groups. The first capture group corresponds to the name of the |
| 41 | + tag, and the second capture group corresponds to the content of |
| 42 | + the tag. |
| 43 | + |
| 44 | + </p> |
| 45 | + |
| 46 | + <sample src="examples/UnsafeHtmlExpansion.js" /> |
| 47 | + |
| 48 | + <p> |
| 49 | + |
| 50 | + While it is generally known regular expressions are |
| 51 | + ill-suited for parsing HTML, variants of this particular transformation |
| 52 | + pattern has long been considered safe. |
| 53 | + |
| 54 | + </p> |
| 55 | + |
| 56 | + <p> |
| 57 | + |
| 58 | + However, the function is not safe. As an example, consider |
| 59 | + the following string which does not result in an alert when it is |
| 60 | + treated as HTML: |
| 61 | + |
| 62 | + </p> |
| 63 | + |
| 64 | + <sample src="examples/UnsafeHtmlExpansion-original.html" /> |
| 65 | + |
| 66 | + <p> |
| 67 | + |
| 68 | + When the above function transforms the string, it becomes |
| 69 | + a string that results in an alert when it is treated as HTML by a |
| 70 | + modern browser: |
| 71 | + |
| 72 | + </p> |
| 73 | + |
| 74 | + <sample src="examples/UnsafeHtmlExpansion-transformed.html" /> |
| 75 | + |
| 76 | + </example> |
| 77 | + |
| 78 | + <references> |
| 79 | + <li>jQuery: |
| 80 | + <a href="https://blog.jquery.com/2020/04/10/jquery-3-5-0-released/">Security fixes in jQuery 3.5.0</a> |
| 81 | + </li> |
| 82 | + <li> |
| 83 | + OWASP: |
| 84 | + <a href="https://cheatsheetseries.owasp.org/cheatsheets/DOM_based_XSS_Prevention_Cheat_Sheet.html">DOM based |
| 85 | + XSS Prevention Cheat Sheet</a>. |
| 86 | + </li> |
| 87 | + <li> |
| 88 | + OWASP: |
| 89 | + <a href="https://cheatsheetseries.owasp.org/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.html">XSS |
| 90 | + (Cross Site Scripting) Prevention Cheat Sheet</a>. |
| 91 | + </li> |
| 92 | + <li> |
| 93 | + OWASP |
| 94 | + <a href="https://owasp.org/www-community/Types_of_Cross-Site_Scripting">Types of Cross-Site</a>. |
| 95 | + </li> |
| 96 | + <li> |
| 97 | + Wikipedia: <a href="http://en.wikipedia.org/wiki/Cross-site_scripting">Cross-site scripting</a>. |
| 98 | + </li> |
| 99 | + </references> |
| 100 | + |
| 101 | +</qhelp> |
0 commit comments