Skip to content

Replace bleach with justhtml for HTML sanitization - #16999

Merged
stevejalim merged 1 commit into
mainfrom
WT-578-replace-bleach
Jan 22, 2026
Merged

stevejalim merged 1 commit into
mainfrom
WT-578-replace-bleach

Conversation

@stevejalim

@stevejalim stevejalim commented Jan 21, 2026 •

Copy link
Copy Markdown
Contributor

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 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

@stevejalim
stevejalim requested a review from a team as a code owner January 21, 2026 15:45
@stevejalim stevejalim added the WMO and FXC Code relevant to both mozilla/bedrock (www.mozilla.org) and mozmeao/springfield (www.firefox.com) label Jan 21, 2026
@stevejalim
stevejalim requested review from Copilot and removed request for a team January 21, 2026 15:46

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

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() and sanitize_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

Comment thread bedrock/base/sanitization.py Resolved
@stevejalim
stevejalim force-pushed the WT-578-replace-bleach branch from b8adf69 to e8c5522 Compare January 21, 2026 16:03
@codecov

codecov Bot commented Jan 21, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.25%. Comparing base (8b25cf2) to head (71228fc).
⚠️ Report is 3 commits behind head on main.

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.
📢 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 22, 2026 13:39

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

Copilot reviewed 15 out of 15 changed files in this pull request and generated 2 comments.

Comment thread bedrock/base/sanitization.py Resolved
      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
@stevejalim
stevejalim force-pushed the WT-578-replace-bleach branch from cd20be7 to 71228fc Compare January 22, 2026 14:36

@stevejalim stevejalim left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

self-r: r+

@stephaniehobson stephaniehobson 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.

Code sanity check and local spot-checks all look good.

R+ 🧼

@stevejalim
stevejalim merged commit d6af030 into main Jan 22, 2026
6 checks passed
@stevejalim
stevejalim deleted the WT-578-replace-bleach branch January 22, 2026 20:51
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