Escape page-hijacking HTML in rendered message text#104
Open
pnogu wants to merge 1 commit into
Open
Conversation
render_markdown_text() runs message content through markdown.markdown(),
which passes raw HTML through verbatim. Any active-content HTML in a
message (outside a code fence) therefore becomes a live element in the
transcript. In practice this bites when a session pastes captive-portal
debug logs containing e.g.
<script>top.self.location.href='http://.../portal?...'</script>
The <script> executes on page load and the browser navigates away, so the
generated transcript never renders at all.
Add neutralize_active_html(), applied to the markdown output, which escapes
script/iframe/meta/etc. tags back to visible text and strips inline event
handlers and javascript: URIs. Markdown never emits these tags itself, so
normal content is unaffected — the existing HTML snapshots pass unchanged.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
render_markdown_text()renders message content withmarkdown.markdown(...), which passes raw HTML through verbatim. Any active-content HTML in a message that isn't inside a code fence therefore becomes a live element in the generated transcript.This is not just a theoretical XSS concern — it makes real transcripts unopenable. I hit it with a Claude Code session that pasted captive-portal debug logs containing a redirect payload:
When you open the generated
index.html, that<script>executes on load and the browser immediately navigates away to the captive-portal URL. The transcript never renders.Fix
Add
neutralize_active_html(), applied to the markdown output inrender_markdown_text():script/iframe/object/embed/applet/meta/link/base/frame/frameset/formtags back to visible text,onerror=,onload=, …) andjavascript:URIs.Markdown never emits any of these tags itself, so normal content is unaffected — the existing HTML snapshot tests pass unchanged. HTML that appears inside a fenced code block is already escaped by markdown and stays visible as text.
Tests
Added three focused tests (script neutralization, event-handler /
javascript:stripping, and a "normal markdown is preserved" case). Full suite:125 passed,18 snapshots passed.