HTML sanitization with optional DOMPurify integration for XSS prevention.
import { HtmlSanitizer } from '@zappzarapp/browser-utils/sanitize';
// Basic sanitization (built-in)
const safe = HtmlSanitizer.sanitize(
'<script>alert("xss")</script><p>Hello</p>'
);
// Result: '<p>Hello</p>'
// Check if input is safe
if (HtmlSanitizer.isSafe('<p>Safe text</p>')) {
element.innerHTML = userInput;
}| Method | Returns | Description |
|---|---|---|
sanitize() |
string |
Remove dangerous HTML elements and attributes |
isSafe() |
boolean |
Check if HTML is safe (no dangerous content) |
stripTags() |
string |
Remove all HTML tags, keeping only text |
// Basic usage - removes scripts, event handlers, etc.
const safe = HtmlSanitizer.sanitize(untrustedHtml);
// With DOMPurify (if available)
const safeWithOptions = HtmlSanitizer.sanitize(untrustedHtml, {
allowedTags: ['p', 'b', 'i', 'a'],
allowedAttributes: ['href', 'class'],
});
// Returns input unchanged if DOMPurify not available and options provided
const result = HtmlSanitizer.sanitize(html, options);| Option | Type | Description |
|---|---|---|
allowedTags |
string[] |
Tags to allow (others removed) |
allowedAttributes |
string[] |
Attributes to allow (others removed) |
allowDataUri |
boolean |
Allow data: URIs (default: false) |
The built-in sanitizer removes:
<script>tags<style>tags<iframe>tags<object>tags<embed>tags<link>tags<meta>tags- Event handler attributes (
onclick,onerror, etc.) javascript:URLsdata:URLs (unless explicitly allowed)
function displayComment(comment: string): void {
const container = document.getElementById('comments');
const sanitized = HtmlSanitizer.sanitize(comment);
container.insertAdjacentHTML('beforeend', sanitized);
}// Get plain text from HTML
const text = HtmlSanitizer.stripTags('<p>Hello <b>World</b></p>');
// Result: 'Hello World'
// Useful for previews or search indexing
const preview = HtmlSanitizer.stripTags(richContent).slice(0, 100);if (HtmlSanitizer.isSafe(userInput)) {
element.innerHTML = userInput;
} else {
// Either sanitize or show error
element.innerHTML = HtmlSanitizer.sanitize(userInput);
console.warn('User input contained unsafe HTML');
}For production use, install DOMPurify for more robust sanitization:
npm install dompurify
npm install --save-dev @types/dompurifyimport DOMPurify from 'dompurify';
// DOMPurify is detected automatically when imported
const safe = HtmlSanitizer.sanitize(untrustedHtml, {
allowedTags: ['p', 'a', 'b', 'i', 'ul', 'li'],
allowedAttributes: ['href', 'class'],
});- Defense in Depth - Always sanitize user-provided HTML before insertion
- DOMPurify Recommended - Built-in sanitizer covers common cases; DOMPurify handles edge cases and browser quirks
- Context Matters - Sanitized HTML is safe for
innerHTML, but still validate for specific contexts (e.g., URL attributes) - No Trusted Input - Even content from your own database should be sanitized before display
- DOMParser Required -
stripTags()requires a browser environment withDOMParser. It throws aSanitizeErrorifDOMParseris unavailable (e.g., in some server-side environments). No regex fallback is used, as regex-based HTML parsing is inherently unsafe.