|
| 1 | +# safe-html-react-parser |
| 2 | + |
| 3 | +A secure wrapper for **html-react-parser** with **isomorphic-dompurify** that automatically sanitizes HTML before parsing. |
| 4 | + |
| 5 | +## What it does |
| 6 | + |
| 7 | +- 🛡️ **Security**: Automatically sanitizes malicious HTML using DOMPurify |
| 8 | +- ⚛️ **React**: Seamlessly integrates with html-react-parser |
| 9 | +- 🌐 **Universal**: Works in both browser and Node.js (SSR) environments |
| 10 | +- 🏷️ **Custom Tags**: Handles project-specific tags like `<custom>` safely |
| 11 | + |
| 12 | +## Requirements |
| 13 | + |
| 14 | +- Node.js >=20.19.5: isomorphic-dompurify@^2.30.1 |
| 15 | + |
| 16 | +## Installation |
| 17 | + |
| 18 | +```bash |
| 19 | +npm install @naverpay/safe-html-react-parser |
| 20 | +``` |
| 21 | + |
| 22 | +## Basic Usage |
| 23 | + |
| 24 | +```tsx |
| 25 | +import { safeParse } from '@naverpay/safe-html-react-parser' |
| 26 | + |
| 27 | +// Basic usage - automatically sanitizes dangerous HTML |
| 28 | +const Component = () => { |
| 29 | + const maliciousHtml = '<p>Hello <script>alert("XSS")</script>World</p>' |
| 30 | + return <div>{safeParse(maliciousHtml)}</div> |
| 31 | +} |
| 32 | +// Result: <div><p>Hello World</p></div> |
| 33 | +``` |
| 34 | + |
| 35 | +## API |
| 36 | + |
| 37 | +### `safeParse(htmlString, options?)` |
| 38 | + |
| 39 | +Parses HTML string into React elements with automatic XSS protection. |
| 40 | + |
| 41 | +#### Parameters |
| 42 | + |
| 43 | +- `htmlString` (string): The HTML string to parse |
| 44 | +- `options` (SafeParseOptions, optional): Configuration options |
| 45 | + |
| 46 | +#### Options |
| 47 | + |
| 48 | +```typescript |
| 49 | +interface SafeParseOptions extends HTMLReactParserOptions { |
| 50 | + // DOMPurify configuration |
| 51 | + sanitizeConfig?: DOMPurify.Config |
| 52 | + |
| 53 | + // Custom tags to preserve during sanitization |
| 54 | + preserveCustomTags?: string[] |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +#### Returns |
| 59 | + |
| 60 | +React elements or array of React elements |
| 61 | + |
| 62 | +## Advanced Usage |
| 63 | + |
| 64 | +### Custom Sanitization Config |
| 65 | + |
| 66 | +```tsx |
| 67 | +import { safeParse } from '@naverpay/safe-html-react-parser' |
| 68 | + |
| 69 | +const html = '<div class="content"><style>body{color:red}</style><p>Text</p></div>' |
| 70 | + |
| 71 | +const result = safeParse(html, { |
| 72 | + sanitizeConfig: { |
| 73 | + ALLOWED_TAGS: ['div', 'p', 'style'], // Allow style tags |
| 74 | + ALLOWED_ATTR: ['class'], |
| 75 | + ALLOW_ARIA_ATTR: true |
| 76 | + } |
| 77 | +}) |
| 78 | +``` |
| 79 | + |
| 80 | +### Preserving Custom Tags |
| 81 | + |
| 82 | +Use `preserveCustomTags` to preserve project-specific tags that would otherwise be removed: |
| 83 | + |
| 84 | +```tsx |
| 85 | +import { safeParse } from '@naverpay/safe-html-react-parser' |
| 86 | + |
| 87 | +// Preserve custom tags like <g>, <path>, etc. |
| 88 | +const svgContent = '<g><path d="M10,10 L20,20"/></g>' |
| 89 | + |
| 90 | +const result = safeParse(svgContent, { |
| 91 | + preserveCustomTags: ['g', 'path'], |
| 92 | + replace: (domNode) => { |
| 93 | + if (domNode.name === 'g') { |
| 94 | + return <g {...domNode.attribs}>{/* custom rendering */}</g> |
| 95 | + } |
| 96 | + if (domNode.name === 'path') { |
| 97 | + return <path {...domNode.attribs} /> |
| 98 | + } |
| 99 | + } |
| 100 | +}) |
| 101 | +``` |
| 102 | + |
| 103 | +### Using with html-react-parser Options |
| 104 | + |
| 105 | +All html-react-parser options are supported: |
| 106 | + |
| 107 | +```tsx |
| 108 | +import { safeParse } from '@naverpay/safe-html-react-parser' |
| 109 | + |
| 110 | +const html = '<div id="content"><p>Hello</p><img src="image.jpg" alt="test"/></div>' |
| 111 | + |
| 112 | +const result = safeParse(html, { |
| 113 | + replace: (domNode) => { |
| 114 | + if (domNode.name === 'img') { |
| 115 | + return <img {...domNode.attribs} loading="lazy" /> |
| 116 | + } |
| 117 | + }, |
| 118 | + trim: true |
| 119 | +}) |
| 120 | +``` |
| 121 | + |
| 122 | +## Default Allowed Tags |
| 123 | + |
| 124 | +By default, the following HTML tags are allowed: |
| 125 | + |
| 126 | +```typescript |
| 127 | +ALLOWED_TAGS: [ |
| 128 | + 'p', 'br', 'strong', 'em', 'b', 'i', 'u', 'span', 'div', |
| 129 | + 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'h', |
| 130 | + 'ul', 'ol', 'li', 'dl', 'dt', 'dd', |
| 131 | + 'a', 'img' |
| 132 | +] |
| 133 | +``` |
| 134 | + |
| 135 | +## Security Notes |
| 136 | + |
| 137 | +- All HTML is sanitized by DOMPurify before parsing |
| 138 | +- Dangerous tags like `<script>`, `<iframe>`, `<object>` are automatically removed |
| 139 | +- Event handlers like `onclick`, `onload` are stripped out |
| 140 | +- Only safe attributes are preserved by default |
| 141 | + |
| 142 | +## Built with |
| 143 | + |
| 144 | +- [html-react-parser@^5.2.7](https://github.com/remarkablemark/html-react-parser) - HTML string to React element parser |
| 145 | +- [isomorphic-dompurify@^2.30.1](https://github.com/kkomelin/isomorphic-dompurify) - Universal XSS sanitizer |
| 146 | + |
| 147 | +## License |
| 148 | + |
| 149 | +MIT |
0 commit comments