Repository navigation
[INFRA-1153] fix(security): strip email style blocks in linear time - #10053
Conversation
Replace the non-greedy <style> regex in generate_plain_text_from_html with a single forward scan that gives the same result and stops once no block can close. Co-Authored-By: Claude Opus 5.5 <[email protected]>
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.
|
Linked to Plane Work Item(s) This comment was auto-generated by Plane |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Walkthrough
Merge Risk: ⚪ Minimal · up to
|
Reword the style-removal docstring and rename its timing test. Co-Authored-By: Claude Opus 5.5 <[email protected]>
lovelace3
left a comment
There was a problem hiding this comment.
No blocking findings.
Verified:
- Equivalence: I ran a differential check of
_remove_style_blocksagainst the oldre.sub(r"<style[^>]*>.*?</style>", …, DOTALL|IGNORECASE)on 200k random strings built from<style/<STYLE/>/</style>/</STYLE>/</partial tokens/newlines. There were 0 mismatches. Both earlybreaks are correct: with no>after an opener there is none after any later opener, and with no</style>after a>there is none further on either. - Timing of the whole
generate_plain_text_from_htmlon ~100–160k-char adversarial inputs (<×N,<a×N,<style>×N,</style>×N,<style×N+>, deep<…>nesting,<!--×N, whitespace/newline runs for the blank-line regex): each took 0.04s or less. pytest plane/tests/unit/utils/test_email.py: 12 passed. Falsification: with the merge-baseutils/email.py, 8 fail. The linear-time test is the real guard; the 7 parity cases fail only because_remove_style_blocksdoesn't exist there.
Side note, unchanged by this PR: on some of those inputs (<a×N, <style>×N), Django's own strip_tags raises SuspiciousOperation (its MAX_STRIP_TAGS_DEPTH guard). It fails fast rather than burning CPU. In send_email_notification, the outer except Exception logs it and releases the lock, so it isn't a worker-pinning path. Comment HTML is also nh3-sanitized on write, so I didn't treat this as a finding.
…10053) * [INFRA-1153] fix(security): strip email style blocks in linear time Replace the non-greedy <style> regex in generate_plain_text_from_html with a single forward scan that gives the same result and stops once no block can close. * [INFRA-1153] fix(security): address review Reword the style-removal docstring and rename its timing test.
Port of upstream makeplane#10053, makeplane#9841 and makeplane#9865. The <style> removal is a single forward scan instead of a backtracking regex. The text/plain part decodes character references, so an invitation link pasted from it keeps its query string. The file's licence header is the project's AGPL one, as upstream corrected it.
Description
generate_plain_text_from_html(used for the text/plain part of every outgoing email) removed<style>blocks with a non-greedy regex whose cost grows quadratically when the input contains<styleopeners that are never closed. It now uses a linear scan._remove_style_blocksinplane/utils/email.py: finds each<style, its closing>, then the next</style>(case-insensitive), each search resuming where the last ended. Once an opener has no closing>or</style>after it, no later block can close either, so the scan stops.re.sub(r"<style[^>]*>.*?</style>", "", ..., re.DOTALL | re.IGNORECASE), including for unclosed and nested blocks.Behaviour change: none. The plain text output is identical for every email template under
templates/emails/(12 templates, raw and rendered).Type of Change
Screenshots and Media (if applicable)
Test Scenarios
<style-prefixed tags with no</style>convert in about 0.01s (about 2s before this change).>. A differential check of 300k random inputs against the old regex also matched.plane/tests/unit/utils/test_email.py+plane/tests/contract/app/test_authentication.py: 46 passed. The new linear-time test fails against the old implementation (1.88s against a 1s bound).References
🤖 Generated with Claude Code
Summary by CodeRabbit