Skip to content

Escape page-hijacking HTML in rendered message text#104

Open
pnogu wants to merge 1 commit into
simonw:mainfrom
pnogu:escape-active-html
Open

Escape page-hijacking HTML in rendered message text#104
pnogu wants to merge 1 commit into
simonw:mainfrom
pnogu:escape-active-html

Conversation

@pnogu

@pnogu pnogu commented Jul 1, 2026

Copy link
Copy Markdown

Problem

render_markdown_text() renders message content with markdown.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:

%...DEBUG: <script>top.self.location.href='http://192.168.x.x/portal?token=...'</script>

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 in render_markdown_text():

  • escapes script/iframe/object/embed/applet/meta/link/base/frame/frameset/form tags back to visible text,
  • strips inline event handlers (onerror=, onload=, …) and javascript: 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.

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>
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.

1 participant