Drop unsafe SMIL attributeName values in SVG sanitization#911
Open
giuscris wants to merge 2 commits into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors DOM attribute sanitization to make it overridable per sanitizer, and tightens SVG SMIL handling by dropping unsafe attributeName values on SMIL animation elements to prevent URI-bearing attributes from being indirectly targeted.
Changes:
- Extracted single-attribute sanitization into
DomSanitizer::sanitizeNodeAttribute()and updated the attribute loop to call it. - Added SMIL-specific logic in
SvgSanitizerto removeattributeNamewhen it targets an unsafe attribute. - Centralized the SMIL element list in
SvgReference::SMIL_ELEMENTS.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| formwork/src/Sanitizer/DomSanitizer.php | Refactors per-attribute sanitization into an overridable method used by the attribute loop. |
| formwork/src/Sanitizer/SvgSanitizer.php | Overrides attribute sanitization to drop unsafe SMIL attributeName values for animation elements. |
| formwork/src/Sanitizer/Reference/SvgReference.php | Adds centralized SMIL_ELEMENTS reference list used by SvgSanitizer. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+243
to
+248
| if (!in_array($domAttr->nodeName, $this->allowedAttributes, true)) { | ||
| $domElement->removeAttribute($domAttr->nodeName); | ||
| } | ||
|
|
||
| $scheme = Uri::scheme($uri); | ||
| if (in_array($domAttr->nodeName, $this->uriAttributes, true)) { | ||
| $uri = $this->sanitizeUri((string) $domAttr->nodeValue); |
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.
This pull request refactors and strengthens the SVG and DOM sanitization logic, with a particular focus on improving the handling of SVG SMIL animation elements. The main changes involve extracting attribute sanitization into its own method, introducing special handling for SMIL
attributeNameattributes, and centralizing SMIL-related reference data.Sanitization refactor and SMIL handling:
sanitizeNodeAttribute, improving code clarity and reusability. (formwork/src/Sanitizer/DomSanitizer.php, formwork/src/Sanitizer/DomSanitizer.phpL234-R257)SvgSanitizer, overriddensanitizeNodeAttributeto add special handling for SMIL animation elements: if an element is a SMIL animation type and itsattributeNameis not considered safe, the attribute is removed. (formwork/src/Sanitizer/SvgSanitizer.php, formwork/src/Sanitizer/SvgSanitizer.phpR50-R62)isSafeSmilAttributeNameto determine if a SMILattributeNameis allowed (i.e., not a URI attribute and present in allowed attributes). (formwork/src/Sanitizer/SvgSanitizer.php, formwork/src/Sanitizer/SvgSanitizer.phpR101-R109)Centralization of SMIL reference data:
SMIL_ELEMENTSinSvgReferencecontaining the list of SVG SMIL animation element names that require special attribute handling. (formwork/src/Sanitizer/Reference/SvgReference.php, formwork/src/Sanitizer/Reference/SvgReference.phpR321-R333)protected array $smilElementsproperty toSvgSanitizer, initialized fromSvgReference::SMIL_ELEMENTS, to make SMIL element checks consistent and maintainable. (formwork/src/Sanitizer/SvgSanitizer.php, formwork/src/Sanitizer/SvgSanitizer.phpR22-R26)