Skip to content

Commit

Permalink
Don't allow duplicates in rel attribute for links
Browse files Browse the repository at this point in the history
Signed-off-by: Sven Strickroth <[email protected]>
  • Loading branch information
csware committed Feb 2, 2024
1 parent e8aa0f1 commit e97ae8c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/main/java/org/owasp/html/HtmlPolicyBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -1045,10 +1045,9 @@ public String apply(String elementName, List<String> attrs) {
for (int i = 0; i <= n; ++i) {
if (i == n || Strings.isHtmlSpace(rels.charAt(i))) {
if (left < i) {
if (skip.isEmpty()
|| !skip.contains(
Strings.toLowerCase(rels.substring(left, i)))) {
String rel = rels.substring(left, i);
final String rel = rels.substring(left, i);
final String lowerCaseRel = Strings.toLowerCase(rel);
if ((skip.isEmpty() || !skip.contains(lowerCaseRel)) && !present.contains(lowerCaseRel)) {
present.add(rel);
sb.append(rel).append(' ');
}
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/org/owasp/html/HtmlPolicyBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,31 @@ public final void testRelLinksWhenRelIsPartOfData() {
assertEquals(toSanitize, pf.sanitize(toSanitize));
}

@Test
public static final void testRelLinksWithDuplicateRels() {
PolicyFactory pf = new HtmlPolicyBuilder()
.allowElements("a")
.allowAttributes("href").onElements("a")
.allowAttributes("rel").onElements("a")
.allowAttributes("target").onElements("a")
.allowStandardUrlProtocols()
.toFactory();
assertEquals("<a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.com\">test</a>", pf.sanitize("<a target=\"_blank\" rel=\"noopener noreferrer noreferrer\" href=\"https://google.com\">test</a>"));
}

@Test
public static final void testRelLinksWithDuplicateRelsRequired() {
PolicyFactory pf = new HtmlPolicyBuilder()
.allowElements("a")
.allowAttributes("href").onElements("a")
.allowAttributes("rel").onElements("a")
.allowAttributes("target").onElements("a")
.allowStandardUrlProtocols()
.requireRelsOnLinks("noreferrer")
.toFactory();
assertEquals("<a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.com\">test</a>", pf.sanitize("<a target=\"_blank\" rel=\"noopener noreferrer noreferrer\" href=\"https://google.com\">test</a>"));
}

@Test
public static final void testFailFastOnSpaceSeparatedStrings() {
boolean failed;
Expand Down

0 comments on commit e97ae8c

Please sign in to comment.