Repository navigation
fix(web): sanitize the parsed markdown tree instead of the string (GHSA-jv5f-3mqh-cxwf) - #17590
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: ⛔ Files ignored due to path filters (1)
📒 Files selected for processing (7)
🚧 Files skipped from review as they are similar to previous changes (5)
Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review. 📝 WalkthroughWalkthroughAdds a shared assistant-markdown sanitization schema and integrates it into five React Markdown rendering pipelines. It also adds the ChangesAssistant Markdown Sanitization
Estimated code review effort: 2 (Simple) | ~10 minutes Merge Risk: ⚪ Minimal · up to The PR sanitizes the parsed markdown tree before rendering across the affected renderers, addressing unsafe raw HTML while preserving documented markdown behavior. No actionable merge-blocking risk remains beyond normal checks and review. Sequence Diagram(s)sequenceDiagram
participant MarkdownRenderer
participant rehypeRaw
participant RehypeSanitizeAssistantMarkdown
participant MarkdownTransformations
MarkdownRenderer->>rehypeRaw: parse raw HTML
rehypeRaw->>RehypeSanitizeAssistantMarkdown: pass HTML tree
RehypeSanitizeAssistantMarkdown->>MarkdownTransformations: pass sanitized tree
MarkdownTransformations->>MarkdownRenderer: render transformed markdown
Suggested labels: Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@web/src/components/next-markdown-content/index.tsx`:
- Around line 368-373: Update the markdown sanitization configuration used with
rehypeSanitizeAssistantMarkdown so markdownSanitizeSchema extends defaultSchema
with an image-only data: protocol allowance for img[src], preserving the
existing base64-image path before react-markdown’s urlTransform runs. Keep
non-image text/script data URLs rejected, and add or retain coverage for both
behaviors.
In `@web/src/constants/markdown-rehype-plugins.ts`:
- Line 17: Rename the exported top-level constants markdownSanitizeSchema and
rehypeSanitizeAssistantMarkdown to PascalCase, then update all five renderer
imports and usages to reference the renamed symbols consistently.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: e22335fa-6a0d-4b57-b452-c0e9225f9a9c
⛔ Files ignored due to path filters (1)
web/package-lock.jsonis excluded by!**/package-lock.json
📒 Files selected for processing (7)
web/package.jsonweb/src/components/floating-chat-widget-markdown.tsxweb/src/components/highlight-markdown/index.tsxweb/src/components/markdown-content/index.tsxweb/src/components/next-markdown-content/index.tsxweb/src/constants/markdown-rehype-plugins.tsweb/src/pages/next-search/markdown-content/index.tsx
|
@kah-ja Please resolve the conflict. |
…SA-jv5f-3mqh-cxwf) The assistant markdown renderers run rehype-raw, so raw HTML inside a message becomes real nodes. Sanitizing the markdown string beforehand cannot hold: preprocessLaTeX() decodes </>/& back into markup afterwards, so an entity-encoded payload passes the check inert and is live again by the time rehype-raw parses it. Sanitize the parsed tree instead, via rehype-sanitize placed after rehype-raw and before rehype-katex, using one shared allow-list. This closes the entity-encoded and raw <svg><script> vectors on all five renderers (the floating chat widget had no sanitization at all) while leaving text nodes untouched, so $a < b$ still reaches KaTeX and Array<number> survives inside code fences. The allow-list keeps data: URLs on img src so the base64 images next-markdown-content already supports keep rendering.
|
Note GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer. |
Summary
The chat and search renderers pass their content to
<Markdown rehypePlugins={[rehypeRaw, ...]}>, so raw HTML in a message becomes real nodes. The sanitization in front of that pipeline cannot hold, becausepreprocessLaTeX()(web/src/utils/chat.ts) decodes<,>and&back into markup afterDOMPurify.sanitize()has run: markup delivered entity-encoded is inert text while the allow-list inspects it, and live markup again by the timerehype-rawparses it.FloatingChatWidgetMarkdownandHighLightMarkdownreachrehype-rawwith no sanitization at all.This is the issue reported privately as GHSA-jv5f-3mqh-cxwf (not published yet).
This PR sanitizes the parsed tree instead of the string:
rehype-sanitizeruns afterrehype-rawand beforerehype-katex, with one shared schema.Why not simply move the string sanitizer to the end of the pipeline
I tried that first and withdrew it (#17589): a sanitizer applied to the markdown string cannot be both safe and faithful here. It HTML-escapes text, and the only thing that ever repaired that escaping was the entity decode -- which is the hole itself. Measured in a browser against this plugin stack:
$x^2 < y$renders as a red KaTeXParseError, because KaTeX receives<.`a < b`in an inline code span displays the literal<.Sanitizing the tree leaves text nodes untouched, so neither happens. This may also be why the sanitize pass added in #16409 was removed again in #16449 as "ineffective after preprocessLaTeX" -- the ordering was right, the layer was not.
What changed
web/src/constants/markdown-rehype-plugins.ts(new):markdownSanitizeSchema=hast-util-sanitize'sdefaultSchemaplus the wrappersreplaceThinkToSection/replaceRetrievingToSectioninject (think,retrieving,section,details,summary) andclassName; exported as a ready[rehypeSanitize, schema]tuple.rehypeRaw:markdown-content,next-markdown-content,next-search/markdown-content,floating-chat-widget-markdown,highlight-markdown.rehype-sanitize@^6.0.0andhast-util-sanitize@^5.0.2added; the lockfile diff is limited to those two entries.The existing
DOMPurify.sanitize()calls are left alone on purpose: they still normalisenull/undefinedcontent into thechat.searchingplaceholder, and they cost nothing as a second layer.Validation
Measured in headless Chromium against the real stack (react-markdown 9, rehype-raw 7, rehype-katex 7, remark-gfm/math/breaks), comparing
mainat 611e059 with this branch:<svg><script>case executes onmainand produces no element at all here;<style>and<math>are likewise dropped. Noon*attribute and nojavascript:URL survives in any of the 12 vectors tried.Array<number>,<think>and<details class="think">wrappers,<br>, images, links, citation markers,$…$and\[…\]math including\right]all render byte-identically.npx jest --runInBand: 5 failed / 71 passed, identical tomain(the failures are pre-existingpreprocessLaTeXandHighLightMarkdownspecs).npx oxlintclean,npx oxfmtreports the touched files correctly formatted.I did not add a unit test for the schema: the jest transform does not load ESM-only packages, so importing
hast-util-sanitizein a spec needstransformIgnorePatterns: ['/node_modules/(?!(hast-util-sanitize|rehype-sanitize|unist-util-position|@ungap)/)']injest.config.ts. That change makes the currently non-runningHighLightMarkdownspecs execute and fail, so I left it out of this PR. Happy to add both the config line and the spec if you want them here.