Repository navigation
Replace bleach with justhtml for HTML sanitization - #16999
Conversation
There was a problem hiding this comment.
Pull request overview
Replaces deprecated bleach library with justhtml for HTML sanitization throughout the codebase. Justhtml is validated against 7,000+ XSS vectors and provides modern sanitization capabilities including tag escaping instead of removal for improved security.
Changes:
- Created shared sanitization utilities in a new module providing
strip_all_tags()andsanitize_html()functions - Updated all existing bleach usages to use the new justhtml-based sanitization
- Updated dependencies to remove bleach and add justhtml
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| requirements/prod.txt | Removed bleach dependency and added justhtml>=0.14.0, updated transitive dependencies |
| requirements/prod.in | Replaced bleach[css]==6.1.0 with justhtml>=0.14.0 |
| requirements/docs.txt | Updated transitive dependencies |
| requirements/dev.txt | Updated transitive dependencies |
| bedrock/base/sanitization.py | Created new shared sanitization module with strip_all_tags() and sanitize_html() functions |
| bedrock/base/tests/test_sanitization.py | Added comprehensive test coverage for new sanitization functions |
| lib/l10n_utils/templatetags/fluent.py | Migrated from bleach to sanitize_html() with updated attribute structure |
| bedrock/wordpress/models.py | Migrated from bleach to strip_all_tags() |
| bedrock/releasenotes/models.py | Migrated from bleach to sanitize_html() with updated attribute structure |
| bedrock/mozorg/templatetags/misc.py | Migrated from bleach to strip_all_tags() |
| bedrock/careers/management/commands/sync_greenhouse.py | Replaced custom bleach filter with justhtml policy including header tag conversion |
b8adf69 to
e8c5522
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #16999 +/- ##
==========================================
+ Coverage 81.10% 81.25% +0.14%
==========================================
Files 157 161 +4
Lines 8319 8390 +71
==========================================
+ Hits 6747 6817 +70
- Misses 1572 1573 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Bleach is deprecated and no longer maintained. This migrates all HTML
sanitization to justhtml, a modern alternative validated against 7,000+
XSS vectors.
## Changes
**New shared module** (`bedrock/base/sanitization.py`):
- `strip_all_tags()` - removes all HTML tags, returns text
- `sanitize_html()` - allowlist-based sanitization with tag escaping
**Updated files**:
- `bedrock/wordpress/models.py` - uses strip_all_tags
- `bedrock/mozorg/templatetags/misc.py` - bleach_tags filter uses strip_all_tags
- `lib/l10n_utils/templatetags/fluent.py` - uses sanitize_html
- `bedrock/releasenotes/models.py` - uses sanitize_html
- `bedrock/careers/management/commands/sync_greenhouse.py` - uses justhtml
directly with its own policy (includes h1/h2/h3→h4 conversion)
**Dependencies**:
- Added: `justhtml>=0.14.0`
- Removed: `bleach[css]==6.1.0`
- Kept: `html5lib==1.1` (still used by BeautifulSoup elsewhere)
## Behavioral differences from bleach
- Script/style tag content is dropped entirely (more secure than bleach's
default of preserving content)
- URL attributes (href, src) are validated against allowed schemes
(http, https, mailto, tel)
Fixes WT-578
cd20be7 to
71228fc
Compare
stephaniehobson
left a comment
There was a problem hiding this comment.
Code sanity check and local spot-checks all look good.
R+ 🧼
Bleach is deprecated and no longer maintained (see github.com/mozilla/bleach/issues/698).
This changeset migrates all HTML sanitization to justhtml, as recommended by bleach's maintainer.
Changes
New shared module (
bedrock/base/sanitization.py):strip_all_tags()- removes all HTML tags, returns textsanitize_html()- allowlist-based sanitization with tag escapingUpdated files:
bedrock/wordpress/models.py- uses strip_all_tagsbedrock/mozorg/templatetags/misc.py- bleach_tags filter uses strip_all_tagslib/l10n_utils/templatetags/fluent.py- uses sanitize_htmlbedrock/releasenotes/models.py- uses sanitize_htmlbedrock/careers/management/commands/sync_greenhouse.py- uses justhtml directly with its own policy (includes h1/h2/h3→h4 conversion)Dependencies:
justhtml>=0.14.0bleach[css]==6.1.0html5lib==1.1(still used by BeautifulSoup elsewhere)Behavioral differences from bleach
Fixes WT-578