``` ## Impact ### Stored * Hijack user sessions * Perform actions on behalf of users * Exfiltrate user data * Redirect user to malicious site * Modify presentation of content (defacing) * Part of attack chain * SSRF if rendered server-side ### Exfiltration Payload ```html ``` `evil.is` ```http GET /session=r0h4xfQm2ej6ssdzrweaMX7Xo85Q7A70 HTTP/1.1 ... ``` ### Reflected * Phishing or other vulnerabilities needed to exploit * If exploited, the implications are the same as stored XSS ## Mitigation ### Complex problem to avoid completely ### Untrusted data * Important to identify *untrusted data* * Data that contains user input * Err on the side of caution ### Input filtering * Can slow down attacks, but not a solution * Good libraries exist * [DOMPurify](https://github.com/cure53/DOMPurify) * **Never do your own sanitation** ### Ad hoc filtering No ` ``` In attribute or tag name ```html
``` In HTML comment ```html ``` ### Further information [OWASP - XSS Prevention](https://cheatsheetseries.owasp.org/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.html) ### DOM based XSS ### General * Becoming more common with the popularity of front-end JS frameworks * ReactJS, VueJS, etc. ### HTML encoding |Library|Encoded|Raw| |---|---|---| |jQuery|`.text`|Everything else| |React|`{var}`|`dangerouslySetInnerHTML`| |Vue|`{{ var }}`|`
`| ### A little bit of HTML * Some applications rely on users being able to edit certain aspects of a document * Add titles, images, tables, etc. * If this is done using HTML, it is very hard to do safely ### Possible solutions * Sanitizers * Can be helpful and slow down attacks, but should not be considered a solution * CSP * Use a more limited markup language, e.g. markdown Note: Content security policy Used to enforce secure origins of content ## Rules of thumb ### Avoidance * Encode all untrusted data before adding to HTML * Don't insert untrusted data into "unusual" locations * Use CSP for added layer of protection ## Epilogue ### Further reading * [OWASP - XSS](https://owasp.org/www-community/attacks/xss/) * [OWASP - DOM Based XSS](https://owasp.org/www-community/attacks/DOM_Based_XSS) * [PortSwigger XSS Cheat Sheet](https://portswigger.net/web-security/cross-site-scripting/cheat-sheet) * [OWASP - XSS Prevention](https://cheatsheetseries.owasp.org/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.html) * [OWASP - DOM Based XSS Prevention](https://cheatsheetseries.owasp.org/cheatsheets/DOM_based_XSS_Prevention_Cheat_Sheet.html) ### Hall of fame * [Samy - Myspace Worm](https://en.wikipedia.org/wiki/Samy_(computer_worm)) * [Interview with the author of the worm](https://darknetdiaries.com/episode/61/) * [XSS in Google](https://www.youtube.com/watch?v=lG7U3fuNw3A)