Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

!important CSS rule is removed #81

Closed
mrpeny opened this issue Apr 6, 2021 · 12 comments
Closed

!important CSS rule is removed #81

mrpeny opened this issue Apr 6, 2021 · 12 comments

Comments

@mrpeny
Copy link

mrpeny commented Apr 6, 2021

We started to use AntiSamy for CSS validation in our WEB project and realized that it removes !important CSS rules from the styles.

Eg. <p style=\"color: red !important\">Some Text</p> resolves to <p style=\"color: red\">Some Text</p>

The following test added to AntiSamyTest fails.

    @Test
    public void givenImportantRuleWhenScanThenPreserved() throws ScanException, PolicyException {
        String s = as.scan("<p style=\"color: red !important\">Some Text</p>", policy, AntiSamy.DOM).getCleanHTML();
        assertTrue(s.contains("!important"));

        s = as.scan("<p style=\"color: red !important\">Some Text</p>", policy, AntiSamy.SAX).getCleanHTML();
        assertTrue(s.contains("!important"));
    }

I see it from the method parameters of org.owasp.validator.css.CssHandler#property that we are aware of the fact if a property is important or not but it looks like the code ignores this information as the argument is not used anywhere.

...
public void property(String name, LexicalUnit value, boolean important)
			throws CSSException {
		// only bother validating and building if we are either inline or within
		// a selector tag

		if (!selectorOpen && !isInline) {
...

Is there a way to get it working or am I missing something? Let me know if you need further information!

Thank you in advance!

@davewichers
Copy link
Collaborator

Thanks for raising this issue. @spassarop - can you confirm the problem (with the provided test case), and see if you can come up with a fix?

@spassarop
Copy link
Collaborator

I can confirm. The important boolean is not used and the code that actually rebuilds the CSS is the following:

if (!isInline) { styleSheet.append('\t'); }
styleSheet.append(name);
styleSheet.append(':');

// append all values
while (value != null) {
styleSheet.append(' ');
styleSheet.append(validator.lexicalValueToString(value));
value = value.getNextLexicalUnit();
}
styleSheet.append(';');
if (!isInline) { styleSheet.append('\n'); }

I guess we could just add "!important" before the ';' append with an if. That for sure make the tests pass and I don't see the inconvenient with just adding the text before ending the CSS property.

@davewichers
Copy link
Collaborator

OK. Can you add a test case with and without !important, so we can verify this change only includes it when it is specified, and doesn't add it when it is not specified?

@spassarop
Copy link
Collaborator

spassarop commented Apr 6, 2021 via email

@davewichers
Copy link
Collaborator

@spassarop - Thanks for jumping on this so quick! @mrpeny - We have implemented a quick fix in the 1.6.3 branch. Can you test the fix out to verify it fixes the problem as you'd expect. I want to make sure that the !important added back in is in exactly the right place and works as you expect.

@mrpeny
Copy link
Author

mrpeny commented Apr 7, 2021

@davewichers I have checked the implementation in the PR and it looks good to me. I have also included 1.6.3-SNAPSHOT into our web project and it works as expected. Thank you for your prompt reply. 👍🏼

When do you plan to release 1.6.3?

@davewichers
Copy link
Collaborator

@mrpeny - Thanks for reviewing. I'd like to get the release out tonight. I just have to update some JavaDocs related to the other fix going out with 1.6.3. Then it should be ready to ship.

@mrpeny
Copy link
Author

mrpeny commented Apr 7, 2021

Thank you.

@davewichers
Copy link
Collaborator

Fixed in release 1.6.3 that just went out.

@mrpeny
Copy link
Author

mrpeny commented Apr 8, 2021

@davewichers when is it expected to appear on Maven Central?

@davewichers
Copy link
Collaborator

It's already there I believe.

@mrpeny
Copy link
Author

mrpeny commented Apr 9, 2021

It's there now. Thank you. Have a nice weekend!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants