Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix repeatedly adding rel values #307

Merged
merged 2 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/main/java/org/owasp/html/HtmlPolicyBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -1045,12 +1045,11 @@ 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);
present.add(rel);
sb.append(rel).append(' ');
final String rel = rels.substring(left, i);
final String lowerCaseRel = Strings.toLowerCase(rel);
if ((skip.isEmpty() || !skip.contains(lowerCaseRel)) && !present.contains(lowerCaseRel)) {
present.add(lowerCaseRel);
sb.append(lowerCaseRel).append(' ');
}
}
left = i + 1;
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
Loading