@liberstudio/cloudflare-list 2.3.4 → 2.3.6

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/README.md CHANGED
@@ -11,14 +11,18 @@ Request → 404 rilevato dal middleware
11
11
  └─ IP estratto (cf-connecting-ip / x-real-ip / x-forwarded-for)
12
12
  └─ throttle 5s per IP
13
13
  └─ log su file (se path non è silentPath)
14
- └─ ASN lookup su api.hackertarget.com
15
- ├─ range trovato → blocca intera subnet su Cloudflare
16
- └─ fallback → blocca singolo IP /32 o /128
14
+ └─ allowedIps? → skip (log only)
15
+ └─ IP Cloudflare? → skip (log only)
16
+ └─ ASN lookup su api.hackertarget.com
17
+ ├─ range trovato → blocca intera subnet su Cloudflare
18
+ └─ fallback → blocca singolo IP /32 o /128
17
19
  ```
18
20
 
19
21
  ### Filtraggio automatico
20
22
 
21
23
  Il modulo ignora automaticamente:
24
+ - IP nella whitelist `allowedIps` (configurazione statica)
25
+ - IP appartenenti ai range ufficiali Cloudflare (scaricati e cachati 24h)
22
26
  - IP privati (10.x, 192.168.x, 172.16–31.x)
23
27
  - Loopback (127.0.0.1, ::1)
24
28
  - IPv4-mapped IPv6 (::ffff:x.x.x.x → convertiti in IPv4)
@@ -68,6 +72,7 @@ import { CloudflareAttacksModule } from "@liberstudio/cloudflare-list";
68
72
  apiToken: "your-cloudflare-api-token",
69
73
  comment: "Blocked by liberstudio/cloudflare-list",
70
74
  logPath: "/var/log/nestjs-attacks.log",
75
+ allowedIps: ["1.2.3.4", "10.0.0.0/8"],
71
76
  excludedPaths: ["/api/health", "/api/webhook"],
72
77
  silentPaths: ["/auth/me", "/auth/refresh"],
73
78
  verbose: false,
@@ -93,6 +98,7 @@ import { CloudflareAttacksModule } from "@liberstudio/cloudflare-list";
93
98
  listId: config.getOrThrow<string>("CLOUDFLARE_LIST_ID"),
94
99
  comment: config.get<string>("CLOUDFLARE_LIST_COMMENT") || "Blocked",
95
100
  logPath: config.get<string>("CLOUDFLARE_LIST_LOG_PATH") || "/var/log/nestjs-attacks.log",
101
+ allowedIps: ["1.2.3.4", "10.0.0.0/8"],
96
102
  excludedPaths: ["/api/health", "/api/webhook", /^\/api\/public\/.*/],
97
103
  silentPaths: ["/auth/me", "/auth/refresh"],
98
104
  verbose: false,
@@ -114,10 +120,26 @@ export class AppModule {}
114
120
  | `listId` | `string` | si | ID della IP List Cloudflare |
115
121
  | `comment` | `string` | si | Commento aggiunto ad ogni IP bloccato |
116
122
  | `logPath` | `string` | si | Percorso del file di log degli attacchi |
123
+ | `allowedIps` | `string[]` | no | IP o CIDR mai bloccati (whitelist, supporta IPv4 e IPv6) |
117
124
  | `excludedPaths` | `(string \| RegExp)[]` | no | Path esclusi da ogni rilevamento e log |
118
125
  | `silentPaths` | `(string \| RegExp)[]` | no | Path bloccati su Cloudflare ma senza log su file |
119
126
  | `verbose` | `boolean` | no | Se `true`, il log include body, query, params e stack |
120
127
 
128
+ ### `allowedIps` — whitelist server
129
+
130
+ Lista di IP o subnet CIDR che non vengono mai bloccati, indipendentemente dal comportamento. Utile per i propri server, proxy interni, CDN proprietarie. Supporta IPv4, CIDR IPv4, IPv6 e CIDR IPv6:
131
+
132
+ ```typescript
133
+ allowedIps: [
134
+ "1.2.3.4", // IP singolo IPv4
135
+ "10.0.0.0/8", // subnet IPv4
136
+ "2001:db8::1", // IP singolo IPv6
137
+ "2001:db8::/32", // subnet IPv6
138
+ ]
139
+ ```
140
+
141
+ > I range ufficiali Cloudflare sono esclusi automaticamente senza bisogno di aggiungerli qui.
142
+
121
143
  ### `excludedPaths` vs `silentPaths`
122
144
 
123
145
  - **`excludedPaths`**: il path viene ignorato completamente — nessun log, nessun blocco. Usato per path infrastrutturali (`/health`, `/webhook`).
@@ -31,6 +31,10 @@ let AttacksService = class AttacksService {
31
31
  this.ASN_LOOKUP_URL = `https://api.hackertarget.com/aslookup`;
32
32
  }
33
33
  async blockIp(rawIp) {
34
+ if ((0, utils_1.isAllowedIp)(rawIp, this.options.allowedIps ?? [])) {
35
+ this.logger.log(`IP ${rawIp} in whitelist — ban saltato`);
36
+ return null;
37
+ }
34
38
  if (await (0, utils_1.isCloudflareIp)(rawIp)) {
35
39
  this.logger.log(`IP ${rawIp} appartiene al range Cloudflare — ban saltato`);
36
40
  return null;
@@ -4,6 +4,7 @@ export interface CloudflareAttacksOptions {
4
4
  listId: string;
5
5
  comment: string;
6
6
  logPath: string;
7
+ allowedIps?: string[];
7
8
  excludedPaths?: (string | RegExp)[];
8
9
  silentPaths?: (string | RegExp)[];
9
10
  verbose?: boolean;
@@ -1 +1,2 @@
1
+ export declare function isAllowedIp(ip: string, allowedIps: string[]): boolean;
1
2
  export declare function isCloudflareIp(ip: string): Promise<boolean>;
@@ -3,6 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.isAllowedIp = isAllowedIp;
6
7
  exports.isCloudflareIp = isCloudflareIp;
7
8
  const axios_1 = __importDefault(require("axios"));
8
9
  const net_1 = require("net");
@@ -60,6 +61,29 @@ function isInCidrV6(ip, cidr) {
60
61
  const mask = ((1n << 128n) - 1n) ^ ((1n << BigInt(128 - prefix)) - 1n);
61
62
  return (ipv6ToBigInt(ip) & mask) === (ipv6ToBigInt(range) & mask);
62
63
  }
64
+ function isAllowedIp(ip, allowedIps) {
65
+ if (!ip || !allowedIps.length)
66
+ return false;
67
+ let pure = ip.includes("/") ? ip.split("/")[0] : ip;
68
+ pure = pure.trim();
69
+ if (pure.startsWith("::ffff:"))
70
+ pure = pure.slice("::ffff:".length);
71
+ const version = (0, net_1.isIP)(pure);
72
+ if (!version)
73
+ return false;
74
+ return allowedIps.some((entry) => {
75
+ let entryPure = entry.includes("/") ? entry.split("/")[0] : entry;
76
+ entryPure = entryPure.trim();
77
+ if (entryPure.startsWith("::ffff:"))
78
+ entryPure = entryPure.slice("::ffff:".length);
79
+ const entryVersion = (0, net_1.isIP)(entryPure);
80
+ if (!entryVersion || entryVersion !== version)
81
+ return false;
82
+ return version === 4
83
+ ? isInCidrV4(pure, entry)
84
+ : isInCidrV6(pure, entry);
85
+ });
86
+ }
63
87
  async function isCloudflareIp(ip) {
64
88
  if (!ip)
65
89
  return false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@liberstudio/cloudflare-list",
3
- "version": "2.3.4",
3
+ "version": "2.3.6",
4
4
  "description": "Modulo NestJS per gestione IP List Cloudflare",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",