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

request-sanitizer

v0.0.6

Published

Request Sanitizer is an nodejs expressjs middleware which sanitize the data of request.body and request.query it can be used as middleware in all route or in specified route. This module sanitize the data escapes,trims and validates and assign it back

Downloads

28

Readme

Request-Sanitizer

Request Sanitizer is an expressjs middleware which sanitize the data of request.body and request.query it can be used as middleware in all route or in specified route. This module sanitize the data escapes,trims and validates and assign it back to request body or query so that data can be used in next middleware

  • Sanitize given key attached to req

  • easily customizable codes

  • you can also pass own sanitize function

Version

0.0.3

Installation

You need request-sanitizer installed locally:

$ npm i request-sanitizer

Dependecies

Request sanitizer is currently extended with the following plugins

  • underscore.js
  • validator.js

How to setup

Import the module where needed

var requestSanitizer = require('request-sanitizer')();

If you have already imported validator.js then you can ignore it

var validator = requestSanitizer.validator

Set options for sanitizer

requestSanitizer.setOptions({
    query :{
    	test : [validator.escape]
    },
    body :{
    	name : [validator.escape,validator.ltrim],
    	test : [validator.ltrim]
    },
});

You have to pass object as arguments for setOptions method of reqestSanitizer. here keys are the field where you want sanitize to be applied for example req object have query and body properties and they have there values where you want sanitization.you have to pass sanitization function in array .

suppose if req.body has three posted key and values like this which are unescaped and contains whitespaces

req.body.name = " <sam  ";
req.body.age = 12;
req.body.sex = "male  "

now to sanitize this you have to setOptions like this which must match fields from req.body object

requestSanitizer.setOptions({
   //sanitize req.body only
   body :{
     //this will sanitize req.body.name
     //you can pass multiple callbacks to sanitize
     name : [validator.escape,validator.ltrim],
     
      //or you can assign function to it
     //It will sanitize req.body.age
     age :validator.ltrim,
     
    //or you can pass your custom validation function
     sex : function(value){
      if(value === 'male') return 'M';
      else return 'F';
     }
   }
});

If you console.log(req.body) in next middleware then output will be like

{
        "name": "&lt;sam", // escaped html characted and whitespaces
        "age": 12,
        "sex": "male" //Removed whitespace from left
    },

To read more validator function please go through https://github.com/chriso/validator.js or you pass your own validator function

After that u can use it as global middleware like

app.use('/*',requestSanitizer.sanitize);

or for some specific routes like

app.post('/home/data/?test=name',requestSanitizer.sanitize,yourmiddilware);

Combining All Codes

var requestSanitizer = require('request-sanitizer')();
var validator = requestSanitizer.validator;
 
requestSanitizer.setOptions({
    query :{
    	test : [validator.escape]
    },
    body :{
    	name : [validator.escape,validator.ltrim],
    	test : [validator.ltrim]
    },
});

module.exports = function(app) {
    app.post('/test/sanitize', requestSanitizer.sanitize, function(req, res, next) {
        var response = {
            body: req.body,
            query: req.query,
        }
        res.json(response);
    });
}