← Back to Pull requests

Sanitize user rich-text HTML server-side with nh3 (security-private#26) #1200

Closed
unknown closed this pull request without merging on Jul 22, 2026 13:36 UTC
View on GitHub
unknown unknown commented on Jul 03, 2026 15:53 UTC
Author

Fixes the stored-XSS surface tracked in EbookFoundation/security-private#26: user-authored rich text (Campaign.description/details, Work.description, edition descriptions — all authored via CKEditor) is rendered on public pages with {{ ... |safe }} and no server-side sanitization. CKEditor's own filtering runs only in the browser and is bypassable, so it is not a security boundary.

What this does


  • Adds regluit/utils/html.py — sanitize_html() using nh3 (maintained Rust/ammonia binding; bleach is deprecated). Allow-list mirrors what the CKEditor toolbar can produce (bold/italic, lists, blockquote, links, images, hr), plus common paste tags. Strips <script>, <style>, event handlers, javascript: URLs, etc.

  • Adds a sanitize template filter (frontend/templatetags/sanitizer.py).

  • Replaces |safe with |sanitize on the browser-rendered rich-text fields: home.html, work.html, download.html, bypub_list.html.

  • Pins nh3 in requirements.txt.

  • Adds SanitizeRichTextTests to frontend/tests.py.
  • Why output-sanitization (not just input)


    Sanitizing at render time is editor-independent and also neutralizes any already-stored markup, so no data migration is needed. It remains correct even if the editor is later swapped (e.g. the eventual CKEditor 5 migration).

    Scope notes


  • PDF/EPUB generation templates (pdf/ask.html, epub/ask.xhtml) are intentionally left as-is — not a browser-XSS vector, and changing them risks document validity.

  • The sanitizer was validated against script/onerror/javascript:/allowed-formatting cases; the full Django test suite runs in CI/staging.
  • Ref: security-private#26 (private — has the full surface analysis).

    Diff: 217 lines (+162 / -55) across 2 files Reviewer: eshellman Review duration: 27223m
    All Vouch review confidence checks passed
    Residual risk 0.24 = Change Risk (0.60) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.60 · 217 lines diff across 2 files
    0.60
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.322
    Shallow Review Depth shallow_review_depth
    0.289
    Sensitive Paths sensitive_paths
    0.060
    Real PR Conversation Timeline
    1 comments
    "Review active on #1200"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed sensitive_module
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 59.9% (MEDIUM) · Higher = larger blast radius
    59.9%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 23.6% · Safe to merge
    23.6%