hast-util-sanitize is a JavaScript utility designed to make HAST (Hypertext Abstract Syntax Tree) nodes safe by removing potentially dangerous user-generated content, primarily to prevent Cross-Site Scripting (XSS) vulnerabilities. Currently at version 5.0.2, the package is actively maintained as part of the unified ecosystem, with a steady release cadence addressing features and breaking changes. It provides a robust, extensible schema, defaulting to GitHub's sanitation rules, allowing developers to customize allowed elements, attributes, and attribute values. This makes it a crucial tool for applications handling untrusted HTML input, either directly or through higher-level wrappers like rehype-sanitize. Its focus on direct HAST manipulation allows for fine-grained control over the sanitization process.
npm install hast-util-sanitizeVerified import paths — ran on the pinned version, not inferred.
Demonstrates sanitizing a HAST tree containing various XSS attack vectors using the default GitHub-style schema, showing the transformation from unsafe to safe HTML.
Migrate your project to use ES modules (`import`/`export`) or ensure you are in an environment that transpiles ESM correctly. Update your Node.js version to 16+.
Always use the public API paths, typically `hast-util-sanitize` for named exports, and avoid relying on internal file structures.
Ensure your TypeScript configuration is compatible, and review any custom type declarations that might now conflict with the official types.
If `rel` or `target` attributes are essential for specific elements (e.g., `a` tags), you must explicitly add them back to your custom schema by extending `defaultSchema`.
Thoroughly review any modifications made to `defaultSchema` or entirely custom schemas to ensure they do not permit dangerous elements, attributes, or protocols.
Refactor your code to use `import { sanitize } from 'hast-util-sanitize';` in an ES module context (e.g., by setting `"type": "module"` in your `package.json` or using `.mjs` files). If stuck in CommonJS, consider dynamic import: `const { sanitize } = await import('hast-util-sanitize');`.Always import symbols directly from the main package entry point: `import { someExport } from 'hast-util-sanitize';`. Do not rely on deep imports into `lib/` or other internal directories.No dependency data recorded yet.