Skip to content

Commit 7d12b6f

Browse files
authored
Merge pull request #329 from AnzhiZhang/main
Add data to DefaultUrlSanitizer protocols
2 parents 3166508 + 804e83c commit 7d12b6f

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

commonmark/src/main/java/org/commonmark/renderer/html/DefaultUrlSanitizer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
/**
66
*
7-
* Allows http, https and mailto protocols for url.
7+
* Allows http, https, mailto, and data protocols for url.
88
* Also allows protocol relative urls, and relative urls.
99
* Implementation based on https://github.com/OWASP/java-html-sanitizer/blob/f07e44b034a45d94d6fd010279073c38b6933072/src/main/java/org/owasp/html/FilterUrlByProtocolAttributePolicy.java
1010
*/
1111
public class DefaultUrlSanitizer implements UrlSanitizer {
1212
private Set<String> protocols;
1313

1414
public DefaultUrlSanitizer() {
15-
this(List.of("http", "https", "mailto"));
15+
this(List.of("http", "https", "mailto", "data"));
1616
}
1717

1818
public DefaultUrlSanitizer(Collection<String> protocols) {

commonmark/src/test/java/org/commonmark/test/HtmlRendererTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,47 @@ public void sanitizedUrlsShouldSetRelNoFollow() {
9393
assertEquals("<p><a rel=\"nofollow\" href=\"https://google.com\"></a></p>\n", sanitizeUrlsRenderer().render(paragraph));
9494
}
9595

96+
@Test
97+
public void sanitizedUrlsShouldAllowSafeProtocols() {
98+
Paragraph paragraph = new Paragraph();
99+
Link link = new Link();
100+
link.setDestination("http://google.com");
101+
paragraph.appendChild(link);
102+
assertEquals("<p><a rel=\"nofollow\" href=\"http://google.com\"></a></p>\n", sanitizeUrlsRenderer().render(paragraph));
103+
104+
paragraph = new Paragraph();
105+
link = new Link();
106+
link.setDestination("https://google.com");
107+
paragraph.appendChild(link);
108+
assertEquals("<p><a rel=\"nofollow\" href=\"https://google.com\"></a></p>\n", sanitizeUrlsRenderer().render(paragraph));
109+
110+
paragraph = new Paragraph();
111+
link = new Link();
112+
link.setDestination("mailto:foo@bar.example.com");
113+
paragraph.appendChild(link);
114+
assertEquals("<p><a rel=\"nofollow\" href=\"mailto:foo@bar.example.com\"></a></p>\n", sanitizeUrlsRenderer().render(paragraph));
115+
116+
String image = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAFiUAABYlAUlSJPAAAAAQSURBVBhXY/iPBVBf8P9/AG8TY51nJdgkAAAAAElFTkSuQmCC";
117+
paragraph = new Paragraph();
118+
link = new Link();
119+
link.setDestination(image);
120+
paragraph.appendChild(link);
121+
assertEquals("<p><a rel=\"nofollow\" href=\"" + image + "\"></a></p>\n", sanitizeUrlsRenderer().render(paragraph));
122+
}
123+
96124
@Test
97125
public void sanitizedUrlsShouldFilterDangerousProtocols() {
98126
Paragraph paragraph = new Paragraph();
99127
Link link = new Link();
100128
link.setDestination("javascript:alert(5);");
101129
paragraph.appendChild(link);
102130
assertEquals("<p><a rel=\"nofollow\" href=\"\"></a></p>\n", sanitizeUrlsRenderer().render(paragraph));
131+
132+
paragraph = new Paragraph();
133+
link = new Link();
134+
link.setDestination("ftp://google.com");
135+
paragraph.appendChild(link);
136+
assertEquals("<p><a rel=\"nofollow\" href=\"\"></a></p>\n", sanitizeUrlsRenderer().render(paragraph));
103137
}
104138

105139
@Test

0 commit comments

Comments
 (0)