Registry / web-framework / react-sanitizer-parser

react-sanitizer-parser

JSON →
library0.1.4jsnpmunverified

The `react-sanitizer-parser` package is a React component and utility library designed to safely render HTML content within React applications, mitigating XSS vulnerabilities. It acts as a convenient wrapper around two well-established libraries: `html-react-parser` for converting HTML strings into React elements and `DOMPurify` for robust HTML sanitization. As of version 0.1.4, it provides a `<ReactSanitizerParser>` component that takes a `dirty` HTML string as children, along with optional `htmlParserOptions` and `sanitizerConfig` props to fine-tune the behavior of its underlying dependencies. Additionally, it re-exports the `parse` function from `html-react-parser` and the `DOMPurify` object directly for more granular, imperative usage. Its primary differentiator is simplifying the integration of HTML parsing and sanitization into React, offering a streamlined API compared to configuring both libraries independently.

npm install react-sanitizer-parser
INSTALL
IMPORT
SIG · REACT-SANITIZER-PA
R
react-sanitizer-parser
web-frameworkjavascriptv0.1.4
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

ReactSanitizerParser
✓ import ReactSanitizerParser from 'react-sanitizer-parser';
✗ import { ReactSanitizerParser } from 'react-sanitizer-parser'; // Not a named export, it's the default
This is the default export, representing the main React component.
parse
✓ import { parse } from 'react-sanitizer-parser';
✗ import parse from 'react-sanitizer-parser'; // 'parse' is a named export, not the default
Re-exports the `parse` function directly from `html-react-parser` for imperative parsing.
DOMPurify
✓ import { DOMPurify } from 'react-sanitizer-parser';
✗ import DOMPurify from 'react-sanitizer-parser'; // 'DOMPurify' is a named export, not the default
Re-exports the `DOMPurify` object directly from the `dompurify` library for imperative sanitization.

This quickstart demonstrates the primary usage of the `ReactSanitizerParser` component, including how to pass `sanitizerConfig` for custom DOMPurify rules. It also shows the direct use of the re-exported `parse` function from `html-react-parser` and the `DOMPurify` object for imperative HTML processing.

import React from 'react'; import ReactSanitizerParser, { parse, DOMPurify } from "react-sanitizer-parser"; const App = () => { const dirtyHtmlWithScript = ` <div> <p>This is some <strong>safe</strong> content.</p> <script>alert('XSS attempt!');</script> <img src="x" onerror="alert('Another XSS!');"> <a href="javascript:alert('Even more XSS!');">Click me</a> <p style="color:red;">Styled paragraph</p> </div> `; // Example of a highly restricted DOMPurify configuration const highlyRestrictedConfig = { USE_PROFILES: { html: false, svg: false, mathMl: false }, // Disable all profiles FORBID_TAGS: ['div', 'span', 'img', 'a', 'p'], FORBID_ATTR: ['style', 'href', 'src', 'onerror'], }; // Using the re-exported DOMPurify directly const pureHtml = DOMPurify.sanitize("<img src=x onerror=alert(1)//>"); console.log("DOMPurify direct sanitize:", pureHtml); // Expect: <img src="x"> // Using the re-exported parse function directly const pureParse = parse("<h2>Parsed directly without sanitization</h2>"); return ( <div> <h1>Using ReactSanitizerParser Component</h1> <p>Default sanitization (scripts, onerror, javascript:href removed, style kept by default):</p> <ReactSanitizerParser>{dirtyHtmlWithScript}</ReactSanitizerParser> <h2>Sanitization with custom DOMPurify config (highly restricted):</h2> <ReactSanitizerParser sanitizerConfig={highlyRestrictedConfig}> {dirtyHtmlWithScript} </ReactSanitizerParser> <h2>Direct parse from html-react-parser re-export (no sanitization by default):</h2> {pureParse} <h2>Result of DOMPurify re-export (imperative sanitization):</h2> <div dangerouslySetInnerHTML={{ __html: pureHtml }} /> </div> ); }; export default App;
Debug
Known issues
gotchaWhile `react-sanitizer-parser` bundles `DOMPurify` for security, the default sanitization configuration might not be strict enough for all applications or high-security contexts. Developers should thoroughly review DOMPurify's configuration options and apply a custom `sanitizerConfig` if their use case requires stricter filtering of HTML elements or attributes.
fix
Pass a custom `sanitizerConfig` prop to `ReactSanitizerParser` or `DOMPurify.sanitize()` directly, specifying allowed tags, attributes, and other rules according to your application's security requirements.
affects: >=0.1.0
breakingAs a wrapper library, `react-sanitizer-parser`'s behavior is intrinsically linked to its underlying dependencies, `html-react-parser` and `DOMPurify`. Major version updates in these dependencies (e.g., `html-react-parser` v5 or `DOMPurify` v3) could introduce breaking changes or subtle behavioral shifts that `react-sanitizer-parser` might not entirely abstract away or immediately update to reflect in its own minor versions.
fix
Always test `react-sanitizer-parser` thoroughly after updating its peer or direct dependencies. Consult the changelogs of `html-react-parser` and `DOMPurify` for potential impacts on sanitization rules or parsing logic.
affects: >=0.1.0
gotchaThe re-exported `parse` function from `html-react-parser` does *not* automatically sanitize HTML. If used directly without prior sanitization via `DOMPurify` or another mechanism, it can expose your application to XSS vulnerabilities by rendering malicious HTML.
fix
When using `import { parse } from 'react-sanitizer-parser';`, ensure that the HTML string passed to `parse()` has been pre-sanitized using `DOMPurify.sanitize()` (also re-exported) or another trusted sanitizer. Example: `parse(DOMPurify.sanitize(dirtyHtml))`.
affects: >=0.1.0
gotchaPassing non-string values to the `children` prop of `<ReactSanitizerParser>` can lead to rendering issues or unexpected behavior, as the component expects an HTML string to parse and sanitize.
fix
Ensure that the `children` prop of `<ReactSanitizerParser>` is always a valid HTML string. If you have dynamic content that might not be a string, convert it explicitly or handle empty states.
affects: >=0.1.0
Errors
Common errors & fixes
Error: React Sanitizer Parser received `children` that are not a string. Expected a string.
The `children` prop provided to the `ReactSanitizerParser` component was not a string.
fix
Ensure that the `children` prop passed to `<ReactSanitizerParser>` is always an HTML string. For example: `<ReactSanitizerParser>{String(myContent)}</ReactSanitizerParser>` if `myContent` might be a number or null.
TypeError: Cannot read properties of undefined (reading 'sanitize')
Attempting to use `DOMPurify.sanitize` without correctly importing the `DOMPurify` object, possibly due to a wrong import type (e.g., default import instead of named).
fix
Use the correct named import for `DOMPurify`: `import { DOMPurify } from 'react-sanitizer-parser';`
Warning: A future version of React will block javascript: URLs as a security precaution.
HTML content passed to `ReactSanitizerParser` or `DOMPurify` contains `javascript:` URLs in attributes (like `href` or `src`), and while `DOMPurify` handles these, React still issues a warning as a general security practice for `dangerouslySetInnerHTML` contexts.
fix
This is typically a warning from React itself, not an error with `react-sanitizer-parser` if `DOMPurify` is configured to disallow `javascript:` URLs (which it does by default). Review your `DOMPurify` configuration via `sanitizerConfig` to ensure strict removal of such URLs. Consider replacing `javascript:` URLs with event handlers or preventing their insertion at the source.
Upgrade
Version history
0.1.4latest on npm
Audit
Dependencies
html-react-parserrequiredCore dependency for converting HTML strings into React elements.
dompurifyrequiredCore dependency for sanitizing HTML content to prevent XSS attacks.
reactrequiredPeer dependency, required for any React application using this component.
react-domrequiredPeer dependency, required for rendering React components.
Agent activity
15 hits · last 30 days
node
12
OpenAI (training)
1
Resources
react-sanitizer-parser — npm install react-sanitizer-parser · libregistry