Skip to content

fix(html): make raw() trust marker unforgeable and hoist escape map#1473

Merged
pi0 merged 1 commit into
mainfrom
fix/html-issues
Jul 15, 2026
Merged

fix(html): make raw() trust marker unforgeable and hoist escape map#1473
pi0 merged 1 commit into
mainfrom
fix/html-issues

Conversation

@pi0x

@pi0x pi0x commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Follow-up fixes for the HTML-escaping change in #1459 (html / raw / escapeHtml).

1. raw() trust marker was globally forgeable (security)

raw() tagged trusted HTML with Symbol.for("h3.rawHTML") — a key from the global symbol registry. Because the key is discoverable, any object carrying it (Symbol.for("h3.rawHTML") set to true) could forge a trusted RawHTML value and be rendered unescaped by the html `` tagged template, re-introducing XSS.

Switched to a module-private, unregistered Symbol("h3.rawHTML") so the marker cannot be reproduced outside this module.

This is a defense-in-depth hardening: plain string/JSON user input can't carry symbol keys, so it requires an object bearing the symbol to reach an interpolation — but it's cheap to close.

2. escapeHtml reallocated the replacement map per matched char (efficiency)

The 5-entry map object literal was built inside the replace callback, allocating a fresh object for every special character matched. escapeHtml runs once per interpolated value on the per-request render path. Hoisted it to a module-level constant. Behavior unchanged.

Tests

Updated the existing test that asserted the forgeable behavior as a feature — it now asserts a forged Symbol.for("h3.rawHTML") object is escaped rather than trusted. Full suite + lint pass.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved HTML safety by rejecting forged trusted-HTML markers.
    • Ensured untrusted HTML-like objects are escaped rather than rendered as markup.
    • Streamlined HTML escaping for more consistent processing.

Use a module-private, unregistered symbol for the `raw()` trust marker
instead of `Symbol.for("h3.rawHTML")`. The global registry key was
discoverable, so any object carrying it (a second h3 realm, third-party
code, or a plain `Symbol.for(...)` call) could forge a trusted RawHTML
value and bypass escaping in the `html\`\`` tagged template.

Also hoist the escape-character map in `escapeHtml` to a module-level
constant so it is no longer reallocated for every matched character on
the per-request render path. Behavior is unchanged.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@pi0x
pi0x requested a review from pi0 as a code owner July 15, 2026 09:55
@coderabbitai

coderabbitai Bot commented Jul 15, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 4d4e149d-d035-4ed3-915c-35c3ec538fab

📥 Commits

Reviewing files that changed from the base of the PR and between 66f47c3 and b95e88d.

📒 Files selected for processing (2)
  • src/utils/response.ts
  • test/utils.test.ts

📝 Walkthrough

Walkthrough

The trusted HTML marker is now module-local instead of globally registered, tests reject forged markers, and HTML escaping uses a shared lookup table.

Changes

Trusted HTML handling

Layer / File(s) Summary
Module-local raw HTML marker
src/utils/response.ts, test/utils.test.ts
The raw HTML marker now uses a module-local symbol, and tests verify that global-symbol markers are escaped rather than trusted.
Shared HTML escape lookup
src/utils/response.ts
escapeHtml uses the shared HTML_ESCAPES table during replacement.

Estimated code review effort: 1 (Trivial) | ~4 minutes

Possibly related PRs

  • h3js/h3#1459: Both changes update trusted HTML branding and spoofed-marker tests.

Suggested reviewers: mixelburg, pi0

Poem

I’m a rabbit guarding markup bright,
No forged global tag slips by tonight.
A local symbol marks what’s true,
Escape tables keep entities in view.
Hop, hop—safe HTML anew!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately reflects the main HTML security and refactor changes in raw() and escape handling.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/html-issues

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.

@pi0
pi0 merged commit 17e0cbd into main Jul 15, 2026
9 checks passed
@pi0
pi0 deleted the fix/html-issues branch July 15, 2026 10:17
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