Skip to content

Commit 68662fc

Browse files
Fix allowAttributes().globally() (#247) (#248)
* 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 <mikesamuel@gmail.com>
1 parent 0166eb4 commit 68662fc

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -964,12 +964,11 @@ public AttributeBuilder matching(
964964
*/
965965
@SuppressWarnings("synthetic-access")
966966
public HtmlPolicyBuilder globally() {
967-
if(attributeNames.get(0).equals("style")) {
968-
return allowStyling();
969-
} else {
970-
return HtmlPolicyBuilder.this.allowAttributesGlobally(
971-
policy, attributeNames);
967+
if (attributeNames.contains("style")) {
968+
allowStyling();
972969
}
970+
return HtmlPolicyBuilder.this.allowAttributesGlobally(
971+
policy, attributeNames);
973972
}
974973

975974
/**

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,6 +1007,12 @@ public static final void testTextareaIsNotTextArea() {
10071007
assertEquals("x<textArea>y</textArea>", textAreaPolicy.sanitize(input));
10081008
}
10091009

1010+
@Test
1011+
public static final void testHtmlPolicyBuilderDefinitionWithNoAttributesDefinedGlobally() {
1012+
// Does not crash with a runtime exception
1013+
new HtmlPolicyBuilder().allowElements().allowAttributes().globally().toFactory();
1014+
}
1015+
10101016
@Test
10111017
public static final void testCSSFontSize() {
10121018
HtmlPolicyBuilder builder = new HtmlPolicyBuilder();

0 commit comments

Comments
 (0)