Skip to content

Escape </style> in output of non-Root nodes (follow-up to 7f5c417) - #2163

Merged
ai merged 1 commit into
postcss:mainfrom
choudhryfrompak:escape-all-node-types
Oct 5, 2026
Merged

ai merged 1 commit into
postcss:mainfrom
choudhryfrompak:escape-all-node-types

Conversation

@choudhryfrompak

@choudhryfrompak choudhryfrompak commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Follow-up to GHSA-xcmx-fw7g-g937: 7f5c417 moved the </style> escaping from GHSA-qx2v-qp2m-jg93 into SafeStringifier, but the only override there is root(). Node#toString() and postcss.stringify() on a Rule, AtRule, Declaration or Comment go straight to the plain Stringifier methods, so they come out unescaped again. 8.5.28 escapes these; this is a regression on main, not released yet.

As discussed in the advisory thread, this isn't a security issue on its own — nothing released is affected, and there's no realistic case for calling node.toString() to build an HTML document directly. This PR just restores the output consistency that existed in 8.5.10–8.5.28: all node types get the same </style>/<!-- escaping, not just Root.

Changes

stringify() now wraps every top-level node that isn't a Root or Document in the existing escapeHTML(), matching what root() already does, including the split-payload case inside a rule's raws.

Tests

Added cases next to the existing GHSA-qx2v-qp2m-jg93 tests covering each node type (Rule, AtRule, Declaration, Comment), a detached Declaration, and postcss.stringify() with a custom builder. Renamed "escapes only Root output of the default stringifier" to "escapes only output of the default stringifier" since its assertion didn't actually test Root-only behavior. Full pnpm test passes (719 tests: coverage, lint, types, size, integration).

Summary by CodeRabbit

  • Bug Fixes
    • Improved stringifier output escaping for rules, declarations, at-rules, and comments. Closing style tags and HTML comment markers are escaped, while script content remains unchanged.

Since the escaping moved to SafeStringifier, only Root output was
escaped. Rule, AtRule, Declaration and Comment returned raw </style>
and <!-- from toString() and postcss.stringify(), which 8.5.28 still
escaped.
@coderabbitai

coderabbitai Bot commented Oct 3, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 77ec7aa5-b8f4-4a0a-95f7-e8f37df9b38c
📥 Commits

Reviewing files that changed from the base of the PR and between 7ac9026 and 693587f.

📒 Files selected for processing (2)
  • lib/stringify.js
  • test/stringifier.test.js

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


📝 Walkthrough

Walkthrough

The default stringifier now applies HTML escaping to non-root and non-document nodes. Tests cover escaping of style-closing tags and comments across several node types.

Changes

Stringifier escaping

Layer / File(s) Summary
Apply and test stringifier escaping
lib/stringify.js, test/stringifier.test.js
The stringifier escapes output for non-root and non-document nodes. Tests cover rules, declarations, at-rules, comments, and direct default-stringifier calls. They also check uppercase style-closing tags and unchanged script content.

Priority: ⬇️ Low

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

Change: Bug fix

Suggested reviewers: ai

Merge Risk: ⚪ Minimal · up to 69358

The escaping change has no established merge-blocking issue; it is ready for normal checks.

Security Architecture Review

Security architecture risk: 🔵 Low · up to 69358

The change strengthens default serialization without an identified weakening of existing controls. Remaining uncertainty concerns how applications supply untrusted CSS and embed the resulting output into HTML.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The directly affected scope is CSS emitted through default serialization of non-Root/non-Document nodes. Application, tenant, and asset exposure cannot be determined because neither an attacker-controlled input path nor a downstream HTML embedding consumer has been established.

Trust Boundaries and Controls

  • observed — The existing control replaces the opening less-than character of case-insensitive style-tag sequences and HTML comment openings with a CSS escape. It is targeted protection, not general HTML sanitization; tests deliberately retain other markup text. Supplying a custom builder to the default stringify function does not bypass this control.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: escaping output from non-Root nodes. The follow-up reference is relevant context.
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.
  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autopilot · 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.

Comment thread lib/stringify.js
if (node.type === 'root' || node.type === 'document') {
str.stringify(node)
} else {
str.escapeHTML(node, () => str.stringify(node))

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.

Can you explain why it is the best place to put escape?

Why not Node#toString for instance?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Node#toString() actually calls this same stringify() function internally, it's the default: toString(stringifier = stringify). And postcss.stringify(node, builder) is also exported directly as its own public API, used independently of toString() (lib/no-work-result.js and lib/lazy-result.js both import it directly too).

If I escaped in toString() instead, a direct call to postcss.stringify(node, builder). which is exactly one of the bypass cases in the original report would stay unescaped, since it never goes through toString() at all. Putting it here covers both entry points with one change, which is also where the existing root() escaping already lives, so it keeps all the escaping logic in one file instead of split across two.

I hope this explaination helps!

@ai
ai merged commit 7647a81 into postcss:main Oct 5, 2026
11 checks passed
pstreef added a commit to moderne-backpatch-alliance/backpatch-postcss that referenced this pull request Nov 22, 2026
Port of upstream "Escape </style> in output of non-Root nodes (postcss#2163)", which repairs the SafeStringifier
escaping of the previous commit.

Upstream-Commit: 7647a81
Co-authored-by: Choudhry Shehryar <[email protected]>
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.

2 participants