Skip to content

Commit

Permalink
Fix allowAttributes().globally() (#247) (#248)
Browse files Browse the repository at this point in the history
* Fix allowAttributes().globally() (#247)

Add guard to .globally() method of HtmlPolicyBuilder to prevent
ArrayOutOfBoundsException when checking to see if the zeroth
element of the attributeNames list contains 'style'.

This restores behaviour present in version 202180219.1 which
allowed for an empty allowed attributes names list to be
specified globally through the builder.

* Allow styling when any attribute name matches "style" globally

---------

Co-authored-by: Mike Samuel <[email protected]>
  • Loading branch information
mymhealthltd-joshengland and mikesamuel authored Feb 2, 2024
1 parent 0166eb4 commit 68662fc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/main/java/org/owasp/html/HtmlPolicyBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -964,12 +964,11 @@ public AttributeBuilder matching(
*/
@SuppressWarnings("synthetic-access")
public HtmlPolicyBuilder globally() {
if(attributeNames.get(0).equals("style")) {
return allowStyling();
} else {
return HtmlPolicyBuilder.this.allowAttributesGlobally(
policy, attributeNames);
if (attributeNames.contains("style")) {
allowStyling();
}
return HtmlPolicyBuilder.this.allowAttributesGlobally(
policy, attributeNames);
}

/**
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/org/owasp/html/HtmlPolicyBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,12 @@ public static final void testTextareaIsNotTextArea() {
assertEquals("x<textArea>y</textArea>", textAreaPolicy.sanitize(input));
}

@Test
public static final void testHtmlPolicyBuilderDefinitionWithNoAttributesDefinedGlobally() {
// Does not crash with a runtime exception
new HtmlPolicyBuilder().allowElements().allowAttributes().globally().toFactory();
}

@Test
public static final void testCSSFontSize() {
HtmlPolicyBuilder builder = new HtmlPolicyBuilder();
Expand Down

0 comments on commit 68662fc

Please sign in to comment.