auto-smart-security 1.0.9 → 1.0.11

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.
@@ -51,11 +51,12 @@ function applySecurity(app, options) {
51
51
  return next();
52
52
  const ip = (0, utils_1.getClientIP)(req);
53
53
  const url = req.originalUrl;
54
- console.log('url =========>', url, ip);
54
+ console.log('url =========>', url, ip, blacklist);
55
55
  /** 1️⃣ Blacklist */
56
56
  if (await blacklist.isBlocked(ip)) {
57
57
  return res.status(403).send('Access denied');
58
58
  }
59
+ console.log('run to here =========>', 1);
59
60
  /** 2️⃣ Bot detection */
60
61
  if (botDetector?.detect(req)) {
61
62
  await blacklist.block(ip);
@@ -67,12 +68,14 @@ function applySecurity(app, options) {
67
68
  });
68
69
  return res.status(403).send('Bot detected');
69
70
  }
71
+ console.log('run to here =========>', 2);
70
72
  /** 3️⃣ Path whitelist */
71
73
  const normalizePath = (url) => url.split('?')[0].replace(/^\/+/, '');
72
74
  const isPathAllowed = (url, whitelist) => {
73
75
  const path = normalizePath(url);
74
- return whitelist.some((p) => path === p || path.startsWith(`${p}/`));
76
+ return whitelist.some((p) => path === p || path.includes(`${p}`));
75
77
  };
78
+ console.log('run to here =========>', 3, options.pathWhitelist, url);
76
79
  if (options.pathWhitelist?.length &&
77
80
  !isPathAllowed(url, options.pathWhitelist)) {
78
81
  await blacklist.block(ip);
@@ -12,12 +12,16 @@ class RedisBlacklistStore {
12
12
  return `${this.prefix}${ip}`;
13
13
  }
14
14
  async isBlocked(ip) {
15
+ if (!this.redis)
16
+ return false;
15
17
  if (this.staticBlacklist.includes(ip))
16
18
  return true;
17
19
  const value = await this.redis.get(this.key(ip));
18
20
  return value === '1';
19
21
  }
20
22
  async block(ip, ttlSeconds) {
23
+ if (!this.redis)
24
+ return;
21
25
  const ttl = ttlSeconds ?? this.ttlSeconds;
22
26
  await this.redis.set(this.key(ip), '1', 'EX', ttl);
23
27
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "auto-smart-security",
3
- "version": "1.0.9",
3
+ "version": "1.0.11",
4
4
  "description": "Production-ready security middleware for Express / NestJS",
5
5
  "author": "Hai Vinh <haivinhinspirit@gmail.com>",
6
6
  "main": "dist/index.js",