Skip to content

Commit 799851e

Browse files
committed
Don't allow duplicates in rel attribute for links
Signed-off-by: Sven Strickroth <email@cs-ware.de>
1 parent 6127999 commit 799851e

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/main/java/org/owasp/html/HtmlPolicyBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1048,7 +1048,7 @@ public String apply(String elementName, List<String> attrs) {
10481048
if (i == n || Strings.isHtmlSpace(rels.charAt(i))) {
10491049
if (left < i) {
10501050
final String rel = Strings.toLowerCase(rels.substring(left, i));
1051-
if (skip.isEmpty() || !skip.contains(rel)) {
1051+
if ((skip.isEmpty() || !skip.contains(rel)) && !usedRels.contains(rel)) {
10521052
sb.append(rels, left, i).append(' ');
10531053
usedRels.add(rel);
10541054
}

src/test/java/org/owasp/html/HtmlPolicyBuilderTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,31 @@ public static final void testRelLinksWhenRelisPartOfData() {
813813
assertEquals(toSanitize, pf.sanitize(toSanitize));
814814
}
815815

816+
@Test
817+
public static final void testRelLinksWithDuplicateRels() {
818+
PolicyFactory pf = new HtmlPolicyBuilder()
819+
.allowElements("a")
820+
.allowAttributes("href").onElements("a")
821+
.allowAttributes("rel").onElements("a")
822+
.allowAttributes("target").onElements("a")
823+
.allowStandardUrlProtocols()
824+
.toFactory();
825+
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>"));
826+
}
827+
828+
@Test
829+
public static final void testRelLinksWithDuplicateRelsRequired() {
830+
PolicyFactory pf = new HtmlPolicyBuilder()
831+
.allowElements("a")
832+
.allowAttributes("href").onElements("a")
833+
.allowAttributes("rel").onElements("a")
834+
.allowAttributes("target").onElements("a")
835+
.allowStandardUrlProtocols()
836+
.requireRelsOnLinks("noreferrer")
837+
.toFactory();
838+
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>"));
839+
}
840+
816841
@Test
817842
public static final void testFailFastOnSpaceSeparatedStrings() {
818843
boolean failed;

0 commit comments

Comments
 (0)