javamail - OWASP HTML Sanitizer allow colon in HTML -
how can allow :
sign in sanitized html? using sanitize html code in generating java mail. code has inline image content id <img src=\"cid:image\" height=\"70\" width=\"70\" />
. upon sanitizing, src
attribute not included in sanitized html.
policyfactory images = new htmlpolicybuilder().allowurlprotocols("http", "https") .allowelements("img") .allowattributes("src").matching(pattern.compile("^cid[:][\\w]+$")) .onelements("img") .allowattributes("border", "height", "width").onelements("img") .tofactory(); string html = "<img src=\"cid:image\" height=\"70\" width=\"70\" />"; final string sanitized = images.sanitize(html); system.out.println(sanitized);
the output of above code is:
<img height="70" width="70" />
why isn't working
or rather, why it's working "too well"
by default, htmlpolicybuilder
disallows url protocols in src
elements. prevents injections such
<img src="javascript:alert('xss')"/>
which potentially lead execution of script after javascript:
(in case, alert('xss')
)
there other protocols (on other elements) can lead similar issues:
even though doesn't use javascript
protocol, it's still possible inject base64-encoded xss injection:
<object src="data:text/html;base64,phnjcmlwdd5hbgvydcgnehnzjyk8l3njcmlwdd4="/>
or
<a href="data:text/html;base64,phnjcmlwdd5hbgvydcgnehnzjyk8l3njcmlwdd4=">click me</a>
because of this, htmlpolicybuilder
assumes any attribute value containing colon (in attributes) should treated dangerous.
how fix it:
you have explicitly tell htmlpolicybuilder
allow cid "protocol", using allowurlprotocols
method:
policyfactory images = new htmlpolicybuilder().allowurlprotocols("http", "https") .allowelements("img") .allowurlprotocols("cid") // allow "cid" .allowattributes("src").matching(pattern.compile("^cid[:][\\w]+$")) .onelements("img") .allowattributes("border", "height", "width").onelements("img") .tofactory(); string html = "<img src=\"cid:image\" height=\"70\" width=\"70\" />"; final string sanitized = images.sanitize(html); system.out.println(sanitized);
output:
<img src="cid:image" height="70" width="70" />
Comments
Post a Comment