-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
56 lines (45 loc) · 1.63 KB
/
app.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
// Import libraries
const express = require('express');
const helmet = require('helmet');
const rateLimit = require('express-rate-limit');
// Initialize the app
const app = express();
const port = 3000;
// Middleware for security headers (using Helmet)
app.use(helmet());
// Middleware for rate limiting
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // limit each IP to 100 requests per window
message: "Too many requests from this IP, please try again after 15 minutes."
});
app.use(limiter);
// Custom WAF Middleware
app.use((req, res, next) => {
// Check for suspicious patterns in query strings or body
const suspiciousPatterns = [/(\%27)|(\')|(\-\-)|(\%23)|(#)/i, /<script.*?>.*?<\/script>/i, /(\bselect\b|\bunion\b|\bdelete\b|\bdrop\b)/i];
const checkPatterns = (input) => suspiciousPatterns.some((pattern) => pattern.test(input));
// Check query strings
if (req.query && Object.values(req.query).some(checkPatterns)) {
return res.status(403).send("Forbidden: Suspicious query detected.");
}
// Check body (if content type is JSON)
if (req.body && typeof req.body === 'object') {
if (Object.values(req.body).some(checkPatterns)) {
return res.status(403).send("Forbidden: Suspicious payload detected.");
}
}
next();
});
// Test Route
app.get('/', (req, res) => {
res.send('WAF is active. Your request is safe.');
});
// Route for testing SQL injection protection
app.get('/test', (req, res) => {
res.send('This route is protected from SQL Injection.');
});
// Start the server
app.listen(port, () => {
console.log(`WAF app listening at http://localhost:${port}`);
});