reguard 0.9.0 → 0.11.0
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 +2 -2
- package/dist/chunks/{chunk-CGXTVKRB.js → chunk-2N762ZXU.js} +152 -37
- package/dist/chunks/{chunk-CUIFGML3.js → chunk-EV6D7UZF.js} +82 -36
- package/dist/chunks/{chunk-W4FDH5AD.js → chunk-VZKXPEWQ.js} +9 -7
- package/dist/chunks/{chunk-PRPLLMXA.js → chunk-XCSVBLHF.js} +39 -34
- package/dist/cjs/fastify.cjs +235 -86
- package/dist/cjs/fetch.cjs +190 -74
- package/dist/cjs/index.cjs +234 -77
- package/dist/cjs/next.cjs +211 -92
- package/dist/cjs/testing.cjs +0 -104
- package/dist/fastify.js +4 -12
- package/dist/fetch.js +3 -3
- package/dist/index.js +3 -3
- package/dist/next.js +24 -22
- package/dist/testing.js +1 -1
- package/dist/types/node/src/fastify.d.ts +15 -7
- package/dist/types/node/src/fetch.d.ts +6 -2
- package/dist/types/node/src/next.d.ts +38 -17
- package/dist/types/shared/src/address.d.ts +25 -6
- package/dist/types/shared/src/collect.d.ts +7 -1
- package/dist/types/shared/src/index.d.ts +17 -5
- package/dist/types/shared/src/paths.d.ts +6 -2
- package/dist/types/shared/src/tools.d.ts +10 -0
- package/dist/types-cjs/node/src/fastify.d.cts +15 -7
- package/dist/types-cjs/node/src/fetch.d.cts +6 -2
- package/dist/types-cjs/node/src/next.d.cts +38 -17
- package/dist/types-cjs/shared/src/address.d.cts +25 -6
- package/dist/types-cjs/shared/src/collect.d.cts +7 -1
- package/dist/types-cjs/shared/src/index.d.cts +17 -5
- package/dist/types-cjs/shared/src/paths.d.cts +6 -2
- package/dist/types-cjs/shared/src/tools.d.cts +10 -0
- package/package.json +3 -2
|
@@ -14,12 +14,13 @@ import {
|
|
|
14
14
|
checkNumber,
|
|
15
15
|
makeLog,
|
|
16
16
|
reguardApi
|
|
17
|
-
} from "./chunk-
|
|
17
|
+
} from "./chunk-VZKXPEWQ.js";
|
|
18
18
|
import {
|
|
19
19
|
Allowance,
|
|
20
20
|
BENIGN_TOOL_FAMILIES,
|
|
21
21
|
RETRY_AFTER,
|
|
22
22
|
SESSION_COOKIE,
|
|
23
|
+
addressKey,
|
|
23
24
|
canonicalPath,
|
|
24
25
|
collectReport,
|
|
25
26
|
collectorBase,
|
|
@@ -39,7 +40,9 @@ import {
|
|
|
39
40
|
normalizeIp,
|
|
40
41
|
parseSiteKey,
|
|
41
42
|
pathTraits,
|
|
43
|
+
presentsAsBrowser,
|
|
42
44
|
privateUrl,
|
|
45
|
+
protectionMatcher,
|
|
43
46
|
pushedVerdict,
|
|
44
47
|
readVerdictFrom,
|
|
45
48
|
renderScriptTag,
|
|
@@ -49,7 +52,7 @@ import {
|
|
|
49
52
|
toolOf,
|
|
50
53
|
wantsPage,
|
|
51
54
|
watchworthy
|
|
52
|
-
} from "./chunk-
|
|
55
|
+
} from "./chunk-2N762ZXU.js";
|
|
53
56
|
|
|
54
57
|
// src/fetch.ts
|
|
55
58
|
var OPTION_KEYS = [
|
|
@@ -113,7 +116,7 @@ function reguardFetch(options = {}) {
|
|
|
113
116
|
checkFunction(options.fetch, "fetch");
|
|
114
117
|
checkFunction(options.onError, "onError");
|
|
115
118
|
const log = makeLog(checkLogger(options.logger), checkBoolean(options.debug, "debug") ?? /^(?:1|true|yes)$/i.test(envValue("REGUARD_DEBUG") ?? ""));
|
|
116
|
-
const ignore = options.ignore?.length ?
|
|
119
|
+
const ignore = options.ignore?.length ? protectionMatcher(options.ignore) : void 0;
|
|
117
120
|
const key = (options.key ?? envValue("REGUARD_KEY"))?.trim();
|
|
118
121
|
const parsed = parseSiteKey(key);
|
|
119
122
|
const doFetch = options.fetch ?? ((input, init) => fetch(input, init));
|
|
@@ -153,7 +156,7 @@ function reguardFetch(options = {}) {
|
|
|
153
156
|
const allDry = !block || BLOCK_REASONS.every((r) => block.dryRun.has(r));
|
|
154
157
|
const denyMaps = (options.sourceMaps ?? (allDry ? "allow" : "deny")) === "deny";
|
|
155
158
|
const allowList = block?.allow.length ? skipMatcher(block.allow) : void 0;
|
|
156
|
-
const skipped =
|
|
159
|
+
const skipped = protectionMatcher(collect.skip);
|
|
157
160
|
const tagSettings = { site, collect, cookieDomain };
|
|
158
161
|
const pace = block?.pace;
|
|
159
162
|
const pages = pace ? new Allowance(pace.documents.burst, pace.documents.perSecond / 1e3, 5e4) : void 0;
|
|
@@ -212,11 +215,10 @@ function reguardFetch(options = {}) {
|
|
|
212
215
|
}
|
|
213
216
|
if (!health.canSend()) queue.length = 0;
|
|
214
217
|
};
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
if (!
|
|
218
|
-
|
|
219
|
-
waitUntil?.(inflight);
|
|
218
|
+
let lastUrgent = 0;
|
|
219
|
+
const flushSoon = (now = false) => {
|
|
220
|
+
if (queue.length && !inflight && (now || Date.now() - lastFlush >= flushIntervalMs || queue.length >= 100)) inflight = flush().finally(() => inflight = void 0);
|
|
221
|
+
return inflight;
|
|
220
222
|
};
|
|
221
223
|
const pushedFor = async (request, verified) => {
|
|
222
224
|
if (!pushed.size) return null;
|
|
@@ -225,7 +227,9 @@ function reguardFetch(options = {}) {
|
|
|
225
227
|
const now = Date.now();
|
|
226
228
|
const claims = [];
|
|
227
229
|
const k = [sid ? `s:${sid}` : ""];
|
|
228
|
-
|
|
230
|
+
const get = (n) => request.headers.get(n) ?? void 0;
|
|
231
|
+
const browser = presentsAsBrowser({ userAgent: get("user-agent") ?? "", acceptLanguage: get("accept-language"), accept: get("accept"), secFetchMode: get("sec-fetch-mode"), secFetchDest: get("sec-fetch-dest"), secChUa: get("sec-ch-ua") });
|
|
232
|
+
if (collect.addresses !== "off" && !verified && !a.internal && !browser && a.address) k.push(`v:${await hmac(parsed.secret, `visitor:${addressKey(a.address) || a.address}`, 22)}`);
|
|
229
233
|
let vid;
|
|
230
234
|
for (const x of k) {
|
|
231
235
|
const e = x ? pushed.get(x) : void 0;
|
|
@@ -254,14 +258,11 @@ function reguardFetch(options = {}) {
|
|
|
254
258
|
if (sid && /^[\w-]{1,64}$/.test(sid)) headers.set("cookie", `${SESSION_COOKIE}=${sid}`);
|
|
255
259
|
headers.set("x-reguard-site", `${site}.${await hmac(parsed.secret, `prefix:${site}`, 16)}`);
|
|
256
260
|
const a = addressFor(request);
|
|
257
|
-
const value = collect.addresses === "off" || !a.address ? "-" : await hmac(parsed.secret, `visitor:${a.address}`, 22);
|
|
261
|
+
const value = collect.addresses === "off" || !a.address ? "-" : await hmac(parsed.secret, `visitor:${addressKey(a.address) || a.address}`, 22);
|
|
258
262
|
headers.set("x-reguard-visitor", `${value}.${await hmac(parsed.secret, `visitor-header:${value}`, 16)}`);
|
|
259
263
|
try {
|
|
260
264
|
const init = { method: request.method, headers, redirect: "manual", signal: AbortSignal.timeout(timeoutMs) };
|
|
261
|
-
if (request.method !== "GET" && request.method !== "HEAD" && request.body)
|
|
262
|
-
init.body = request.body;
|
|
263
|
-
init.duplex = "half";
|
|
264
|
-
}
|
|
265
|
+
if (request.method !== "GET" && request.method !== "HEAD" && request.body) init.body = await request.arrayBuffer();
|
|
265
266
|
const r = await doFetch(`${collector}${p.decoded}${url.search}`, init);
|
|
266
267
|
const out = new Headers();
|
|
267
268
|
r.headers.forEach((v, k) => {
|
|
@@ -293,7 +294,7 @@ function reguardFetch(options = {}) {
|
|
|
293
294
|
} catch (e) {
|
|
294
295
|
log.once("onBlock-threw", "warn", `block.onBlock threw: ${e?.message ?? e}`);
|
|
295
296
|
}
|
|
296
|
-
log.debug(`${d.dryRun ? "would block" : "blocked"} ${request.method} ${d.path}: ${d.reason}`);
|
|
297
|
+
log.debug(`${d.dryRun ? "would block" : "blocked"} ${request.method} ${d.path}: ${d.reason}${d.pace ? ` (${d.pace})` : ""} from ${d.clientIp}`);
|
|
297
298
|
if (d.dryRun) return void 0;
|
|
298
299
|
const respond = block.respond;
|
|
299
300
|
if (respond) {
|
|
@@ -313,9 +314,9 @@ function reguardFetch(options = {}) {
|
|
|
313
314
|
if (denyMaps && isSourceMapPath(p)) return new Response(null, { status: 404, headers: { "cache-control": "no-store" } });
|
|
314
315
|
const cookie = request.headers.get("cookie");
|
|
315
316
|
const pending = (async () => {
|
|
316
|
-
const
|
|
317
|
-
const pushedV = await pushedFor(request, !!
|
|
318
|
-
return { browser, pushed: pushedV, merged: mergeVerdicts(
|
|
317
|
+
const browser2 = await readVerdict(cookie);
|
|
318
|
+
const pushedV = await pushedFor(request, !!browser2);
|
|
319
|
+
return { browser: browser2, pushed: pushedV, merged: mergeVerdicts(browser2, pushedV) };
|
|
319
320
|
})();
|
|
320
321
|
verdicts.set(request, pending);
|
|
321
322
|
if (!block) return void 0;
|
|
@@ -328,10 +329,11 @@ function reguardFetch(options = {}) {
|
|
|
328
329
|
})();
|
|
329
330
|
if (allowed) return void 0;
|
|
330
331
|
const get = (n) => request.headers.get(n) ?? void 0;
|
|
331
|
-
|
|
332
|
+
const browser = presentsAsBrowser({ userAgent: get("user-agent") ?? "", acceptLanguage: get("accept-language"), accept: get("accept"), secFetchMode: get("sec-fetch-mode"), secFetchDest: get("sec-fetch-dest"), secChUa: get("sec-ch-ua") });
|
|
333
|
+
if (pages && !browser && (request.method === "GET" || request.method === "HEAD") && requestKind(p.decoded) === "document" && !isSpeculativeHeaders(get)) {
|
|
332
334
|
const tool = toolOf(get("user-agent"));
|
|
333
335
|
const a = addressFor(request);
|
|
334
|
-
if (!(tool && BENIGN_TOOL_FAMILIES.has(tool.family)) && a.address && !a.internal && !pages.take(a.address)) {
|
|
336
|
+
if (!(tool && BENIGN_TOOL_FAMILIES.has(tool.family)) && a.address && !a.internal && !pages.take(addressKey(a.address) || a.address)) {
|
|
335
337
|
const answered = await act(request, decide(request, "pace", p, null, "document"));
|
|
336
338
|
if (answered) return answered;
|
|
337
339
|
}
|
|
@@ -347,7 +349,7 @@ function reguardFetch(options = {}) {
|
|
|
347
349
|
};
|
|
348
350
|
const record = (request, response, ctx) => {
|
|
349
351
|
if (!collect.requests) return;
|
|
350
|
-
|
|
352
|
+
const task = (async () => {
|
|
351
353
|
try {
|
|
352
354
|
const url = new URL(request.url);
|
|
353
355
|
const p = canonicalPath(url.pathname);
|
|
@@ -356,18 +358,19 @@ function reguardFetch(options = {}) {
|
|
|
356
358
|
const sid = cookieValue(get("cookie"), SESSION_COOKIE);
|
|
357
359
|
const decision = decisions.get(request);
|
|
358
360
|
const target = `${url.pathname}${url.search}`;
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
if (!
|
|
361
|
+
const toolLike = () => watchworthy({ userAgent: get("user-agent") ?? "", acceptLanguage: get("accept-language"), accept: get("accept"), secFetchMode: get("sec-fetch-mode"), secChUa: get("sec-ch-ua"), secure: url.protocol === "https:" ? true : void 0, path: url.pathname, status: response?.status });
|
|
362
|
+
const tool = toolLike();
|
|
363
|
+
if (!decision) {
|
|
364
|
+
if (!sid && (options.watchSessionless === false || collect.addresses === "off" || !tool)) return;
|
|
365
|
+
if (sid && !response && !tool) return;
|
|
363
366
|
}
|
|
364
367
|
const a = addressFor(request);
|
|
365
368
|
const hash = (s) => hmac(parsed.secret, s, 16);
|
|
366
|
-
const contentType = response
|
|
367
|
-
const responseBytes = Number(response
|
|
369
|
+
const contentType = response?.headers.get("content-type") ?? "";
|
|
370
|
+
const responseBytes = Number(response?.headers.get("content-length") ?? 0) || 0;
|
|
368
371
|
const requestBytes = Number(get("content-length") ?? 0) || 0;
|
|
369
372
|
let exchange;
|
|
370
|
-
if (!isSpeculativeHeaders(get) && isApiLike(target, contentType, requestBytes)) {
|
|
373
|
+
if (response && !isSpeculativeHeaders(get) && isApiLike(target, contentType, requestBytes)) {
|
|
371
374
|
const input = { method: request.method, url: target, requestContentType: get("content-type") ?? "", requestBody: "", requestBytes, status: response.status, responseContentType: contentType, responseBytes };
|
|
372
375
|
const wanted = /* @__PURE__ */ new Set();
|
|
373
376
|
describeExchange((s) => (wanted.add(s), ""), input);
|
|
@@ -387,7 +390,7 @@ function reguardFetch(options = {}) {
|
|
|
387
390
|
url: collect.pages === "masked" ? privateUrl(target) : "",
|
|
388
391
|
...collect.pages !== "off" ? { item: await hash(`path:${url.pathname}`) } : {},
|
|
389
392
|
...collect.pages !== "masked" ? { traits: pathTraits(target, get("rsc") === "1") } : {},
|
|
390
|
-
status: response.status,
|
|
393
|
+
...response ? { status: response.status } : {},
|
|
391
394
|
headers: Object.fromEntries(["rsc", "next-router-prefetch", "sec-purpose", "purpose"].flatMap((n) => get(n) !== void 0 ? [[n, get(n)]] : [])),
|
|
392
395
|
client: {
|
|
393
396
|
ua: get("user-agent") ?? "",
|
|
@@ -401,26 +404,28 @@ function reguardFetch(options = {}) {
|
|
|
401
404
|
hasReferer: !!get("referer"),
|
|
402
405
|
...url.protocol === "https:" ? { secure: true } : {}
|
|
403
406
|
},
|
|
404
|
-
...!sid && a.address && collect.addresses !== "off" ? { visitor: await hmac(parsed.secret, `visitor:${a.address}`, 22) } : {},
|
|
407
|
+
...!sid && a.address && collect.addresses !== "off" ? { visitor: await hmac(parsed.secret, `visitor:${addressKey(a.address) || a.address}`, 22) } : {},
|
|
405
408
|
...exchange ? { exchange } : {},
|
|
406
409
|
...decision?.pace ? { pace: decision.pace } : {},
|
|
407
410
|
...decision ? { decision: { reason: decision.reason, dryRun: decision.dryRun } } : {},
|
|
408
411
|
...a.internal ? { internal: true } : {}
|
|
409
412
|
});
|
|
410
413
|
if (queue.length > 5e3) queue.splice(0, queue.length - 5e3);
|
|
411
|
-
const waitUntil = ctx?.waitUntil ? (p2) => ctx.waitUntil.call(ctx, p2) : void 0;
|
|
412
414
|
ensureTimer();
|
|
413
|
-
|
|
415
|
+
const urgent = !!decision || tool && Date.now() - lastUrgent >= 500;
|
|
416
|
+
if (urgent) lastUrgent = Date.now();
|
|
417
|
+
await flushSoon(urgent);
|
|
414
418
|
} catch (e) {
|
|
415
419
|
onError(e);
|
|
416
420
|
}
|
|
417
421
|
})();
|
|
422
|
+
ctx?.waitUntil?.call(ctx, task);
|
|
418
423
|
};
|
|
419
424
|
let timer;
|
|
420
425
|
const ensureTimer = () => {
|
|
421
426
|
if (timer !== void 0 || platform === "workers" || typeof setInterval !== "function") return;
|
|
422
427
|
try {
|
|
423
|
-
timer = setInterval(() => flushSoon(
|
|
428
|
+
timer = setInterval(() => void flushSoon(true), flushIntervalMs);
|
|
424
429
|
timer.unref?.();
|
|
425
430
|
} catch {
|
|
426
431
|
timer = null;
|