Browse Source

remove callback in sanitizeAll.runAllChecks for sync processing

pull/942/head
Lily He 8 years ago
parent
commit
80a3a259ef
  1. 9
      sanitizer/autocomplete.js
  2. 9
      sanitizer/nearby.js
  3. 9
      sanitizer/place.js
  4. 9
      sanitizer/reverse.js
  5. 33
      sanitizer/sanitizeAll.js
  6. 9
      sanitizer/search.js
  7. 9
      sanitizer/search_fallback.js
  8. 10
      sanitizer/structured_geocoding.js
  9. 68
      test/unit/sanitizer/sanitizeAll.js

9
sanitizer/autocomplete.js

@ -20,12 +20,7 @@ module.exports.middleware = (_api_pelias_config) => {
}; };
return ( req, res, next ) => { return ( req, res, next ) => {
sanitizeAll.runAllChecks(req, sanitizers, ( err, clean ) => { sanitizeAll.runAllChecks(req, sanitizers);
if( err ){ next();
res.status(400); // 400 Bad Request
return next(err);
}
next();
});
}; };
}; };

9
sanitizer/nearby.js

@ -15,11 +15,6 @@ module.exports.sanitizer_list = sanitizers;
// middleware // middleware
module.exports.middleware = function( req, res, next ){ module.exports.middleware = function( req, res, next ){
sanitize(req, sanitizers, ( err, clean ) => { sanitizeAll.runAllChecks(req, sanitizers);
if( err ){ next();
res.status(400); // 400 Bad Request
return next(err);
}
next();
});
}; };

9
sanitizer/place.js

@ -14,11 +14,6 @@ module.exports.sanitizer_list = sanitizers;
// middleware // middleware
module.exports.middleware = function(req, res, next){ module.exports.middleware = function(req, res, next){
sanitize(req, sanitizers, ( err, clean ) => { sanitizeAll.runAllChecks(req, sanitizers);
if( err ){ next();
res.status(400); // 400 Bad Request
return next(err);
}
next();
});
}; };

9
sanitizer/reverse.js

@ -23,11 +23,6 @@ module.exports.sanitizer_list = sanitizers;
// middleware // middleware
module.exports.middleware = function( req, res, next ){ module.exports.middleware = function( req, res, next ){
sanitize(req, sanitizers, ( err, clean ) => { sanitizeAll.runAllChecks(req, sanitizers);
if( err ){ next();
res.status(400); // 400 Bad Request
return next(err);
}
next();
});
}; };

33
sanitizer/sanitizeAll.js

@ -1,8 +1,5 @@
'use strict'; 'use strict';
function sanitize( req, sanitizers ){
const async = require('async');
function sanitize( req, sanitizers, cb ){
// init an object to store clean (sanitized) input parameters if not initialized // init an object to store clean (sanitized) input parameters if not initialized
req.clean = req.clean || {}; req.clean = req.clean || {};
@ -29,11 +26,11 @@ function sanitize( req, sanitizers, cb ){
req.warnings = req.warnings.concat( sanity.warnings ); req.warnings = req.warnings.concat( sanity.warnings );
} }
} }
return cb( undefined, req.clean );
} }
// Adds to goodParameters every acceptable parameter passed through API call // Adds to goodParameters every acceptable parameter passed through API call
function checkParameters(req, sanitizers, cb) { function checkParameters( req, sanitizers ) {
req.warnings = req.warnings || [];
// source of input parameters // source of input parameters
// (in this case from the GET querystring params) // (in this case from the GET querystring params)
const params = req.query || {}; const params = req.query || {};
@ -41,9 +38,9 @@ function checkParameters(req, sanitizers, cb) {
for (let s in sanitizers) { for (let s in sanitizers) {
// checks if there is a function that returns valid params // checks if function exists
if (typeof sanitizers[s].expected === 'function'){ if (typeof sanitizers[s].expected === 'function'){
/** func returns {array} ex: [{ name: 'text' }] */ /** expected() returns {array} ex: [{ name: 'text' }] */
for (let t in sanitizers[s].expected()) { for (let t in sanitizers[s].expected()) {
/** {object} prop */ /** {object} prop */
const prop = sanitizers[s].expected()[t]; const prop = sanitizers[s].expected()[t];
@ -54,21 +51,21 @@ function checkParameters(req, sanitizers, cb) {
} }
} }
} }
// If there are any unexpected parameters, add a warning to messages // If there are any unexpected parameters & goodParameters isn't empty,
for (let p in params) { // add a warning message
if (!goodParameters.hasOwnProperty(p)){ if (Object.keys(goodParameters).length !== 0) {
req.warnings = req.warnings.concat('Invalid Parameter: ' + p); for (let p in params) {
if (!goodParameters.hasOwnProperty(p)){
req.warnings = req.warnings.concat('Invalid Parameter: ' + p);
}
} }
} }
return cb( undefined, req.clean );
} }
// runs both sanitize and checkParameters functions in async parallel // runs both sanitize and checkParameters functions in async parallel
function runAllChecks (req, sanitizers, cb) { function runAllChecks (req, sanitizers) {
async.parallel([ sanitize(req, sanitizers);
sanitize.bind(null, req, sanitizers), checkParameters(req, sanitizers);
checkParameters.bind(null, req, sanitizers)
], cb);
} }
// export function // export function

9
sanitizer/search.js

@ -21,13 +21,8 @@ module.exports.middleware = (_api_pelias_config) => {
}; };
return ( req, res, next ) => { return ( req, res, next ) => {
sanitizeAll.runAllChecks(req, sanitizers, ( err, clean ) => { sanitizeAll.runAllChecks(req, sanitizers);
if( err ){ next();
res.status(400); // 400 Bad Request
return next(err);
}
next();
});
}; };
}; };

9
sanitizer/search_fallback.js

@ -23,12 +23,7 @@ module.exports.middleware = function( req, res, next ){
} }
// calls to sanitize the input // calls to sanitize the input
// omits check if parameters are valid since it only calls _text_addressit // omits check if parameters are valid since it only calls _text_addressit
sanitizeAll.sanitize(req, sanitizers, ( err, clean ) => { sanitizeAll.sanitize(req, sanitizers);
if( err ){ next();
res.status(400); // 400 Bad Request
return next(err);
}
next();
});
}; };

10
sanitizer/structured_geocoding.js

@ -22,13 +22,9 @@ module.exports.middleware = (_api_pelias_config) => {
}; };
return ( req, res, next ) => { return ( req, res, next ) => {
sanitizeAll.runAllChecks(req, sanitizers, ( err, clean ) => { sanitizeAll.runAllChecks(req, sanitizers);
if( err ){ next();
res.status(400); // 400 Bad Request
return next(err);
}
next();
});
}; };
}; };

68
test/unit/sanitizer/sanitizeAll.js

@ -35,10 +35,9 @@ module.exports.tests.all = function(test, common) {
warnings: ['warning 1', 'warning 2', 'warning 3'] warnings: ['warning 1', 'warning 2', 'warning 3']
}; };
sanitizeAll.sanitize(req, sanitizers, function (){ sanitizeAll.runAllChecks(req, sanitizers);
t.deepEquals(req, expected_req); t.deepEquals(req, expected_req);
t.end(); t.end();
});
}); });
@ -82,11 +81,9 @@ module.exports.tests.all = function(test, common) {
warnings: ['pre-existing warning', 'warning 1', 'warning 2', 'warning 3'] warnings: ['pre-existing warning', 'warning 1', 'warning 2', 'warning 3']
}; };
sanitizeAll.sanitize(req, sanitizers, function () { sanitizeAll.runAllChecks(req, sanitizers);
t.deepEquals(req, expected_req); t.deepEquals(req, expected_req);
t.end(); t.end();
});
}); });
test('req.query should be passed to individual sanitizers when available', function(t) { test('req.query should be passed to individual sanitizers when available', function(t) {
@ -97,7 +94,7 @@ module.exports.tests.all = function(test, common) {
}; };
var sanitizers = { var sanitizers = {
'first': { 'first': {
sanitize: function(params) { sanitize: function (params) {
req.clean.query = params; req.clean.query = params;
return { return {
errors: [], errors: [],
@ -120,11 +117,9 @@ module.exports.tests.all = function(test, common) {
warnings: [] warnings: []
}; };
sanitizeAll.sanitize(req, sanitizers, function () { sanitizeAll.runAllChecks(req, sanitizers);
t.deepEquals(req, expected_req); t.deepEquals(req, expected_req);
t.end(); t.end();
});
}); });
test('an empty object should be passed to individual sanitizers when req.query is unavailable', function(t) { test('an empty object should be passed to individual sanitizers when req.query is unavailable', function(t) {
@ -152,11 +147,9 @@ module.exports.tests.all = function(test, common) {
warnings: [] warnings: []
}; };
sanitizeAll.sanitize(req, sanitizers, function () { sanitizeAll.runAllChecks(req, sanitizers);
t.deepEquals(req, expected_req); t.deepEquals(req, expected_req);
t.end(); t.end();
});
}); });
test('unexpected parameters should throw warning', function(t) { test('unexpected parameters should throw warning', function(t) {
@ -178,12 +171,10 @@ module.exports.tests.all = function(test, common) {
} }
}; };
sanitizeAll.checkParameters(req, sanitizers, function () { sanitizeAll.checkParameters(req, sanitizers);
t.equals(req.errors.length, 0); t.equals(req.errors.length, 0);
t.deepEquals(req.warnings[0], 'Invalid Parameter: unknown_value'); t.deepEquals(req.warnings[0], 'Invalid Parameter: unknown_value');
t.end(); t.end();
});
}); });
test('expected parameters should not throw warning', function(t) { test('expected parameters should not throw warning', function(t) {
@ -205,14 +196,14 @@ module.exports.tests.all = function(test, common) {
} }
}; };
sanitizeAll.checkParameters(req, sanitizers, function () { sanitizeAll.checkParameters(req, sanitizers);
t.equals(req.errors.length, 0); t.equals(req.errors.length, 0);
t.equals(req.warnings.length, 0); t.equals(req.warnings.length, 0);
t.end(); t.end();
});
}); });
test('runAllChecks calls both sanitize and expectedParameters function', function(t) { test('sanitizer without expected() should not validate parameters', function(t) {
var req = { var req = {
query: { query: {
value: 'query' value: 'query'
@ -227,12 +218,6 @@ module.exports.tests.all = function(test, common) {
errors: [], errors: [],
warnings: ['warning 1'] warnings: ['warning 1']
}; };
},
expected: function _expected () {
// add value as a valid parameter
return [{
name: 'value'
}];
} }
} }
}; };
@ -250,10 +235,9 @@ module.exports.tests.all = function(test, common) {
warnings: ['warning 1'] warnings: ['warning 1']
}; };
sanitizeAll.runAllChecks(req, sanitizers, function () { sanitizeAll.runAllChecks(req, sanitizers);
t.deepEquals(req, expected_req); t.deepEquals(req, expected_req);
t.end(); t.end();
});
}); });
}; };

Loading…
Cancel
Save