diff --git a/lib/index.js b/lib/index.js index 36fc7c7..5d92196 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,4 +1,4 @@ -var rawbody = require('raw-body'); +const rawbody = require('raw-body'); function hasSql(value) { @@ -7,22 +7,22 @@ function hasSql(value) { } // sql regex reference: http://www.symantec.com/connect/articles/detection-sql-injection-and-cross-site-scripting-attacks - var sql_meta = new RegExp('(%27)|(\')|(--)|(%23)|(#)', 'i'); + var sql_meta = new RegExp(/(%27)|(\')|(--)|(%23)|(#)/, 'i'); if(sql_meta.test(value)){ return true; } - var sql_meta2 = new RegExp('((%3D)|(=))[^\n]*((%27)|(\')|(--)|(%3B)|(;))', 'i'); + var sql_meta2 = new RegExp(/((%3D)|(=))[^\n]*((%27)|(\')|(--)|(%3B)|(;))/, 'i'); if(sql_meta2.test(value)){ return true; } - var sql_typical = new RegExp('w*((%27)|(\'))((%6F)|o|(%4F))((%72)|r|(%52))', 'i'); + var sql_typical = new RegExp(/w*((%27)|(\'))((%6F)|o|(%4F))((%72)|r|(%52))/, 'i'); if(sql_typical.test(value)){ return true; } - var sql_union = new RegExp('((%27)|(\'))union', 'i'); + var sql_union = new RegExp(/((%27)|(\'))union/, 'i'); if(sql_union.test(value)){ return true; } @@ -30,12 +30,20 @@ function hasSql(value) { return false; } +function isNullAndUndefined(value) { + if(value === null && value === undefined) { + return true; + } + + return false; +} + function middleware(req, res, next) { var containsSql = false; - if (req.originalUrl !== null && req.originalUrl !== undefined) { - if (hasSql(req.originalUrl) === true) { + if (!isNullAndUndefined(req.originalUrl)) { + if (hasSql(req.originalUrl)) { containsSql = true; } } @@ -49,18 +57,18 @@ function middleware(req, res, next) { return next(err); } - if (body !== null && body !== undefined) { + if (!isNullAndUndefined(body)) { if (typeof body !== 'string') { body = JSON.stringify(body); } - if (hasSql(body) === true) { + if (hasSql(body)) { containsSql = true; } } - if (containsSql === true) { + if (containsSql) { res.send(403); } else { @@ -72,4 +80,5 @@ function middleware(req, res, next) { } } + module.exports = middleware; \ No newline at end of file