fix(docworker): Fix sanitizing Markdown and HTML - #437
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR hardens the document worker against unsafe HTML/Markdown content and unsafe resource fetching during rendering by introducing explicit sanitization and a URL security policy applied to both WeasyPrint resource loading and Jinja-initiated HTTP requests.
Changes:
- Add HTML sanitization via
nh3and make Markdown rendering sanitize by default (opt-out for template-controlled content). - Introduce a
securityconfiguration section and a sharedUrlPolicyused to restrictfile:access, private/loopback/link-local targets, and redirect behavior. - Add unit tests and update end-user documentation describing the new security behavior.
Reviewed changes
Copilot reviewed 19 out of 20 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
uv.lock |
Adds nh3 and a test extra (pytest) to the locked dependency set. |
pyproject.toml |
Updates Ruff Bandit configuration to allow safe Markup(...) usage for sanitizer output. |
packages/dsw-document-worker/pyproject.toml |
Adds nh3 as a runtime dependency and introduces a test optional dependency with pytest. |
packages/dsw-document-worker/Makefile |
Replaces the “no tests” target with an actual pytest invocation. |
packages/dsw-document-worker/dsw/document_worker/sanitizer.py |
New strict allow-list HTML sanitizer built on nh3. |
packages/dsw-document-worker/dsw/document_worker/model/utils.py |
Makes render_markdown(..., sanitize=True) sanitize by default and adds opt-out. |
packages/dsw-document-worker/dsw/document_worker/urls.py |
Adds UrlPolicy and URL validation utilities for HTTP/file/data URL handling. |
packages/dsw-document-worker/dsw/document_worker/model/http.py |
Wraps template HTTP requests with URL policy checks, redirect handling, and credential dropping. |
packages/dsw-document-worker/dsw/document_worker/templates/steps/template.py |
Wires UrlPolicy into the template-exposed requests wrapper. |
packages/dsw-document-worker/dsw/document_worker/templates/steps/conversion.py |
Adds a restricted WeasyPrint URL fetcher enforcing UrlPolicy. |
packages/dsw-document-worker/dsw/document_worker/config.py |
Adds security config keys + SecurityConfig model and includes it in the app config. |
packages/dsw-document-worker/config.example.yml |
Documents the new security configuration section. |
packages/dsw-document-worker/support/steps/weasyprint.md |
Documents restricted resource fetching behavior during PDF generation. |
packages/dsw-document-worker/support/steps/jinja.md |
Documents security restrictions applied to template HTTP requests. |
packages/dsw-document-worker/support/JinjaFilters.md |
Documents the Markdown filter’s default sanitization and opt-out. |
packages/dsw-document-worker/tests/test_sanitizer.py |
Adds sanitizer + Markdown rendering security tests. |
packages/dsw-document-worker/tests/test_urls.py |
Adds URL policy tests (private ranges, schemes, file constraints). |
packages/dsw-document-worker/tests/test_http.py |
Adds tests for the template HTTP wrapper (blocked URLs, redirect rules, forbidden kwargs). |
packages/dsw-document-worker/CHANGELOG.md |
Notes new security config and changed Markdown sanitization default. |
.cspell/dictionary.txt |
Adds a term used in networking code (IPPROTO). |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
MarekSuchanek
force-pushed
the
fix/sanitize-md-html
branch
from
August 17, 2026 19:21
cb8c545 to
8cc355f
Compare
MarekSuchanek
force-pushed
the
fix/sanitize-md-html
branch
from
August 17, 2026 19:22
8cc355f to
a3b3712
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.