
Security News
The Hidden Blast Radius of the Axios Compromise
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.
A tiny, zero-dependency, run-anywhere HTML sanitization library written in TypeScript.
A tiny, zero-dependency, run-anywhere HTML sanitization library written in TypeScript.
npm install unsane
Unsane requires Node.js 14 or later.
// ES Modules
import { sanitize } from "unsane";
// CommonJS
const { sanitize } = require("unsane");
// Input: potentially malicious HTML
const dirty =
'<script>alert("xss")</script><div onclick="alert(`pwned`)">Hello</div>';
// Output: clean HTML with dangerous elements/attributes removed
const clean = sanitize(dirty);
// -> '<div>Hello</div>'
You can customize the sanitizer behavior with options:
import { sanitize } from "unsane";
const options = {
// Custom list of allowed tags
allowedTags: ["p", "span", "strong", "em", "a", "img"],
// Custom list of allowed attributes for each tag
allowedAttributes: {
a: ["href", "target"],
img: ["src", "alt", "width", "height"],
"*": ["id", "class"], // Attributes allowed on all elements
},
// Allowed URL schemes for href/src attributes
allowedProtocols: ["http:", "https:", "mailto:"]
};
const dirty =
'<script>alert("xss")</script><a href="https://example.com" onclick="hack()" style="color:red">Link</a>';
const clean = sanitize(dirty, options);
// -> '<a href="https://example.com">Link</a>'
Available options:
allowedTags – array of tag names that are kept in the sanitized output.allowedAttributes – object mapping tag names to allowed attributes. Use
"*" for attributes allowed on all tags.allowedProtocols – array of URL protocols allowed in attributes like
href or src.import { encode, decode, escape } from "unsane";
// Encode special characters into entities
const encoded = encode('<div>"text"</div>');
// -> '<div>"text"</div>'
// Decode HTML entities
const decoded = decode("<div>"text"</div>");
// -> '<div>"text"</div>'
// Escape HTML special characters
const escaped = escape('<script>"alert"</script>');
// -> '<script>"alert"</script>'
You can also sanitize input directly from the command line:
echo '<script>alert("xss")</script>' | npx unsane
This reads HTML from stdin and prints the sanitized result to stdout.
This library is designed to be lightweight while providing comprehensive HTML sanitization:
| Metric | Size |
|---|---|
| Unpacked | ~15.69 KB |
| Minified | ~3.1 KB |
| Minified + Gzipped | ~1.31 KB |
You can check the package size yourself with:
npm run analyze-size
<!DOCTYPE>, <html>, <head>) are normalized but not guaranteed to preserve structure.style attributes are dropped), JavaScript, MathML, or SVG namespaces—content in those namespaces is removed rather than partially sanitized. It does not attempt to sanitize inline <style> blocks or external resources (<link>, <script>, <iframe>, etc.) and should be paired with CSPs.allowedTags/allowedAttributes meet your application’s needs, run application-specific allowlist tests, and apply additional sanitization for CSS/URL rewriting if end users can supply styles or alternate protocols.Unsane is designed to protect against common XSS vectors:
<script>, <style>, <iframe>, etc.onclick, onerror, etc.)javascript: URLs and other dangerous protocolsWorks in all modern browsers as well as Node.js environments. No DOM or browser APIs are required.
Please see CONTRIBUTING.md for instructions on setting up the project and running tests. The dist directory is generated and should not be committed.
MIT
FAQs
A tiny, zero-dependency, run-anywhere HTML sanitization library written in TypeScript.
We found that unsane demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.

Research
A supply chain attack on Axios introduced a malicious dependency, plain-crypto-js@4.2.1, published minutes earlier and absent from the project’s GitHub releases.

Research
Malicious versions of the Telnyx Python SDK on PyPI delivered credential-stealing malware via a multi-stage supply chain attack.