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 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-autosanitizer

v1.0.2

Published

automatic sanitization of req body fields, params and query. uses caja. automatically does sanitization and escaping as middleware.

Downloads

1,408

Readme

npm version HitCount Inline docs

express-autosanitizer

Automatic sanitization of req body, params and query strings. uses caja. automatically does sanitization and escaping as middleware.

If this does make your life easier, please consider making a donation to my Patreon. 🤝

important note: do not use with large amounts of input, it might overflow. it goes through the fields recursively. in that case, use singular sanitization instead.

Install

npm i -S express-autosanitizer

Usage

Import

const expAutoSan = require('express-autosanitizer');

important note: if you intend to use it with app.use(), mount the middleware below the express.json() (or bodyParser()) instantiation

Use middleware everywhere (safe):

writes sanitized data to req.autosan (req.autosan.body, req.autosan.params, req.autosan.query)

app.use(express.json());

// Mount here
app.use(expAutoSan.all);

app.post('/', (req, res, next) => {
  //req is automatically sanitized, as middleware is used for all routes
  doYourStuff(req.autosan.body);
  res.render("pagewithtrusteddata");
});

Use middleware everywhere (UNSAFE):

writes sanitized data to req, mutes req object so it might cause problems. p.s: this is to apply sanitization for lazy people like me.

app.use(express.json());

// Mount here
app.use(expAutoSan.allUnsafe);

//no extra middleware needed
app.post('/', (req, res, next) => {
  //req is automatically sanitized, as middleware is used for all routes
  doYourStuff(req.body);
  res.render("pagewithtrusteddata");
});

Use middleware for a route (safe):

writes sanitized data to req.autosan (req.autosan.body, req.autosan.params, req.autosan.query)


//use the middleware
app.post('/', expAutoSan.route, (req, res, next) => {
  //req is automatically sanitized, as middleware is used for body, query and params of this route
  //req is not mutated, results are stored in req.autosan.body, req.autosan.params, req.autosan.query
  doYourStuff(req.autosan.body);
  .
  .
  .
  res.render("pagewithtrusteddata");
});

Use middleware for a route (UNSAFE):

writes sanitized data to req (req.body, req.params, req.query)


//use different middleware
app.post('/', expAutoSan.routeUnsafe, (req, res, next) => {
  //req is automatically sanitized, as middleware is used for body, query and params of this route
  //req IS mutated, results are stored in req.body, req.params, req.query
  doYourStuff(req.body);
  .
  .
  .
  res.render("pagewithtrusteddata");
});

Use as function (safe):

app.get('/', (req, res, next) => {
  //you can pass array/object/string or whatever you want, only string parts will be sanitized
  //again, do not pass highly-nested structures, this middleware works recursively
  let mySanitizedData = expAutoSan.sanitizeIt(myDirtyData);
  doYourStuff(mySanitizedData);
  .
  .
  .
  res.render("pagewithtrusteddata");
});

What it does

When you use it on a field or a route, it will remove all script tags, and escape html characters. this improves security in your app.

Caveats

This module uses "sanitizer" module, the sanitization logic is done in that package, review the package yourself. This package goes over the data recursively, it is your duty to be wise enough not to use data that will crash it, you should be fine for most cases (forms, ajax apps, api etc).

License

NPM

Copyright (c) 2019 Antonio Ramirez [email protected], MIT License