auto-smart-security 1.0.13 → 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.
@@ -9,10 +9,30 @@ const utils_1 = require("./utils");
9
9
  const bot_detector_1 = require("./bot-detector");
10
10
  const rate_limiter_1 = require("./rate-limiter");
11
11
  const memory_store_1 = require("./blacklist/memory.store");
12
+ const STATIC_EXTENSIONS = [
13
+ '.png',
14
+ '.jpg',
15
+ '.jpeg',
16
+ '.gif',
17
+ '.webp',
18
+ '.svg',
19
+ '.ico',
20
+ '.bmp',
21
+ '.css',
22
+ '.js',
23
+ '.map',
24
+ '.woff',
25
+ '.woff2',
26
+ '.ttf',
27
+ '.eot',
28
+ ];
12
29
  function applySecurity(app, options) {
13
30
  // Dev mode → skip security
14
31
  if (options.mode === 'dev')
15
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
16
36
  if (options.rateLimit) {
17
37
  if (options.trustProxy === undefined) {
18
38
  throw new Error('[auto-smart-security] rateLimit requires trustProxy to be a number (e.g. 1). Do NOT use true.');
@@ -44,6 +64,11 @@ function applySecurity(app, options) {
44
64
  });
45
65
  }));
46
66
  }
67
+ /** ================= IMAGES ================= */
68
+ const isStaticAsset = (url) => {
69
+ const path = url.split('?')[0].toLowerCase();
70
+ return STATIC_EXTENSIONS.some((ext) => path.endsWith(ext));
71
+ };
47
72
  /** ================= MAIN SECURITY ================= */
48
73
  app.use(async (req, res, next) => {
49
74
  // pass OPTIONS requests
@@ -51,12 +76,12 @@ function applySecurity(app, options) {
51
76
  return next();
52
77
  const ip = (0, utils_1.getClientIP)(req);
53
78
  const url = req.originalUrl;
54
- console.log('url =========>', url, ip, blacklist);
79
+ if (isStaticAsset(url))
80
+ return next();
55
81
  /** 1️⃣ Blacklist */
56
82
  if (await blacklist.isBlocked(ip)) {
57
83
  return res.status(403).send('Access denied');
58
84
  }
59
- console.log('run to here =========>', 1);
60
85
  /** 2️⃣ Bot detection */
61
86
  if (botDetector?.detect(req)) {
62
87
  await blacklist.block(ip);
@@ -68,23 +93,35 @@ function applySecurity(app, options) {
68
93
  });
69
94
  return res.status(403).send('Bot detected');
70
95
  }
71
- console.log('run to here =========>', 2);
72
96
  /** 3️⃣ Path whitelist */
73
97
  const normalizePath = (url) => url.split('?')[0].replace(/^\/+/, '');
74
98
  const isPathAllowed = (url, whitelist) => {
75
99
  const path = normalizePath(url);
76
100
  return whitelist.some((p) => path === p || path.includes(`${p}`));
77
101
  };
78
- console.log('run to here =========>', 3, options.pathWhitelist, url);
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
+ }
79
113
  if (options.pathWhitelist?.length &&
80
114
  !isPathAllowed(url, options.pathWhitelist)) {
81
- await blacklist.block(ip);
82
- options.onBlock?.({
83
- ip,
84
- reason: 'path-not-allowed',
85
- url,
86
- });
87
- return res.status(403).send('Blocked path');
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
+ }
88
125
  }
89
126
  next();
90
127
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "auto-smart-security",
3
- "version": "1.0.13",
3
+ "version": "1.0.15",
4
4
  "description": "Production-ready security middleware for Express / NestJS",
5
5
  "author": "Hai Vinh <haivinhinspirit@gmail.com>",
6
6
  "main": "dist/index.js",