Skip to content
Open
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
8 changes: 6 additions & 2 deletions src/main/java/org/owasp/html/HtmlPolicyBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,7 @@ private HtmlTagSkipType getHtmlTagSkipType(String elementName) {
*/
public final class AttributeBuilder {
private final List<String> attributeNames;
private AttributePolicy policy = AttributePolicy.IDENTITY_ATTRIBUTE_POLICY;
private AttributePolicy policy;

AttributeBuilder(List<? extends String> attributeNames) {
this.attributeNames = List.copyOf(attributeNames);
Expand All @@ -884,7 +884,11 @@ public final class AttributeBuilder {
* transformation by a previous policy.
*/
public AttributeBuilder matching(AttributePolicy attrPolicy) {
this.policy = AttributePolicy.Util.join(this.policy, attrPolicy);
if (this.policy == null) {
this.policy = attrPolicy;
} else {
this.policy = AttributePolicy.Util.join(this.policy, attrPolicy);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return this;
}

Expand Down
23 changes: 23 additions & 0 deletions src/test/java/org/owasp/html/SanitizersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,29 @@ public static final void testStyleGlobally() {
String want = "<h1 style=\"color:green\">This is some green text</h1>";
assertEquals(want, policyBuilder.sanitize(input));
}

@Test
public static final void testStyleWithOtherAttributesGlobally() {
PolicyFactory policyBuilder = new HtmlPolicyBuilder()
.allowAttributes("style", "align").globally()
.allowElements("a", "label", "h1", "h2", "h3", "h4", "h5", "h6")
.toFactory();
String input = "<h1 style=\"color:green ;name:user ;\" align=\"center\">This is some green centered text</h1>";
String want = "<h1 style=\"color:green\" align=\"center\">This is some green centered text</h1>";
assertEquals(want, policyBuilder.sanitize(input));
}

@Test
public static final void testStyleGloballyWithCustomPolicy() {
PolicyFactory policyBuilder = new HtmlPolicyBuilder()
.allowAttributes("style")
.matching(AttributePolicy.IDENTITY_ATTRIBUTE_POLICY).globally()
.allowElements("a", "label", "h1", "h2", "h3", "h4", "h5", "h6")
.toFactory();
String input = "<h1 style=\"color:green; display: grid;\">This is some green centered text</h1>";
String want = "<h1 style=\"color:green; display: grid;\">This is some green centered text</h1>";
assertEquals(want, policyBuilder.sanitize(input));
}

static int fac(int n) {
int ifac = 1;
Expand Down