Skip to content

Add AllowImportantInStyles policy option to optionally retain !important declarations - #229

Open
AdamMagued wants to merge 1 commit into
microcosm-cc:mainfrom
AdamMagued:allow-important-in-styles
Open

AdamMagued wants to merge 1 commit into
microcosm-cc:mainfrom
AdamMagued:allow-important-in-styles

Conversation

@AdamMagued

Copy link
Copy Markdown

Summary

Add Policy.AllowImportantInStyles(allow bool) *Policy to allow retaining !important annotations on allowed inline CSS styles when explicitly opted in.

Technical Context & Root Cause

When parsing style declarations, github.com/aymerick/douceur/parser strips !important from the declaration value and stores it in the Declaration.Important boolean field. During sanitization in sanitizeStyles, Bluemonday rebuilt declarations solely as dec.Property + ": " + dec.Value, omitting !important.

For security reasons, stripping !important remains the default behavior (allowImportantInStyles = false). When opted in via p.AllowImportantInStyles(true), any allowed declaration with dec.Important == true preserves !important in the sanitized output.

Verification

  • Added unit tests in TestAllowImportantInStyles covering default stripping, opt-in retention, mixed declarations, global and element-scoped styles, invalid property stripping, and disabling after enabling.
  • Added regression test TestIssue221 covering issue Feature request: Optional support for !important #221 examples and concurrent execution under the race detector.
  • Ran test suite with go test -race -v ./...; all tests pass.

Fixes #221

Add Policy.AllowImportantInStyles(bool) to permit !important declarations in style attributes when explicitly opted in, while preserving the default behavior of stripping !important for security.
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

Successfully merging this pull request may close these issues.

Feature request: Optional support for !important

1 participant