Skip to content

Commit b493617

Browse files
author
Gabor Garancsi
committed
Do not ignore attributes allowed globally together with 'style' (#237)
Also, allowStyling() internally allows the 'style' attribute, so it is not necessary to ignore it.
1 parent 06b299c commit b493617

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

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

+4-5
Original file line numberDiff line numberDiff line change
@@ -968,12 +968,11 @@ public AttributeBuilder matching(
968968
*/
969969
@SuppressWarnings("synthetic-access")
970970
public HtmlPolicyBuilder globally() {
971-
if(attributeNames.get(0).equals("style")) {
972-
return allowStyling();
973-
} else {
974-
return HtmlPolicyBuilder.this.allowAttributesGlobally(
975-
policy, attributeNames);
971+
if (attributeNames.contains("style")) {
972+
allowStyling();
976973
}
974+
return HtmlPolicyBuilder.this.allowAttributesGlobally(policy,
975+
attributeNames);
977976
}
978977

979978
/**

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

+11
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,17 @@ public static final void testStyleGlobally() {
500500
String want = "<h1 style=\"color:green\">This is some green text</h1>";
501501
assertEquals(want, policyBuilder.sanitize(input));
502502
}
503+
504+
@Test
505+
public static final void testStyleWithOtherAttributesGlobally() {
506+
PolicyFactory policyBuilder = new HtmlPolicyBuilder()
507+
.allowAttributes("style", "align").globally()
508+
.allowElements("a", "label", "h1", "h2", "h3", "h4", "h5", "h6")
509+
.toFactory();
510+
String input = "<h1 style=\"color:green ;name:user ;\" align=\"center\">This is some green centered text</h1>";
511+
String want = "<h1 style=\"color:green\" align=\"center\">This is some green centered text</h1>";
512+
assertEquals(want, policyBuilder.sanitize(input));
513+
}
503514

504515
static int fac(int n) {
505516
int ifac = 1;

0 commit comments

Comments
 (0)