|
| 1 | +<!DOCTYPE qhelp PUBLIC |
| 2 | + "-//Semmle//qhelp//EN" |
| 3 | + "qhelp.dtd"> |
| 4 | +<qhelp> |
| 5 | + |
| 6 | +<overview> |
| 7 | +<p> |
| 8 | +Writing text from a webpage to the same webpage without properly sanitizing the |
| 9 | +input first, might allow for a cross-site scripting vulnerability. |
| 10 | +</p> |
| 11 | +<p> |
| 12 | +A webpage with this vulnerability unescapes an otherwise sanitized text, |
| 13 | +and thereby allows an attacker to use sanitized text in the DOM to perform a |
| 14 | +cross-site scripting attack. |
| 15 | +</p> |
| 16 | +</overview> |
| 17 | + |
| 18 | +<recommendation> |
| 19 | +<p> |
| 20 | +To guard against cross-site scripting, consider using contextual output encoding/escaping before |
| 21 | +writing text to the page, or one of the other solutions that are mentioned in the references. |
| 22 | +</p> |
| 23 | +</recommendation> |
| 24 | + |
| 25 | +<example> |
| 26 | +<p> |
| 27 | +The following example shows a webpage using a <code>data-target</code> attribute |
| 28 | +to select and manipulate a DOM element using the JQuery library. In the example, the |
| 29 | +<code>data-target</code> attribute is read into the <code>target</code> variable, and the |
| 30 | +<code>$</code> function is then supposed to use the <code>target</code> variable as a CSS |
| 31 | +selector to determine which element should be manipulated. |
| 32 | +</p> |
| 33 | +<sample src="examples/XssThroughDom.js" /> |
| 34 | +<p> |
| 35 | +However, if an attacker can control the <code>data-target</code> attribute, |
| 36 | +then the value of <code>target</code> can be used to cause the <code>$</code> function |
| 37 | +to execute arbitary JavaScript. |
| 38 | +</p> |
| 39 | +<p> |
| 40 | +The above vulnerability can be fixed by using <code>$.find</code> instead of <code>$</code>. |
| 41 | +The <code>$.find</code> function will only interpret <code>target</code> as a CSS selector |
| 42 | +and never as HTML, thereby preventing an XSS attack. |
| 43 | +</p> |
| 44 | +<sample src="examples/XssThroughDomFixed.js" /> |
| 45 | +</example> |
| 46 | + |
| 47 | +<references> |
| 48 | +<li> |
| 49 | +OWASP: |
| 50 | +<a href="https://cheatsheetseries.owasp.org/cheatsheets/DOM_based_XSS_Prevention_Cheat_Sheet.html">DOM based |
| 51 | +XSS Prevention Cheat Sheet</a>. |
| 52 | +</li> |
| 53 | +<li> |
| 54 | +OWASP: |
| 55 | +<a href="https://cheatsheetseries.owasp.org/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.html">XSS |
| 56 | +(Cross Site Scripting) Prevention Cheat Sheet</a>. |
| 57 | +</li> |
| 58 | +<li> |
| 59 | +OWASP |
| 60 | +<a href="https://www.owasp.org/index.php/DOM_Based_XSS">DOM Based XSS</a>. |
| 61 | +</li> |
| 62 | +<li> |
| 63 | +OWASP |
| 64 | +<a href="https://www.owasp.org/index.php/Types_of_Cross-Site_Scripting">Types of Cross-Site |
| 65 | +Scripting</a>. |
| 66 | +</li> |
| 67 | +<li> |
| 68 | +Wikipedia: <a href="http://en.wikipedia.org/wiki/Cross-site_scripting">Cross-site scripting</a>. |
| 69 | +</li> |
| 70 | +</references> |
| 71 | +</qhelp> |
0 commit comments