...
XSS flaws occur whenever an application takes untrusted data and sends it to a web browser without proper validation or escaping. XSS allows attackers to execute scripts in the victim's victim’s browser which can hijack user sessions, deface web sites, or redirect the user to malicious sites.
https://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet
https://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet#XSS_Prevention_Rules_Summary
A 3.1 Defenses:
Encode untrusted data:
Data Type | Context | Code Sample | Defense |
String | HTML Body | <span>UNTRUSTED DATA</span> | |
String | Safe HTML Attributes | <input value="UNTRUSTED DATA"> |
|
String | GET Parameter | <a href="/site/search?value=UNTRUSTED DATA">clickme</a> | |
String | Untrusted URL in a SRC or HREF attribute | <a href="UNTRUSTED URL">clickme</a> |
|
String | CSS Value | <div style="width:UNTRUSTED DATA">Selection</div> |
|
String | JavaScript Variable | <script>var currentValue='UNTRUSTED DATA'</script> |
|
HTML | HTML Body | <div>UNTRUSTED HTML</div> | |
String | DOM XSS | <script>document.write("UNTRUSTED INPUT: " + document.location.hash);<script/> |
A 3.2. Sanitizers:
...
A 3.2.1
...
OWASP
...
Java
...
HTML
...
Sanitizer
...
Project
If you are using Maven then follow the maven directions to add a dependency. Otherwise, download prebuilt jars;or;git clone git@github.com:OWASP/java-html-sanitizer.git;and build the latest source.
...
Other contexts can be found in the org.owasp.Encode class methods, including CSS strings, CSS urls, XML contexts, URIs and URI components.
A 3.2.3 DOM Purify
...
DOMPurify
A 3.2.4 MentalJS
MentalJS is a JavaScript parser and sandbox. It whitelists JavaScript code by adding a "$" suffix to variables and accessors. https://github.com/hackvertor/MentalJS
A 3.2.5 OWASP JSON
...
https://www.owasp.org/index.php/OWASP_JSON_Sanitizer#tab=Input
Sanitizer
A 3.2.6 Third Party Javascript Management Cheat Sheet
...
A 3.3 AJAX Security Best Practices:
- 1.1 Client Side (Javascript)
- Use .innerText instead of .innerHtml
- Don't use eval
- Canonicalize data to consumer (read: encode before use)
- Don't rely on client logic for security
- Don't rely on client business logic
- Avoid writing serialization code
- Avoid building XML or JSON dynamically
- Never transmit secrets to the client
- Don't perform encryption in client side code
- Don't perform security impacting logic on client side1.2
- Server Side
- Protect against JSON Hijacking for Older BrowsersUse CSRF Protection
- Always return JSON with an Object on the outside
- Avoid writing serialization code. Remember ref vs. value types!
- Services can be called by users directly
- Avoid building XML or JSON by hand, use the framework
- Use JSON And XML Schema for Webservices
A 3.4 HTTP Response Splitting
...
If these contain unvalidated user input, the application is vulnerable when used with application frameworks that cannot detect this issue.
If the application has to use user-supplied input in HTTP headers, it should check for double "“\n" n” or "“\r\n" n” values in the input data and eliminate it.
Many application servers and frameworks have basic protection against HTTP response splitting, but it is not adequate to task, and you should not allow unvalidated user input in HTTP headers.
...
Code Block |
---|
XMLInputFactory factory = XMLInputFactory.newInstance(); factory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE); |
http://web-in-security.blogspot.in/2016/03/xxe-cheat-sheet.html
http://web-in-security.blogspot.de/2016/03/xml-parser-evaluation.html
...
Learn More
- OWASP XSS Prevention Cheat Sheet
- XSS Prevention Rules Summary
- OWASP Cross-Site Scripting Article
- Unraveling Some Mysteries around DOM-based XSS
- ESAPI Project Home Page
- ESAPI Encoder API
- ASVS: Output Encoding/Escaping Requirements (V6)
- ASVS: Input Validation Requirements (V5)
- Testing Guide: 1st3 Chapters on Data Validation Testing
- OWASP Code Review Guide: Chapter on XSS Review
- CWE Entry 79 on Cross-Site Scripting
- Rsnake’s XSS Attack Cheat Sheet
- XXE Cheat Sheet
- XML Parser Evaluation
- RSPEC proposal about Java and XXE