The xss module filters untrusted HTML using configurable tag and attribute whitelists. It runs on Node.js, in browsers, and includes a command-line processor.
Overview
xss is a library for sanitizing user-supplied HTML to reduce cross-site scripting risk. It accepts HTML input and removes or escapes tags and attributes that are not permitted by a whitelist you define, with optional custom handlers for matched or ignored elements.
Configuration covers default and custom whitelists, hooks for tags and attributes, HTML and attribute escaping, CSS filtering when style is allowed, stripping of disallowed tags or tag bodies, and optional comment removal. It can return sanitized markup alone or, through a variant API, both the cleaned HTML and a list of removed elements.
The same core logic is exposed for server-side Node.js use, browser script inclusion with shim or AMD loading, and a CLI that processes files or interactive stdin input. Documentation positions it for sites that let users submit formatted HTML, such as forums, blogs, and shops.
Compared with other validators mentioned in the project materials, maintainers publish benchmark figures showing higher throughput for this module on sample test code. Extension points include FilterXSS instances with persistent options and integration with a separate cssfilter module for style values.
Key features
- Configurable HTML tag whitelist
- Custom tag and attribute handlers
- CSS filtering for style attributes
- Strip or escape disallowed tags
- Browser and Node.js APIs
- Command-line file processing
- Removed-element reporting option
- HTML comment filtering
Use cases
- Sanitizing forum or blog user HTML
- Filtering rich text in web applications
- Batch cleaning HTML files via CLI
- Client-side XSS mitigation in browsers
- Controlled formatting in e-commerce content