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
package/dist/cjs/fetch.cjs
CHANGED
|
@@ -287,6 +287,13 @@ function browserIncoherence(h) {
|
|
|
287
287
|
}
|
|
288
288
|
return reasons;
|
|
289
289
|
}
|
|
290
|
+
function presentsAsBrowser(h) {
|
|
291
|
+
const ua = h.userAgent ?? "";
|
|
292
|
+
if (!/^Mozilla\/5\.0 \(/.test(ua) || !h.secFetchDest) return false;
|
|
293
|
+
const tool = toolOf(ua);
|
|
294
|
+
if (tool && tool.family !== "headless_browser") return false;
|
|
295
|
+
return browserIncoherence(h).length === 0;
|
|
296
|
+
}
|
|
290
297
|
var SENSITIVE_PATH = /^\/(\.env|\.git|\.svn|\.hg|\.DS_Store|\.aws|\.ssh|\.htaccess|\.htpasswd|web\.config|wp-admin|wp-login\.php|xmlrpc\.php|phpinfo|phpmyadmin|server-status|server-info|actuator|backup|dump\.sql|config\.(?:json|ya?ml|php|js)|swagger|openapi|api-docs|admin|debug|console)(?=$|[/.?#_-])/i;
|
|
291
298
|
var DISCOVERY_FILE = /^\/(?:robots\.txt|sitemap[\w-]*\.xml(?:\.gz)?|\.well-known\/security\.txt|security\.txt|humans\.txt|crossdomain\.xml|asset-manifest\.json|_next\/static\/[^/]+\/_buildManifest\.js)$/i;
|
|
292
299
|
var BROWSER_AUTOMATIC = /^\/(?:favicon\.ico|apple-touch-icon[\w.-]*\.png|browserconfig\.xml|site\.webmanifest|manifest\.json|\.well-known\/appspecific\/)/i;
|
|
@@ -370,18 +377,21 @@ function resolveCollect(input, warn = () => {
|
|
|
370
377
|
}
|
|
371
378
|
return out;
|
|
372
379
|
}
|
|
373
|
-
function skipMatcher(patterns) {
|
|
380
|
+
function skipMatcher(patterns, ignoreCase = false) {
|
|
374
381
|
if (!patterns.length) return () => false;
|
|
375
382
|
const res = patterns.map((p) => {
|
|
376
383
|
const tail = p.endsWith("/*");
|
|
377
384
|
const body = (tail ? p.slice(0, -2) : p).split("*").map((part) => part.replace(/[.+?^${}()|[\]\\]/g, "\\$&")).join(".*");
|
|
378
|
-
return new RegExp(`^${body}${tail ? "(?:/.*)?" : ""}
|
|
385
|
+
return new RegExp(`^${body}${tail ? "(?:/.*)?" : ""}$`, ignoreCase ? "i" : "");
|
|
379
386
|
});
|
|
380
387
|
return (path) => {
|
|
381
388
|
const p = path.split(/[?#]/)[0] ?? path;
|
|
382
389
|
return res.some((re) => re.test(p));
|
|
383
390
|
};
|
|
384
391
|
}
|
|
392
|
+
function protectionMatcher(patterns) {
|
|
393
|
+
return skipMatcher(patterns, true);
|
|
394
|
+
}
|
|
385
395
|
function collectAttributes(c) {
|
|
386
396
|
const a = {};
|
|
387
397
|
if (c.pages !== DEFAULT_COLLECT.pages) a["data-pages"] = c.pages;
|
|
@@ -464,6 +474,98 @@ var CLOUDFLARE_RANGES = [
|
|
|
464
474
|
"2a06:98c0::/29",
|
|
465
475
|
"2c0f:f248::/32"
|
|
466
476
|
];
|
|
477
|
+
var CLOUDFRONT_RANGES = [
|
|
478
|
+
"13.124.199.0/24",
|
|
479
|
+
"130.176.0.0/18",
|
|
480
|
+
"130.176.128.0/21",
|
|
481
|
+
"130.176.136.0/23",
|
|
482
|
+
"130.176.140.0/22",
|
|
483
|
+
"130.176.144.0/20",
|
|
484
|
+
"130.176.160.0/19",
|
|
485
|
+
"130.176.192.0/19",
|
|
486
|
+
"130.176.64.0/21",
|
|
487
|
+
"130.176.72.0/22",
|
|
488
|
+
"130.176.76.0/24",
|
|
489
|
+
"130.176.78.0/23",
|
|
490
|
+
"130.176.80.0/22",
|
|
491
|
+
"130.176.86.0/23",
|
|
492
|
+
"130.176.88.0/21",
|
|
493
|
+
"130.176.96.0/19",
|
|
494
|
+
"15.158.0.0/16",
|
|
495
|
+
"18.68.0.0/16",
|
|
496
|
+
"204.246.166.0/24",
|
|
497
|
+
"205.251.218.0/24",
|
|
498
|
+
"24.110.128.0/17",
|
|
499
|
+
"3.172.0.0/18",
|
|
500
|
+
"3.172.64.0/18",
|
|
501
|
+
"3.29.57.0/26",
|
|
502
|
+
"51.72.40.128/28",
|
|
503
|
+
"51.72.40.144/28",
|
|
504
|
+
"51.72.96.0/23",
|
|
505
|
+
"51.74.20.0/24",
|
|
506
|
+
"51.74.21.0/24",
|
|
507
|
+
"51.74.22.0/24",
|
|
508
|
+
"52.46.0.0/18",
|
|
509
|
+
"52.82.128.0/23",
|
|
510
|
+
"52.82.134.0/23",
|
|
511
|
+
"54.182.128.0/20",
|
|
512
|
+
"54.182.144.0/21",
|
|
513
|
+
"54.182.154.0/23",
|
|
514
|
+
"54.182.156.0/22",
|
|
515
|
+
"54.182.160.0/21",
|
|
516
|
+
"54.182.172.0/22",
|
|
517
|
+
"54.182.176.0/21",
|
|
518
|
+
"54.182.184.0/22",
|
|
519
|
+
"54.182.188.0/23",
|
|
520
|
+
"54.182.224.0/21",
|
|
521
|
+
"54.182.240.0/21",
|
|
522
|
+
"54.182.248.0/22",
|
|
523
|
+
"54.239.134.0/23",
|
|
524
|
+
"54.239.170.0/23",
|
|
525
|
+
"54.239.204.0/22",
|
|
526
|
+
"54.239.208.0/21",
|
|
527
|
+
"64.252.128.0/18",
|
|
528
|
+
"64.252.64.0/18",
|
|
529
|
+
"70.132.0.0/18",
|
|
530
|
+
"2001:3fc6:4::/48",
|
|
531
|
+
"2001:3fc6:5::/48",
|
|
532
|
+
"2406:da10:847f:a100::/56",
|
|
533
|
+
"2406:da11:438:2300::/56",
|
|
534
|
+
"2406:da12:4b:c200::/56",
|
|
535
|
+
"2406:da12:8b2e:9c00::/56",
|
|
536
|
+
"2406:da14:17bd:2f00::/56",
|
|
537
|
+
"2406:da14:80bb:ea00::/56",
|
|
538
|
+
"2406:da16:c01:c200::/56",
|
|
539
|
+
"2406:da18:9fa:1b00::/56",
|
|
540
|
+
"2406:da19:e19:4a00::/56",
|
|
541
|
+
"2406:da1a:6df:6c00::/56",
|
|
542
|
+
"2406:da1b:e7c:ad00::/56",
|
|
543
|
+
"2406:da1c:787:2b00::/56",
|
|
544
|
+
"2406:da1c:8d8c:4600::/56",
|
|
545
|
+
"2406:da1e:705:1600::/56",
|
|
546
|
+
"2406:da1f:396:9100::/56",
|
|
547
|
+
"2600:1f11:e79:a800::/56",
|
|
548
|
+
"2600:1f13:417:4d00::/56",
|
|
549
|
+
"2600:1f16:1923:3000::/56",
|
|
550
|
+
"2600:1f17:4356:f100::/56",
|
|
551
|
+
"2600:1f18:7530:7200::/56",
|
|
552
|
+
"2600:1f1a:4568:b500::/56",
|
|
553
|
+
"2600:1f1c:da:600::/56",
|
|
554
|
+
"2600:1f1e:a7:2300::/56",
|
|
555
|
+
"2600:9000:1000::/36",
|
|
556
|
+
"2600:9000:5200::/40",
|
|
557
|
+
"2600:9000:6000::/36",
|
|
558
|
+
"2a05:d011:531:1800::/56",
|
|
559
|
+
"2a05:d012:581:b400::/56",
|
|
560
|
+
"2a05:d014:1362:b000::/56",
|
|
561
|
+
"2a05:d016:9ed:de00::/56",
|
|
562
|
+
"2a05:d018:1a3a:2d00::/56",
|
|
563
|
+
"2a05:d019:80b:e300::/56",
|
|
564
|
+
"2a05:d01a:8a9:be00::/56",
|
|
565
|
+
"2a05:d01c:343:c300::/56",
|
|
566
|
+
"2a05:d025:e59:fb00::/56"
|
|
567
|
+
];
|
|
568
|
+
var PORT_SUFFIXED_HEADERS = ["cloudfront-viewer-address"];
|
|
467
569
|
var NON_PUBLIC_RANGES = [
|
|
468
570
|
"0.0.0.0/8",
|
|
469
571
|
"10.0.0.0/8",
|
|
@@ -593,63 +695,73 @@ function inCidr(c, b) {
|
|
|
593
695
|
return true;
|
|
594
696
|
}
|
|
595
697
|
var compile = (list) => list.map(parseCidr).filter((c) => c !== null);
|
|
596
|
-
var
|
|
597
|
-
var
|
|
598
|
-
function
|
|
599
|
-
const
|
|
600
|
-
if (!
|
|
601
|
-
|
|
602
|
-
|
|
698
|
+
var compiled;
|
|
699
|
+
var tables = () => compiled ??= { nonPublic: compile(NON_PUBLIC_RANGES), cloudflare: compile(CLOUDFLARE_RANGES), cloudfront: compile(CLOUDFRONT_RANGES) };
|
|
700
|
+
function addressKey(address) {
|
|
701
|
+
const ip = normalizeIp(address);
|
|
702
|
+
if (!ip) return "";
|
|
703
|
+
if (!ip.includes(":")) return ip;
|
|
704
|
+
const b = parseIp(ip);
|
|
705
|
+
const prefix = new Uint8Array(16);
|
|
706
|
+
prefix.set(b.subarray(0, 8));
|
|
707
|
+
return `${formatIp(prefix)}/64`;
|
|
603
708
|
}
|
|
604
709
|
function isPublicIp(address) {
|
|
605
710
|
const b = parseIp(address);
|
|
606
|
-
return !!b && !
|
|
711
|
+
return !!b && !tables().nonPublic.some((c) => inCidr(c, b));
|
|
607
712
|
}
|
|
608
|
-
function
|
|
609
|
-
|
|
610
|
-
const entries = xff.split(",").map((s) => s.trim()).filter(Boolean).slice(-MAX_FORWARDED);
|
|
611
|
-
return [...entries, req.peer];
|
|
713
|
+
function forwardedOf(req) {
|
|
714
|
+
return (req.header("x-forwarded-for") ?? "").split(",").map((t) => t.trim()).filter(Boolean).slice(-MAX_FORWARDED).map((t) => normalizeIp(t)).filter((x) => !!x);
|
|
612
715
|
}
|
|
613
716
|
var result = (address, via) => ({ address, internal: !isPublicIp(address), via });
|
|
614
|
-
function walk(req, trusted, via) {
|
|
615
|
-
const
|
|
616
|
-
|
|
717
|
+
function walk(req, trusted, via, cdns) {
|
|
718
|
+
const peer = normalizeIp(req.peer);
|
|
719
|
+
const chain = [...forwardedOf(req), ...peer ? [peer] : []];
|
|
720
|
+
let edge = "";
|
|
617
721
|
for (let i = chain.length - 1; i >= 0; i--) {
|
|
618
|
-
const ip =
|
|
619
|
-
if (!ip) continue;
|
|
620
|
-
last ||= ip;
|
|
722
|
+
const ip = chain[i];
|
|
621
723
|
const b = parseIp(ip);
|
|
622
|
-
|
|
623
|
-
|
|
724
|
+
const { nonPublic, cloudflare, cloudfront } = tables();
|
|
725
|
+
if (nonPublic.some((c) => inCidr(c, b)) || trusted.some((c) => inCidr(c, b))) continue;
|
|
726
|
+
if (cdns && cloudfront.some((c) => inCidr(c, b))) {
|
|
727
|
+
edge = ip;
|
|
728
|
+
continue;
|
|
729
|
+
}
|
|
730
|
+
if (cdns && cloudflare.some((c) => inCidr(c, b))) {
|
|
731
|
+
const client = normalizeIp(req.header("cf-connecting-ip"));
|
|
732
|
+
return client ? result(client, "cloudflare") : { address: ip, internal: false, via };
|
|
733
|
+
}
|
|
734
|
+
return { address: ip, internal: false, via: peer && i === chain.length - 1 ? "socket" : via };
|
|
624
735
|
}
|
|
625
|
-
return { address:
|
|
736
|
+
if (edge) return { address: edge, internal: false, via };
|
|
737
|
+
return { address: chain[chain.length - 1] ?? "", internal: true, via: "socket" };
|
|
738
|
+
}
|
|
739
|
+
function headerAddress(req, name) {
|
|
740
|
+
const first = req.header(name)?.split(",")[0]?.trim();
|
|
741
|
+
if (!first) return null;
|
|
742
|
+
if (PORT_SUFFIXED_HEADERS.includes(name) && !first.startsWith("[")) return normalizeIp(first.replace(/:\d{1,5}$/, ""));
|
|
743
|
+
return normalizeIp(first);
|
|
626
744
|
}
|
|
627
745
|
function resolveClientAddress(req, mode = "auto", platform = null) {
|
|
628
746
|
const peer = normalizeIp(req.peer) ?? "";
|
|
629
747
|
if (mode === "socket") return result(peer, "socket");
|
|
630
748
|
if (typeof mode === "object" && "header" in mode) {
|
|
631
|
-
const
|
|
632
|
-
const ip = normalizeIp(first);
|
|
749
|
+
const ip = headerAddress(req, mode.header.toLowerCase());
|
|
633
750
|
return ip ? result(ip, "header") : result(peer, "socket");
|
|
634
751
|
}
|
|
635
752
|
if (typeof mode === "object" && "hops" in mode) {
|
|
636
|
-
const
|
|
637
|
-
if (
|
|
638
|
-
|
|
639
|
-
return result(chain[at], at === chain.length - 1 ? "socket" : "hops");
|
|
753
|
+
const xff = forwardedOf(req);
|
|
754
|
+
if (xff.length < mode.hops) return walk(req, [], "hops", false);
|
|
755
|
+
return result(xff[xff.length - mode.hops], "hops");
|
|
640
756
|
}
|
|
641
|
-
if (typeof mode === "object" && "proxies" in mode) return walk(req, compile(mode.proxies), "proxies");
|
|
757
|
+
if (typeof mode === "object" && "proxies" in mode) return walk(req, compile(mode.proxies), "proxies", false);
|
|
642
758
|
const framework = normalizeIp(req.framework);
|
|
643
759
|
if (framework) return result(framework, "framework");
|
|
644
760
|
if (platform) {
|
|
645
|
-
const ip =
|
|
761
|
+
const ip = headerAddress(req, PLATFORM_HEADERS[platform]);
|
|
646
762
|
if (ip) return result(ip, `platform:${platform}`);
|
|
647
763
|
}
|
|
648
|
-
|
|
649
|
-
const ip = normalizeIp(req.header("cf-connecting-ip"));
|
|
650
|
-
if (ip) return result(ip, "cloudflare");
|
|
651
|
-
}
|
|
652
|
-
return walk(req, [], "forwarded");
|
|
764
|
+
return walk(req, [], "forwarded", true);
|
|
653
765
|
}
|
|
654
766
|
function clientIpModeProblem(value) {
|
|
655
767
|
if (value === "auto" || value === "socket") return null;
|
|
@@ -699,7 +811,7 @@ function canonicalPath(target) {
|
|
|
699
811
|
const cut = t.search(/[?#]/);
|
|
700
812
|
let raw = cut === -1 ? t : t.slice(0, cut);
|
|
701
813
|
if (!raw.startsWith("/")) raw = `/${raw}`;
|
|
702
|
-
return { raw, decoded: normalizeSegments(decodeOnce(raw)) };
|
|
814
|
+
return { raw, decoded: normalizeSegments(decodeOnce(raw).replace(/\\/g, "/")) };
|
|
703
815
|
}
|
|
704
816
|
function matchesEither(matcher, p) {
|
|
705
817
|
return matcher(p.raw) || matcher(p.decoded);
|
|
@@ -861,7 +973,7 @@ var ReguardConfigError = class extends TypeError {
|
|
|
861
973
|
}
|
|
862
974
|
};
|
|
863
975
|
var BLOCK_REASONS = ["flagged", "flagged_server", "require", "pace"];
|
|
864
|
-
var LABELS = ["
|
|
976
|
+
var LABELS = ["reverse_engineering", "automated_exploration", "unauthorized_extraction"];
|
|
865
977
|
var STAGES = ["candidate", "alert", "enforce"];
|
|
866
978
|
var fail = (option, code, detail) => {
|
|
867
979
|
throw new ReguardConfigError(option, code, detail);
|
|
@@ -869,14 +981,16 @@ var fail = (option, code, detail) => {
|
|
|
869
981
|
var show = (v) => typeof v === "function" ? "a function" : JSON.stringify(v) ?? String(v);
|
|
870
982
|
var isObject = (v) => !!v && typeof v === "object" && !Array.isArray(v);
|
|
871
983
|
var camel = (s) => s.replace(/_([a-z])/g, (_, c) => c.toUpperCase());
|
|
872
|
-
function checkKeys(obj, allowed, prefix, moved = {}) {
|
|
984
|
+
function checkKeys(obj, allowed, prefix, moved = {}, nested) {
|
|
985
|
+
const like = (k) => (a) => a === camel(k).replace(/Seconds$|Secs$/, "Ms") || a === camel(k) || a.toLowerCase() === k.toLowerCase();
|
|
873
986
|
for (const k of Object.keys(obj)) {
|
|
874
987
|
if (obj[k] === void 0 || allowed.includes(k)) continue;
|
|
875
988
|
const path = prefix ? `${prefix}.${k}` : k;
|
|
876
989
|
if (moved[k]) fail(path, "removed_option", `was removed: ${moved[k]}`);
|
|
877
|
-
const
|
|
878
|
-
const
|
|
879
|
-
|
|
990
|
+
const here = allowed.find(like(k));
|
|
991
|
+
const below = here ? void 0 : nested?.keys.find(like(k));
|
|
992
|
+
const hint = here ? `${prefix ? `${prefix}.` : ""}${here}` : below ? `${nested.under}.${below}` : void 0;
|
|
993
|
+
fail(path, "unknown_option", `isn't an option${hint ? ` (did you mean ${hint}?)` : ""}`);
|
|
880
994
|
}
|
|
881
995
|
}
|
|
882
996
|
function checkBoolean(v, option) {
|
|
@@ -1251,7 +1365,7 @@ function reguardFetch(options = {}) {
|
|
|
1251
1365
|
checkFunction(options.fetch, "fetch");
|
|
1252
1366
|
checkFunction(options.onError, "onError");
|
|
1253
1367
|
const log = makeLog(checkLogger(options.logger), checkBoolean(options.debug, "debug") ?? /^(?:1|true|yes)$/i.test(envValue("REGUARD_DEBUG") ?? ""));
|
|
1254
|
-
const ignore = options.ignore?.length ?
|
|
1368
|
+
const ignore = options.ignore?.length ? protectionMatcher(options.ignore) : void 0;
|
|
1255
1369
|
const key = (options.key ?? envValue("REGUARD_KEY"))?.trim();
|
|
1256
1370
|
const parsed = parseSiteKey(key);
|
|
1257
1371
|
const doFetch = options.fetch ?? ((input, init) => fetch(input, init));
|
|
@@ -1291,7 +1405,7 @@ function reguardFetch(options = {}) {
|
|
|
1291
1405
|
const allDry = !block || BLOCK_REASONS.every((r) => block.dryRun.has(r));
|
|
1292
1406
|
const denyMaps = (options.sourceMaps ?? (allDry ? "allow" : "deny")) === "deny";
|
|
1293
1407
|
const allowList = block?.allow.length ? skipMatcher(block.allow) : void 0;
|
|
1294
|
-
const skipped =
|
|
1408
|
+
const skipped = protectionMatcher(collect.skip);
|
|
1295
1409
|
const tagSettings = { site, collect, cookieDomain };
|
|
1296
1410
|
const pace = block?.pace;
|
|
1297
1411
|
const pages = pace ? new Allowance(pace.documents.burst, pace.documents.perSecond / 1e3, 5e4) : void 0;
|
|
@@ -1350,11 +1464,10 @@ function reguardFetch(options = {}) {
|
|
|
1350
1464
|
}
|
|
1351
1465
|
if (!health.canSend()) queue.length = 0;
|
|
1352
1466
|
};
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
if (!
|
|
1356
|
-
|
|
1357
|
-
waitUntil?.(inflight);
|
|
1467
|
+
let lastUrgent = 0;
|
|
1468
|
+
const flushSoon = (now = false) => {
|
|
1469
|
+
if (queue.length && !inflight && (now || Date.now() - lastFlush >= flushIntervalMs || queue.length >= 100)) inflight = flush().finally(() => inflight = void 0);
|
|
1470
|
+
return inflight;
|
|
1358
1471
|
};
|
|
1359
1472
|
const pushedFor = async (request, verified) => {
|
|
1360
1473
|
if (!pushed.size) return null;
|
|
@@ -1363,7 +1476,9 @@ function reguardFetch(options = {}) {
|
|
|
1363
1476
|
const now = Date.now();
|
|
1364
1477
|
const claims = [];
|
|
1365
1478
|
const k = [sid ? `s:${sid}` : ""];
|
|
1366
|
-
|
|
1479
|
+
const get = (n) => request.headers.get(n) ?? void 0;
|
|
1480
|
+
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") });
|
|
1481
|
+
if (collect.addresses !== "off" && !verified && !a.internal && !browser && a.address) k.push(`v:${await hmac(parsed.secret, `visitor:${addressKey(a.address) || a.address}`, 22)}`);
|
|
1367
1482
|
let vid;
|
|
1368
1483
|
for (const x of k) {
|
|
1369
1484
|
const e = x ? pushed.get(x) : void 0;
|
|
@@ -1392,14 +1507,11 @@ function reguardFetch(options = {}) {
|
|
|
1392
1507
|
if (sid && /^[\w-]{1,64}$/.test(sid)) headers.set("cookie", `${SESSION_COOKIE}=${sid}`);
|
|
1393
1508
|
headers.set("x-reguard-site", `${site}.${await hmac(parsed.secret, `prefix:${site}`, 16)}`);
|
|
1394
1509
|
const a = addressFor(request);
|
|
1395
|
-
const value = collect.addresses === "off" || !a.address ? "-" : await hmac(parsed.secret, `visitor:${a.address}`, 22);
|
|
1510
|
+
const value = collect.addresses === "off" || !a.address ? "-" : await hmac(parsed.secret, `visitor:${addressKey(a.address) || a.address}`, 22);
|
|
1396
1511
|
headers.set("x-reguard-visitor", `${value}.${await hmac(parsed.secret, `visitor-header:${value}`, 16)}`);
|
|
1397
1512
|
try {
|
|
1398
1513
|
const init = { method: request.method, headers, redirect: "manual", signal: AbortSignal.timeout(timeoutMs) };
|
|
1399
|
-
if (request.method !== "GET" && request.method !== "HEAD" && request.body)
|
|
1400
|
-
init.body = request.body;
|
|
1401
|
-
init.duplex = "half";
|
|
1402
|
-
}
|
|
1514
|
+
if (request.method !== "GET" && request.method !== "HEAD" && request.body) init.body = await request.arrayBuffer();
|
|
1403
1515
|
const r = await doFetch(`${collector}${p.decoded}${url.search}`, init);
|
|
1404
1516
|
const out = new Headers();
|
|
1405
1517
|
r.headers.forEach((v, k) => {
|
|
@@ -1431,7 +1543,7 @@ function reguardFetch(options = {}) {
|
|
|
1431
1543
|
} catch (e) {
|
|
1432
1544
|
log.once("onBlock-threw", "warn", `block.onBlock threw: ${e?.message ?? e}`);
|
|
1433
1545
|
}
|
|
1434
|
-
log.debug(`${d.dryRun ? "would block" : "blocked"} ${request.method} ${d.path}: ${d.reason}`);
|
|
1546
|
+
log.debug(`${d.dryRun ? "would block" : "blocked"} ${request.method} ${d.path}: ${d.reason}${d.pace ? ` (${d.pace})` : ""} from ${d.clientIp}`);
|
|
1435
1547
|
if (d.dryRun) return void 0;
|
|
1436
1548
|
const respond = block.respond;
|
|
1437
1549
|
if (respond) {
|
|
@@ -1451,9 +1563,9 @@ function reguardFetch(options = {}) {
|
|
|
1451
1563
|
if (denyMaps && isSourceMapPath(p)) return new Response(null, { status: 404, headers: { "cache-control": "no-store" } });
|
|
1452
1564
|
const cookie = request.headers.get("cookie");
|
|
1453
1565
|
const pending = (async () => {
|
|
1454
|
-
const
|
|
1455
|
-
const pushedV = await pushedFor(request, !!
|
|
1456
|
-
return { browser, pushed: pushedV, merged: mergeVerdicts(
|
|
1566
|
+
const browser2 = await readVerdict(cookie);
|
|
1567
|
+
const pushedV = await pushedFor(request, !!browser2);
|
|
1568
|
+
return { browser: browser2, pushed: pushedV, merged: mergeVerdicts(browser2, pushedV) };
|
|
1457
1569
|
})();
|
|
1458
1570
|
verdicts.set(request, pending);
|
|
1459
1571
|
if (!block) return void 0;
|
|
@@ -1466,10 +1578,11 @@ function reguardFetch(options = {}) {
|
|
|
1466
1578
|
})();
|
|
1467
1579
|
if (allowed) return void 0;
|
|
1468
1580
|
const get = (n) => request.headers.get(n) ?? void 0;
|
|
1469
|
-
|
|
1581
|
+
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") });
|
|
1582
|
+
if (pages && !browser && (request.method === "GET" || request.method === "HEAD") && requestKind(p.decoded) === "document" && !isSpeculativeHeaders(get)) {
|
|
1470
1583
|
const tool = toolOf(get("user-agent"));
|
|
1471
1584
|
const a = addressFor(request);
|
|
1472
|
-
if (!(tool && BENIGN_TOOL_FAMILIES.has(tool.family)) && a.address && !a.internal && !pages.take(a.address)) {
|
|
1585
|
+
if (!(tool && BENIGN_TOOL_FAMILIES.has(tool.family)) && a.address && !a.internal && !pages.take(addressKey(a.address) || a.address)) {
|
|
1473
1586
|
const answered = await act(request, decide(request, "pace", p, null, "document"));
|
|
1474
1587
|
if (answered) return answered;
|
|
1475
1588
|
}
|
|
@@ -1485,7 +1598,7 @@ function reguardFetch(options = {}) {
|
|
|
1485
1598
|
};
|
|
1486
1599
|
const record = (request, response, ctx) => {
|
|
1487
1600
|
if (!collect.requests) return;
|
|
1488
|
-
|
|
1601
|
+
const task = (async () => {
|
|
1489
1602
|
try {
|
|
1490
1603
|
const url = new URL(request.url);
|
|
1491
1604
|
const p = canonicalPath(url.pathname);
|
|
@@ -1494,18 +1607,19 @@ function reguardFetch(options = {}) {
|
|
|
1494
1607
|
const sid = cookieValue(get("cookie"), SESSION_COOKIE);
|
|
1495
1608
|
const decision = decisions.get(request);
|
|
1496
1609
|
const target = `${url.pathname}${url.search}`;
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
if (!
|
|
1610
|
+
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 });
|
|
1611
|
+
const tool = toolLike();
|
|
1612
|
+
if (!decision) {
|
|
1613
|
+
if (!sid && (options.watchSessionless === false || collect.addresses === "off" || !tool)) return;
|
|
1614
|
+
if (sid && !response && !tool) return;
|
|
1501
1615
|
}
|
|
1502
1616
|
const a = addressFor(request);
|
|
1503
1617
|
const hash = (s) => hmac(parsed.secret, s, 16);
|
|
1504
|
-
const contentType = response
|
|
1505
|
-
const responseBytes = Number(response
|
|
1618
|
+
const contentType = response?.headers.get("content-type") ?? "";
|
|
1619
|
+
const responseBytes = Number(response?.headers.get("content-length") ?? 0) || 0;
|
|
1506
1620
|
const requestBytes = Number(get("content-length") ?? 0) || 0;
|
|
1507
1621
|
let exchange;
|
|
1508
|
-
if (!isSpeculativeHeaders(get) && isApiLike(target, contentType, requestBytes)) {
|
|
1622
|
+
if (response && !isSpeculativeHeaders(get) && isApiLike(target, contentType, requestBytes)) {
|
|
1509
1623
|
const input = { method: request.method, url: target, requestContentType: get("content-type") ?? "", requestBody: "", requestBytes, status: response.status, responseContentType: contentType, responseBytes };
|
|
1510
1624
|
const wanted = /* @__PURE__ */ new Set();
|
|
1511
1625
|
describeExchange((s) => (wanted.add(s), ""), input);
|
|
@@ -1525,7 +1639,7 @@ function reguardFetch(options = {}) {
|
|
|
1525
1639
|
url: collect.pages === "masked" ? privateUrl(target) : "",
|
|
1526
1640
|
...collect.pages !== "off" ? { item: await hash(`path:${url.pathname}`) } : {},
|
|
1527
1641
|
...collect.pages !== "masked" ? { traits: pathTraits(target, get("rsc") === "1") } : {},
|
|
1528
|
-
status: response.status,
|
|
1642
|
+
...response ? { status: response.status } : {},
|
|
1529
1643
|
headers: Object.fromEntries(["rsc", "next-router-prefetch", "sec-purpose", "purpose"].flatMap((n) => get(n) !== void 0 ? [[n, get(n)]] : [])),
|
|
1530
1644
|
client: {
|
|
1531
1645
|
ua: get("user-agent") ?? "",
|
|
@@ -1539,26 +1653,28 @@ function reguardFetch(options = {}) {
|
|
|
1539
1653
|
hasReferer: !!get("referer"),
|
|
1540
1654
|
...url.protocol === "https:" ? { secure: true } : {}
|
|
1541
1655
|
},
|
|
1542
|
-
...!sid && a.address && collect.addresses !== "off" ? { visitor: await hmac(parsed.secret, `visitor:${a.address}`, 22) } : {},
|
|
1656
|
+
...!sid && a.address && collect.addresses !== "off" ? { visitor: await hmac(parsed.secret, `visitor:${addressKey(a.address) || a.address}`, 22) } : {},
|
|
1543
1657
|
...exchange ? { exchange } : {},
|
|
1544
1658
|
...decision?.pace ? { pace: decision.pace } : {},
|
|
1545
1659
|
...decision ? { decision: { reason: decision.reason, dryRun: decision.dryRun } } : {},
|
|
1546
1660
|
...a.internal ? { internal: true } : {}
|
|
1547
1661
|
});
|
|
1548
1662
|
if (queue.length > 5e3) queue.splice(0, queue.length - 5e3);
|
|
1549
|
-
const waitUntil = ctx?.waitUntil ? (p2) => ctx.waitUntil.call(ctx, p2) : void 0;
|
|
1550
1663
|
ensureTimer();
|
|
1551
|
-
|
|
1664
|
+
const urgent = !!decision || tool && Date.now() - lastUrgent >= 500;
|
|
1665
|
+
if (urgent) lastUrgent = Date.now();
|
|
1666
|
+
await flushSoon(urgent);
|
|
1552
1667
|
} catch (e) {
|
|
1553
1668
|
onError(e);
|
|
1554
1669
|
}
|
|
1555
1670
|
})();
|
|
1671
|
+
ctx?.waitUntil?.call(ctx, task);
|
|
1556
1672
|
};
|
|
1557
1673
|
let timer;
|
|
1558
1674
|
const ensureTimer = () => {
|
|
1559
1675
|
if (timer !== void 0 || platform === "workers" || typeof setInterval !== "function") return;
|
|
1560
1676
|
try {
|
|
1561
|
-
timer = setInterval(() => flushSoon(
|
|
1677
|
+
timer = setInterval(() => void flushSoon(true), flushIntervalMs);
|
|
1562
1678
|
timer.unref?.();
|
|
1563
1679
|
} catch {
|
|
1564
1680
|
timer = null;
|