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

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -