Repository navigation
fix(server): stop sanitize_input from double-escaping web-UI text - #785
Closed
Giorgio-Germani wants to merge 1 commit into
Closed
Giorgio-Germani wants to merge 1 commit into
Giorgio-Germani wants to merge 1 commit into
Conversation
bleach.clean(tags=[], strip=True) entity-escapes surviving text
("&" -> "&"), so every web-UI write through sanitize_input stored
double-escaped data (Jinja re-escapes on output). API writes were
unaffected, which is why only some entries were broken.
- decode entities once after stripping tags; XSS safety stays with
output encoding (Jinja auto-escape, markdown/nl2br filters)
- verified no template relies on pre-escaped storage (no |safe on
plain text fields; markdown/nl2br escape internally)
- add scripts/fix_double_escaped_entities.py: idempotent one-off
cleanup for tasks, time_entries, clients, projects, issues, users,
stock items (dry-run by default, --apply to write)
- unit tests covering the regression
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.
Problem
Every text written through a web-UI form that passes
sanitize_input()is stored HTML-escaped in the DB:bleach.clean(value, tags=[], strip=True)removes tags but entity-escapes the surviving text (&→&,<→<). Jinja then auto-escapes again on output, so stored data was effectively double-escaped.API-v1 writes (desktop, mobile, browser extension) never call
sanitize_input(), which is why only web-created entries were affected — and raw API consumers displayed them with literal&(e.g. task "Error Handling & Security Improvements", notes "Referenzen & Über uns …"). We hit this with real data on our self-hosted instance and traced it back to this function.Fix
sanitize_input()now decodes entities once after stripping tags:<script>alert(1)</script>hello→alert(1)hello— inert plain text; bleach keeps text between stripped tags).markdownandnl2brtemplate filters escape internally).|safeoccurrences inapp/templates/: every user-text path goes through| markdown | safeor| nl2br | safe, both escape internally — no template relied on pre-escaped storage.Also included
tests/test_validation_sanitize.py— 6 regression tests (plain&preserved, tags stripped, entities typed as text decode to inert plain text, truncation intact).scripts/fix_double_escaped_entities.py— optional one-off cleanup for existing rows: idempotent (html.unescapeonly where an entity pattern matches), dry-run by default,--applyto write. Covers the 7 models whose fields flow throughsanitize_input()(tasks, time entries, clients, projects, issues, users, stock items). We ran it against our instance after taking apg_dumpbackup: 5 rows fixed, re-run confirms no-op.Testing
pytest tests/test_validation_sanitize.py(new, 6 pass)