GithubHelp home page GithubHelp logo

brophdawg11 / express-sanitize-escape Goto Github PK

View Code? Open in Web Editor NEW

This project forked from fingerfoodstudios/express-sanitize-escape

0.0 1.0 1.0 48 KB

An express.js middleware for sanitizing all query and body parameters automatically

License: MIT License

JavaScript 100.00%

express-sanitize-escape's Introduction

express-sanitized

Build Status

Installation

npm install express-sanitize-escape

Usage

Place this directly after all express.use(bodyParser) middlewares and before any express middleware that accesses query or body parameters, e.g.:

var bodyParser = require('body-parser');
var express = require('express');
var expressSanitized = require('express-sanitize-escape');

app.use(bodyParser.urlencoded);
app.use(bodyParser.json);
app.use(expressSanitized.middleware()); // this line follows app.use(bodyParser.json) or the last body parser middleware

The above sanitizes req.body and req.query. In order to sanitize req.params as well, pass in an express router or app to expressSanitized.sanitizeParams, along with the names of the params to sanitize, e.g.:

var express = require('express');
var expressSanitized = require('express-sanitize-escape');

var router = express.router();
expressSanitized.sanitizeParams(router, ['id','name']);

router.get('/:id', function(req, res, next)
{
    // req.params.id is now sanitized.
    ...
});

router.get('/:name', function(req, res, next)
{
    // req.params.name is now sanitized.
    ...
});

Output

The string

'<script>document.write('cookie monster')</script> download now'

will be sanitized to ' download now'.

and

< > ' " &

will be escaped to &lt; &gt; &#39; &quot; &amp;

Limitations

This is a basic implementation of Caja-HTML-Sanitizer with the specific purpose of mitigating against persistent XSS risks. And node-htmlencode to escape all html entities

Caveats

This module trusts the dependencies to provide basic persistent XSS risk mitigation. A user of this package should review all packages and make their own decision on security and fitness for purpose.

This module was inspired by express-sanitizer and express-sanitized. The difference here is: This middleware automatically sanitizes post and query values parameter. And automatically html escapes all strings.

Changelog

v1.0.0

  • This is a breaking change.
  • Change to use exports instead of module exports
  • Middleware is now exports.middleware so app.use(expressSanitized()) is now app.use(expressSanitized.middleware())
  • Added a function to decode the body expressSanitized.htmlDecodeBody()
  • Added tests for unicode characters

v0.6.3

  • Added function to sanitize request params of a router

v0.6.1

  • Added additional test for nested object and an array
  • Added chai js for testing

v0.6.0

  • Added htmlencoding
  • Change filer to recurse through all values of the object and sanitize only values that are strings
  • Updated docs to express 4.x and new bodyParser middleware
  • Added License file

v0.5.1

  • Initial release

Contributors

License

Copyright (c) 2016 Finger Food Studios [email protected], MIT License

express-sanitize-escape's People

Contributors

amri91 avatar anthony-ffs avatar askhogan avatar brophdawg11 avatar justinffs avatar

Watchers

 avatar

Forkers

urbn

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.