-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (48 loc) · 1.46 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
function hasSqlInjection(value) {
const sqlMeta = new RegExp('(%27)|(\')|(--)|(%23)|(#)', 'i');
if (sqlMeta.test(value)) {
return true;
}
const sqlMeta2 = new RegExp('((%3D)|(=))[^\n]*((%27)|(\')|(--)|(%3B)|(;))', 'i');
if (sqlMeta2.test(value)) {
return true;
}
const sqlTypical = new RegExp('w*((%27)|(\'))((%6F)|o|(%4F))((%72)|r|(%52))', 'i');
if (sqlTypical.test(value)) {
return true;
}
const sqlUnion = new RegExp('((%27)|(\'))union', 'i');
if (sqlUnion.test(value)) {
return true;
}
return false;
}
function middleware(req, res, next) {
let containsSql = false;
let message = {error: 'Request is detecting SQL Injection'};
if (req.originalUrl !== null && req.originalUrl !== undefined) {
if (hasSqlInjection(req.originalUrl) === true) {
containsSql = true;
}
}
if (containsSql === false) {
let body = req.body;
if(body !== null && body !== undefined){
if (typeof body !== 'string') {
body = JSON.stringify(body);
}
if (hasSqlInjection(body) === true) {
containsSql = true;
}
}
if(containsSql === true){
res.status(403).json(message);
}
else{
next();
}
} else {
res.status(403).json(message);
}
}
module.exports = middleware;