
Security News
PodRocket Podcast: Inside the Recent npm Supply Chain Attacks
Socket CEO Feross Aboukhadijeh discusses the recent npm supply chain attacks on PodRocket, covering novel attack vectors and how developers can protect themselves.
harden-react-markdown
Advanced tools
A security-focused wrapper for react-markdown that filters URLs based on allowed prefixes
A wrapper for react-markdown that ensures that untrusted markdown does not contain images from and links to unexpected origins.
This is particularly important for markdown returned from LLMs in AI agents which might have been subject to prompt injection.
This package validates URL prefixes and URL origins. Prefix allow-lists can be circumvented with open redirects, so make sure to make the prefixes are specific enough to avoid such attacks.
E.g. it is more secure to allow https://example.com/images/
than it is to allow all of
https://example.com/
which may contain open redirects.
Additionally, URLs may contain path traversal like /../
. This package does not resolve these.
It is your responsibility that your web server does not allow such traversal.
npm install harden-react-markdown react react-markdown
# or
yarn add harden-react-markdown react react-markdown
# or
pnpm add harden-react-markdown react react-markdown
import React from "react";
import ReactMarkdown from "react-markdown";
import hardenReactMarkdown from "harden-react-markdown";
// Create a hardened version of ReactMarkdown
const HardenedMarkdown = hardenReactMarkdown(ReactMarkdown);
function MyComponent() {
const markdown = `
# My Document
[Safe Link](https://github.com/user/repo)
[Blocked Link](https://malicious-site.com)


`;
return (
<HardenedMarkdown
defaultOrigin="https://mysite.com"
allowedLinkPrefixes={["https://github.com/", "https://docs."]}
allowedImagePrefixes={["https://via.placeholder.com/", "/"]}
>
{markdown}
</HardenedMarkdown>
);
}
hardenReactMarkdown(MarkdownComponent)
Creates a hardened version of any react-markdown compatible component.
MarkdownComponent
: A React component that accepts Options
from react-markdownA new component with enhanced security that accepts all original props plus:
defaultOrigin?: string
allowedLinkPrefixes
or allowedImagePrefixes
are provided"https://mysite.com"
allowedLinkPrefixes?: string[]
[blocked]
"*"
to allow all URLs (disables filtering. However, javascript:
and data:
URLs are always disallowed)[]
(blocks all links)['https://github.com/', 'https://docs.example.com/']
or ['*']
allowedImagePrefixes?: string[]
"*"
to allow all URLs (disables filtering. However, javascript:
and data:
URLs are always disallowed)[]
(blocks all images)['https://via.placeholder.com/', '/']
or ['*']
All other props are passed through to the wrapped markdown component.
const HardenedMarkdown = hardenReactMarkdown(ReactMarkdown);
// Blocks all external links and images by default
<HardenedMarkdown>{markdownContent}</HardenedMarkdown>;
<HardenedMarkdown
defaultOrigin="https://mysite.com"
allowedLinkPrefixes={[
"https://github.com/",
"https://docs.github.com/",
"https://www.npmjs.com/",
]}
allowedImagePrefixes={[
"https://via.placeholder.com/",
"https://images.unsplash.com/",
"/", // Allow relative images
]}
>
{markdownContent}
</HardenedMarkdown>
<HardenedMarkdown
defaultOrigin="https://mysite.com"
allowedLinkPrefixes={["https://mysite.com/"]}
allowedImagePrefixes={["https://mysite.com/"]}
>
{`
[Relative Link](/internal-page)

`}
</HardenedMarkdown>
<HardenedMarkdown allowedLinkPrefixes={["*"]} allowedImagePrefixes={["*"]}>
{`
[Any Link](https://anywhere.com/link)

`}
</HardenedMarkdown>
Note: Using "*"
disables URL filtering entirely. Only use this when you trust the markdown source.
const CustomMarkdown = (props) => (
<div className="custom-wrapper">
<ReactMarkdown {...props} />
</div>
);
const HardenedCustomMarkdown = hardenReactMarkdown(CustomMarkdown);
<HardenedCustomMarkdown
defaultOrigin="https://mysite.com"
allowedLinkPrefixes={["https://trusted.com/"]}
>
{markdownContent}
</HardenedCustomMarkdown>;
href
attributes in <a>
elementssrc
attributes in <img>
elementsdefaultOrigin
../
attacks"*"
prefix to disable filtering (only when markdown is trusted)[blocked]
indicatorjavascript:
, data:
, vbscript:
and other dangerous protocolsFull TypeScript support with strict type checking:
// Type-safe component creation
const HardenedMarkdown = hardenReactMarkdown(ReactMarkdown);
// Inferred prop types include both react-markdown Options and security options
type Props = Parameters<typeof HardenedMarkdown>[0];
// Works with custom markdown components
const CustomMarkdown = (props: Options & { customProp?: string }) => (
<ReactMarkdown {...props} />
);
const HardenedCustom = hardenReactMarkdown(CustomMarkdown);
// Props now include customProp + security options
The package includes comprehensive tests covering:
Run tests:
npm test
git checkout -b feature/amazing-feature
)git commit -m 'Add some amazing feature'
)git push origin feature/amazing-feature
)MIT License - see the LICENSE file for details.
If you discover a security vulnerability, please send an e-mail to security@vercel.com.
FAQs
A security-focused wrapper for react-markdown that filters URLs based on allowed prefixes
The npm package harden-react-markdown receives a total of 157,028 weekly downloads. As such, harden-react-markdown popularity was classified as popular.
We found that harden-react-markdown demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket CEO Feross Aboukhadijeh discusses the recent npm supply chain attacks on PodRocket, covering novel attack vectors and how developers can protect themselves.
Security News
Maintainers back GitHub’s npm security overhaul but raise concerns about CI/CD workflows, enterprise support, and token management.
Product
Socket Firewall is a free tool that blocks malicious packages at install time, giving developers proactive protection against rising supply chain attacks.