# Client-side cross-site scripting (experimental) Directly writing user input (for example, a URL query parameter) to a webpage without properly sanitizing the input first, allows for a cross-site scripting vulnerability. This kind of vulnerability is also called *DOM-based* cross-site scripting, to distinguish it from other types of cross-site scripting. Note: This CodeQL query is an experimental query. Experimental queries generate alerts using machine learning. They might include more false positives but they will improve over time. ## Recommendation To guard against cross-site scripting, consider using contextual output encoding/escaping before writing user input to the page, or one of the other solutions that are mentioned in the references. ## Example The following example shows part of the page URL being written directly to the document, leaving the website vulnerable to cross-site scripting. ```javascript function setLanguageOptions() { var href = document.location.href, deflt = href.substring(href.indexOf("default=")+8); document.write(""); document.write(""); } ``` ## References * OWASP: [DOM based XSS Prevention Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/DOM_based_XSS_Prevention_Cheat_Sheet.html). * OWASP: [XSS (Cross Site Scripting) Prevention Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.html). * OWASP [DOM Based XSS](https://www.owasp.org/index.php/DOM_Based_XSS). * OWASP [Types of Cross-Site Scripting](https://www.owasp.org/index.php/Types_of_Cross-Site_Scripting). * Wikipedia: [Cross-site scripting](http://en.wikipedia.org/wiki/Cross-site_scripting).