Skip to content

fix(html): prevent extraction of CSS from hidden conditional comments - #14248

Draft
PsyCrow-code wants to merge 1 commit into
nextcloud:mainfrom
PsyCrow-code:fix/html-hidden-conditional-css
Draft

PsyCrow-code wants to merge 1 commit into
nextcloud:mainfrom
PsyCrow-code:fix/html-hidden-conditional-css

Conversation

@PsyCrow-code

@PsyCrow-code PsyCrow-code commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Context

This fixes a rendering problem observed in a real bank-notification email in a private Nextcloud Mail installation (Nextcloud 35, Mail 5.12.x). The text in table cells appeared compressed/overlapping in the browser, rather than having normal line spacing.

The message has Microsoft Outlook-only, downlevel-hidden conditional markup. HTMLPurifier's ExtractStyleBlocks filter runs before comments are removed, so a <style> element meant only for Outlook can escape the comment and affect the regular HTML seen in Mail.

The following minimal, anonymized reproduction uses illustrative values rather than any private email content:

<style>.regular { color: #203047; }</style>
<!--[if mso]>
<style>table td { font-size: 1px; line-height: 1px; }</style>
<![endif]-->
<table>
  <tr><td class="regular">Account notice<br>Reference: DEMO-042<br>Status: Available</td></tr>
</table>

Steps to reproduce on unpatched Mail: open an HTML message containing this hidden MSO style and a normal table, or pass the snippet through Html::sanitizeHtmlMailBody() and render the returned HTML. The MSO-only rule is extracted, and its line-height: 1px is applied to regular table cells. With this patch, the hidden MSO rule and content are discarded before extraction; normal styles and message content remain.

Related upstream behavior: ezyang/htmlpurifier#146.

Download the synthetic example email or view its source. This is a directly importable .eml fixture with no private sender, account or transaction data.

Approach

Remove only downlevel-hidden conditional comments before constructing the sanitized HTML via HTMLPurifier. The regex executes directly before the existing downlevel-revealed marker handling.

  • Preserve standard CSS blocks before/after the conditional.
  • Preserve downlevel-revealed <!--[if !mso]><!--> content and its styles, along with <![if !mso]> markers.
  • Cover lowercase, uppercase and version-gated hidden conditional variants in unit tests.
  • No vendored dependency changes, global CSS relaxations, or modifications to the frontend renderer.

Why here? Filtering after ExtractStyleBlocks is too late: the unwanted CSS has already been collected. Changing HTMLPurifier itself would affect other consumers, while this is a narrowly scoped Mail workaround.

Screenshots

These are genuine screenshots of the same email rendered in Nextcloud Mail before and after applying this change. Private information is redacted.

Before: MSO-only CSS causes overlapping text After: text has normal line spacing
Before: Nextcloud Mail email with overlapping text caused by hidden Outlook CSS After: the same email in Nextcloud Mail with readable text after filtering hidden conditional CSS

The source message contains confidential information; the independent, synthetic .eml fixture linked above is provided for reproducible testing.

Testing

  • Isolated regression exercise using HTMLPurifier 4.19.1: baseline 3/6 conditional variants passed; patched 6/6 passed.
  • PHP syntax validation and git diff --check passed.
  • Equivalent fix checked in a private Nextcloud Mail installation using the affected message.
  • Attach redacted before/after Mail UI screenshots of the affected message.
  • Full Nextcloud Mail PHPUnit suite in upstream CI / Nextcloud test environment (not run locally on the standalone checkout).
  • Recheck rendered message in the browser on Mail 5.13.3 (separate from earlier installation tests).

This PR remains draft pending maintainer review and any further verification requested.

AI assistance

Assisted by ChatGPT (GPT-6) for analysis, tests and PR documentation; code and local observations manually reviewed.

Remove hidden conditional comments before HTMLPurifier extracts style blocks to prevent Outlook-specific CSS from affecting visible message content. Preserve downlevel-revealed content and add regression tests.

Assisted-by: ChatGPT:gpt-6
Signed-off-by: PsyCrow <ing.josegonzalez@gmail.com>

@ChristophWurst ChristophWurst left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Fill out the PR original repository template, please.

Problem description sounds speculative. Please provide example emails and demonstrate the issues with screenshots of the rendered email.

Thanks

PsyCrow-code commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor Author

@ChristophWurst Thanks for the review. I've rewritten the description using the repository's Context / Approach / Screenshots / Testing / AI assistance template. I also added the concrete bank-notification rendering context and an importable, anonymized .eml reproduction of the hidden MSO stylesheet issue: fixture.

The original message contains private account information, so the fixture is synthetic. I have explicitly marked the real before/after Nextcloud Mail UI screenshots as outstanding; this PR remains in draft rather than suggesting that the requested visual evidence is complete.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants