Skip to content

[INFRA-1153] fix(security): strip email style blocks in linear time - #10053

Merged
sriramveeraghanta merged 2 commits into
previewfrom
fix/infra-1153-linear-email-plaintext
Oct 18, 2026
Merged

sriramveeraghanta merged 2 commits into
previewfrom
fix/infra-1153-linear-email-plaintext

Conversation

@dheeru0198

@dheeru0198 dheeru0198 commented Oct 16, 2026 •

Copy link
Copy Markdown
Member

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 <style openers that are never closed. It now uses a linear scan.

  • New _remove_style_blocks in plane/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.
  • The result is identical to the old re.sub(r"<style[^>]*>.*?</style>", "", ..., re.DOTALL | re.IGNORECASE), including for unclosed and nested blocks.
  • No other changes to the conversion (tag stripping, entity decoding, and blank-line collapsing are unchanged).
  • Complements fix(security): stop rendering raw comment HTML unescaped in email notifications #9651, which changes how comment HTML is rendered in the issue-updates template. This PR hardens the shared helper that every email task calls.

Behaviour change: none. The plain text output is identical for every email template under templates/emails/ (12 templates, raw and rendered).

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • Feature (non-breaking change which adds functionality)
  • Improvement (change that would cause existing functionality to not work as expected)
  • Code refactoring
  • Performance improvements
  • Documentation update

Screenshots and Media (if applicable)

Test Scenarios

  • ~100k chars of <style-prefixed tags with no </style> convert in about 0.01s (about 2s before this change).
  • Style-block removal matches the original regex on representative and edge-case HTML: multiple blocks, attributes, mixed case, unclosed, nested, and openers with no >. A differential check of 300k random inputs against the old regex also matched.
  • Plain text for all email templates (raw and rendered with sample context) is byte-identical before and after.
  • 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

  • INFRA-1153

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved conversion of HTML email content to plain text by removing complete style sections, including repeated sections.
    • Preserved remaining content when a style section is missing its opening or closing delimiter.
    • Improved handling of large inputs containing unclosed style tags to avoid excessive processing 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.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Copilot AI balanced review requested due to automatic review settings October 16, 2026 19:38

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.

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.

@makeplane

makeplane Bot commented Oct 16, 2026

Copy link
Copy Markdown

@coderabbitai

coderabbitai Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 80da52e3-5110-4297-a319-28c976786712
📥 Commits

Reviewing files that changed from the base of the PR and between 8e46f99 and 54d1142.

📒 Files selected for processing: 2
  • apps/api/plane/tests/unit/utils/test_email.py
  • apps/api/plane/utils/email.py
🚧 Files skipped from review as they are similar to previous changes: 2
  • apps/api/plane/tests/unit/utils/test_email.py
  • apps/api/plane/utils/email.py

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 0 remain after this review.

📝 Walkthrough

Email HTML conversion now uses a case-insensitive scanner to remove complete style blocks. Tests compare its output with the prior regex behavior and check conversion time for input containing many style-like tags.

Changes: Email style removal

Layer / File(s) Summary
Style scanning and validation
apps/api/plane/utils/email.py, apps/api/plane/tests/unit/utils/test_email.py
generate_plain_text_from_html uses _remove_style_blocks before stripping HTML tags. Tests compare the helper’s output with the prior regex behavior and check conversion time for input containing 11,000 style-like tags.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Merge Risk: ⚪ Minimal · up to 54d11

No actionable issue remains; the change is mergeable after normal checks.

Pre-merge checks: Passed 5
Check name Status Explanation
Title check Passed The title clearly identifies the security fix and the linear-time style-block removal change.
Description check Passed The description includes the required sections, explains the change, identifies the bug-fix type, and documents test scenarios and references. The unchecked performance-improvements option is non-crit…
Docstring Coverage Passed Docstring coverage is 80.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 5 functions across 2 files.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Description check

Explanation

The description includes the required sections, explains the change, identifies the bug-fix type, and documents test scenarios and references. The unchecked performance-improvements option is non-critical because the bug-fix classification is selected and performance validation is described.

✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Reword the style-removal docstring and rename its timing test.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@lovelace3 lovelace3 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

No blocking findings.

Verified:

  • Equivalence: I ran a differential check of _remove_style_blocks against the old re.sub(r"<style[^>]*>.*?</style>", …, DOTALL|IGNORECASE) on 200k random strings built from <style/<STYLE/>/</style>/</STYLE>/</partial tokens/newlines. There were 0 mismatches. Both early breaks 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_html on ~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-base utils/email.py, 8 fail. The linear-time test is the real guard; the 7 parity cases fail only because _remove_style_blocks doesn'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.

@sriramveeraghanta
sriramveeraghanta merged commit 77ec6e7 into preview Oct 18, 2026
16 checks passed
@sriramveeraghanta
sriramveeraghanta deleted the fix/infra-1153-linear-email-plaintext branch October 18, 2026 09:57
sriramveeraghanta pushed a commit that referenced this pull request Oct 22, 2026
…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.
nettnikl pushed a commit to nettnikl/hangar that referenced this pull request Dec 1, 2026
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants