npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

xss-sanitize

v1.1.1

Published

An up-to-date alternative to the deprecated xss-clean package. Express middleware to sanitize req.body, req.query, and req.params.

Downloads

285

Readme

xss-sanitize

An up-to-date replacement for the deprecated xss-clean middleware.
Sanitizes incoming data (req.body, req.query, req.params) in Express apps to prevent XSS attacks.

Installation

npm install xss-sanitize

Usage Example

const express = require('express');
const helmet = require('helmet');
const xssSanitize = require('xss-sanitize');

const app = express();

app.use(express.json());         // parse JSON body
app.use(helmet());               // set secure headers
app.use(xssSanitize());          // sanitize req.body and req.query globally

app.post('/test/create', (req, res) => {
  console.log(req.body);         // sanitized input
  console.log(req.raw.body);     // original unsanitized input
  res.send('Saved safely!');
});

// Route-level param sanitization
app.route('/test/:id').get(xssSanitize.paramSanitize(), (req, res) => {
  console.log(req.params);       // sanitized params
  console.log(req.raw.params);   // original unsanitized params
  res.send('Params sanitized!');
});

app.listen(3000, () => console.log('Server running on port 3000'));

Note: Always use body parsers (express.json() / express.urlencoded()) before xssSanitize().

Example XSS Sanitization

// Run the test server
node test.js

// Example GET request:
// URL: /test/<script>alert(1)</script>?search=<script>alert(2)</script> 
// Note: Some tools like Postman may block <script> in URL params. Test using a browser instead.
// Body (sent as JSON): { "name": "<script>alert(3)</script>" }

// After hitting the route:

// Sanitized output
req.body         -> { name: "&lt;script&gt;alert(3)&lt;/script&gt;" }
req.query        -> { search: "&lt;script&gt;alert(2)&lt;/script&gt;" }
req.params       -> { testId: "&lt;script&gt;alert(1)&lt;/script&gt;" }

// Raw (unsanitized) values
req.raw.body     -> { name: "<script>alert(3)</script>" }
req.raw.query    -> { search: "<script>alert(2)</script>" }
req.raw.params   -> { testId: "<script>alert(1)</script>" }

Options

app.use(xssSanitize({
  whiteList: {},                    // remove all tags
  stripIgnoreTag: true,             // remove non-whitelisted tags instead of escaping
  stripIgnoreTagBody: ['script'],   // remove script content entirely
  // optional advanced callbacks:
  onTag: function(tag, html, options) {
    console.log('Found tag:', tag);
  }
}));

| Option | Description | | -------------------- | ---------------------------------------------------------------------------------------------------------- | | whiteList | An object specifying which HTML tags and attributes are allowed. Empty object {} removes all tags. | | stripIgnoreTag | If true, removes all tags not in the whitelist instead of escaping them. | | stripIgnoreTagBody | If true and stripIgnoreTag is set, removes the content inside ignored tags (e.g., <script> content). | | onTag | Callback function for each tag found. You can modify or block tags programmatically. (needs callback) | | onIgnoreTag | Callback function for each ignored tag. Useful for logging or custom behavior. (needs callback) | | onIgnoreTagAttr | Callback function for each ignored attribute of a tag. (needs callback) | | css | Configure how inline CSS is handled. Default is to allow safe CSS. | | safeAttrValue | Callback to transform or validate attribute values (e.g., URLs in <a href>). (needs callback) | | escapeHtml | If true (default), escapes HTML tags instead of stripping them. | | safeProtocol | Array of allowed protocols in URLs (['http', 'https', 'mailto'] by default). | | allowCommentTag | If true, preserves HTML comments; default is false. |

Tips

  • Combine xss-sanitize with other security middleware like helmet for full protection.
  • Middleware ordering is important: body parser → security middleware → xss-sanitize → route handlers.
  • Use xssSanitize.paramSanitize() per route to sanitize req.params.
  • Access original, unsanitized values via req.raw.

License

MIT © 2025 Mohammad Kalhor