diff --git a/store-conf/conf/antisamy.xml b/store-conf/conf/antisamy.xml new file mode 100644 index 00000000000..88ad723b673 --- /dev/null +++ b/store-conf/conf/antisamy.xml @@ -0,0 +1,2774 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + g + grin + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/store-conf/conf/owasp_policy.xml b/store-conf/conf/owasp_policy.xml index 6dd11438d5f..3b96208945b 100644 --- a/store-conf/conf/owasp_policy.xml +++ b/store-conf/conf/owasp_policy.xml @@ -5,8 +5,8 @@ KBD = accesshtml,tabindex--> cid,http,https,mailto,data,smb - applet,frameset,object,script,style,title - + applet,frameset,object,script,title + style KBD,charset,coords,href,hreflang,name,rel,rev,shape,target,type cid,http,https,mailto,smb @@ -212,7 +212,7 @@ CORE,LANG,align,background,char,charoff,valign - CORE,LANG,align,background,bgcolor,char,charoff,valign + CORE,LANG,align,background,bgcolor,char,charoff,valign,height CORE,LANG,KBD,alt,coords,href,nohref,shape,target diff --git a/store/build.xml b/store/build.xml index 503e4de76e1..8a2a3499c1b 100644 --- a/store/build.xml +++ b/store/build.xml @@ -282,7 +282,12 @@ - + + + + + + diff --git a/store/conf/owasp_policy.xml b/store/conf/owasp_policy.xml deleted file mode 100644 index 6dd11438d5f..00000000000 --- a/store/conf/owasp_policy.xml +++ /dev/null @@ -1,250 +0,0 @@ - - - - cid,http,https,mailto,data,smb - applet,frameset,object,script,style,title - - - KBD,charset,coords,href,hreflang,name,rel,rev,shape,target,type - cid,http,https,mailto,smb - - - href - - - CORE,LANG - - - CORE,LANG - - - CORE,LANG,cite - - - CORE,LANG,background - - - CORE,clear - - - CORE,LANG - - - CORE,LANG,cite,datetime - - - CORE,LANG,align - - - LANG - - - CORE,LANG,align - - - CORE,LANG,align - - - CORE,LANG,align - - - CORE,LANG,align - - - CORE,LANG,align - - - CORE,LANG,align - - - CORE,LANG,align,noshade,size,width - - - LANG,xmlns - - - CORE,LANG,align,alt,border,height,hspace,ismap,longdesc,src,usemap,vspace,width,dfsrc,data-mce-src - cid,http,https,data,doc - - - CORE,LANG,cite - - - CORE,LANG,for - - - CORE,LANG,align - - - CORE,LANG,width - - - CORE,LANG,cite - - - CORE,LANG - - - CORE,LANG - - - CORE,LANG - - - CORE,LANG - - - - - - CORE,LANG - - - CORE,LANG,color,face,size - - - CORE,LANG - - - CORE,LANG,color,face,size - - - CORE,LANG - - - CORE,LANG - - - CORE,LANG - - - CORE,LANG - - - CORE,LANG - - - CORE,LANG - - - CORE,LANG,compact - - - CORE,LANG - - - CORE,LANG - - - CORE,LANG,type,value - - - CORE,LANG,compact,start,type - - - CORE,LANG,compact,type - - - CORE,LANG - - - CORE,LANG,compact - - - CORE,LANG - - - CORE,LANG - - - CORE,LANG - - - CORE,LANG - - - CORE,LANG - - - CORE,LANG - - - CORE,LANG - - - CORE,LANG - - - CORE,LANG - - - CORE,LANG - - - CORE,LANG,align - - - CORE,LANG,alink,background,char,charoff,span,valign,width - - - CORE,LANG,alink,background,char,charoff,span,valign,width - - - CORE,LANG,align,valign,background,bgcolor,border,cellpadding,cellspacing,frame,rules,summary,width - - - - CORE,LANG,align,background,char,charoff,valign - - - CORE,LANG,abbr,align,axis,background,bgcolor,char,charoff,colspan,headers,height,nowrap,rowspan,scope,,valign,width - - - CORE,LANG,align,background,char,charoff,valign - - - CORE,LANG,abbr,align,axis,background,bgcolor,char,charoff,colspan,headers,height,nowrap,rowspan,scope,valign,width - - - CORE,LANG,align,background,char,charoff,valign - - - CORE,LANG,align,background,bgcolor,char,charoff,valign - - - CORE,LANG,KBD,alt,coords,href,nohref,shape,target - - - CORE,LANG,KBD,disabled,name,type,value - - - CORE,LANG - - - CORE,LANG,action,accept,acceptcharset,enctype,method,name,target - - - CORE,LANG,accept,align,alt,checked,disabled,maxlength,name,readonly,size,src,type,value - - - CORE,LANG,align - - - CORE,LANG,name - - - CORE,LANG,disabled,label - - - CORE,LANG,KBD,disabled,label,selected,value - - - CORE,LANG,KBD,disabled,multiple,name,size - - - CORE,LANG,cols,disabled,name,readonly,rows - - diff --git a/store/ivy.xml b/store/ivy.xml index 224f08eabcd..80b0137f489 100644 --- a/store/ivy.xml +++ b/store/ivy.xml @@ -43,7 +43,7 @@ - + @@ -114,5 +114,10 @@ + + + + + diff --git a/store/src/java-test/antisamy.xml b/store/src/java-test/antisamy.xml new file mode 100644 index 00000000000..88ad723b673 --- /dev/null +++ b/store/src/java-test/antisamy.xml @@ -0,0 +1,2774 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + g + grin + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/store/src/java-test/com/zimbra/cs/html/owasp/OwaspHtmlSanitizerTest.java b/store/src/java-test/com/zimbra/cs/html/owasp/OwaspHtmlSanitizerTest.java index 889bb5fb506..2285547b250 100644 --- a/store/src/java-test/com/zimbra/cs/html/owasp/OwaspHtmlSanitizerTest.java +++ b/store/src/java-test/com/zimbra/cs/html/owasp/OwaspHtmlSanitizerTest.java @@ -35,7 +35,6 @@ import com.zimbra.cs.mime.Mime; import com.zimbra.cs.mime.ParsedMessage; import com.zimbra.cs.servlet.ZThreadLocal; -import com.zimbra.soap.RequestContext; public class OwaspHtmlSanitizerTest { @@ -45,7 +44,11 @@ public class OwaspHtmlSanitizerTest { public static void init() throws Exception { MailboxTestUtil.initServer(); EMAIL_BASE_DIR = MailboxTestUtil.getZimbraServerDir("") + EMAIL_BASE_DIR; - OwaspPolicy.load("conf/owasp_policy.xml"); + try { + OwaspPolicy.load("store-conf/conf/owasp_policy.xml"); + } catch (Exception e) { + OwaspPolicy.load("../store-conf/conf/owasp_policy.xml"); + } } /* @@ -316,7 +319,7 @@ public void testBug97443() throws Exception { + ""; String result = new OwaspHtmlSanitizer(html,true,null).sanitize(); Assert.assertTrue(result - .contains("
javascript-blocked test
alert
")); + .contains("
javascript-blocked test
alert
")); html = "" + "
javascript-blocked test
" @@ -324,7 +327,7 @@ public void testBug97443() throws Exception { + ""; result = new OwaspHtmlSanitizer(html,true,null).sanitize(); Assert.assertTrue(result - .contains("
javascript-blocked test
alert
")); + .contains("
javascript-blocked test
alert
")); } @Test @@ -333,7 +336,7 @@ public void testBug78902() throws Exception { String html = ""; String result = new OwaspHtmlSanitizer(html,true,null).sanitize(); Assert.assertTrue(result - .contains("")); + .contains("")); html = "My pictures "; result = new OwaspHtmlSanitizer(html,true,null).sanitize(); Assert.assertEquals(result, - ""); + ""); html = ""; result = new OwaspHtmlSanitizer(html,true,null).sanitize(); Assert.assertEquals(result, - ""); + ""); html = "
  • \"/>AAAAAAAAAA
  • "; result = new OwaspHtmlSanitizer(html,true,null).sanitize(); @@ -571,13 +574,14 @@ public void testBug102637() throws Exception { public void testBug73037() throws Exception { String html = ""; - String hrefVal = "smb://Aurora._smb._tcp.local/untitled/folder/03%20DANDIYA%20MIX.mp3"; + String hrefVal = "smb://Aurora._smb._tcp.local/untitled/folder/03 DANDIYA MIX.mp3"; String result = new OwaspHtmlSanitizer(html, true, null).sanitize(); Assert.assertTrue(result.contains(hrefVal)); html = ""; result = new OwaspHtmlSanitizer(html, true, null).sanitize(); + hrefVal = "smb://Aurora._smb._tcp.local/untitled/folder/03%20DANDIYA%20MIX.mp3"; Assert.assertTrue(result.contains(hrefVal)); html = ""; - hrefVal = "//Shared_srv/folder/file%20with%20spaces.txt"; + hrefVal = "//Shared_srv/folder/file with spaces.txt"; result = new OwaspHtmlSanitizer(html, true, null).sanitize(); Assert.assertTrue(result.contains(hrefVal)); } @@ -666,4 +670,5 @@ public void testBug102910() throws Exception { Assert.assertTrue(!result.contains("@zimbra")); Assert.assertTrue(result.contains("@zimbra")); } + } diff --git a/store/src/java/com/zimbra/cs/html/owasp/OwaspDefang.java b/store/src/java/com/zimbra/cs/html/owasp/OwaspDefang.java index 3d56921d47f..afa4c9611d4 100644 --- a/store/src/java/com/zimbra/cs/html/owasp/OwaspDefang.java +++ b/store/src/java/com/zimbra/cs/html/owasp/OwaspDefang.java @@ -70,7 +70,10 @@ private String runSanitizer(String html, boolean neuterImages) { try { sanitizedHtml = future.get(finishBefore, TimeUnit.SECONDS); } catch (InterruptedException | ExecutionException | TimeoutException e) { - ZimbraLog.soap.warn("Exception during HTML sanitization"+ e.getMessage()); + if (ZimbraLog.soap.isDebugEnabled()) { + ZimbraLog.soap.debug("Exception during HTML sanitization", e); + } + ZimbraLog.soap.warn("Exception during HTML sanitization: %s", e.getMessage()); return null; } return sanitizedHtml; diff --git a/store/src/java/com/zimbra/cs/html/owasp/OwaspHtmlSanitizer.java b/store/src/java/com/zimbra/cs/html/owasp/OwaspHtmlSanitizer.java index f3e982dd6c7..4301c6d1087 100644 --- a/store/src/java/com/zimbra/cs/html/owasp/OwaspHtmlSanitizer.java +++ b/store/src/java/com/zimbra/cs/html/owasp/OwaspHtmlSanitizer.java @@ -24,6 +24,7 @@ import org.owasp.html.HtmlStreamRenderer; import org.owasp.html.PolicyFactory; import com.zimbra.common.util.StringUtil; +import com.zimbra.cs.html.owasp.policies.StyleTagReceiver; /* * Task to sanitize HTML code @@ -74,7 +75,7 @@ public void handle(final String x) { }); // create a thread-specific policy instantiatePolicy(); - final Policy policy = POLICY_DEFINITION.apply(renderer); + final Policy policy = POLICY_DEFINITION.apply(new StyleTagReceiver(renderer)); // run the html through the sanitizer HtmlSanitizer.sanitize(html, policy); // return the resulting HTML from the builder diff --git a/store/src/java/com/zimbra/cs/html/owasp/OwaspPolicy.java b/store/src/java/com/zimbra/cs/html/owasp/OwaspPolicy.java index 0e3ccd535cb..b1c4ac63b9a 100644 --- a/store/src/java/com/zimbra/cs/html/owasp/OwaspPolicy.java +++ b/store/src/java/com/zimbra/cs/html/owasp/OwaspPolicy.java @@ -65,10 +65,11 @@ public class OwaspPolicy { static { try { load(null); - } catch (DocumentException de) { - throw new RuntimeException(de); - } catch (Exception ce) { - throw new RuntimeException(ce); + } catch (Exception e) { + if (ZimbraLog.mailbox.isDebugEnabled()) { + ZimbraLog.mailbox.debug("Failed to load OWASP policy file", e); + } + ZimbraLog.mailbox.warn("Failed to load OWASP policy file: %s", e.getMessage()); } } @@ -114,6 +115,7 @@ private OwaspPolicy(String file) throws DocumentException, Exception { } else { ZimbraLog.mailbox .warn(String.format("Owasp policy file '%s' is not readable", mPolicyFile)); + throw new Exception(String.format("Owasp policy file '%s' is not readable", mPolicyFile)); } } diff --git a/store/src/java/com/zimbra/cs/html/owasp/OwaspPolicyProducer.java b/store/src/java/com/zimbra/cs/html/owasp/OwaspPolicyProducer.java index c8dee2a7ec7..242b2349ef4 100644 --- a/store/src/java/com/zimbra/cs/html/owasp/OwaspPolicyProducer.java +++ b/store/src/java/com/zimbra/cs/html/owasp/OwaspPolicyProducer.java @@ -60,15 +60,15 @@ private static void setUp(boolean neuterImages) { for (String urlProtocol : urlProtocols) { policyBuilder.allowUrlProtocols(urlProtocol.trim()); } - if(neuterImages) { - if(policyNeuterImagesTrue == null) { - policyNeuterImagesTrue = policyBuilder.allowStyling(CssSchema.union(CssSchema.DEFAULT, ADDITIONAL_CSS)) - .toFactory(); + if (neuterImages) { + if (policyNeuterImagesTrue == null) { + policyNeuterImagesTrue = policyBuilder + .allowStyling(CssSchema.union(CssSchema.DEFAULT, ADDITIONAL_CSS)).toFactory(); } } else { - if(policyNeuterImagesFalse == null) { - policyNeuterImagesFalse = policyBuilder.allowStyling(CssSchema.union(CssSchema.DEFAULT, ADDITIONAL_CSS)) - .toFactory(); + if (policyNeuterImagesFalse == null) { + policyNeuterImagesFalse = policyBuilder + .allowStyling(CssSchema.union(CssSchema.DEFAULT, ADDITIONAL_CSS)).toFactory(); } } } diff --git a/store/src/java/com/zimbra/cs/html/owasp/policies/StyleTagReceiver.java b/store/src/java/com/zimbra/cs/html/owasp/policies/StyleTagReceiver.java new file mode 100644 index 00000000000..7f25becd22b --- /dev/null +++ b/store/src/java/com/zimbra/cs/html/owasp/policies/StyleTagReceiver.java @@ -0,0 +1,104 @@ +/* + * ***** BEGIN LICENSE BLOCK ***** + * Zimbra Collaboration Suite Server + * Copyright (C) 2019 Synacor, Inc. + * + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software Foundation, + * version 2 of the License. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * You should have received a copy of the GNU General Public License along with this program. + * If not, see . + * ***** END LICENSE BLOCK ***** + */ +package com.zimbra.cs.html.owasp.policies; + +import java.io.File; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.List; + +import org.owasp.html.HtmlStreamEventReceiver; +import org.owasp.validator.html.AntiSamy; +import org.owasp.validator.html.Policy; +import org.owasp.validator.html.PolicyException; +import org.owasp.validator.html.ScanException; + +import com.zimbra.common.localconfig.LC; +import com.zimbra.common.util.ZimbraLog; + +public class StyleTagReceiver implements HtmlStreamEventReceiver { + + private static final String STYLE_TAG = "style"; + private static final String STYLE_OPENING_TAG = ""; + private static final AntiSamy as = new AntiSamy(); + private static Policy policy = null; + private final HtmlStreamEventReceiver wrapped; + private boolean inStyleTag; + + static { + final String FS = File.separator; + String antisamyXML = LC.zimbra_home.value() + FS + "conf" + FS + "antisamy.xml"; + File myFile = new File(antisamyXML); + try { + URL url = myFile.toURI().toURL(); + policy = Policy.getInstance(url); + } catch (PolicyException | MalformedURLException e) { + ZimbraLog.mailbox.warn("Failed to load antisamy policy: %s", e.getMessage()); + } + ZimbraLog.mailbox.info("Antisamy policy loaded"); + } + + public StyleTagReceiver(HtmlStreamEventReceiver wrapped) { + this.wrapped = wrapped; + } + + @Override + public void openDocument() { + wrapped.openDocument(); + inStyleTag = false; + } + + @Override + public void closeDocument() { + wrapped.closeDocument(); + } + + @Override + public void openTag(String elementName, List attrs) { + wrapped.openTag(elementName, attrs); + inStyleTag = STYLE_TAG.equalsIgnoreCase(elementName); + } + + @Override + public void closeTag(String elementName) { + wrapped.closeTag(elementName); + inStyleTag = false; + } + + @Override + public void text(String text) { + if (inStyleTag) { + String sanitizedStyle = ""; + if (policy != null) { + try { + sanitizedStyle = as + .scan(STYLE_OPENING_TAG + text + STYLE_CLOSING_TAG, policy, AntiSamy.DOM) + .getCleanHTML(); + sanitizedStyle = sanitizedStyle.replace(STYLE_OPENING_TAG, "") + .replace(STYLE_CLOSING_TAG, ""); + } catch (ScanException | PolicyException e) { + ZimbraLog.mailbox.warn("Failed to sanitize html style element"); + sanitizedStyle = ""; + } + } + wrapped.text(sanitizedStyle); + } else { + wrapped.text(text); + } + } +}