|
|
(2 intermediate revisions by one other user not shown) |
Line 2: |
Line 2: |
| <div style="width:100%;height:160px;border:0,margin:0;overflow: hidden;">[[File:Cheatsheets-header.jpg|link=]]</div> | | <div style="width:100%;height:160px;border:0,margin:0;overflow: hidden;">[[File:Cheatsheets-header.jpg|link=]]</div> |
| | | |
− | {| style="padding: 0;margin:0;margin-top:10px;text-align:left;" |-
| + | The Cheat Sheet Series project has been moved to [https://github.com/OWASP/CheatSheetSeries GitHub]! |
− | | valign="top" style="border-right: 1px dotted gray;padding-right:25px;" |
| |
− | Last revision (mm/dd/yy): '''{{REVISIONMONTH}}/{{REVISIONDAY}}/{{REVISIONYEAR}}'''
| |
− | __TOC__{{TOC hidden}}
| |
| | | |
− | = Introduction =
| + | Please visit [https://cheatsheetseries.owasp.org/cheatsheets/DOM_based_XSS_Prevention_Cheat_Sheet.html DOM based XSS Prevention Cheat Sheet] to see the latest version of the cheat sheet. |
− | When looking at XSS (Cross-Site Scripting), there are three generally recognized forms of [[XSS]]. [[XSS#Stored_and_Reflected_XSS_Attacks | Reflected, Stored]], and [[DOM Based XSS]]. The [[XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet | XSS Prevention Cheatsheet]] does an excellent job of addressing Reflected and Stored XSS. This cheatsheet addresses DOM (Document Object Model) based XSS and is an extension (and assumes comprehension of) the [[XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet | XSS Prevention Cheatsheet]].
| |
− | | |
− | In order to understand DOM based XSS, one needs to see the fundamental difference between Reflected and Stored XSS when compared to DOM based XSS. The primary difference is where the attack is injected into the application. Reflected and Stored XSS are server side injection issues while DOM based XSS is a client (browser) side injection issue. All of this code originates on the server, which means it is the application owner's responsibility to make it safe from XSS, regardless of the type of XSS flaw it is. Also, XSS attacks always <b>execute</b> in the browser. The difference between Reflected/Stored XSS is where the attack is added or injected into the application. With Reflected/Stored the attack is injected into the application during server-side processing of requests where untrusted input is dynamically added to HTML. For DOM XSS, the attack is injected into the application during runtime in the client directly.
| |
− | | |
− | When a browser is rendering HTML and any other associated content like CSS, JavaScript, etc. it identifies various rendering contexts for the different kinds of input and follows different rules for each context. A rendering context is associated with the parsing of HTML tags and their attributes. The HTML parser of the rendering context dictates how data is presented and laid out on the page and can be further broken down into the standard contexts of HTML, HTML attribute, URL, and CSS. The JavaScript or VBScript parser of an execution context is associated with the parsing and execution of script code. Each parser has distinct and separate semantics in the way they can possibly execute script code which make creating consistent rules for mitigating vulnerabilities in various contexts difficult. The complication is compounded by the differing meanings and treatment of encoded values within each subcontext (HTML, HTML attribute, URL, and CSS) within the execution context.
| |
− | | |
− | For the purposes of this article, we refer to the HTML, HTML attribute, URL, and CSS contexts as subcontexts because each of these contexts can be reached and set within a JavaScript execution context. In JavaScript code, the main context is JavaScript but with the right tags and context closing characters, an attacker can try to attack the other 4 contexts using equivalent JavaScript DOM methods.<br/>
| |
− | | |
− | The following is an example vulnerability which occurs in the JavaScript context and HTML subcontext:<br/>
| |
− | | |
− | <syntaxhighlight lang="JSP">
| |
− | <script>
| |
− | var x = '<%= taintedVar %>';
| |
− | var d = document.createElement('div');
| |
− | d.innerHTML = x;
| |
− | document.body.appendChild(d);
| |
− | </script>
| |
− | </syntaxhighlight>
| |
− | | |
− | Let’s look at the individual subcontexts of the execution context in turn.
| |
− | | |
− | ==RULE #1 - HTML Escape then JavaScript Escape Before Inserting Untrusted Data into HTML Subcontext within the Execution Context==
| |
− | | |
− | There are several methods and attributes which can be used to directly render HTML content within JavaScript. These methods constitute the HTML Subcontext within the Execution Context. If these methods are provided with untrusted input, then an XSS vulnerability could result. For example:
| |
− | | |
− | ===Example Dangerous HTML Methods===
| |
− | ====Attributes====
| |
− | <syntaxhighlight lang="Javascript">
| |
− | element.innerHTML = "<HTML> Tags and markup";
| |
− | element.outerHTML = "<HTML> Tags and markup";
| |
− | </syntaxhighlight>
| |
− | | |
− | ====Methods====
| |
− | <syntaxhighlight lang="Javascript">
| |
− | document.write("<HTML> Tags and markup");
| |
− | document.writeln("<HTML> Tags and markup");
| |
− | </syntaxhighlight>
| |
− | | |
− | ===Guideline===
| |
− | To make dynamic updates to HTML in the DOM safe, we recommend a) HTML encoding, and then b) JavaScript encoding all untrusted input, as shown in these examples:
| |
− | | |
− | <syntaxhighlight lang="Javascript">
| |
− | element.innerHTML = "<%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%>";
| |
− | element.outerHTML = "<%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%>";
| |
− | </syntaxhighlight>
| |
− | | |
− | <syntaxhighlight lang="Javascript">
| |
− | document.write("<%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%>");
| |
− | document.writeln("<%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%>");
| |
− | </syntaxhighlight>
| |
− | | |
− | Note: The Encoder.encodeForHTML() and Encoder.encodeForJS() are just notional encoders. Various options for actual encoders are listed later in this document.
| |
− | | |
− | == RULE #2 - JavaScript Escape Before Inserting Untrusted Data into HTML Attribute Subcontext within the Execution Context ==
| |
− | | |
− | The HTML attribute *subcontext* within the *execution* context is divergent from the standard encoding rules. This is because the rule to HTML attribute encode in an HTML attribute rendering context is necessary in order to mitigate attacks which try to exit out of an HTML attributes or try to add additional attributes which could lead to XSS. When you are in a DOM execution context you only need to JavaScript encode HTML attributes which do not execute code (attributes other than event handler, CSS, and URL attributes).
| |
− | | |
− | For example, the general rule is to HTML Attribute encode untrusted data (data from the database, HTTP request, user, back-end system, etc.) placed in an HTML Attribute. This is the appropriate step to take when outputting data in a rendering context, however using HTML Attribute encoding in an execution context will break the application display of data.
| |
− | | |
− | === SAFE but BROKEN example===
| |
− | <syntaxhighlight lang="JSP">
| |
− | var x = document.createElement("input");
| |
− | x.setAttribute("name", "company_name");
| |
− | // In the following line of code, companyName represents untrusted user input
| |
− | // The Encoder.encodeForHTMLAttr() is unnecessary and causes double-encoding
| |
− | x.setAttribute("value", '<%=Encoder.encodeForJS(Encoder.encodeForHTMLAttr(companyName))%>');
| |
− | var form1 = document.forms[0];
| |
− | form1.appendChild(x);
| |
− | </syntaxhighlight>
| |
− | | |
− | The problem is that if companyName had the value “Johnson & Johnson”. What would be displayed in the input text field would be “Johnson &amp; Johnson”. The appropriate encoding to use in the above case would be only JavaScript encoding to disallow an attacker from closing out the single quotes and in-lining code, or escaping to HTML and opening a new script tag.
| |
− | | |
− | ===SAFE and FUNCTIONALLY CORRECT example===
| |
− | <syntaxhighlight lang="jsp">
| |
− | var x = document.createElement("input");
| |
− | x.setAttribute("name", "company_name");
| |
− | x.setAttribute("value", '<%=Encoder.encodeForJS(companyName)%>');
| |
− | var form1 = document.forms[0];
| |
− | form1.appendChild(x);
| |
− | </syntaxhighlight>
| |
− | | |
− | It is important to note that when setting an HTML attribute which does not execute code, the value is set directly within the object attribute of the HTML element so there is no concerns with injecting up.
| |
− | | |
− | ==RULE #3 - Be Careful when Inserting Untrusted Data into the Event Handler and JavaScript code Subcontexts within an Execution Context==
| |
− | | |
− | Putting dynamic data within JavaScript code is especially dangerous because JavaScript encoding has different semantics for JavaScript encoded data when compared to other encodings. In many cases, JavaScript encoding does not stop attacks within an execution context. For example, a JavaScript encoded string will execute even though it is JavaScript encoded.
| |
− | | |
− | Therefore, the primary recommendation is to <u>avoid including untrusted data in this context</u>. If you must, the following examples describe some approaches that do and do not work.
| |
− | | |
− | <syntaxhighlight lang="javascript">
| |
− | var x = document.createElement("a");
| |
− | x.href="#";
| |
− | // In the line of code below, the encoded data on the right (the second argument to setAttribute)
| |
− | // is an example of untrusted data that was properly JavaScript encoded but still executes.
| |
− | x.setAttribute("onclick", "\u0061\u006c\u0065\u0072\u0074\u0028\u0032\u0032\u0029");
| |
− | var y = document.createTextNode("Click To Test");
| |
− | x.appendChild(y);
| |
− | document.body.appendChild(x);
| |
− | </syntaxhighlight>
| |
− | | |
− | The setAttribute(<i>name_string</i>,<i>value_string</i>) method is dangerous because it implicitly coerces the <i>value_string</i> into the DOM attribute datatype of <i>name_string</i>. In the case above, the attribute name is an JavaScript event handler, so the attribute value is implicitly converted to JavaScript code and evaluated. In the case above, JavaScript encoding does not mitigate against DOM based XSS. Other JavaScript methods which take code as a string types will have a similar problem as outline above (setTimeout, setInterval, new Function, etc.). This is in stark contrast to JavaScript encoding in the event handler attribute of a HTML tag (HTML parser) where JavaScript encoding mitigates against XSS.<br/>
| |
− | | |
− | <syntaxhighlight lang="html">
| |
− | <!-- Does NOT work -->
| |
− | <a id="bb" href="#" onclick="\u0061\u006c\u0065\u0072\u0074\u0028\u0031\u0029"> Test Me</a>
| |
− | </syntaxhighlight>
| |
− | | |
− | An alternative to using Element.setAttribute(...) to set DOM attributes is to set the attribute directly. Directly setting event handler attributes will allow JavaScript encoding to mitigate against DOM based XSS. Please note, it is always dangerous design to put untrusted data directly into a command execution context.<br/>
| |
− | | |
− | <syntaxhighlight lang="html">
| |
− | <a id="bb" href="#"> Test Me</a>
| |
− | </syntaxhighlight>
| |
− | <syntaxhighlight lang="javascript">
| |
− | //The following does NOT work because the event handler is being set to a string. "alert(7)" is JavaScript encoded.
| |
− | document.getElementById("bb").onclick = "\u0061\u006c\u0065\u0072\u0074\u0028\u0037\u0029";
| |
− |
| |
− | //The following does NOT work because the event handler is being set to a string.
| |
− | document.getElementById("bb").onmouseover = "testIt";
| |
− | | |
− | //The following does NOT work because of the encoded "(" and ")". "alert(77)" is JavaScript encoded.
| |
− | document.getElementById("bb").onmouseover = \u0061\u006c\u0065\u0072\u0074\u0028\u0037\u0037\u0029;
| |
− | | |
− | //The following does NOT work because of the encoded ";". "testIt;testIt" is JavaScript encoded.
| |
− | document.getElementById("bb").onmouseover = \u0074\u0065\u0073\u0074\u0049\u0074\u003b\u0074\u0065\u0073\u0074\u0049\u0074;
| |
− |
| |
− | //The following DOES WORK because the encoded value is a valid variable name or function reference. "testIt" is JavaScript encoded
| |
− | document.getElementById("bb").onmouseover = \u0074\u0065\u0073\u0074\u0049\u0074;
| |
− | | |
− | function testIt() {
| |
− | alert("I was called.");
| |
− | }
| |
− | </syntaxhighlight>
| |
− | | |
− | There are other places in JavaScript where JavaScript encoding is accepted as valid executable code.<br/>
| |
− | | |
− | <syntaxhighlight lang="javascript">
| |
− | for ( var \u0062=0; \u0062 < 10; \u0062++){
| |
− | \u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074
| |
− | .\u0077\u0072\u0069\u0074\u0065\u006c\u006e
| |
− | ("\u0048\u0065\u006c\u006c\u006f\u0020\u0057\u006f\u0072\u006c\u0064");
| |
− | }
| |
− | \u0077\u0069\u006e\u0064\u006f\u0077
| |
− | .\u0065\u0076\u0061\u006c
| |
− | \u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074
| |
− | .\u0077\u0072\u0069\u0074\u0065(111111111);
| |
− | </syntaxhighlight>
| |
− | | |
− | or
| |
− | | |
− | <syntaxhighlight lang="javascript">
| |
− | var s = "\u0065\u0076\u0061\u006c";
| |
− | var t = "\u0061\u006c\u0065\u0072\u0074\u0028\u0031\u0031\u0029";
| |
− | window[s](t);
| |
− | </syntaxhighlight>
| |
− | | |
− | Because JavaScript is based on an international standard (ECMAScript), JavaScript encoding enables the support of international characters in programming constructs and variables in addition to alternate string representations (string escapes).
| |
− | | |
− | However the opposite is the case with HTML encoding. HTML tag elements are well defined and do not support alternate representations of the same tag. So HTML encoding cannot be used to allow the developer to have alternate representations of the <code><a></code> tag for example.
| |
− | | |
− | ===HTML Encoding’s Disarming Nature===
| |
− | | |
− | In general, HTML encoding serves to castrate HTML tags which are placed in HTML and HTML attribute contexts.
| |
− | Working example (no HTML encoding):
| |
− | | |
− | <syntaxhighlight lang="html">
| |
− | <a href="..." >
| |
− | </syntaxhighlight>
| |
− | | |
− | Normally encoded example (Does Not Work – DNW):
| |
− | | |
− | <syntaxhighlight lang="html">
| |
− | &#x3c;a href=... &#x3e;
| |
− | </syntaxhighlight>
| |
− | | |
− | HTML encoded example to highlight a fundamental difference with JavaScript encoded values (DNW):
| |
− | | |
− | <syntaxhighlight lang="html">
| |
− | <&#x61; href=...>
| |
− | </syntaxhighlight>
| |
− | | |
− | If HTML encoding followed the same semantics as JavaScript encoding. The line above could have possibily worked to render a link. This difference makes JavaScript encoding a less viable weapon in our fight against XSS.
| |
− | | |
− | ==RULE #4 - JavaScript Escape Before Inserting Untrusted Data into the CSS Attribute Subcontext within the Execution Context==
| |
− | | |
− | Normally executing JavaScript from a CSS context required either passing <code>javascript:attackCode()</code> to the CSS url() method or invoking the CSS expression() method passing JavaScript code to be directly executed. From my experience, calling the expression() function from an execution context (JavaScript) has been disabled. In order to mitigate against the CSS url() method, ensure that you are URL encoding the data passed to the CSS url() method.
| |
− | | |
− | <syntaxhighlight lang="jsp">
| |
− | document.body.style.backgroundImage = "url(<%=Encoder.encodeForJS(Encoder.encodeForURL(companyName))%>)";
| |
− | </syntaxhighlight>
| |
− | | |
− | == RULE #5 - URL Escape then JavaScript Escape Before Inserting Untrusted Data into URL Attribute Subcontext within the Execution Context ==
| |
− | | |
− | The logic which parses URLs in both execution and rendering contexts looks to be the same. Therefore there is little change in the encoding rules for URL attributes in an execution (DOM) context.
| |
− | | |
− | <syntaxhighlight lang="jsp">
| |
− | var x = document.createElement("a");
| |
− | x.setAttribute("href", '<%=Encoder.encodeForJS(Encoder.encodeForURL(userRelativePath))%>');
| |
− | var y = document.createTextElement("Click Me To Test");
| |
− | x.appendChild(y);
| |
− | document.body.appendChild(x);
| |
− | </syntaxhighlight>
| |
− | | |
− | If you utilize fully qualified URLs then this will break the links as the colon in the protocol identifier (“http:” or “javascript:”) will be URL encoded preventing the “http” and “javascript” protocols from being invoked.
| |
− | | |
− | == RULE #6 - Populate the DOM using safe JavaScript functions or properties ==
| |
− | | |
− | The most fundamental safe way to populate the DOM with untrusted data is to use the safe assignment property, textContent. Here is an example of safe usage.
| |
− | | |
− | <syntaxhighlight lang="html">
| |
− | <script>
| |
− | element.textContent = untrustedData; //does not execute code
| |
− | </script>
| |
− | </syntaxhighlight>
| |
− | | |
− | == RULE #7 - Fixing DOM Cross-site Scripting Vulnerabilities ==
| |
− | | |
− | The best way to fix DOM based cross-site scripting is to use the right output method (sink). For example if you want to use user input to write in a ''div tag'' element don’t use innerHtml, instead use innerText/textContent. This will solve the problem, and it is the right way to re-mediate DOM based XSS vulnerabilities.
| |
− | | |
− | It is always a bad idea to use a user-controlled input in dangerous sources such as eval. 99% of the time it is an indication of bad or lazy programming practice, so simply don’t do it instead of trying to sanitize the input.
| |
− | | |
− | Finally, to fix the problem in our initial code, instead of trying to encode the output correctly which is a hassle and can easily go wrong we would simply use element.textContent to write it in a content like this:
| |
− | <syntaxhighlight lang="html">
| |
− | <b>Current URL:</b> <span id="contentholder"></span>
| |
− | .....
| |
− | <script>
| |
− | document.getElementById("contentholder").textContent = document.baseURI;
| |
− | </script>
| |
− | </syntaxhighlight>
| |
− | It does the same thing but this time it is not vulnerable to DOM based cross-site scripting vulnerabilities.
| |
− | | |
− | =Guidelines for Developing Secure Applications Utilizing JavaScript=
| |
− | | |
− | DOM based XSS is extremely difficult to mitigate against because of its large attack surface and lack of standardization across browsers. The guidelines below are an attempt to provide guidelines for developers when developing Web based JavaScript applications (Web 2.0) such that they can avoid XSS.
| |
− | | |
− | == GUIDELINE #1 - Untrusted data should only be treated as displayable text ==
| |
− | | |
− | Avoid treating untrusted data as code or markup within JavaScript code.
| |
− | | |
− | == GUIDELINE #2 - Always JavaScript encode and delimit untrusted data as quoted strings when entering the application when building templated Javascript ==
| |
− | | |
− | Always JavaScript encode and delimit untrusted data as quoted strings when entering the application al illustrated in the following example.
| |
− | | |
− | <syntaxhighlight lang="jsp">
| |
− | var x = "<%= Encode.forJavaScript(untrustedData) %>";
| |
− | </syntaxhighlight>
| |
− | | |
− | == GUIDELINE #3 - Use <b>document.createElement("..."), element.setAttribute("...","value"), element.appendChild(...)</b> and similar to build dynamic interfaces ==
| |
− | | |
− | <b>document.createElement("..."), element.setAttribute("...","value"), element.appendChild(...)</b> and similar are save ways to build dynamic interfaces.
| |
− | | |
− | Please note, <b>element.setAttribute</b> is only safe for a limited number of attributes. Dangerous attributes include any attribute that is a command execution context, such as onclick or onblur. Examples of safe attributes includes align, alink, alt, bgcolor, border, cellpadding, cellspacing, class, color, cols, colspan, coords, dir, face, height, hspace, ismap, lang, marginheight, marginwidth, multiple, nohref, noresize, noshade, nowrap, ref, rel, rev, rows, rowspan, scrolling, shape, span, summary, tabindex, title, usemap, valign, value, vlink, vspace, width.<br/>
| |
− | | |
− | == GUIDELINE #4 - Avoid sending untrusted data into HTML rendering methods ==
| |
− | | |
− | Avoid populating the following methods with untrusted data.
| |
− | | |
− | #<b>element.innerHTML = "..."; </b>
| |
− | #<b>element.outerHTML = "..."; </b>
| |
− | #<b>document.write(...); </b>
| |
− | #<b>document.writeln(...); </b><br/>
| |
− | | |
− | == GUIDELINE #5 - Avoid the numerous methods which implicitly eval() data passed to it ==
| |
− | | |
− | There are numerous methods which implicitly eval() data passed to it that must be avoided . Make sure that any untrusted data passed to these methods is delimited with string delimiters and enclosed within a closure or JavaScript encoded to N-levels based on usage, and wrapped in a custom function. Ensure to follow step 4 above to make sure that the untrusted data is not sent to dangerous methods within the custom function or handle it by adding an extra layer of encoding.<br/>
| |
− | | |
− | <b>Utilizing an Enclosure (as suggested by Gaz)</b>
| |
− | | |
− | The example that follows illustrates using closures to avoid double JavaScript encoding.
| |
− | | |
− | <syntaxhighlight lang="jsp">
| |
− | setTimeout((function(param) { return function() {
| |
− | customFunction(param);
| |
− | }
| |
− | })("<%=Encoder.encodeForJS(untrustedData)%>"), y);
| |
− | </syntaxhighlight>
| |
− | | |
− | The other alternative is using N-levels of encoding.<br/>
| |
− | | |
− | <b>N-Levels of Encoding</b><br/>
| |
− | | |
− | If your code looked like the following, you would need to only double JavaScript encode input data.
| |
− | | |
− | <syntaxhighlight lang="javascript">
| |
− | setTimeout("customFunction('<%=doubleJavaScriptEncodedData%>', y)");
| |
− | function customFunction (firstName, lastName)
| |
− | alert("Hello" + firstName + " " + lastNam);
| |
− | }
| |
− | </syntaxhighlight>
| |
− | | |
− | The <b>doubleJavaScriptEncodedData</b> has its first layer of JavaScript encoding reversed (upon execution) in the single quotes. Then the implicit <b>eval()</b> of <b>setTimeout()</b> reverses another layer of JavaScript encoding to pass the correct value to <b>customFunction</b>. The reason why you only need to double JavaScript encode is that the <b>customFunction</b> function did not itself pass the input to another method which implicitly or explicitly called <b>eval()</b>. If "firstName" was passed to another JavaScript method which implicitly or explicitly called eval() then <b><%=doubleJavaScriptEncodedData%></b>> above would need to be changed to <b><%=tripleJavaScriptEncodedData%></b>.<br/>
| |
− | | |
− | An important implementation note is that if the JavaScript code tries to utilize the double or triple encoded data in string comparisons, the value may be interpreted as different values based on the number of evals() the data has passed through before being passed to the if comparison and the number of times the value was JavaScript encoded.<br/>
| |
− | | |
− | If "A" is double JavaScript encoded then the following if check will return false.<br/>
| |
− | | |
− | <syntaxhighlight lang="javascript">
| |
− | var x = "doubleJavaScriptEncodedA"; //\u005c\u0075\u0030\u0030\u0034\u0031
| |
− | if (x == "A") {
| |
− | alert("x is A");
| |
− | } else if (x == "\u0041") {
| |
− | alert("This is what pops");
| |
− | }
| |
− | </syntaxhighlight>
| |
− | | |
− | This brings up an interesting design point. Ideally, the correct way to apply encoding and avoid the problem stated above is to server-side encode for the output context where data is introduced into the application. Then client-side encode (using a JavaScript encoding library such as ESAPI4JS) for the individual subcontext (DOM methods) which untrusted data is passed to. ESAPI4JS (located at http://bit.ly/9hRTLH) and jQuery Encoder (located at https://github.com/chrisisbeef/jquery-encoder/blob/master/src/main/javascript/org/owasp/esapi/jquery/encoder.js) are two client side encoding libraries developed by Chris Schmidt.<br/>
| |
− | Here are some examples of how they are used:<br/>
| |
− | | |
− | <syntaxhighlight lang="jsp">
| |
− | var input = "<%=Encoder.encodeForJS(untrustedData)%>"; //server-side encoding
| |
− | </syntaxhighlight>
| |
− | | |
− | <syntaxhighlight lang="jsp">
| |
− | document.writeln(ESAPI4JS.encodeForHTML(input)); //HTML encoding is happening in JavaScript
| |
− | </syntaxhighlight>
| |
− | | |
− | One option is utilize ECMAScript 5 immutable properties in the JavaScript library.<br/>
| |
− | Another option provided by Gaz (Gareth) was to use a specific code construct to limit mutability with anonymous closures.<br/>
| |
− | | |
− | An example follows:<br/>
| |
− | | |
− | <syntaxhighlight lang="javascript">
| |
− | function escapeHTML(str) {
| |
− | str = str + "''";
| |
− | var out = "''";
| |
− | for(var i=0; i<str.length; i++) {
| |
− | if(str[i] === '<') {
| |
− | out += '&lt;';
| |
− | } else if(str[i] === '>') {
| |
− | out += '&gt;';
| |
− | } else if(str[i] === "'") {
| |
− | out += '&#39;';
| |
− | } else if(str[i] === '"') {
| |
− | out += '&quot;';
| |
− | } else {
| |
− | out += str[i];
| |
− | }
| |
− | }
| |
− | return out;
| |
− | }
| |
− | </syntaxhighlight>
| |
− | | |
− | <br/>Chris Schmidt has put together another implementation of a JavaScript encoder at http://yet-another-dev.blogspot.com/2011/02/client-side-contextual-encoding-for.html.
| |
− | | |
− | == GUIDELINE #6 - Limit the usage of untrusted data to only right side operations ==
| |
− | | |
− | Not only is it good design to limit the usage of untrusted data to right side operations, but also be aware of data which may be passed to the application which look like code (eg. <b>location</b>, <b>eval()</b>).
| |
− | | |
− | If you want to change different object attributes based on user input then use a level of indirection.<br/>
| |
− | | |
− | Instead of: <br/>
| |
− | <syntaxhighlight lang="javascript">
| |
− | window[userData] = "moreUserData";
| |
− | </syntaxhighlight>
| |
− | | |
− | Do the following instead:<br/>
| |
− | <syntaxhighlight lang="javascript">
| |
− | if (userData==="location") {
| |
− | window.location = "static/path/or/properly/url/encoded/value";
| |
− | }
| |
− | </syntaxhighlight>
| |
− | | |
− | == GUIDELINE #7 - When URL encoding in DOM be aware of character set issues ==
| |
− | | |
− | When URL encoding in DOM be aware of character set issues as the character set in JavaScript DOM is not clearly defined (Mike Samuel).
| |
− | | |
− | == GUIDELINE #8 - Limit access to properties objects when using object[x] accessors ==
| |
− | | |
− | Limit access to properties objects when using object[x] accessors. (Mike Samuel). In other words use a level of indirection between untrusted input and specified object properties.
| |
− | Here is an example of the problem when using map types:<br/>
| |
− | | |
− | <syntaxhighlight lang="jsp" line='line'>
| |
− | var myMapType = {};
| |
− | myMapType[<%=untrustedData%>] = "moreUntrustedData";
| |
− | </syntaxhighlight>
| |
− | | |
− | Although the developer writing the code above was trying to add additional keyed elements to the <b>myMapType</b> object. This could be used by an attacker to subvert internal and external attributes of the <b>myMapType</b> object.
| |
− | | |
− | == GUIDELINE #9 - Run your JavaScript in a ECMAScript 5 canopy or sandbox ==
| |
− | | |
− | Run your JavaScript in a ECMAScript 5 canopy or sand box to make it harder for your JavaScript API to be compromised (Gareth Heyes and John Stevens).
| |
− | | |
− | == GUIDELINE #10 - Don’t <b>eval()</b> JSON to convert it to native JavaScript objects ==
| |
− | | |
− | Don’t <b>eval()</b> JSON to convert it to native JavaScript objects. Instead use <b>JSON.toJSON()</b> and <b>JSON.parse()</b> (Chris Schmidt).
| |
− | | |
− | = Common Problems Associated with Mitigating DOM Based XSS =
| |
− | ==Complex Contexts==
| |
− | In many cases the context isn’t always straightforward to discern.<br/>
| |
− | <syntaxhighlight lang="jsp">
| |
− | <a href="javascript:myFunction('<%=untrustedData%>', 'test');">Click Me</a>
| |
− | ...
| |
− | <script>
| |
− | Function myFunction (url,name) {
| |
− | window.location = url;
| |
− | }
| |
− | </script>
| |
− | </syntaxhighlight>
| |
− | | |
− | In the above example, untrusted data started in the rendering URL context (<b>href</b> attribute of an <b><a></b> tag) then changed to a JavaScript execution context (<b>javascript:</b> protocol handler) which passed the untrusted data to an execution URL subcontext (<b>window.location</b> of myFunction). Because the data was introduced in JavaScript code and passed to a URL subcontext the appropriate server-side encoding would be the following:<br/>
| |
− | | |
− | <syntaxhighlight lang="jsp">
| |
− | <a href="javascript:myFunction('<%=Encoder.encodeForJS( Encoder.encodeForURL(untrustedData)) %>', 'test');">Click Me</a>
| |
− | ...
| |
− | </syntaxhighlight>
| |
− | | |
− | Or if you were using ECMAScript 5 with an immutable JavaScript client-side encoding libraries you could do the following:<br/>
| |
− | | |
− | <syntaxhighlight lang="jsp">
| |
− | <!--server side URL encoding has been removed. Now only JavaScript encoding on server side. -->
| |
− | <a href="javascript:myFunction('<%=Encoder.encodeForJS(untrustedData)%>', 'test');">Click Me</a>
| |
− | ...
| |
− | <script>
| |
− | Function myFunction (url,name) {
| |
− | var encodedURL = ESAPI4JS.encodeForURL(url); //URL encoding using client-side scripts
| |
− | window.location = encodedURL;
| |
− | }
| |
− | </script>
| |
− | </syntaxhighlight>
| |
− | | |
− | == Inconsistencies of Encoding Libraries ==
| |
− | There are a number of open source encoding libraries out there:<br/>
| |
− | | |
− | #[[ESAPI]]
| |
− | #Apache Commons String Utils
| |
− | #Jtidy
| |
− | #Your company’s custom implementation.
| |
− | | |
− | Some work on a black list while others ignore important characters like “<” and “>”. ESAPI is one of the few which works on a whitelist and encodes all non-alphanumeric characters.
| |
− | It is important to use an encoding library that understands which characters can be used to exploit vulnerabilities in their respective contexts. Misconceptions abound related to the proper encoding that is required.
| |
− | | |
− | ==Encoding Misconceptions==
| |
− | Many security training curriculums and papers advocate the blind usage of HTML encoding to resolve XSS. This logically seems to be prudent advice as the JavaScript parser does not understand HTML encoding. However, if the pages returned from your web application utilize a content type of “text/xhtml” or the file type extension of “*.xhtml” then HTML encoding may not work to mitigate against XSS.<br/>
| |
− | | |
− | For example: <br/>
| |
− | | |
− | <syntaxhighlight lang="html">
| |
− | <script>
| |
− | &#x61;lert(1);
| |
− | </script>
| |
− | </syntaxhighlight>
| |
− | | |
− | The HTML encoded value above is still executable. If that isn’t enough to keep in mind, you have to remember that encodings are lost when you retrieve them using the value attribute of a DOM element. <br/>
| |
− | | |
− | Let’s look at the sample page and script:<br/>
| |
− | | |
− | <syntaxhighlight lang="html">
| |
− | <form name="myForm" ...>
| |
− | <input type="text" name="lName" value="<%=Encoder.encodeForHTML(last_name)%>">
| |
− | ...
| |
− | </form>
| |
− | <script>
| |
− | var x = document.myForm.lName.value; //when the value is retrieved the encoding is reversed
| |
− | document.writeln(x); //any code passed into lName is now executable.
| |
− | </script>
| |
− | </syntaxhighlight>
| |
− | | |
− | Finally there is the problem that certain methods in JavaScript which are usually safe can be unsafe in certain contexts.<br/>
| |
− | | |
− | ==Usually Safe Methods==
| |
− | | |
− | One example of an attribute which is thought to be safe is innerText. Some papers or guides advocate its use as an alternative to innerHTML to mitigate against XSS in innerHTML. However, depending on the tag which innerText is applied, code can be executed. Also note, innerText is non standard and is not supported in FireFox<br/>
| |
− | | |
− | <syntaxhighlight lang="html">
| |
− | <script>
| |
− | var tag = document.createElement("script");
| |
− | tag.innerText = "<%=untrustedData%>"; //executes code
| |
− | </script>
| |
− | </syntaxhighlight>
| |
− | | |
− | = Authors and Contributing Editors =
| |
− | | |
− | Jim Manico - jim[at]owasp.org<br/>
| |
− | Abraham Kang - abraham.kang[at]owasp.org<br/>
| |
− | Gareth (Gaz) Heyes<br/>
| |
− | Stefano Di Paola<br/>
| |
− | Achim Hoffmann - achim[at]owasp.org<br/>
| |
− | Robert (RSnake) Hansen<br/>
| |
− | Mario Heiderich<br/>
| |
− | John Steven<br/>
| |
− | Chris (Chris BEEF) Schmidt<br/>
| |
− | Mike Samuel<br/>
| |
− | Jeremy Long<br/>
| |
− | Dhiraj Mishra - mishra.dhiraj[at]owasp.org<br/>
| |
− | Eduardo (SirDarkCat) Alberto Vela Nava<br/>
| |
− | Jeff Williams - jeff.williams[at]owasp.org <br/>
| |
− | Erlend Oftedal
| |
− | | |
− | = Other Cheatsheets =
| |
− | | |
− | {{Cheatsheet_Navigation_Body}}
| |
− | | |
− | |}
| |
− | | |
− | [[Category:Cheatsheets]]
| |