@Test public void shouldBeSafe() { collector.checkThat("ONLY_OPENED_IMG_TAG", HtmlSanitizer.isSafe(ONLY_OPENED_IMG_TAG).isSafe(), is(true)); collector.checkThat("SELF_CLOSING_IMG_TAG", HtmlSanitizer.isSafe(SELF_CLOSING_IMG_TAG).isSafe(), is(true)); collector.checkThat("CLOSED_IMG_TAG", HtmlSanitizer.isSafe(CLOSED_IMG_TAG).isSafe(), is(true)); collector.checkThat("BASE64_IMG_TAG", HtmlSanitizer.isSafe(BASE64_IMG_TAG).isSafe(), is(true)); collector.checkThat("DIV_TAG_WITH_STYLE_ATT_WITH_SINGLE_FIELD", HtmlSanitizer.isSafe(DIV_TAG_WITH_STYLE_ATT_WITH_SINGLE_FIELD).isSafe(), is(true)); collector.checkThat("DIV_TAG_WITH_STYLE_ATT_WITH_SINGLE_FIELD_WITH_SPACES", HtmlSanitizer.isSafe(DIV_TAG_WITH_STYLE_ATT_WITH_SINGLE_FIELD_WITH_SPACES).isSafe(), is(true)); collector.checkThat("DIV_TAG_WITH_STYLE_ATT_WITH_SINGLE_FIELD_WITH_SEMICOLON", HtmlSanitizer.isSafe(DIV_TAG_WITH_STYLE_ATT_WITH_SINGLE_FIELD_WITH_SEMICOLON).isSafe(), is(true)); collector.checkThat("DIV_TAG_WITH_STYLE_ATT_WITH_SINGLE_FIELD_WITH_SPACES_AND_SEMICOLON", HtmlSanitizer.isSafe(DIV_TAG_WITH_STYLE_ATT_WITH_SINGLE_FIELD_WITH_SPACES_AND_SEMICOLON).isSafe(), is(true)); collector.checkThat("DIV_TAG_WITH_STYLE_ATT_WITH_FLOAT_FIELD", HtmlSanitizer.isSafe(DIV_TAG_WITH_STYLE_ATT_WITH_FLOAT_FIELD).isSafe(), is(true)); collector.checkThat("DIV_TAG_WITH_STYLE_ATT_WITH_TWO_FIELD", HtmlSanitizer.isSafe(DIV_TAG_WITH_STYLE_ATT_WITH_TWO_FIELD).isSafe(), is(true)); collector.checkThat("DIV_TAG_WITH_STYLE_ATT_WITH_TWO_FIELD_WITH_SPACES", HtmlSanitizer.isSafe(DIV_TAG_WITH_STYLE_ATT_WITH_TWO_FIELD_WITH_SPACES).isSafe(), is(true)); collector.checkThat("DIV_TAG_WITH_STYLE_ATT_WITH_TWO_FIELD_WITH_SEMICOLON", HtmlSanitizer.isSafe(DIV_TAG_WITH_STYLE_ATT_WITH_TWO_FIELD_WITH_SEMICOLON).isSafe(), is(true)); collector.checkThat("DIV_TAG_WITH_STYLE_ATT_WITH_TWO_FIELD_WITH_SPACES_AND_SEMICOLON", HtmlSanitizer.isSafe(DIV_TAG_WITH_STYLE_ATT_WITH_TWO_FIELD_WITH_SPACES_AND_SEMICOLON).isSafe(), is(true)); collector.checkThat("DIV_TAG_WITH_STYLE_ATT_WITH_SINGLE_QUOTE", HtmlSanitizer.isSafe(DIV_TAG_WITH_STYLE_ATT_WITH_SINGLE_QUOTE).isSafe(), is(true)); collector.checkThat("DIV_TAG_WITH_STYLE_ATT_WITH_TWO_SEMICOLON", HtmlSanitizer.isSafe(DIV_TAG_WITH_STYLE_ATT_WITH_TWO_SEMICOLON).isSafe(), is(true)); }
public static SanitizeInfos isSafe(String content) { if (content == null || content.isEmpty()) { return new SanitizeInfos(true); } String toSanitize = htmlRenderer.render(mdParser.parse(content)); List<String> sanitizedChanges = new ArrayList<>(); HtmlChangeListener<List<String>> listener = new HtmlChangeListener<List<String>>() { @Override public void discardedTag(@Nullable List<String> context, String elementName) { context.add("Tag not allowed: " + elementName); } @Override public void discardedAttributes(@Nullable List<String> context, String tagName, String... attributeNames) { context.add("Attribute not allowed: [" + tagName + "]" + Arrays.toString(attributeNames)); } }; HtmlSanitizer.sanitize(toSanitize, listener, sanitizedChanges); return new SanitizeInfos(sanitizedChanges.isEmpty(), sanitizedChanges.toString()); }
HtmlSanitizer { public static SanitizeInfos isSafe(String content) { if (content == null || content.isEmpty()) { return new SanitizeInfos(true); } String toSanitize = htmlRenderer.render(mdParser.parse(content)); List<String> sanitizedChanges = new ArrayList<>(); HtmlChangeListener<List<String>> listener = new HtmlChangeListener<List<String>>() { @Override public void discardedTag(@Nullable List<String> context, String elementName) { context.add("Tag not allowed: " + elementName); } @Override public void discardedAttributes(@Nullable List<String> context, String tagName, String... attributeNames) { context.add("Attribute not allowed: [" + tagName + "]" + Arrays.toString(attributeNames)); } }; HtmlSanitizer.sanitize(toSanitize, listener, sanitizedChanges); return new SanitizeInfos(sanitizedChanges.isEmpty(), sanitizedChanges.toString()); } }
HtmlSanitizer { public static SanitizeInfos isSafe(String content) { if (content == null || content.isEmpty()) { return new SanitizeInfos(true); } String toSanitize = htmlRenderer.render(mdParser.parse(content)); List<String> sanitizedChanges = new ArrayList<>(); HtmlChangeListener<List<String>> listener = new HtmlChangeListener<List<String>>() { @Override public void discardedTag(@Nullable List<String> context, String elementName) { context.add("Tag not allowed: " + elementName); } @Override public void discardedAttributes(@Nullable List<String> context, String tagName, String... attributeNames) { context.add("Attribute not allowed: [" + tagName + "]" + Arrays.toString(attributeNames)); } }; HtmlSanitizer.sanitize(toSanitize, listener, sanitizedChanges); return new SanitizeInfos(sanitizedChanges.isEmpty(), sanitizedChanges.toString()); } private  HtmlSanitizer(); }
HtmlSanitizer { public static SanitizeInfos isSafe(String content) { if (content == null || content.isEmpty()) { return new SanitizeInfos(true); } String toSanitize = htmlRenderer.render(mdParser.parse(content)); List<String> sanitizedChanges = new ArrayList<>(); HtmlChangeListener<List<String>> listener = new HtmlChangeListener<List<String>>() { @Override public void discardedTag(@Nullable List<String> context, String elementName) { context.add("Tag not allowed: " + elementName); } @Override public void discardedAttributes(@Nullable List<String> context, String tagName, String... attributeNames) { context.add("Attribute not allowed: [" + tagName + "]" + Arrays.toString(attributeNames)); } }; HtmlSanitizer.sanitize(toSanitize, listener, sanitizedChanges); return new SanitizeInfos(sanitizedChanges.isEmpty(), sanitizedChanges.toString()); } private  HtmlSanitizer(); static String sanitize(String content); static String sanitize(String content, HtmlChangeListener<CTX> listener, CTX context); static SanitizeInfos isSafe(String content); }
HtmlSanitizer { public static SanitizeInfos isSafe(String content) { if (content == null || content.isEmpty()) { return new SanitizeInfos(true); } String toSanitize = htmlRenderer.render(mdParser.parse(content)); List<String> sanitizedChanges = new ArrayList<>(); HtmlChangeListener<List<String>> listener = new HtmlChangeListener<List<String>>() { @Override public void discardedTag(@Nullable List<String> context, String elementName) { context.add("Tag not allowed: " + elementName); } @Override public void discardedAttributes(@Nullable List<String> context, String tagName, String... attributeNames) { context.add("Attribute not allowed: [" + tagName + "]" + Arrays.toString(attributeNames)); } }; HtmlSanitizer.sanitize(toSanitize, listener, sanitizedChanges); return new SanitizeInfos(sanitizedChanges.isEmpty(), sanitizedChanges.toString()); } private  HtmlSanitizer(); static String sanitize(String content); static String sanitize(String content, HtmlChangeListener<CTX> listener, CTX context); static SanitizeInfos isSafe(String content);  }