diff --git a/core/src/main/java/hudson/markup/MyspacePolicy.java b/core/src/main/java/hudson/markup/MyspacePolicy.java index 2e6bc107f8..f0543f9538 100644 --- a/core/src/main/java/hudson/markup/MyspacePolicy.java +++ b/core/src/main/java/hudson/markup/MyspacePolicy.java @@ -58,7 +58,7 @@ public class MyspacePolicy { tag("select", "multiple"); tag("option", "value","label","selected"); tag("textarea"); - tag("h1,h2,h3,h4,h5,h6,p,i,b,u,strong,em,small,big,pre,code,cite,samp,sub,sup,strike,center,lockquote"); + tag("h1,h2,h3,h4,h5,h6,p,i,b,u,strong,em,small,big,pre,code,cite,samp,sub,sup,strike,center,blockquote"); tag("hr,br,col"); tag("font", "color", "face", "size"); tag("a", "nohref","rel"); @@ -66,6 +66,7 @@ public class MyspacePolicy { tag("span,div"); tag("img", "src",ONSITE_OR_OFFSITE_URL, "hspace","vspace"); + tag("iframe", "src"); tag("link", "type","rel"); tag("ul,ol,li,dd,dl,dt,thead,tbody,tfoot"); tag("table", "noresize"); @@ -73,12 +74,13 @@ public class MyspacePolicy { tag("colgroup", "span"); tag("col", "span"); tag("fieldset,legend"); + allowStandardUrlProtocols(); }}.toFactory(); } public static void main(String[] args) throws IOException { // Fetch the HTML to sanitize. - String html = "
"; + String html = "Google"; // Set up an output channel to receive the sanitized HTML. HtmlStreamRenderer renderer = HtmlStreamRenderer.create( System.out, diff --git a/core/src/test/java/hudson/markup/MyspacePolicyTest.java b/core/src/test/java/hudson/markup/MyspacePolicyTest.java new file mode 100644 index 0000000000..cb1a63e2bb --- /dev/null +++ b/core/src/test/java/hudson/markup/MyspacePolicyTest.java @@ -0,0 +1,88 @@ +package hudson.markup; + +import com.google.common.base.Throwables; +import org.junit.Assert; +import org.junit.Test; +import org.owasp.html.Handler; +import org.owasp.html.HtmlSanitizer; +import org.owasp.html.HtmlStreamRenderer; + +import java.io.IOException; + +/** + * @author Kohsuke Kawaguchi + */ +public class MyspacePolicyTest extends Assert { + @Test + public void testPolicy() { + assertIntact("CB"); + assertIntact("relative"); + assertIntact("myself"); + assertReject("javascript","test"); + + assertIntact(""); + assertIntact(""); + assertIntact(""); + assertReject("javascript",""); + + assertIntact("basic tag"); + assertIntact("

basic block tags

"); + + assertIntact(""); + assertIntact("
  1. x
"); + assertIntact("
abc
foo
"); + assertIntact("
header
something
"); + assertIntact("

title

blurb
"); + + assertIntact(""); + assertIntact(""); + assertReject("javascript",""); + + assertReject("script",""); + assertReject("script",""); + assertReject("script",""); + + assertIntact(""); + assertIntact(""); + assertIntact("
inline CSS
"); + assertIntact("

"); + + assertIntact("
"); + } + + private void assertIntact(String input) { + input = input.replace('\'','\"'); + assertSanitize(input,input); + } + + private void assertReject(String problematic, String input) { + String out = sanitize(input); + assertFalse(out, out.contains(problematic)); + } + + private void assertSanitize(String expected, String input) { + assertEquals(expected,sanitize(input)); + } + + private String sanitize(String input) { + StringBuilder buf = new StringBuilder(); + HtmlStreamRenderer renderer = HtmlStreamRenderer.create( + buf, + // Receives notifications on a failure to write to the output. + new Handler() { + public void handle(IOException ex) { + Throwables.propagate(ex); // System.out suppresses IOExceptions + } + }, + // Our HTML parser is very lenient, but this receives notifications on + // truly bizarre inputs. + new Handler() { + public void handle(String x) { + throw new AssertionError(x); + } + } + ); + HtmlSanitizer.sanitize(input, MyspacePolicy.POLICY_DEFINITION.apply(renderer)); + return buf.toString(); + } +}