Skip to content

Commit 020d5d0

Browse files
authored
Fixed allowAtributes("style").globally() (#218)
* allowAtributes("style") * Global style test
1 parent 33d319f commit 020d5d0

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -968,8 +968,12 @@ public AttributeBuilder matching(
968968
*/
969969
@SuppressWarnings("synthetic-access")
970970
public HtmlPolicyBuilder globally() {
971-
return HtmlPolicyBuilder.this.allowAttributesGlobally(
972-
policy, attributeNames);
971+
if(attributeNames.get(0).equals("style")) {
972+
return allowStyling();
973+
} else {
974+
return HtmlPolicyBuilder.this.allowAttributesGlobally(
975+
policy, attributeNames);
976+
}
973977
}
974978

975979
/**
@@ -1142,4 +1146,4 @@ final class AttributeGuardIntermediates {
11421146
this.styleUrlPolicy = styleUrlPolicy;
11431147
this.cssSchema = cssSchema;
11441148
}
1145-
}
1149+
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,17 @@ public static final void testStyleTagInTable() {
434434
pf.sanitize(input));
435435
}
436436

437+
@Test
438+
public static final void testStyleGlobally() {
439+
PolicyFactory policyBuilder = new HtmlPolicyBuilder()
440+
.allowAttributes("style").globally()
441+
.allowElements("a", "label", "h1", "h2", "h3", "h4", "h5", "h6")
442+
.toFactory();
443+
String input = "<h1 style=\"color:green ;name:user ;\">This is some green text</h1>";
444+
String want = "<h1 style=\"color:green\">This is some green text</h1>";
445+
assertEquals(want, policyBuilder.sanitize(input));
446+
}
447+
437448
static int fac(int n) {
438449
int ifac = 1;
439450
for (int i = 1; i <= n; ++i) {

0 commit comments

Comments
 (0)