auto-smart-security 1.0.14 → 1.0.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/apply-security.js +24 -7
- package/package.json +1 -1
package/dist/apply-security.js
CHANGED
|
@@ -30,6 +30,9 @@ function applySecurity(app, options) {
|
|
|
30
30
|
// Dev mode → skip security
|
|
31
31
|
if (options.mode === 'dev')
|
|
32
32
|
return;
|
|
33
|
+
const pathViolationMap = new Map();
|
|
34
|
+
const PATH_VIOLATION_TTL = 60000; // 1 minute
|
|
35
|
+
const PATH_VIOLATION_LIMIT = 10; // accept 10 violation per minute
|
|
33
36
|
if (options.rateLimit) {
|
|
34
37
|
if (options.trustProxy === undefined) {
|
|
35
38
|
throw new Error('[auto-smart-security] rateLimit requires trustProxy to be a number (e.g. 1). Do NOT use true.');
|
|
@@ -96,15 +99,29 @@ function applySecurity(app, options) {
|
|
|
96
99
|
const path = normalizePath(url);
|
|
97
100
|
return whitelist.some((p) => path === p || path.includes(`${p}`));
|
|
98
101
|
};
|
|
102
|
+
function recordPathViolation(ip) {
|
|
103
|
+
const now = Date.now();
|
|
104
|
+
const entry = pathViolationMap.get(ip);
|
|
105
|
+
if (!entry || now - entry.ts > PATH_VIOLATION_TTL) {
|
|
106
|
+
pathViolationMap.set(ip, { count: 1, ts: now });
|
|
107
|
+
return false; // not block
|
|
108
|
+
}
|
|
109
|
+
entry.count++;
|
|
110
|
+
pathViolationMap.set(ip, entry);
|
|
111
|
+
return entry.count > PATH_VIOLATION_LIMIT; // block if limit exceeded
|
|
112
|
+
}
|
|
99
113
|
if (options.pathWhitelist?.length &&
|
|
100
114
|
!isPathAllowed(url, options.pathWhitelist)) {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
ip
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
115
|
+
const shouldBlock = recordPathViolation(ip); // check violation count
|
|
116
|
+
if (shouldBlock) {
|
|
117
|
+
await blacklist.block(ip);
|
|
118
|
+
options.onBlock?.({
|
|
119
|
+
ip,
|
|
120
|
+
reason: 'path-not-allowed',
|
|
121
|
+
url,
|
|
122
|
+
});
|
|
123
|
+
return res.status(403).send('Blocked path');
|
|
124
|
+
}
|
|
108
125
|
}
|
|
109
126
|
next();
|
|
110
127
|
});
|