Repository navigation
Add SVG sanitization for Wagtail image uploads - #16977
Conversation
Implements three-layer defense to prevent XSS attacks via malicious SVG files: 1. Fast regex detection for obvious threats (scripts, event handlers) 2. XML parsing with defusedxml to detect dangerous elements/attributes 3. py-svg-hush as safety net for edge cases SVGs containing scripts, event handlers (onclick, onload, etc.), or javascript: URLs are rejected with clear error messages. Clean SVGs from design tools pass without false positives. Changes: - Add SanitizingWagtailImageField that extends WagtailImageField - Monkey-patch Wagtail's formfield_for_dbfield to use custom field - Add defusedxml==0.7.1 and py-svg-hush==0.2.0 dependencies - Comprehensive test coverage (19 tests) for detection and validation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #16977 +/- ##
==========================================
+ Coverage 80.36% 80.56% +0.19%
==========================================
Files 163 164 +1
Lines 9100 9203 +103
==========================================
+ Hits 7313 7414 +101
- Misses 1787 1789 +2 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR adds SVG sanitization to prevent stored XSS attacks via malicious SVG uploads in Wagtail. A custom SanitizingWagtailImageField validates SVG files using three detection layers: regex pattern matching, XML parsing with defusedxml, and py-svg-hush as a safety net. Malicious SVGs with scripts or event handlers are rejected during form validation, before storage.
Key Changes
- Custom
SanitizingWagtailImageFieldwith three-layer SVG threat detection - Monkey-patch of Wagtail's
formfield_for_dbfieldto apply sanitization globally - Addition of
defusedxml==0.7.1andpy-svg-hush==0.2.0dependencies
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
requirements/prod.txt |
Added py-svg-hush and defusedxml dependencies with hashes; updated botocore and google-auth |
requirements/prod.in |
Added defusedxml and py-svg-hush to direct dependencies |
requirements/docs.txt |
Updated pathspec version |
requirements/dev.txt |
Mirrored production dependency updates |
bedrock/cms/tests/test_svg_sanitization.py |
Comprehensive test suite (19 tests) covering sanitization logic |
bedrock/cms/test_image_models.py |
Integration test verifying BedrockImage uses sanitizing field |
bedrock/cms/fields.py |
Core implementation of SanitizingWagtailImageField with three-layer detection |
bedrock/cms/apps.py |
Monkey-patch registration in Django app ready() hook |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
While we only ever upload SVGs to Wagtail that we have authored ourselves, we still want to be as protected as can be. As such, this changeset aims to prevent Stored XSS attacks via malicious SVG files. 1. Fast regex detection for obvious threats (scripts, event handlers) 2. XML parsing with defusedxml to detect dangerous elements/attributes 3. py-svg-hush as safety net for edge cases SVGs containing scripts, event handlers (onclick, onload, etc.), or javascript: URLs are rejected with clear error messages. Clean SVGs from design tools pass without false positives. Changes: - Add SanitizingWagtailImageField that extends WagtailImageField - Monkey-patch Wagtail's formfield_for_dbfield to use custom field - Add defusedxml==0.7.1 and py-svg-hush==0.2.0 dependencies - Comprehensive test coverage (19 tests) for detection and validation Port of mozilla/bedrock#16977
While we only ever upload SVGs to Wagtail that we have authored ourselves, we still want to be as protected as can be. As such, this changeset aims to prevent Stored XSS attacks via malicious SVG files. 1. Fast regex detection for obvious threats (scripts, event handlers) 2. XML parsing with defusedxml to detect dangerous elements/attributes 3. py-svg-hush as safety net for edge cases SVGs containing scripts, event handlers (onclick, onload, etc.), or javascript: URLs are rejected with clear error messages. Clean SVGs from design tools pass without false positives. Changes: - Add SanitizingWagtailImageField that extends WagtailImageField - Monkey-patch Wagtail's formfield_for_dbfield to use custom field - Add defusedxml==0.7.1 and py-svg-hush==0.2.0 dependencies - Comprehensive test coverage (19 tests) for detection and validation Port of mozilla/bedrock#16977
While we only ever upload SVGs to Wagtail that we have authored ourselves, we still want to be as protected as can be. As such, this changeset aims to prevent Stored XSS attacks via malicious SVG files. 1. Fast regex detection for obvious threats (scripts, event handlers) 2. XML parsing with defusedxml to detect dangerous elements/attributes 3. py-svg-hush as safety net for edge cases SVGs containing scripts, event handlers (onclick, onload, etc.), or javascript: URLs are rejected with clear error messages. Clean SVGs from design tools pass without false positives. Changes: - Add SanitizingWagtailImageField that extends WagtailImageField - Monkey-patch Wagtail's formfield_for_dbfield to use custom field - Add defusedxml==0.7.1 and py-svg-hush==0.2.0 dependencies - Comprehensive test coverage (19 tests) for detection and validation Port of mozilla/bedrock#16977
While we only ever upload SVGs to Wagtail that we have authored ourselves, we still want to be as protected as can be. As such, this changeset aims to prevent Stored XSS attacks via malicious SVG files.
SVGs containing scripts, event handlers (onclick, onload, etc.), or javascript: URLs are rejected with clear error messages. Clean SVGs from design tools pass without false positives.
Changes:
Opinions very welcome on whether there's a better place to run the check - this change aims to catch a bad file as soon as possible, before the original image can be stored
Issue / Bugzilla link
JIRA: WT-583
Testing
Go to /cms-admin/ and try to upload two SVGs:
This one should be rejected evil.svg (which has a
<script>tag in it)This one should be accepted: one.svg