This site is the archived OWASP Foundation Wiki and is no longer accepting Account Requests.
To view the new OWASP Foundation website, please visit https://owasp.org
Difference between revisions of "OWASP Java HTML Sanitizer Project"
(→Quick Download) |
(→Creating a HTML Policy) |
||
Line 94: | Line 94: | ||
= Creating a HTML Policy = | = Creating a HTML Policy = | ||
− | You can | + | You can view a few basic prepackaged policies for links, tables, integers and images here: [https://github.com/OWASP/java-html-sanitizer/blob/master/src/main/java/org/owasp/html/Sanitizers.java https://github.com/OWASP/java-html-sanitizer/blob/master/src/main/java/org/owasp/html/Sanitizers.java]. |
PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS); | PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS); |
Revision as of 23:03, 14 April 2016
OWASP HTML Sanitizer ProjectThe OWASP HTML Sanitizer is a fast and easy to configure HTML Sanitizer written in Java which lets you include HTML authored by third-parties in your web application while protecting against XSS. The existing dependencies are on guava and JSR 305. The other jars are only needed by the test suite. The JSR 305 dependency is a compile-only dependency, only needed for annotations. This code was written with security best practices in mind, has an extensive test suite, and has undergone adversarial security review. A great place to get started using the OWASP Java HTML Sanitizer is here: https://github.com/OWASP/java-html-sanitizer/blob/master/docs/getting_started.md. Benefits
LicensingThe OWASP HTML Sanitizer is free to use and is dual licensed under the Apache 2 License and the New BSD License. |
What is this?The OWASP HTML Sanitizer Projects provides Java based HTML sanitization of untrusted HTML! Code RepoOWASP HTML Sanitizer at GitHub Email ListQuestions? Please sign up for our Project Support List Project LeadersAuthor/Project Leader Related Projects
Ohloh |
Quick Downloadv20160413.1 at Maven Central News and Events
Change LogFor recent release notes, please visit the changelog on GitHub. Classifications
|
You can view a few basic prepackaged policies for links, tables, integers and images here: https://github.com/OWASP/java-html-sanitizer/blob/master/src/main/java/org/owasp/html/Sanitizers.java.
PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS); String safeHTML = policy.sanitize(untrustedHTML);
or the tests show how to configure your own policy here: https://github.com/OWASP/java-html-sanitizer/blob/master/src/test/java/org/owasp/html/HtmlPolicyBuilderTest.java
PolicyFactory policy = new HtmlPolicyBuilder() .allowElements("a") .allowUrlProtocols("https") .allowAttributes("href").onElements("a") .requireRelNofollowOnLinks() .build(); String safeHTML = policy.sanitize(untrustedHTML);
... or you can write custom policies to do things like changing h1s to divs with a certain class ...
PolicyFactory policy = new HtmlPolicyBuilder() .allowElements("p") .allowElements( new ElementPolicy() { public String apply(String elementName, List<String> attrs) { attrs.add("class"); attrs.add("header-" + elementName); return "div"; } }, "h1", "h2", "h3", "h4", "h5", "h6")) .build(); String safeHTML = policy.sanitize(untrustedHTML);
You can also use the default "ebay" and "slashdot" policies. The Slashdot policy (defined here https://github.com/OWASP/java-html-sanitizer/blob/master/src/main/java/org/owasp/html/examples/SlashdotPolicyExample.java) allows the following tags ("a", "p", "div", "i", "b", "em", "blockquote", "tt", "strong"n "br", "ul", "ol", "li") and only certain attributes. This policy also allows for the custom slashdot tags, "quote" and "ecode".
How was this project tested?
This code was written with security best practices in mind, has an extensive test suite, and has undergone adversarial security review.
How is this project deployed?
This project is best deployed through Maven https://github.com/OWASP/java-html-sanitizer/blob/master/docs/getting_started.md
- Maintaining a fully featured HTML sanitizer is a lot of work. We intend to continue to handle community questions and bug reports in a very timely manner.
- There are no plans for major new features other than supporting incoming requests for advanced sanitization such as additional HTML5 support.