Repository navigation
fix(vscode): sanitize chat webview markdown and block remote images - #1927
Mahdi-Hasan wants to merge 2 commits into
Conversation
|
nc-review could not produce a verdict this run (agent exit |
3a4c462 to
b3ebbd0
Compare
The chat panel put marked.parse() output for replies, thoughts and replayed user messages straight into innerHTML. Raw HTML in a reply could add <style> rules, inline styles or the panel's own Tailwind classes and restyle or cover the webview. The CSP also allowed any https: image, so a reply could make the editor fetch a tracking pixel. All five marked sinks now go through setMarkdown(), which renders via the new media/markdown-utils.js. It runs marked output through a vendored DOMPurify 3.4.17 build (media/purify.min.js, loaded the same way as marked.min.js) with the HTML profile only, no <style>, style="", or <form>, prefixed ids, and no class other than a code block's language-*. If DOMPurify is missing or reports no DOM support, the panel shows the text instead of unsanitized HTML. img-src drops https:. Uploaded and pasted images are data: URIs and nothing else in the panel loads remote images. markdown-utils.spec.ts runs the shipped marked, DOMPurify and helper in jsdom (new devDependency of the extension) and checks the policy and the CSP. chat-panel-markdown.spec.ts drives the panel harness to check every sink is sanitized and that it fails closed. Fixes Nano-Collective#1727 Co-Authored-By: Claude Opus 5.5 <[email protected]>
The spec imported @/vscode/chat-panel-harness, which does not exist in source/. The harness lives in plugins/vscode/src, where the other chat-panel specs are. Co-Authored-By: Claude Haiku 5.5 <[email protected]>
b3ebbd0 to
6b81d0a
Compare
There was a problem hiding this comment.
Thanks @Mahdi-Hasan for the work here. The approach is solid and the vendored DOMPurify checks out, but #1727 is assigned to @Dhirenderchoudhary, who said they would fix it, so I'm closing this PR. Please comment on an issue and wait to be assigned before opening a PR, so work is not duplicated.
One note for whoever picks the fix up: the class filter keeps only language-*, which strips the hljs-* classes the highlighter emits and removes syntax colours from code blocks. Allow hljs and hljs-[\w-]+ and add a spec that runs a fenced block through the sanitizer.
Description
Sanitizes every markdown render in the VS Code chat webview and stops remote image loads. Fixes #1727.
marked.parse()output is written toinnerHTML, not 3: user message, first agent chunk, throttled re-render, turn-end flush, and thoughts. All 5 now go through onesetMarkdown()helper, which renders via the newmedia/markdown-utils.js.markdown-utils.jsruns marked's output through DOMPurify 3.4.17. It ships asmedia/purify.min.jsthe same waymarked.min.jsdoes, byte-identical to the npm build.<style>,style=""or<form>.idandnameget auser-content-prefix.language-*. Without this, a reply could use the panel's Tailwind classes (e.g.fixed inset-0) to cover the UI.img-srcno longer allowshttps:. Pasted and uploaded images aredata:URIs, and nothing else in the panel loads remote images.Type of Change
Changeset
pnpm changeset) describing this change for the changelogTesting
Automated Tests
New features include passing tests in
.spec.ts/tsxfilesAll existing tests pass (
pnpm test:allcompletes successfully)Tests cover both success and error scenarios
plugins/vscode/src/markdown-utils.spec.ts(14 tests) loads the shipped marked, DOMPurify and helper into jsdom. It checks what gets removed, what still renders, the plain-text fallbacks, and the CSP. jsdom is a new devDependency of the extension only, and it isn't packaged because vsce runs with--no-dependencies.source/vscode/chat-panel-markdown.spec.ts(5 tests) drives the panel harness and checks that all 5 places are sanitized.Format, types (root and vscode), lint, knip and audit pass.
I developed this on Windows, where
pnpm test:avahas many failures onmaintoo (POSIX paths,spawn EINVAL). This branch adds no new failures. CI on Linux should settle it.Manual Testing
Checklist