Skip to content

📑 fix: Sanitize Markdown Artifacts#12249

Merged
danny-avila merged 4 commits into
devfrom
fix/markdown-artifact-xss
Mar 15, 2026
Merged

📑 fix: Sanitize Markdown Artifacts#12249
danny-avila merged 4 commits into
devfrom
fix/markdown-artifact-xss

Conversation

@danny-avila

Copy link
Copy Markdown
Owner

Replace marked-react with react-markdown + remark-gfm for artifact markdown preview. react-markdown's skipHtml strips raw HTML tags, and a urlTransform guard blocks javascript: and data: protocol links.

Replace marked-react with react-markdown + remark-gfm for artifact
markdown preview. react-markdown's skipHtml strips raw HTML tags,
and a urlTransform guard blocks javascript: and data: protocol links.
Copilot AI review requested due to automatic review settings March 15, 2026 21:51

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens markdown artifact previews in the client by replacing the current markdown renderer with react-markdown + remark-gfm, aiming to prevent raw HTML rendering and block unsafe link protocols in the sandboxed preview.

Changes:

  • Swap marked-react for react-markdown with remark-gfm, enabling skipHtml and a urlTransform safety guard.
  • Update Sandpack markdown artifact dependencies to include react-markdown and remark-gfm.
  • Adjust markdown artifact unit tests to reflect the new renderer output.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
client/src/utils/markdown.ts Updates the generated Sandpack MarkdownRenderer implementation to use react-markdown with HTML skipping and URL protocol filtering.
client/src/utils/artifacts.ts Updates the dependency map for markdown/plaintext artifacts to install react-markdown and remark-gfm in Sandpack.
client/src/utils/tests/markdown.test.ts Updates expectations to match the new generated MarkdownRenderer code.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

const markdownDependencies = {
'marked-react': '^2.0.0',
'react-markdown': '^9.0.1',
'remark-gfm': '^4.0.0',
Comment on lines +26 to +32
<ReactMarkdown
remarkPlugins={[remarkGfm]}
skipHtml={true}
urlTransform={(url) => (isSafeUrl(url) ? url : '')}
>
{content}
</ReactMarkdown>
Comment thread client/src/utils/markdown.ts Outdated
- Convert isSafeUrl from denylist to allowlist (http, https, mailto, tel
  plus relative/anchor URLs); unknown protocols are now fail-closed
- Add remark-breaks to restore single-newline-to-<br> behavior that was
  silently dropped when replacing marked-react
- Export isSafeUrl from the host module and add 16 direct unit tests
  covering allowed protocols, blocked schemes (javascript, data, blob,
  vbscript, file, custom), edge cases (empty, whitespace, mixed case)
- Hoist remarkPlugins to a module-level constant to avoid per-render
  array allocation in the generated Sandpack component
- Fix import order in generated template (shortest to longest per
  AGENTS.md) and remove pre-existing trailing whitespace
- urlTransform returns null (not '') for blocked URLs so react-markdown
  omits the href/src attribute entirely instead of producing <a href="">
- Hoist urlTransform to module-level constant alongside remarkPlugins
- Add JSDoc sync-guard comments tying the exported isSafeUrl to its
  template-string mirror, so future maintainers know to update both
- Add synchronization test asserting the embedded isSafeUrl contains the
  same allowlist set, URL parsing, and relative-path checks as the export
@danny-avila danny-avila changed the title 🛡️ fix: Sanitize markdown artifact rendering 🛡️ fix: Sanitize Markdown Artifacts Mar 15, 2026
@danny-avila danny-avila changed the title 🛡️ fix: Sanitize Markdown Artifacts 📑 fix: Sanitize Markdown Artifacts Mar 15, 2026
@danny-avila danny-avila merged commit f9927f0 into dev Mar 15, 2026
7 checks passed
@danny-avila danny-avila deleted the fix/markdown-artifact-xss branch March 15, 2026 22:40
jcbartle pushed a commit to jcbartle/LibreChat that referenced this pull request May 11, 2026
* 🛡️ fix: Sanitize markdown artifact rendering to prevent stored XSS

Replace marked-react with react-markdown + remark-gfm for artifact
markdown preview. react-markdown's skipHtml strips raw HTML tags,
and a urlTransform guard blocks javascript: and data: protocol links.

* fix: Update useArtifactProps test to expect react-markdown dependencies

* fix: Harden markdown artifact sanitization

- Convert isSafeUrl from denylist to allowlist (http, https, mailto, tel
  plus relative/anchor URLs); unknown protocols are now fail-closed
- Add remark-breaks to restore single-newline-to-<br> behavior that was
  silently dropped when replacing marked-react
- Export isSafeUrl from the host module and add 16 direct unit tests
  covering allowed protocols, blocked schemes (javascript, data, blob,
  vbscript, file, custom), edge cases (empty, whitespace, mixed case)
- Hoist remarkPlugins to a module-level constant to avoid per-render
  array allocation in the generated Sandpack component
- Fix import order in generated template (shortest to longest per
  AGENTS.md) and remove pre-existing trailing whitespace

* fix: Return null for blocked URLs, add sync-guard comments and test

- urlTransform returns null (not '') for blocked URLs so react-markdown
  omits the href/src attribute entirely instead of producing <a href="">
- Hoist urlTransform to module-level constant alongside remarkPlugins
- Add JSDoc sync-guard comments tying the exported isSafeUrl to its
  template-string mirror, so future maintainers know to update both
- Add synchronization test asserting the embedded isSafeUrl contains the
  same allowlist set, URL parsing, and relative-path checks as the export
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