An independently built Nuxt module for sanitizing HTML with a configurable allowlist, powered by the parse5 HTML5 parser. The same sanitizer runs on the server and in the browser.
- 🛡️ Sanitize HTML content with a configurable allowlist and a fixed safety cap
- 🎯
v-purrrifydirective for templates with full TypeScript support - 📦 Support for multiple sanitization profiles
- 🖥️ SSR support without a browser DOM
- ⚡ ESM-only modules — no CJS support
- 🔧 Opt-in uppercase configuration aliases alongside camelCase options
Install the module to your Nuxt application:
npx nuxt module add purrrifyOr manually:
bun add -D purrrifyThen add it to your nuxt.config.ts:
export default defineNuxtConfig({
modules: ['purrrify']
})Use the v-purrrify directive to sanitize HTML content in your templates:
<template>
<div v-purrrify="dirtyHtml" />
</template>
<script setup>
const dirtyHtml = `
<div>
<h1>Welcome to My Website</h1>
<p>This is a <strong>safe</strong> paragraph.</p>
<img src="image.jpg" onerror="alert('Hacked!')" />
<a href="https://example.com" onclick="stealCookies()">Click me!</a>
<script>alert('XSS!')<\/script>
</div>`
</script>Define different sanitization configurations using the supported options:
export default defineNuxtConfig({
modules: ['purrrify'],
purrrify: {
profiles: {
headingsOnly: {
allowedTags: ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']
},
plainText: {
allowedTags: [],
allowedAttributes: {}
}
}
}
})Pass the profile name as an argument to the directive:
<template>
<div v-purrrify:headingsOnly="dirtyHtml" />
</template>Purrrify can sanitize Vue’s built-in v-html before HTML reaches the DOM or SSR response. This feature is disabled by default and does not change existing directives.
Enable the compiler integration in Nuxt config:
export default defineNuxtConfig({
modules: ['purrrify'],
purrrify: {
safeVHtml: true,
profiles: {
headingsOnly: { allowedTags: ['h1', 'h2', 'h3'] }
}
}
})<template>
<!-- Opt in with the default sanitizer -->
<div v-html="dirtyHtml" purrrify />
<!-- Selecting a profile also opts this element in -->
<div v-html="dirtyHtml" purrrify-profile="headingsOnly" />
<!-- Bound controls and profile changes are reactive -->
<div v-html="dirtyHtml" :purrrify="enabled" :purrrify-profile="selectedProfile" />
<!-- Unmarked v-html retains normal Vue behavior; use only trusted HTML -->
<div v-html="trustedHtml" />
</template>safeVHtml: true is shorthand for safeVHtml: { mode: 'opt-in' }. The purrrify and purrrify-profile controls are consumed by the compiler on selected HTML elements; they do not become rendered attributes or component props. Normal attributes such as class, id, and ARIA attributes on the containing element are unchanged.
export default defineNuxtConfig({
modules: ['purrrify'],
purrrify: {
safeVHtml: { mode: 'all', profile: 'article' },
profiles: {
article: { allowedTags: ['p', 'strong', 'em', 'a'] },
headingsOnly: { allowedTags: ['h1', 'h2', 'h3'] }
}
}
})<template>
<!-- Automatically sanitized with the configured default profile -->
<div v-html="dirtyHtml" />
<!-- Override the default profile -->
<div v-html="dirtyHtml" purrrify-profile="headingsOnly" />
<!-- Explicit bypass: trusted HTML only -->
<div v-html="trustedHtml" :purrrify="false" />
</template>- Omit
profileto use the default sanitizer. Unknown profile names also use the default sanitizer. - Only the boolean
falsebypasses sanitization. Literalpurrrify="false"is a string and does not bypass it. - Both modes preserve reactive updates and use identical sanitization for SSR and client rendering. HTML is sanitized before insertion, not after mounting.
- This integration covers templates processed by Nuxt’s Vue compiler. It does not intercept render-function
innerHTML, third-party precompiled components, or direct DOM assignments. - On protected elements, use explicit bindings such as
:class="classes". Objectv-bind="attrs", computedv-bind:[key], and additionalinnerHTML/textContentbindings are rejected because they could overwrite sanitized content or produce different SSR/client output. - Use normal HTML containers. Selected SVG/MathML and raw-text targets such as
script,style, ortextareaare rejected at compile time. Component props are not intercepted. - Controls require this feature to be enabled. When disabled, Vue handles
v-htmland attributes normally. Changing the module option requires restarting the dev server or rebuilding.
Enable compatibility: true to accept uppercase configuration aliases alongside camelCase options. Existing camelCase profiles keep working, and camelCase takes precedence when both forms specify the same setting.
Purrrify can be used as an opt-in replacement for Radya’s @radya/nuxt-dompurify module while keeping the existing directive syntax and supported profile options. Use purrrify in your modules list instead of registering both modules, and enable compatibility in Nuxt config:
export default defineNuxtConfig({
modules: ['purrrify'],
purrrify: {
compatibility: true
},
dompurify: {
profiles: {
headingsOnly: {
ALLOWED_TAGS: ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'],
ALLOWED_ATTR: []
}
}
}
})Your existing template works unchanged, with no custom element props, extra attributes, or directive modifiers:
<template>
<div v-sanitize-html:headingsOnly="dirtyHtml" />
</template>Purrrify provides native SSR support for this directive: HTML is sanitized during server rendering and on client updates using the same parse5-based engine. No browser DOM, jsdom setup, or client-only wrapper is required.
Compatibility mode reads dompurify.profiles as well as purrrify.profiles; a profile defined in purrrify.profiles takes precedence when names overlap. The directive alias itself remains available without compatibility mode; the opt-in enables uppercase options and the alternate config key. Supported aliases are listed below, and Purrrify’s safety cap still applies.
export default defineNuxtConfig({
modules: ['purrrify'],
purrrify: {
compatibility: true,
profiles: {
headings: { ALLOWED_TAGS: ['h1', 'h2'], ALLOWED_ATTR: [] },
links: { allowedTags: ['a'], allowedAttributes: ['href', 'title'] },
mixed: { ALLOWED_TAGS: ['p', 'strong'], allowedAttributes: { '*': ['class'] } }
}
}
})<template>
<div v-purrrify:headings="dirtyHtml" />
</template>Compatibility is configuration support, not a different sanitization engine. All profiles use parse5 and Purrrify’s fixed safety rules. Unsupported options are rejected rather than silently ignored.
| Uppercase alias | CamelCase option |
|---|---|
ALLOWED_TAGS |
allowedTags |
ALLOWED_ATTR |
allowedAttributes (a flat list applies to all tags) |
ADD_TAGS |
additionalTags |
ADD_ATTR |
additionalAttributes |
FORBID_TAGS |
forbiddenTags |
FORBID_ATTR |
forbiddenAttributes |
FORBID_CONTENTS |
nonTextTags |
ALLOW_DATA_ATTR |
allowDataAttributes |
ALLOW_ARIA_ATTR |
allowAriaAttributes |
KEEP_CONTENT |
keepContent |
Compatibility mode retains safe data-* and aria-* attributes unless disabled. For standalone calls, pass compatibility: true inside the options object. Without that switch, uppercase aliases are rejected.
The ESM-only purrrify/sanitizer subpath exports sanitizeHtml (default and named), defaults, and the SanitizerOptions type. Only ESM imports are supported; there are no CJS entry points.
import sanitizeHtml, { defaults, type SanitizerOptions } from 'purrrify/sanitizer'
// Alternatively: import { sanitizeHtml } from 'purrrify/sanitizer'
const options: SanitizerOptions = {
allowedTags: [...defaults.allowedTags, 'img'],
allowedAttributes: { ...defaults.allowedAttributes, img: ['src', 'alt'] }
}
const cleanHtml = sanitizeHtml('<p>Hello</p><img src="https://example.com/cat.jpg">', options)Profiles and standalone calls accept these camelCase options (readonly arrays are supported):
| Option | Type / behavior |
|---|---|
allowedTags |
string[]; defaults include formatting and table tags, but not img. Images require opt-in, as above. |
allowedAttributes |
string[] or Record<string, string[]>; exact attribute names or trailing * patterns (such as data-*), with a global '*' tag key. |
allowedClasses |
Record<string, string[]>; class allowlists by tag (or '*'). The class attribute must also be allowed in allowedAttributes. |
allowedSchemes |
string[]; can only restrict the safe schemes: http, https, ftp, mailto, tel. |
allowedSchemesByTag |
Record<string, string[]>; overrides allowedSchemes for that tag, still bounded by the safe schemes. |
allowProtocolRelative |
boolean; defaults to true. Set to false to reject protocol-relative URLs such as //example.com. |
nonTextTags |
string[]; additional tags whose entire subtrees should be dropped. |
additionalTags / additionalAttributes |
string[]; extend the chosen allowlists, within the safety cap. |
forbiddenTags / forbiddenAttributes |
string[]; exclude entries even when allowed or added. |
allowDataAttributes / allowAriaAttributes |
boolean; retain safe data-* / aria-* attributes without listing them individually. false removes them even if allowlisted. |
keepContent |
boolean; defaults to true. Set to false to remove the contents of disallowed elements too. Dangerous subtrees are always removed. |
compatibility |
boolean; defaults to false. Accept uppercase aliases in standalone calls or individual profiles. |
Options cannot override the safety cap: scripts, CSS/style, SVG/MathML, embeds, custom elements, event attributes, id/name, and srcset cannot be enabled.
- SSR and browser use the same
parse5ESM parser. No native DOM is required, but the sanitizer is not dependency-free. - Use sanitized output only in normal HTML containers (for example, the directive’s
<div>), not in script, style, textarea, or foreign-content insertion contexts such as SVG/MathML. - Output is a string, not
TrustedHTML. Strict Trusted Types enforcement requires application-level integration.
Local development
# Install dependencies
bun install
# Generate type stubs
bun run dev:prepare
# Develop with the playground
bun run dev
# Build the playground
bun run dev:build
# Run ESLint
bun run lint
# Run Vitest
bun run test
bun run test:watch
# Release new version
bun run releaseMIT — Copyright (c) 2026 Yanuar Aditia.