Skip to content

Add SVG sanitization for Wagtail image uploads - #16977

Merged
stevejalim merged 4 commits into
mainfrom
WT-536--svg-sanitization
Jan 7, 2026
Merged

stevejalim merged 4 commits into
mainfrom
WT-536--svg-sanitization

Conversation

@stevejalim

@stevejalim stevejalim commented Jan 6, 2026 •

Copy link
Copy Markdown
Contributor

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
Screenshot 2026-01-06 at 14 55 16

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

  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
@stevejalim
stevejalim requested a review from a team as a code owner January 6, 2026 15:28
@stevejalim stevejalim added the WMO and FXC Code relevant to both mozilla/bedrock (www.mozilla.org) and mozmeao/springfield (www.firefox.com) label Jan 6, 2026
@codecov

codecov Bot commented Jan 6, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.05825% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.56%. Comparing base (2ed16f3) to head (c9800b2).
⚠️ Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
bedrock/cms/fields.py 97.53% 2 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@stevejalim
stevejalim requested a review from Copilot January 6, 2026 15:54

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 SanitizingWagtailImageField with three-layer SVG threat detection
  • Monkey-patch of Wagtail's formfield_for_dbfield to apply sanitization globally
  • Addition of defusedxml==0.7.1 and py-svg-hush==0.2.0 dependencies

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.

Comment thread bedrock/cms/apps.py Resolved
Comment thread bedrock/cms/fields.py Resolved
Comment thread bedrock/cms/fields.py Resolved
Comment thread bedrock/cms/fields.py Resolved
@stevejalim
stevejalim requested a review from maribedran January 6, 2026 16:29
@stevejalim
stevejalim merged commit 0be5625 into main Jan 7, 2026
6 checks passed
@stevejalim
stevejalim deleted the WT-536--svg-sanitization branch January 7, 2026 14:45
stevejalim added a commit to mozmeao/springfield that referenced this pull request Jan 7, 2026
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
stevejalim added a commit to mozmeao/springfield that referenced this pull request Jan 7, 2026
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
lucianoratamero pushed a commit to mozmeao/springfield that referenced this pull request Jan 8, 2026
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

WMO and FXC Code relevant to both mozilla/bedrock (www.mozilla.org) and mozmeao/springfield (www.firefox.com)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants