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

Added option to allow custom CSS properties (variables). #556

Merged
merged 2 commits into from
Jul 26, 2024

Conversation

leniency
Copy link
Contributor

I added an option to allow custom CSS properties (variables) without specifying all the potential options (ie, allow all --* css properties)

I ran into an issue where I wanted to accept CSS variables, but based on the execution order, it would leave the value part unsanitized. The SanitizeStyleDeclaration would reject the name immediately, and while it can be cancelled in the OnRemovingStyle event, that leaves the value part unevaluated.

With this update, evaluation of the property name can be customized by overriding the IsAllowedCssProperty method. The default handling checks the AllowedCssProperties, same as before. Then if the AllowCssCustomProperties flag is true, it will allow any property beginning with --. This method can be override if developers want to refine the prefix.

It allows css variables both in <style> tags (if allowed) as well as tag style attributes (<div style="--my-var:1px"></div>)

The flag can be set like this:

var sanitizer = new HtmlSanitizer { AllowCssCustomProperties = true };

…th '--' characters. Defaults to 'false' so custom properties will be removed. Added protected function to allow for custom evaluation of the custom property name prior to marking it for removal.
Copy link

codecov bot commented Jul 25, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.69%. Comparing base (f71dbb5) to head (28c725c).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #556      +/-   ##
==========================================
+ Coverage   94.63%   94.69%   +0.05%     
==========================================
  Files           6        6              
  Lines         839      848       +9     
  Branches       91       92       +1     
==========================================
+ Hits          794      803       +9     
  Misses         34       34              
  Partials       11       11              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@mganss
Copy link
Owner

mganss commented Jul 26, 2024

Thanks. Could you add this to HtmlSanitizerOptions, too?

@leniency
Copy link
Contributor Author

Done - added AllowDataAttributes as well.

@mganss mganss merged commit 27e4297 into mganss:master Jul 26, 2024
5 checks passed
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.

2 participants