fullshield 0.4.0 → 1.0.1
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/CHANGELOG.md +119 -1
- package/README.md +17 -5
- package/dist/agent.js +50 -10582
- package/dist/bench/score.js +2 -118
- package/dist/cli/index.js +134 -15538
- package/dist/compliance.js +2 -164
- package/dist/confirm.js +2 -146
- package/dist/dashboard.js +2 -179
- package/dist/iac.js +2 -132
- package/dist/incremental.js +2 -13
- package/dist/index.d.ts +191 -5
- package/dist/index.js +94 -11869
- package/dist/llm.js +2 -105
- package/dist/mcp.js +48 -10692
- package/dist/runtime/auto.js +5 -1260
- package/dist/runtime/dlp.js +1 -313
- package/dist/runtime/express.js +1 -579
- package/dist/runtime/fastify.js +1 -579
- package/dist/runtime/hono.js +1 -570
- package/dist/runtime/index.js +1 -229
- package/dist/runtime/koa.js +1 -573
- package/dist/runtime/rasp/loader.js +1 -14
- package/dist/runtime/rasp.js +14 -1436
- package/dist/runtime/register.js +6 -1268
- package/dist/supply.js +8 -324
- package/dist/threat.js +3 -63
- package/dist/vex.js +2 -180
- package/dist/workspaces.js +2 -23
- package/models/index.json +356 -0
- package/package.json +22 -7
- package/rules/DEPS-LOCKMISMATCH-001.md +7 -0
- package/rules/DEPS-PHANTOM-001.md +7 -0
- package/rules/README.md +1 -0
- package/rules/SECRET-AWS-001.md +6 -0
- package/rules/SECRET-GITHUB-001.md +6 -0
- package/rules/SUPPLY-CAP-001.md +17 -0
- package/rules/SUPPLY-PROV-001.md +15 -0
package/dist/runtime/fastify.js
CHANGED
|
@@ -1,579 +1 @@
|
|
|
1
|
-
// src/runtime/dlp/index.ts
|
|
2
|
-
var DLP_FLAG = "FULLSHIELD_EXPERIMENTAL_DLP";
|
|
3
|
-
var DLP_MAX_BYTES = 64 * 1024;
|
|
4
|
-
var DLP_GENERIC = { error: "internal error" };
|
|
5
|
-
var SECRET_RES = [
|
|
6
|
-
/\bAKIA[0-9A-Z]{16}\b/g,
|
|
7
|
-
/\bsk_(?:live|test)_[A-Za-z0-9]{16,}\b/g,
|
|
8
|
-
/\b(?:ghp|gho|ghu|ghs|ghr)_[A-Za-z0-9]{36}\b/g,
|
|
9
|
-
/\bxox[baprs]-[A-Za-z0-9-]{10,}\b/g,
|
|
10
|
-
/\bAIza[0-9A-Za-z_-]{35}\b/g,
|
|
11
|
-
/\bsk-ant-[A-Za-z0-9_-]{16,}\b|\bsk-[A-Za-z0-9_-]{20,}\b/g,
|
|
12
|
-
/-----BEGIN (?:[A-Z]+ )*PRIVATE KEY-----/g,
|
|
13
|
-
/\b(?:postgres(?:ql)?|mysql|mariadb|mongodb(?:\+srv)?|redis|amqp):\/\/[^\s'"@/:]+:[^\s'"@]+@/g
|
|
14
|
-
];
|
|
15
|
-
var STACK_RE = /(?:Error|Exception|TypeError|ReferenceError|SyntaxError|RangeError)[^\n]{0,240}(?:\n|\r\n|\\n)[ \t]*at[ \t]+\S+[^\n]{0,320}(?:\n|\r\n|\\n)[ \t]*at[ \t]+/g;
|
|
16
|
-
var INTERNAL_IP_RE = /\b(?:10\.\d{1,3}\.\d{1,3}\.\d{1,3}|192\.168\.\d{1,3}\.\d{1,3}|172\.(?:1[6-9]|2\d|3[01])\.\d{1,3}\.\d{1,3}|127\.\d{1,3}\.\d{1,3}\.\d{1,3}|169\.254\.\d{1,3}\.\d{1,3})\b/g;
|
|
17
|
-
var HOST_TLDS = [".internal", ".corp", ".lan", ".local"];
|
|
18
|
-
var BULK_EMAILS = 5;
|
|
19
|
-
var PII_LEARN = 2;
|
|
20
|
-
var REDACT = "[REDACTED]";
|
|
21
|
-
var EMPTY_HITS = [];
|
|
22
|
-
function isDlpEnabled() {
|
|
23
|
-
return process.env[DLP_FLAG] === "1";
|
|
24
|
-
}
|
|
25
|
-
function allMatches(re, text) {
|
|
26
|
-
re.lastIndex = 0;
|
|
27
|
-
return [...text.matchAll(re)];
|
|
28
|
-
}
|
|
29
|
-
function maskSample(value) {
|
|
30
|
-
if (value.length <= 4) return "****";
|
|
31
|
-
return `${value.slice(0, 4)}****`.slice(0, 40);
|
|
32
|
-
}
|
|
33
|
-
function luhnOk(digits) {
|
|
34
|
-
if (digits.length < 13 || digits.length > 19) return false;
|
|
35
|
-
const first = digits.charCodeAt(0) - 48;
|
|
36
|
-
if (first < 3 || first > 6) return false;
|
|
37
|
-
if (digits.split("").every((c) => c === digits[0])) return false;
|
|
38
|
-
let sum = 0;
|
|
39
|
-
let alt = false;
|
|
40
|
-
for (let i = digits.length - 1; i >= 0; i -= 1) {
|
|
41
|
-
let n = digits.charCodeAt(i) - 48;
|
|
42
|
-
if (alt) {
|
|
43
|
-
n *= 2;
|
|
44
|
-
if (n > 9) n -= 9;
|
|
45
|
-
}
|
|
46
|
-
sum += n;
|
|
47
|
-
alt = !alt;
|
|
48
|
-
}
|
|
49
|
-
return sum % 10 === 0;
|
|
50
|
-
}
|
|
51
|
-
var SECRET_MARKERS = [
|
|
52
|
-
"AKIA",
|
|
53
|
-
"sk_",
|
|
54
|
-
"ghp_",
|
|
55
|
-
"gho_",
|
|
56
|
-
"ghu_",
|
|
57
|
-
"ghs_",
|
|
58
|
-
"ghr_",
|
|
59
|
-
"xox",
|
|
60
|
-
"AIza",
|
|
61
|
-
"sk-",
|
|
62
|
-
"BEGIN ",
|
|
63
|
-
"postgres",
|
|
64
|
-
"mysql",
|
|
65
|
-
"mariadb",
|
|
66
|
-
"mongodb",
|
|
67
|
-
"redis",
|
|
68
|
-
"amqp"
|
|
69
|
-
];
|
|
70
|
-
function hasAny(text, markers) {
|
|
71
|
-
for (const m of markers) {
|
|
72
|
-
if (text.includes(m)) return true;
|
|
73
|
-
}
|
|
74
|
-
return false;
|
|
75
|
-
}
|
|
76
|
-
function collectSpans(text) {
|
|
77
|
-
const spans = [];
|
|
78
|
-
if (hasAny(text, SECRET_MARKERS)) {
|
|
79
|
-
for (const re of SECRET_RES) {
|
|
80
|
-
for (const m of allMatches(re, text)) {
|
|
81
|
-
if (m[0] === void 0 || m.index === void 0) continue;
|
|
82
|
-
if (re.source.includes("postgres") && /\$\{|^<[^>]+>$|^\{[^}]+\}$/.test(m[0])) continue;
|
|
83
|
-
spans.push({ start: m.index, end: m.index + m[0].length, ruleId: "DLP-SECRET-001", text: m[0] });
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
if (text.includes(" at ") || text.includes("Error") || text.includes("Exception")) {
|
|
88
|
-
for (const m of allMatches(STACK_RE, text)) {
|
|
89
|
-
if (m[0] === void 0 || m.index === void 0) continue;
|
|
90
|
-
spans.push({ start: m.index, end: m.index + m[0].length, ruleId: "DLP-STACK-001", text: m[0] });
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
if (/\d/.test(text)) {
|
|
94
|
-
for (const m of allMatches(INTERNAL_IP_RE, text)) {
|
|
95
|
-
if (m[0] === void 0 || m.index === void 0) continue;
|
|
96
|
-
spans.push({ start: m.index, end: m.index + m[0].length, ruleId: "DLP-INTERNAL-001", text: m[0] });
|
|
97
|
-
}
|
|
98
|
-
spans.push(...findCards(text));
|
|
99
|
-
}
|
|
100
|
-
if (text.includes("localhost") || text.includes("LOCALHOST")) {
|
|
101
|
-
spans.push(...findLocalhost(text));
|
|
102
|
-
}
|
|
103
|
-
if (text.includes(".internal") || text.includes(".corp") || text.includes(".lan") || text.includes(".local") || text.includes(".INTERNAL") || text.includes(".CORP") || text.includes(".LAN") || text.includes(".LOCAL")) {
|
|
104
|
-
spans.push(...findInternalHosts(text));
|
|
105
|
-
}
|
|
106
|
-
return spans;
|
|
107
|
-
}
|
|
108
|
-
function isWord(c) {
|
|
109
|
-
return c !== void 0 && /[A-Za-z0-9-]/.test(c);
|
|
110
|
-
}
|
|
111
|
-
function findIgnoreCase(hay, needle, from) {
|
|
112
|
-
const n = needle.length;
|
|
113
|
-
outer: for (let i = from; i <= hay.length - n; i += 1) {
|
|
114
|
-
for (let j = 0; j < n; j += 1) {
|
|
115
|
-
const a = hay.charCodeAt(i + j);
|
|
116
|
-
const b = needle.charCodeAt(j);
|
|
117
|
-
if (a !== b && a !== b - 32 && a !== b + 32) continue outer;
|
|
118
|
-
}
|
|
119
|
-
return i;
|
|
120
|
-
}
|
|
121
|
-
return -1;
|
|
122
|
-
}
|
|
123
|
-
function findLocalhost(text) {
|
|
124
|
-
const spans = [];
|
|
125
|
-
let from = 0;
|
|
126
|
-
while (from < text.length) {
|
|
127
|
-
const idx = findIgnoreCase(text, "localhost", from);
|
|
128
|
-
if (idx < 0) break;
|
|
129
|
-
if (!isWord(text[idx - 1]) && !isWord(text[idx + 9])) {
|
|
130
|
-
spans.push({ start: idx, end: idx + 9, ruleId: "DLP-INTERNAL-001", text: text.slice(idx, idx + 9) });
|
|
131
|
-
}
|
|
132
|
-
from = idx + 9;
|
|
133
|
-
}
|
|
134
|
-
return spans;
|
|
135
|
-
}
|
|
136
|
-
function findInternalHosts(text) {
|
|
137
|
-
const spans = [];
|
|
138
|
-
for (const tld of HOST_TLDS) {
|
|
139
|
-
let from = 0;
|
|
140
|
-
while (from < text.length) {
|
|
141
|
-
const idx = findIgnoreCase(text, tld, from);
|
|
142
|
-
if (idx < 0) break;
|
|
143
|
-
let start = idx - 1;
|
|
144
|
-
while (start >= 0 && /[A-Za-z0-9.-]/.test(text[start] ?? "")) start -= 1;
|
|
145
|
-
start += 1;
|
|
146
|
-
const label = text.slice(start, idx).toLowerCase();
|
|
147
|
-
const end = idx + tld.length;
|
|
148
|
-
if (label.length > 0 && /^[a-z0-9-]+(?:\.[a-z0-9-]+)*$/.test(label) && !isWord(text[end])) {
|
|
149
|
-
spans.push({ start, end, ruleId: "DLP-INTERNAL-001", text: text.slice(start, end) });
|
|
150
|
-
}
|
|
151
|
-
from = idx + tld.length;
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
return spans;
|
|
155
|
-
}
|
|
156
|
-
function findCards(text) {
|
|
157
|
-
const spans = [];
|
|
158
|
-
let i = 0;
|
|
159
|
-
while (i < text.length) {
|
|
160
|
-
const code = text.charCodeAt(i);
|
|
161
|
-
if (code < 48 || code > 57) {
|
|
162
|
-
i += 1;
|
|
163
|
-
continue;
|
|
164
|
-
}
|
|
165
|
-
let j = i;
|
|
166
|
-
let digits = "";
|
|
167
|
-
while (j < text.length) {
|
|
168
|
-
const c = text.charCodeAt(j);
|
|
169
|
-
if (c >= 48 && c <= 57) {
|
|
170
|
-
digits += text[j] ?? "";
|
|
171
|
-
j += 1;
|
|
172
|
-
} else if (c === 32 || c === 9 || c === 45) {
|
|
173
|
-
j += 1;
|
|
174
|
-
} else {
|
|
175
|
-
break;
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
if (luhnOk(digits)) {
|
|
179
|
-
spans.push({ start: i, end: j, ruleId: "DLP-CARD-001", text: text.slice(i, j) });
|
|
180
|
-
}
|
|
181
|
-
i = j > i ? j : i + 1;
|
|
182
|
-
}
|
|
183
|
-
return spans;
|
|
184
|
-
}
|
|
185
|
-
function findEmails(text) {
|
|
186
|
-
const spans = [];
|
|
187
|
-
let from = 0;
|
|
188
|
-
while (from < text.length) {
|
|
189
|
-
const at = text.indexOf("@", from);
|
|
190
|
-
if (at < 0) break;
|
|
191
|
-
let start = at - 1;
|
|
192
|
-
while (start >= 0 && /[A-Za-z0-9._%+-]/.test(text[start] ?? "")) start -= 1;
|
|
193
|
-
start += 1;
|
|
194
|
-
let end = at + 1;
|
|
195
|
-
while (end < text.length && /[A-Za-z0-9.-]/.test(text[end] ?? "")) end += 1;
|
|
196
|
-
const local = text.slice(start, at);
|
|
197
|
-
const domain = text.slice(at + 1, end);
|
|
198
|
-
const dot = domain.lastIndexOf(".");
|
|
199
|
-
const tld = dot > 0 ? domain.slice(dot + 1) : "";
|
|
200
|
-
if (local.length >= 1 && local.length <= 64 && domain.length >= 3 && domain.length <= 255 && /^[A-Za-z]{2,24}$/.test(tld)) {
|
|
201
|
-
spans.push({ start, end, ruleId: "DLP-PII-001", text: text.slice(start, end) });
|
|
202
|
-
}
|
|
203
|
-
from = at + 1;
|
|
204
|
-
}
|
|
205
|
-
return spans;
|
|
206
|
-
}
|
|
207
|
-
function countEmails(text) {
|
|
208
|
-
if (!text.includes("@")) return { count: 0, spans: [] };
|
|
209
|
-
const spans = findEmails(text);
|
|
210
|
-
return { count: spans.length, spans };
|
|
211
|
-
}
|
|
212
|
-
function redact(text, spans) {
|
|
213
|
-
const ordered = [...spans].sort((a, b) => b.start - a.start);
|
|
214
|
-
let out = text;
|
|
215
|
-
const taken = [];
|
|
216
|
-
for (const s of ordered) {
|
|
217
|
-
if (taken.some(([a, b]) => s.start < b && s.end > a)) continue;
|
|
218
|
-
taken.push([s.start, s.end]);
|
|
219
|
-
out = `${out.slice(0, s.start)}${REDACT}${out.slice(s.end)}`;
|
|
220
|
-
}
|
|
221
|
-
return out;
|
|
222
|
-
}
|
|
223
|
-
function uniqueHits(spans) {
|
|
224
|
-
const seen = /* @__PURE__ */ new Set();
|
|
225
|
-
const hits = [];
|
|
226
|
-
for (const s of spans) {
|
|
227
|
-
if (seen.has(s.ruleId)) continue;
|
|
228
|
-
seen.add(s.ruleId);
|
|
229
|
-
hits.push({ ruleId: s.ruleId, sample: maskSample(s.text) });
|
|
230
|
-
}
|
|
231
|
-
return hits;
|
|
232
|
-
}
|
|
233
|
-
function toText(body) {
|
|
234
|
-
if (body === null || body === void 0) return void 0;
|
|
235
|
-
if (typeof body === "string") return { text: body, kind: "string" };
|
|
236
|
-
if (typeof body === "number" || typeof body === "boolean") return { text: String(body), kind: "string" };
|
|
237
|
-
if (typeof Buffer !== "undefined" && Buffer.isBuffer(body)) return void 0;
|
|
238
|
-
try {
|
|
239
|
-
return { text: JSON.stringify(body), kind: "json" };
|
|
240
|
-
} catch {
|
|
241
|
-
return void 0;
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
function createDlpInspector(options = {}) {
|
|
245
|
-
if (!isDlpEnabled() || options.dlp === void 0) return void 0;
|
|
246
|
-
const mode = options.dlp.mode;
|
|
247
|
-
const maxBytes = options.dlp.maxBytes ?? DLP_MAX_BYTES;
|
|
248
|
-
const routes = /* @__PURE__ */ new Map();
|
|
249
|
-
let skipped = 0;
|
|
250
|
-
const inspect = (body, ctx) => {
|
|
251
|
-
const ct = ctx.contentType ?? "";
|
|
252
|
-
if (ct.length > 0 && !/json|text|javascript|xml/i.test(ct)) {
|
|
253
|
-
return { action: "allow", body, hits: [], skippedOverCap: false };
|
|
254
|
-
}
|
|
255
|
-
const serialized = toText(body);
|
|
256
|
-
if (serialized === void 0) {
|
|
257
|
-
return { action: "allow", body, hits: [], skippedOverCap: false };
|
|
258
|
-
}
|
|
259
|
-
if (serialized.text.length > maxBytes) {
|
|
260
|
-
skipped += 1;
|
|
261
|
-
return { action: "skip", body, hits: [], skippedOverCap: true };
|
|
262
|
-
}
|
|
263
|
-
const spans = collectSpans(serialized.text);
|
|
264
|
-
const emails = countEmails(serialized.text);
|
|
265
|
-
let pii = routes.get(ctx.path);
|
|
266
|
-
if (pii === void 0) {
|
|
267
|
-
pii = { samples: 0, seenLow: false, seenHigh: false };
|
|
268
|
-
routes.set(ctx.path, pii);
|
|
269
|
-
}
|
|
270
|
-
if (pii.samples < PII_LEARN) {
|
|
271
|
-
pii.samples += 1;
|
|
272
|
-
if (emails.count <= 1) pii.seenLow = true;
|
|
273
|
-
if (emails.count >= BULK_EMAILS) pii.seenHigh = true;
|
|
274
|
-
} else if (pii.seenLow && !pii.seenHigh && emails.count >= BULK_EMAILS) {
|
|
275
|
-
spans.push(...emails.spans);
|
|
276
|
-
}
|
|
277
|
-
const hits = uniqueHits(spans);
|
|
278
|
-
if (hits.length === 0) {
|
|
279
|
-
return { action: "allow", body, hits: EMPTY_HITS, skippedOverCap: false };
|
|
280
|
-
}
|
|
281
|
-
const ip = ctx.ip ?? "unknown";
|
|
282
|
-
for (const hit of hits) {
|
|
283
|
-
const event = { ruleId: hit.ruleId, path: ctx.path, ip, sample: hit.sample };
|
|
284
|
-
options.onEvent?.(event);
|
|
285
|
-
}
|
|
286
|
-
if (mode === "monitor") {
|
|
287
|
-
return { action: "allow", body, hits, skippedOverCap: false };
|
|
288
|
-
}
|
|
289
|
-
if (mode === "block") {
|
|
290
|
-
return { action: "block", body: { ...DLP_GENERIC }, hits, skippedOverCap: false };
|
|
291
|
-
}
|
|
292
|
-
const maskedText = redact(serialized.text, spans);
|
|
293
|
-
let masked = maskedText;
|
|
294
|
-
if (serialized.kind === "json") {
|
|
295
|
-
try {
|
|
296
|
-
masked = JSON.parse(maskedText);
|
|
297
|
-
} catch {
|
|
298
|
-
masked = maskedText;
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
return { action: "mask", body: masked, hits, skippedOverCap: false };
|
|
302
|
-
};
|
|
303
|
-
const api = inspect;
|
|
304
|
-
Object.defineProperty(api, "inspect", { value: inspect });
|
|
305
|
-
Object.defineProperty(api, "skippedOverCap", { get: () => skipped });
|
|
306
|
-
return api;
|
|
307
|
-
}
|
|
308
|
-
function applyDlpWrite(dlp, body, ctx) {
|
|
309
|
-
const result = dlp.inspect(body, ctx);
|
|
310
|
-
return { body: result.body, block: result.action === "block" };
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
// src/runtime/signatures.ts
|
|
314
|
-
var SQLI = /('\s*or\s+'?1'?\s*='?1)|union\s+select|;\s*drop\s+table|;\s*delete\s+from/i;
|
|
315
|
-
var XSS = /<\s*script\b|javascript:\s*|onerror\s*=/i;
|
|
316
|
-
var PATH = /(\.\.\/|\.\.\\|%2e%2e%2f|%2e%2e\\)/i;
|
|
317
|
-
function flattenValues(value, out = [], depth = 0) {
|
|
318
|
-
if (depth > 8 || out.length > 200) return out;
|
|
319
|
-
if (typeof value === "string") {
|
|
320
|
-
out.push(value);
|
|
321
|
-
return out;
|
|
322
|
-
}
|
|
323
|
-
if (Array.isArray(value)) {
|
|
324
|
-
for (const item of value) flattenValues(item, out, depth + 1);
|
|
325
|
-
return out;
|
|
326
|
-
}
|
|
327
|
-
if (value !== null && typeof value === "object") {
|
|
328
|
-
for (const item of Object.values(value)) {
|
|
329
|
-
flattenValues(item, out, depth + 1);
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
return out;
|
|
333
|
-
}
|
|
334
|
-
function detectAttack(text) {
|
|
335
|
-
if (PATH.test(text)) return "path";
|
|
336
|
-
if (XSS.test(text)) return "xss";
|
|
337
|
-
if (SQLI.test(text)) return "sqli";
|
|
338
|
-
return void 0;
|
|
339
|
-
}
|
|
340
|
-
function inspectValues(values) {
|
|
341
|
-
for (const value of values) {
|
|
342
|
-
const hit = detectAttack(value);
|
|
343
|
-
if (hit !== void 0) return hit;
|
|
344
|
-
}
|
|
345
|
-
return void 0;
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
// src/runtime/lru.ts
|
|
349
|
-
var LruMap = class {
|
|
350
|
-
constructor(max) {
|
|
351
|
-
this.max = max;
|
|
352
|
-
if (max < 1) throw new Error("LruMap max must be >= 1");
|
|
353
|
-
}
|
|
354
|
-
max;
|
|
355
|
-
map = /* @__PURE__ */ new Map();
|
|
356
|
-
get size() {
|
|
357
|
-
return this.map.size;
|
|
358
|
-
}
|
|
359
|
-
has(key) {
|
|
360
|
-
return this.map.has(key);
|
|
361
|
-
}
|
|
362
|
-
get(key) {
|
|
363
|
-
if (!this.map.has(key)) return void 0;
|
|
364
|
-
const value = this.map.get(key);
|
|
365
|
-
this.map.delete(key);
|
|
366
|
-
this.map.set(key, value);
|
|
367
|
-
return value;
|
|
368
|
-
}
|
|
369
|
-
set(key, value) {
|
|
370
|
-
if (this.map.has(key)) this.map.delete(key);
|
|
371
|
-
this.map.set(key, value);
|
|
372
|
-
while (this.map.size > this.max) {
|
|
373
|
-
const oldest = this.map.keys().next().value;
|
|
374
|
-
if (oldest === void 0) break;
|
|
375
|
-
this.map.delete(oldest);
|
|
376
|
-
}
|
|
377
|
-
return this;
|
|
378
|
-
}
|
|
379
|
-
};
|
|
380
|
-
|
|
381
|
-
// src/runtime/rateLimit.ts
|
|
382
|
-
var DEFAULT_MAX_KEYS = 1e4;
|
|
383
|
-
function createRateLimiter(options) {
|
|
384
|
-
const maxKeys = options.maxKeys ?? DEFAULT_MAX_KEYS;
|
|
385
|
-
const hits = new LruMap(maxKeys);
|
|
386
|
-
return {
|
|
387
|
-
allow(key, now = Date.now()) {
|
|
388
|
-
const windowStart = now - options.windowMs;
|
|
389
|
-
const prev = hits.get(key);
|
|
390
|
-
if (prev === void 0) {
|
|
391
|
-
hits.set(key, [now]);
|
|
392
|
-
return true;
|
|
393
|
-
}
|
|
394
|
-
let keep = 0;
|
|
395
|
-
for (let i = 0; i < prev.length; i += 1) {
|
|
396
|
-
const t = prev[i];
|
|
397
|
-
if (t !== void 0 && t > windowStart) {
|
|
398
|
-
prev[keep] = t;
|
|
399
|
-
keep += 1;
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
prev.length = keep;
|
|
403
|
-
if (prev.length >= options.max) {
|
|
404
|
-
hits.set(key, prev);
|
|
405
|
-
return false;
|
|
406
|
-
}
|
|
407
|
-
prev.push(now);
|
|
408
|
-
hits.set(key, prev);
|
|
409
|
-
return true;
|
|
410
|
-
},
|
|
411
|
-
get size() {
|
|
412
|
-
return hits.size;
|
|
413
|
-
},
|
|
414
|
-
get maxKeys() {
|
|
415
|
-
return maxKeys;
|
|
416
|
-
}
|
|
417
|
-
};
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
// src/runtime/inspect.ts
|
|
421
|
-
var RULE = {
|
|
422
|
-
sqli: "RUNTIME-SQLI-001",
|
|
423
|
-
xss: "RUNTIME-XSS-001",
|
|
424
|
-
path: "RUNTIME-PATH-001"
|
|
425
|
-
};
|
|
426
|
-
function bodyStats(value, depth = 0) {
|
|
427
|
-
if (value === null || value === void 0) return { bytes: 0, depth, keys: 0 };
|
|
428
|
-
if (typeof value === "string") return { bytes: value.length, depth, keys: 0 };
|
|
429
|
-
if (typeof value === "number" || typeof value === "boolean") {
|
|
430
|
-
return { bytes: String(value).length, depth, keys: 0 };
|
|
431
|
-
}
|
|
432
|
-
if (typeof value !== "object") return { bytes: 0, depth, keys: 0 };
|
|
433
|
-
const entries = Object.entries(value);
|
|
434
|
-
let bytes = 2;
|
|
435
|
-
let keys = entries.length;
|
|
436
|
-
let maxDepth = depth;
|
|
437
|
-
for (const [k, v] of entries) {
|
|
438
|
-
bytes += k.length;
|
|
439
|
-
const inner = bodyStats(v, depth + 1);
|
|
440
|
-
bytes += inner.bytes;
|
|
441
|
-
keys += inner.keys;
|
|
442
|
-
if (inner.depth > maxDepth) maxDepth = inner.depth;
|
|
443
|
-
}
|
|
444
|
-
return { bytes, depth: maxDepth, keys };
|
|
445
|
-
}
|
|
446
|
-
var DEFAULT_MAX_ROUTES = 1e3;
|
|
447
|
-
var DEFAULT_MAX_PARAM_NAMES = 200;
|
|
448
|
-
function keysOf(value) {
|
|
449
|
-
if (value === null || value === void 0 || typeof value !== "object") return [];
|
|
450
|
-
return Object.keys(value);
|
|
451
|
-
}
|
|
452
|
-
function createInspector(options = {}) {
|
|
453
|
-
const mode = options.mode ?? "monitor";
|
|
454
|
-
const authRoutes = new Set(options.authRoutes ?? ["/login"]);
|
|
455
|
-
const limiter = createRateLimiter(options.rateLimit ?? { windowMs: 6e4, max: 20 });
|
|
456
|
-
const maxBodyBytes = options.maxBodyBytes ?? 1e6;
|
|
457
|
-
const maxBodyDepth = options.maxBodyDepth ?? 8;
|
|
458
|
-
const maxBodyKeys = options.maxBodyKeys ?? 200;
|
|
459
|
-
const anomaly = options.anomaly;
|
|
460
|
-
const maxRoutes = anomaly?.maxRoutes ?? DEFAULT_MAX_ROUTES;
|
|
461
|
-
const maxParamNames = anomaly?.maxParamNames ?? DEFAULT_MAX_PARAM_NAMES;
|
|
462
|
-
const routes = new LruMap(maxRoutes);
|
|
463
|
-
let windowStart;
|
|
464
|
-
const inspect = ((req) => {
|
|
465
|
-
const path = req.path ?? req.url?.split("?")[0] ?? "/";
|
|
466
|
-
const ip = req.ip ?? "unknown";
|
|
467
|
-
const emit = (ruleId, sample, shouldBlock) => {
|
|
468
|
-
const event = { ruleId, path, ip, sample: sample.slice(0, 40) };
|
|
469
|
-
options.onEvent?.(event);
|
|
470
|
-
if (shouldBlock) options.onBlock?.(event);
|
|
471
|
-
return { block: shouldBlock, event };
|
|
472
|
-
};
|
|
473
|
-
if (authRoutes.has(path) && !limiter.allow(`${ip}:${path}`)) {
|
|
474
|
-
return emit("RUNTIME-RATELIMIT-001", path, mode === "block");
|
|
475
|
-
}
|
|
476
|
-
const stats = bodyStats(req.body);
|
|
477
|
-
if (stats.bytes > maxBodyBytes || stats.depth > maxBodyDepth || stats.keys > maxBodyKeys) {
|
|
478
|
-
return emit("RUNTIME-BODY-001", "oversized-body", mode === "block");
|
|
479
|
-
}
|
|
480
|
-
const values = [
|
|
481
|
-
...flattenValues(req.query),
|
|
482
|
-
...flattenValues(req.body),
|
|
483
|
-
...flattenValues(req.params)
|
|
484
|
-
];
|
|
485
|
-
const kind = inspectValues(values);
|
|
486
|
-
if (kind !== void 0) {
|
|
487
|
-
return emit(RULE[kind], kind, mode === "block");
|
|
488
|
-
}
|
|
489
|
-
if (anomaly !== void 0) {
|
|
490
|
-
const now = anomaly.now ?? Date.now;
|
|
491
|
-
const t = now();
|
|
492
|
-
if (windowStart === void 0) windowStart = t;
|
|
493
|
-
const learning = t - windowStart < anomaly.learnMinutes * 6e4;
|
|
494
|
-
let baseline = routes.get(path);
|
|
495
|
-
if (baseline === void 0) {
|
|
496
|
-
baseline = {
|
|
497
|
-
count: 0,
|
|
498
|
-
bodySum: 0,
|
|
499
|
-
paramNames: new LruMap(maxParamNames),
|
|
500
|
-
status401: 0,
|
|
501
|
-
status404: 0,
|
|
502
|
-
recent404: 0
|
|
503
|
-
};
|
|
504
|
-
routes.set(path, baseline);
|
|
505
|
-
}
|
|
506
|
-
const names = [...keysOf(req.query), ...keysOf(req.body), ...keysOf(req.params)];
|
|
507
|
-
const status = req.statusCode;
|
|
508
|
-
if (learning) {
|
|
509
|
-
baseline.count += 1;
|
|
510
|
-
baseline.bodySum += stats.bytes;
|
|
511
|
-
for (const n of names) baseline.paramNames.set(n, true);
|
|
512
|
-
if (status === 401) baseline.status401 += 1;
|
|
513
|
-
if (status === 404) baseline.status404 += 1;
|
|
514
|
-
} else {
|
|
515
|
-
const newParam = names.some((n) => !baseline.paramNames.has(n));
|
|
516
|
-
const spike401 = status === 401 && baseline.status401 === 0 && baseline.count > 0;
|
|
517
|
-
if (status === 404) baseline.recent404 += 1;
|
|
518
|
-
else baseline.recent404 = 0;
|
|
519
|
-
const burst404 = baseline.recent404 >= 3 && baseline.status404 === 0 && baseline.count > 0;
|
|
520
|
-
if (newParam || spike401 || burst404) {
|
|
521
|
-
const event = {
|
|
522
|
-
ruleId: "RUNTIME-ANOMALY-001",
|
|
523
|
-
path,
|
|
524
|
-
ip,
|
|
525
|
-
sample: newParam ? "new-param" : spike401 ? "401-spike" : "404-burst"
|
|
526
|
-
};
|
|
527
|
-
anomaly.onAnomaly?.(event);
|
|
528
|
-
options.onEvent?.(event);
|
|
529
|
-
return { block: false, event };
|
|
530
|
-
}
|
|
531
|
-
}
|
|
532
|
-
}
|
|
533
|
-
return { block: false };
|
|
534
|
-
});
|
|
535
|
-
Object.defineProperty(inspect, "limiterSize", { get: () => limiter.size });
|
|
536
|
-
Object.defineProperty(inspect, "routeCount", { get: () => routes.size });
|
|
537
|
-
inspect.paramCount = (path) => routes.get(path)?.paramNames.size ?? 0;
|
|
538
|
-
return inspect;
|
|
539
|
-
}
|
|
540
|
-
|
|
541
|
-
// src/runtime/fastify.ts
|
|
542
|
-
function shieldGuard(options = {}) {
|
|
543
|
-
const inspect = createInspector(options);
|
|
544
|
-
const dlp = createDlpInspector(options);
|
|
545
|
-
return function fullshieldFastify(instance, _opts, done) {
|
|
546
|
-
instance.addHook("preHandler", (req, reply, next) => {
|
|
547
|
-
const result = inspect({
|
|
548
|
-
method: req.method,
|
|
549
|
-
url: req.url,
|
|
550
|
-
path: req.url?.split("?")[0],
|
|
551
|
-
ip: req.ip,
|
|
552
|
-
query: req.query,
|
|
553
|
-
body: req.body,
|
|
554
|
-
params: req.params,
|
|
555
|
-
headers: req.headers
|
|
556
|
-
});
|
|
557
|
-
if (result.block) {
|
|
558
|
-
reply.code(403).send({ error: "blocked", ruleId: result.event?.ruleId });
|
|
559
|
-
return;
|
|
560
|
-
}
|
|
561
|
-
next();
|
|
562
|
-
});
|
|
563
|
-
if (dlp !== void 0) {
|
|
564
|
-
instance.addHook("onSend", (req, reply, payload, sendDone) => {
|
|
565
|
-
const path = req.url?.split("?")[0] ?? "/";
|
|
566
|
-
const out = applyDlpWrite(dlp, payload, { path, ip: req.ip });
|
|
567
|
-
if (out.block) {
|
|
568
|
-
reply.code(500);
|
|
569
|
-
sendDone?.(null, JSON.stringify({ error: "internal error" }));
|
|
570
|
-
return;
|
|
571
|
-
}
|
|
572
|
-
sendDone?.(null, typeof out.body === "string" ? out.body : JSON.stringify(out.body));
|
|
573
|
-
});
|
|
574
|
-
}
|
|
575
|
-
done();
|
|
576
|
-
};
|
|
577
|
-
}
|
|
578
|
-
|
|
579
|
-
export { shieldGuard };
|
|
1
|
+
var U="FULLSHIELD_EXPERIMENTAL_DLP";var Z={error:"internal error"},F=[/\bAKIA[0-9A-Z]{16}\b/g,/\bsk_(?:live|test)_[A-Za-z0-9]{16,}\b/g,/\b(?:ghp|gho|ghu|ghs|ghr)_[A-Za-z0-9]{36}\b/g,/\bxox[baprs]-[A-Za-z0-9-]{10,}\b/g,/\bAIza[0-9A-Za-z_-]{35}\b/g,/\bsk-ant-[A-Za-z0-9_-]{16,}\b|\bsk-[A-Za-z0-9_-]{20,}\b/g,/-----BEGIN (?:[A-Z]+ )*PRIVATE KEY-----/g,/\b(?:postgres(?:ql)?|mysql|mariadb|mongodb(?:\+srv)?|redis|amqp):\/\/[^\s'"@/:]+:[^\s'"@]+@/g],$=/(?:Error|Exception|TypeError|ReferenceError|SyntaxError|RangeError)[^\n]{0,240}(?:\n|\r\n|\\n)[ \t]*at[ \t]+\S+[^\n]{0,320}(?:\n|\r\n|\\n)[ \t]*at[ \t]+/g,V=/\b(?:10\.\d{1,3}\.\d{1,3}\.\d{1,3}|192\.168\.\d{1,3}\.\d{1,3}|172\.(?:1[6-9]|2\d|3[01])\.\d{1,3}\.\d{1,3}|127\.\d{1,3}\.\d{1,3}\.\d{1,3}|169\.254\.\d{1,3}\.\d{1,3})\b/g,X=[".internal",".corp",".lan",".local"],N=5,Y=2,q="[REDACTED]",J=[];function W(){return process.env[U]==="1"}function D(n,e){return n.lastIndex=0,[...e.matchAll(n)]}function Q(n){return n.length<=4?"****":`${n.slice(0,4)}****`.slice(0,40)}function nn(n){if(n.length<13||n.length>19)return false;let e=n.charCodeAt(0)-48;if(e<3||e>6||n.split("").every(r=>r===n[0]))return false;let t=0,s=false;for(let r=n.length-1;r>=0;r-=1){let o=n.charCodeAt(r)-48;s&&(o*=2,o>9&&(o-=9)),t+=o,s=!s;}return t%10===0}var en=["AKIA","sk_","ghp_","gho_","ghu_","ghs_","ghr_","xox","AIza","sk-","BEGIN ","postgres","mysql","mariadb","mongodb","redis","amqp"];function tn(n,e){for(let t of e)if(n.includes(t))return true;return false}function sn(n){let e=[];if(tn(n,en))for(let t of F)for(let s of D(t,n))s[0]===void 0||s.index===void 0||t.source.includes("postgres")&&/\$\{|^<[^>]+>$|^\{[^}]+\}$/.test(s[0])||e.push({start:s.index,end:s.index+s[0].length,ruleId:"DLP-SECRET-001",text:s[0]});if(n.includes(" at ")||n.includes("Error")||n.includes("Exception"))for(let t of D($,n))t[0]===void 0||t.index===void 0||e.push({start:t.index,end:t.index+t[0].length,ruleId:"DLP-STACK-001",text:t[0]});if(/\d/.test(n)){for(let t of D(V,n))t[0]===void 0||t.index===void 0||e.push({start:t.index,end:t.index+t[0].length,ruleId:"DLP-INTERNAL-001",text:t[0]});e.push(...an(n));}return (n.includes("localhost")||n.includes("LOCALHOST"))&&e.push(...rn(n)),(n.includes(".internal")||n.includes(".corp")||n.includes(".lan")||n.includes(".local")||n.includes(".INTERNAL")||n.includes(".CORP")||n.includes(".LAN")||n.includes(".LOCAL"))&&e.push(...on(n)),e}function O(n){return n!==void 0&&/[A-Za-z0-9-]/.test(n)}function M(n,e,t){let s=e.length;n:for(let r=t;r<=n.length-s;r+=1){for(let o=0;o<s;o+=1){let a=n.charCodeAt(r+o),i=e.charCodeAt(o);if(a!==i&&a!==i-32&&a!==i+32)continue n}return r}return -1}function rn(n){let e=[],t=0;for(;t<n.length;){let s=M(n,"localhost",t);if(s<0)break;!O(n[s-1])&&!O(n[s+9])&&e.push({start:s,end:s+9,ruleId:"DLP-INTERNAL-001",text:n.slice(s,s+9)}),t=s+9;}return e}function on(n){let e=[];for(let t of X){let s=0;for(;s<n.length;){let r=M(n,t,s);if(r<0)break;let o=r-1;for(;o>=0&&/[A-Za-z0-9.-]/.test(n[o]??"");)o-=1;o+=1;let a=n.slice(o,r).toLowerCase(),i=r+t.length;a.length>0&&/^[a-z0-9-]+(?:\.[a-z0-9-]+)*$/.test(a)&&!O(n[i])&&e.push({start:o,end:i,ruleId:"DLP-INTERNAL-001",text:n.slice(o,i)}),s=r+t.length;}}return e}function an(n){let e=[],t=0;for(;t<n.length;){let s=n.charCodeAt(t);if(s<48||s>57){t+=1;continue}let r=t,o="";for(;r<n.length;){let a=n.charCodeAt(r);if(a>=48&&a<=57)o+=n[r]??"",r+=1;else if(a===32||a===9||a===45)r+=1;else break}nn(o)&&e.push({start:t,end:r,ruleId:"DLP-CARD-001",text:n.slice(t,r)}),t=r>t?r:t+1;}return e}function un(n){let e=[],t=0;for(;t<n.length;){let s=n.indexOf("@",t);if(s<0)break;let r=s-1;for(;r>=0&&/[A-Za-z0-9._%+-]/.test(n[r]??"");)r-=1;r+=1;let o=s+1;for(;o<n.length&&/[A-Za-z0-9.-]/.test(n[o]??"");)o+=1;let a=n.slice(r,s),i=n.slice(s+1,o),u=i.lastIndexOf("."),p=u>0?i.slice(u+1):"";a.length>=1&&a.length<=64&&i.length>=3&&i.length<=255&&/^[A-Za-z]{2,24}$/.test(p)&&e.push({start:r,end:o,ruleId:"DLP-PII-001",text:n.slice(r,o)}),t=s+1;}return e}function cn(n){if(!n.includes("@"))return {count:0,spans:[]};let e=un(n);return {count:e.length,spans:e}}function ln(n,e){let t=[...e].sort((o,a)=>a.start-o.start),s=n,r=[];for(let o of t)r.some(([a,i])=>o.start<i&&o.end>a)||(r.push([o.start,o.end]),s=`${s.slice(0,o.start)}${q}${s.slice(o.end)}`);return s}function dn(n){let e=new Set,t=[];for(let s of n)e.has(s.ruleId)||(e.add(s.ruleId),t.push({ruleId:s.ruleId,sample:Q(s.text)}));return t}function pn(n){if(n!=null){if(typeof n=="string")return {text:n,kind:"string"};if(typeof n=="number"||typeof n=="boolean")return {text:String(n),kind:"string"};if(!(typeof Buffer<"u"&&Buffer.isBuffer(n)))try{return {text:JSON.stringify(n),kind:"json"}}catch{return}}}function P(n={}){if(!W()||n.dlp===void 0)return;let e=n.dlp.mode,t=n.dlp.maxBytes??65536,s=new Map,r=0,o=(i,u)=>{let p=u.contentType??"";if(p.length>0&&!/json|text|javascript|xml/i.test(p))return {action:"allow",body:i,hits:[],skippedOverCap:false};let d=pn(i);if(d===void 0)return {action:"allow",body:i,hits:[],skippedOverCap:false};if(d.text.length>t)return r+=1,{action:"skip",body:i,hits:[],skippedOverCap:true};let g=sn(d.text),f=cn(d.text),c=s.get(u.path);c===void 0&&(c={samples:0,seenLow:false,seenHigh:false},s.set(u.path,c)),c.samples<Y?(c.samples+=1,f.count<=1&&(c.seenLow=true),f.count>=N&&(c.seenHigh=true)):c.seenLow&&!c.seenHigh&&f.count>=N&&g.push(...f.spans);let m=dn(g);if(m.length===0)return {action:"allow",body:i,hits:J,skippedOverCap:false};let E=u.ip??"unknown";for(let A of m){let w={ruleId:A.ruleId,path:u.path,ip:E,sample:A.sample};n.onEvent?.(w);}if(e==="monitor")return {action:"allow",body:i,hits:m,skippedOverCap:false};if(e==="block")return {action:"block",body:{...Z},hits:m,skippedOverCap:false};let b=ln(d.text,g),h=b;if(d.kind==="json")try{h=JSON.parse(b);}catch{h=b;}return {action:"mask",body:h,hits:m,skippedOverCap:false}},a=o;return Object.defineProperty(a,"inspect",{value:o}),Object.defineProperty(a,"skippedOverCap",{get:()=>r}),a}function z(n,e,t){let s=n.inspect(e,t);return {body:s.body,block:s.action==="block"}}var fn=/('\s*or\s+'?1'?\s*='?1)|union\s+select|;\s*drop\s+table|;\s*delete\s+from/i,mn=/<\s*script\b|javascript:\s*|onerror\s*=/i,gn=/(\.\.\/|\.\.\\|%2e%2e%2f|%2e%2e\\)/i;function k(n,e=[],t=0){if(t>8||e.length>200)return e;if(typeof n=="string")return e.push(n),e;if(Array.isArray(n)){for(let s of n)k(s,e,t+1);return e}if(n!==null&&typeof n=="object")for(let s of Object.values(n))k(s,e,t+1);return e}function hn(n){if(gn.test(n))return "path";if(mn.test(n))return "xss";if(fn.test(n))return "sqli"}function B(n){for(let e of n){let t=hn(e);if(t!==void 0)return t}}var y=class{constructor(e){this.max=e;if(e<1)throw new Error("LruMap max must be >= 1")}max;map=new Map;get size(){return this.map.size}has(e){return this.map.has(e)}get(e){if(!this.map.has(e))return;let t=this.map.get(e);return this.map.delete(e),this.map.set(e,t),t}set(e,t){for(this.map.has(e)&&this.map.delete(e),this.map.set(e,t);this.map.size>this.max;){let s=this.map.keys().next().value;if(s===void 0)break;this.map.delete(s);}return this}};var bn=1e4;function K(n){let e=n.maxKeys??bn,t=new y(e);return {allow(s,r=Date.now()){let o=r-n.windowMs,a=t.get(s);if(a===void 0)return t.set(s,[r]),true;let i=0;for(let u=0;u<a.length;u+=1){let p=a[u];p!==void 0&&p>o&&(a[i]=p,i+=1);}return a.length=i,a.length>=n.max?(t.set(s,a),false):(a.push(r),t.set(s,a),true)},get size(){return t.size},get maxKeys(){return e}}}var yn={sqli:"RUNTIME-SQLI-001",xss:"RUNTIME-XSS-001",path:"RUNTIME-PATH-001"};function G(n,e=0){if(n==null)return {bytes:0,depth:e,keys:0};if(typeof n=="string")return {bytes:n.length,depth:e,keys:0};if(typeof n=="number"||typeof n=="boolean")return {bytes:String(n).length,depth:e,keys:0};if(typeof n!="object")return {bytes:0,depth:e,keys:0};let t=Object.entries(n),s=2,r=t.length,o=e;for(let[a,i]of t){s+=a.length;let u=G(i,e+1);s+=u.bytes,r+=u.keys,u.depth>o&&(o=u.depth);}return {bytes:s,depth:o,keys:r}}var kn=1e3,En=200;function _(n){return n==null||typeof n!="object"?[]:Object.keys(n)}function H(n={}){let e=n.mode??"monitor",t=new Set(n.authRoutes??["/login"]),s=K(n.rateLimit??{windowMs:6e4,max:20}),r=n.maxBodyBytes??1e6,o=n.maxBodyDepth??8,a=n.maxBodyKeys??200,i=n.anomaly,u=i?.maxRoutes??kn,p=i?.maxParamNames??En,d=new y(u),g,f=(c=>{let m=c.path??c.url?.split("?")[0]??"/",E=c.ip??"unknown",b=(T,R,S)=>{let l={ruleId:T,path:m,ip:E,sample:R.slice(0,40)};return n.onEvent?.(l),S&&n.onBlock?.(l),{block:S,event:l}};if(t.has(m)&&!s.allow(`${E}:${m}`))return b("RUNTIME-RATELIMIT-001",m,e==="block");let h=G(c.body);if(h.bytes>r||h.depth>o||h.keys>a)return b("RUNTIME-BODY-001","oversized-body",e==="block");let A=[...k(c.query),...k(c.body),...k(c.params)],w=B(A);if(w!==void 0)return b(yn[w],w,e==="block");if(i!==void 0){let R=(i.now??Date.now)();g===void 0&&(g=R);let S=R-g<i.learnMinutes*6e4,l=d.get(m);l===void 0&&(l={count:0,bodySum:0,paramNames:new y(p),status401:0,status404:0,recent404:0},d.set(m,l));let v=[..._(c.query),..._(c.body),..._(c.params)],x=c.statusCode;if(S){l.count+=1,l.bodySum+=h.bytes;for(let L of v)l.paramNames.set(L,true);x===401&&(l.status401+=1),x===404&&(l.status404+=1);}else {let L=v.some(I=>!l.paramNames.has(I)),C=x===401&&l.status401===0&&l.count>0;x===404?l.recent404+=1:l.recent404=0;let j=l.recent404>=3&&l.status404===0&&l.count>0;if(L||C||j){let I={ruleId:"RUNTIME-ANOMALY-001",path:m,ip:E,sample:L?"new-param":C?"401-spike":"404-burst"};return i.onAnomaly?.(I),n.onEvent?.(I),{block:false,event:I}}}}return {block:false}});return Object.defineProperty(f,"limiterSize",{get:()=>s.size}),Object.defineProperty(f,"routeCount",{get:()=>d.size}),f.paramCount=c=>d.get(c)?.paramNames.size??0,f}function vn(n={}){let e=H(n),t=P(n);return function(r,o,a){r.addHook("preHandler",(i,u,p)=>{let d=e({method:i.method,url:i.url,path:i.url?.split("?")[0],ip:i.ip,query:i.query,body:i.body,params:i.params,headers:i.headers});if(d.block){u.code(403).send({error:"blocked",ruleId:d.event?.ruleId});return}p();}),t!==void 0&&r.addHook("onSend",(i,u,p,d)=>{let g=i.url?.split("?")[0]??"/",f=z(t,p,{path:g,ip:i.ip});if(f.block){u.code(500),d?.(null,JSON.stringify({error:"internal error"}));return}d?.(null,typeof f.body=="string"?f.body:JSON.stringify(f.body));}),a();}}export{vn as shieldGuard};
|