Skip to content

Attributes allowed globally together with "style" are lost #237

Open
@corebonts

Description

@corebonts

Since the commit 020d5d0 all attributes that are allowed globally are ignored, if "style" is given as the first attribute.

Problematic code:

    public HtmlPolicyBuilder globally() {
      if(attributeNames.get(0).equals("style")) {
        return allowStyling();
      } else {
        return HtmlPolicyBuilder.this.allowAttributesGlobally(
            policy, attributeNames);
      }
    }

Proof

@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 text</h1>";
   String want = "<h1 style=\"color:green\" align=\"center\">This is some green text</h1>";
   assertEquals(want, policyBuilder.sanitize(input));
 }

Note that align="center" is missing from the output.

I will file a PR to fix the issue

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions