Skip to content

Commit

Permalink
Merge 57c72f7 into cdf0bab
Browse files Browse the repository at this point in the history
  • Loading branch information
atomic-structure committed Oct 4, 2020
2 parents cdf0bab + 57c72f7 commit 5912142
Show file tree
Hide file tree
Showing 11 changed files with 5,561 additions and 1,054 deletions.
16 changes: 16 additions & 0 deletions .eslintrc.js
@@ -0,0 +1,16 @@
module.exports = {
env: {
browser: true,
commonjs: true,
es2021: true,
mocha: true
},
extends: [
'standard'
],
parserOptions: {
ecmaVersion: 12
},
rules: {
}
}
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -3,3 +3,4 @@
coverage/
node_modules/
npm-debug.log
.nyc_output/
1 change: 1 addition & 0 deletions .npmignore
Expand Up @@ -9,3 +9,4 @@ test/
.travis.yml
gulpfile.js
npm-debug.log
.nyc_output/
3 changes: 3 additions & 0 deletions .travis.yml
Expand Up @@ -8,3 +8,6 @@ node_js:
- "6"
- "8"
- "10"
- "12"

script: 'npm run coveralls'
114 changes: 0 additions & 114 deletions gulpfile.js

This file was deleted.

178 changes: 79 additions & 99 deletions lib/index.js
@@ -1,10 +1,10 @@
'use strict';
'use strict'

var defaults = require('lodash/defaults');
var isString = require('lodash/isString');
var isArray = require('lodash/isArray');
var defaults = require('lodash/defaults')
var isString = require('lodash/isString')
var isArray = require('lodash/isArray')

var typeis = require('type-is');
var typeis = require('type-is')

/**
* @public
Expand All @@ -16,136 +16,116 @@ var typeis = require('type-is');
* @return {function}
*/
module.exports = function (options) {

options = defaults(options || {}, {
checkQuery: true,
checkBody: true,
checkBodyOnlyForContentType: 'urlencoded',
whitelist: null
});

if (isString(options.whitelist)) {
options.whitelist = [ options.whitelist ];
}

if (options.whitelist !== null && !isArray(options.whitelist)) {
console.error(
'[HPP] ' +
options = defaults(options || {}, {
checkQuery: true,
checkBody: true,
checkBodyOnlyForContentType: 'urlencoded',
whitelist: null
})

if (isString(options.whitelist)) {
options.whitelist = [options.whitelist]
}

if (options.whitelist !== null && !isArray(options.whitelist)) {
console.error(
'[HPP] ' +
'Please pass either a string or an array to "options.whitelist". ' +
'Deactivated the whitelist!'
);
options.whitelist = null;
}

if (isArray(options.whitelist)) {

options.whitelist = options.whitelist.filter(function (elem) {

if (!isString(elem)) {
)
options.whitelist = null
}

console.error(
'[HPP] ' +
if (isArray(options.whitelist)) {
options.whitelist = options.whitelist.filter(function (elem) {
if (!isString(elem)) {
console.error(
'[HPP] ' +
'Please pass only strings into the "options.whitelist" array. ' +
'Removed the entry <' + elem + '>!'
);

return false;
}
)

return true;
return false
}

});

}
return true
})
}

/**
/**
* @private
* @param {object} req
* @return {boolean}
*/
function _correctContentType(req) {
return typeis(req, options.checkBodyOnlyForContentType);
}
function _correctContentType (req) {
return typeis(req, options.checkBodyOnlyForContentType)
}

/**
/**
* @private
* @param {string} keyReqPart e.g 'body' or 'query'
* @param {string} keyPolluted e.g 'bodyPolluted' or 'queryPolluted'
* @param {object} req
*/
function _putAside(keyReqPart, keyPolluted, req) {

var whitelist = options.whitelist;

var reqPart = req[keyReqPart];
var reqPolluted = req[keyPolluted];

// Put aside only once in case multiple HPP middlewares are used
if (reqPolluted === undefined) { // Check identical to lodash's isUndefined(reqPolluted)
function _putAside (keyReqPart, keyPolluted, req) {
var whitelist = options.whitelist

reqPolluted = req[keyPolluted] = {};
var reqPart = req[keyReqPart]
var reqPolluted = req[keyPolluted]

var parameters = Object.keys(reqPart);
// Put aside only once in case multiple HPP middlewares are used
if (reqPolluted === undefined) { // Check identical to lodash's isUndefined(reqPolluted)
reqPolluted = req[keyPolluted] = {}

for ( var i = 0, parametersLen = parameters.length; i < parametersLen; i+=1 ) {
var parameters = Object.keys(reqPart)

var paramKey = parameters[i];
var paramValue = reqPart[paramKey];

if (!isArray(paramValue)) {
continue;
}

// Put aside
reqPolluted[paramKey] = paramValue;
// Select the first parameter value
reqPart[paramKey] = paramValue[paramValue.length-1];

}
for (var i = 0, parametersLen = parameters.length; i < parametersLen; i += 1) {
var paramKey = parameters[i]
var paramValue = reqPart[paramKey]

if (!isArray(paramValue)) {
continue
}

// Processed separately to allow multiple whitelists from multiple HPP middlewares as well as
// for performance reasons
if (whitelist !== null) { // Validation at top ensures whitelist is either null or an array

for (var k = 0, whitelistLen = whitelist.length; k < whitelistLen; k += 1) {

var whitelistedParam = whitelist[k];

if (reqPolluted[whitelistedParam]) {
// Put back
reqPart[whitelistedParam] = reqPolluted[whitelistedParam];
delete reqPolluted[whitelistedParam];
}
// Put aside
reqPolluted[paramKey] = paramValue
// Select the first parameter value
reqPart[paramKey] = paramValue[paramValue.length - 1]
}
}

}
// Processed separately to allow multiple whitelists from multiple HPP middlewares as well as
// for performance reasons
if (whitelist !== null) { // Validation at top ensures whitelist is either null or an array
for (var k = 0, whitelistLen = whitelist.length; k < whitelistLen; k += 1) {
var whitelistedParam = whitelist[k]

if (reqPolluted[whitelistedParam]) {
// Put back
reqPart[whitelistedParam] = reqPolluted[whitelistedParam]
delete reqPolluted[whitelistedParam]
}

}
}
}

/**
/**
* @public
* @param {object} req
* @param {object} [req.query]
* @param {object} [req.body]
* @param {object} res
* @param {function} next
*/
return function hpp(req, res, next) {

if (options.checkQuery && req.query) {
_putAside('query', 'queryPolluted', req);
}

if (options.checkBody && req.body && _correctContentType(req)) {
_putAside('body', 'bodyPolluted', req);
}

next();

};
return function hpp (req, res, next) {
if (options.checkQuery && req.query) {
_putAside('query', 'queryPolluted', req)
}

};
if (options.checkBody && req.body && _correctContentType(req)) {
_putAside('body', 'bodyPolluted', req)
}

next()
}
}

0 comments on commit 5912142

Please sign in to comment.