✨ feat(clean): per-property inline-style value allowlist#561
Merged
Conversation
Merging this PR will not alter performance
Performance Changes
Comparing Footnotes
|
Add Policy.allowed_styles, a per-element, per-property value allowlist
for the style attribute keyed {tag: {property: [pattern, ...]}} with
"*" matching every tag, porting sanitize-html's allowedStyles. A
declaration survives only when its property is listed for the element's
tag or "*" (their pattern lists union) and its value matches one of the
patterns via an unanchored re.search.
This narrows on top of css_properties and the non-configurable
dangerous-value baseline: a property must still be in css_properties,
and expression() or a url() with a disallowed scheme is dropped even
when a pattern would admit it. The <style> element body stays governed
by css_properties alone. The non-allowed_styles path keeps its existing
zero-overhead scrub.
Closes tox-dev#533
Add the reference note, explanation, tutorial step, and a benchmark op that the earlier PR left out. - bench: sanitize-styles op over a style-attribute-rich input with a per-property value allowlist, auto-registered in core.OPERATIONS and gated on the spec corpus like the other sanitize ops. - reference: an allowed_styles note and example on the Policy docs. - explanation: docs/explanation/sanitizing.rst on why value validation narrows on top of the name allowlist and never weakens the baseline. - tutorial: a sanitize() step with an allowed_styles Policy in getting-started.
2d4a2cb to
db93818
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
Policy.allowed_styles, a per-element, per-property value allowlist for thestyleattribute, porting sanitize-html'sallowedStyles.Key it
{tag: {property: [pattern, ...]}}, with"*"as a tag matching every element. A declaration survives only when its property is listed for the element's own tag or"*"(their pattern lists union, so either can admit it) and its value matches one of the patterns, using an unanchoredre.search— the same semantics as sanitize-html'sRegExp.test. Patterns are strings or precompiledre.Pattern, compiled once inSanitizer.__init__.Precedence: this narrows on top of the existing
css_propertiesname allowlist and the non-configurable dangerous-value baseline, and never weakens it. A property still has to be incss_properties, andexpression()or aurl()with a disallowed scheme is dropped even when a caller's pattern would match it. When no rule applies to an element (its tag and"*"both absent),allowed_stylesimposes nothing extra. The<style>element body stays governed bycss_propertiesalone, since sanitize-html'sallowedStylestargets inline styles.The narrowing runs inside the existing style-scrub walk; the non-
allowed_stylespath keeps its current zero-overhead scrub.Closes #533
🤖 Generated with Claude Code