Skip to content

Commit d48e5a6

Browse files
New file to use Java HTML Sanitizer that implements old AntiSamy policy. Contributed by Mike Samuel.
1 parent 2db8128 commit d48e5a6

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright (c) 2007-2010, Arshan Dabirsiaghi, Jason Li
3+
* Copyright (c) 2011, Mike Samuel [Convert from XML to Java]
4+
*
5+
* All rights reserved.
6+
*
7+
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
8+
*
9+
* Redistributions of source code must retain the above copyright notice, this
10+
* list of conditions and the following disclaimer. Redistributions in binary
11+
* form must reproduce the above copyright notice, this list of conditions and
12+
* the following disclaimer in the documentation and/or other materials
13+
* provided with the distribution. Neither the name of OWASP nor the names
14+
* of its contributors may be used to endorse or promote products derived
15+
* from this software without specific prior written permission.
16+
*
17+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28+
*/
29+
30+
package org.owasp.esapi.reference.validation;
31+
32+
import org.owasp.html.AttributePolicy;
33+
import org.owasp.html.Handler;
34+
import org.owasp.html.HtmlPolicyBuilder;
35+
import org.owasp.html.HtmlSanitizer;
36+
import org.owasp.html.HtmlStreamRenderer;
37+
38+
import org.owasp.esapi.ESAPI;
39+
import org.owasp.esapi.Logger;
40+
import org.owasp.esapi.errors.IntrusionException;
41+
42+
import java.io.IOException;
43+
44+
/**
45+
* This class implements the
46+
* {@link AntiSamy https://www.owasp.org/index.php/Antisamy} functionality
47+
* and its basic policy file originally used with ESAPI 2.0.1. It is intended
48+
* to be immune to XSS and CSS phishing attacks.
49+
*
50+
* This code based on AntiSamyTest.java at:
51+
* http://owasp-java-html-sanitizer.googlecode.com/svn/trunk/src/tests/org/owasp/html/AntiSamyTest.java
52+
* by Mike Samuel. It has been rewritten (mostly be stripping out the JUnit
53+
* tests) to be used with ESAPI.
54+
*
55+
* @author Arshan Dabirsiaghi (original AntiSamy rules, expressed as XML)
56+
* @author Mike Samuel (converted AntiSamy XML rules to Java for HTML Sanitizer)
57+
*/
58+
public class HTMLSanitizerAntiSamyPolicy {
59+
private static final Logger logger = ESAPI.getLogger("HTMLSanitizerAntiSamyRules");
60+
private static HtmlSanitizer.Policy makePolicy(Appendable buffer) {
61+
final HtmlStreamRenderer renderer = HtmlStreamRenderer.create(
62+
buffer,
63+
new Handler<IOException>() {
64+
public void handle(IOException ex) {
65+
// OPEN ITEM: Some other exception type more appropriate here?
66+
throw new IntrusionException("Error creating AntiSamy policy for HTML Sanitizer", ex);
67+
}
68+
},
69+
new Handler<String>() {
70+
public void handle(String errorMessage) {
71+
logger.error(Logger.SECURITY_FAILURE, errorMessage);
72+
// OPEN ITEM: Should we also throw something here??? If so what?
73+
}
74+
});
75+
76+
return new HtmlPolicyBuilder()
77+
.allowElements(
78+
"a", "b", "br", "div", "font", "i", "img", "input", "li",
79+
"ol", "p", "span", "td", "ul")
80+
.allowAttributes("checked", "type").onElements("input")
81+
.allowAttributes("color").onElements("font")
82+
.allowAttributes("href").onElements("a")
83+
.allowAttributes("src").onElements("img")
84+
.allowAttributes("class", "id", "title").globally()
85+
.allowAttributes("char").matching(
86+
new AttributePolicy() {
87+
public String apply(
88+
String elementName, String attributeName, String value) {
89+
return value.length() == 1 ? value : null;
90+
}
91+
}).onElements("td")
92+
.allowStandardUrlProtocols()
93+
.requireRelNofollowOnLinks()
94+
.allowStyling()
95+
.build(renderer);
96+
}
97+
98+
public static String sanitize(String dirtyHtml) {
99+
StringBuilder sb = new StringBuilder();
100+
101+
HtmlSanitizer.sanitize(dirtyHtml, makePolicy(sb));
102+
103+
return sb.toString();
104+
}
105+
}

0 commit comments

Comments
 (0)