|
| 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 important |
| 10 | + technique for preventing cross-site scripting attacks. Usually, this |
| 11 | + is done by escaping <code><</code>, <code>></code>, |
| 12 | + <code>&</code> and <code>"</code>. But the context in which |
| 13 | + the sanitized value is used decides which characters that actually |
| 14 | + need to be sanitized. |
| 15 | + |
| 16 | + </p> |
| 17 | + |
| 18 | + <p> |
| 19 | + |
| 20 | + As a consequence, some programs only sanitize |
| 21 | + <code><</code> and <code>></code> since those are the most |
| 22 | + common dangerous characters. The lack of sanitization for |
| 23 | + <code>"</code> is problematic when an incompletely sanitized |
| 24 | + value is used as an HTML attribute in a string that |
| 25 | + <strong>later</strong> is parsed as HTML. |
| 26 | + |
| 27 | + </p> |
| 28 | + |
| 29 | + </overview> |
| 30 | + |
| 31 | + <recommendation> |
| 32 | + |
| 33 | + Sanitize all relevant HTML meta-characters when constructing |
| 34 | + HTML dynamically, pay special attention to where the sanitized value is used. |
| 35 | + |
| 36 | + </recommendation> |
| 37 | + |
| 38 | + <example> |
| 39 | + |
| 40 | + <p> |
| 41 | + |
| 42 | + The following example code writes part of an HTTP request (which is |
| 43 | + controlled by the user) to an HTML attribute of the server response. |
| 44 | + |
| 45 | + The user-controlled value is, however, not sanitized for |
| 46 | + <code>"</code>. This leaves the website vulnerable to cross-site |
| 47 | + scripting since an attacker can use a string like <code>" |
| 48 | + onclick="alert(42)</code> to inject JavaScript code into the response. |
| 49 | + |
| 50 | + </p> |
| 51 | + <sample src="examples/IncompleteHtmlAttributeSanitization.js" /> |
| 52 | + |
| 53 | + |
| 54 | + <p> |
| 55 | + |
| 56 | + Sanitizing the user-controlled data for |
| 57 | + <code>"</code> prevents the vulnerability: |
| 58 | + |
| 59 | + </p> |
| 60 | + |
| 61 | + <sample src="examples/IncompleteHtmlAttributeSanitizationGood.js" /> |
| 62 | + |
| 63 | + </example> |
| 64 | + |
| 65 | + <references> |
| 66 | + <li> |
| 67 | + OWASP: |
| 68 | + <a href="https://cheatsheetseries.owasp.org/cheatsheets/DOM_based_XSS_Prevention_Cheat_Sheet.html">DOM based |
| 69 | + XSS Prevention Cheat Sheet</a>. |
| 70 | + </li> |
| 71 | + <li> |
| 72 | + OWASP: |
| 73 | + <a href="https://cheatsheetseries.owasp.org/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.html">XSS |
| 74 | + (Cross Site Scripting) Prevention Cheat Sheet</a>. |
| 75 | + </li> |
| 76 | + <li> |
| 77 | + OWASP |
| 78 | + <a href="https://www.owasp.org/index.php/Types_of_Cross-Site_Scripting">Types of Cross-Site |
| 79 | + Scripting</a>. |
| 80 | + </li> |
| 81 | + <li> |
| 82 | + Wikipedia: <a href="http://en.wikipedia.org/wiki/Cross-site_scripting">Cross-site scripting</a>. |
| 83 | + </li> |
| 84 | + </references> |
| 85 | + |
| 86 | +</qhelp> |
0 commit comments