@webdecoy/node 0.28.0 → 0.30.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 +13 -0
- package/dist/{chunk-P6IWJ7K5.mjs → chunk-VPPTUL5K.mjs} +3 -1
- package/dist/index.d.mts +55 -7
- package/dist/index.d.ts +55 -7
- package/dist/index.js +257 -92
- package/dist/index.mjs +255 -92
- package/dist/vercel.js +3 -1
- package/dist/vercel.mjs +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -11,7 +11,7 @@ import {
|
|
|
11
11
|
resolveGateEnv,
|
|
12
12
|
ruleBlockResponse,
|
|
13
13
|
shouldSkipPath
|
|
14
|
-
} from "./chunk-
|
|
14
|
+
} from "./chunk-VPPTUL5K.mjs";
|
|
15
15
|
import {
|
|
16
16
|
ATTACK_SIGNATURE_IDS,
|
|
17
17
|
AgentVerifier,
|
|
@@ -103,25 +103,43 @@ var MemoryClientSignalStore = class {
|
|
|
103
103
|
};
|
|
104
104
|
var CLIENT_SESSION_COOKIE = "wd_cs";
|
|
105
105
|
var CLIENT_SESSION_HEADER = "x-wd-session";
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
106
|
+
var CLIENT_RECEIPT_COOKIE = "__Host-wd_cs";
|
|
107
|
+
var CLIENT_RECEIPT_PREFIX = "receipt:";
|
|
108
|
+
var CLIENT_RECEIPT_TTL_MS = 15 * 6e4;
|
|
109
|
+
function getClientSignalHost(headers) {
|
|
110
|
+
const value = headers.host ?? headers[":authority"];
|
|
111
|
+
if (typeof value !== "string" || value.length > 255 || !/^[a-z0-9.:[\]-]+$/i.test(value)) {
|
|
112
|
+
return void 0;
|
|
113
|
+
}
|
|
114
|
+
try {
|
|
115
|
+
return new URL("https://" + value).host || void 0;
|
|
116
|
+
} catch {
|
|
117
|
+
return void 0;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
function readCookie(header, name) {
|
|
121
|
+
if (!header) return void 0;
|
|
122
|
+
for (const part of header.split(";")) {
|
|
123
|
+
const eq = part.indexOf("=");
|
|
124
|
+
if (eq >= 0 && part.slice(0, eq).trim() === name) {
|
|
125
|
+
return part.slice(eq + 1).trim();
|
|
116
126
|
}
|
|
117
127
|
}
|
|
118
|
-
|
|
119
|
-
|
|
128
|
+
return void 0;
|
|
129
|
+
}
|
|
130
|
+
function readClientSession(headers) {
|
|
131
|
+
const receipt = readCookie(headers.cookie, CLIENT_RECEIPT_COOKIE);
|
|
132
|
+
if (receipt !== void 0) {
|
|
133
|
+
return /^[0-9a-f]{48}$/.test(receipt) ? CLIENT_RECEIPT_PREFIX + receipt : void 0;
|
|
134
|
+
}
|
|
135
|
+
const legacy = readCookie(headers.cookie, CLIENT_SESSION_COOKIE) || headers[CLIENT_SESSION_HEADER];
|
|
136
|
+
return legacy && !legacy.startsWith(CLIENT_RECEIPT_PREFIX) ? legacy : void 0;
|
|
120
137
|
}
|
|
121
138
|
var ClientSignalRule = class {
|
|
122
139
|
constructor(config) {
|
|
123
140
|
this.name = "client-signals";
|
|
124
141
|
this.store = config.store;
|
|
142
|
+
this.siteKey = config.siteKey;
|
|
125
143
|
this.minScore = config.minScore;
|
|
126
144
|
this.action = config.action ?? "DENY";
|
|
127
145
|
this.dryRun = config.dryRun ?? false;
|
|
@@ -147,7 +165,10 @@ var ClientSignalRule = class {
|
|
|
147
165
|
};
|
|
148
166
|
}
|
|
149
167
|
const verdict = this.store.sync ? this.store.get(sessionId) : context.prepared?.[this.name];
|
|
150
|
-
|
|
168
|
+
const issuedReceipt = sessionId.startsWith(CLIENT_RECEIPT_PREFIX);
|
|
169
|
+
const receipt = verdict?.receipt;
|
|
170
|
+
const usableReceipt = receipt && Date.now() < receipt.expiresAt && getClientSignalHost(context.headers) === receipt.host && (this.siteKey === void 0 || this.siteKey === receipt.siteKey);
|
|
171
|
+
if (!verdict || verdict.sessionId !== sessionId || (issuedReceipt || this.siteKey !== void 0) && !usableReceipt) {
|
|
151
172
|
return {
|
|
152
173
|
action: "ALLOW",
|
|
153
174
|
rule: this.name,
|
|
@@ -170,7 +191,7 @@ var ClientSignalRule = class {
|
|
|
170
191
|
metadata: {
|
|
171
192
|
score: verdict.score,
|
|
172
193
|
recommendation: verdict.recommendation,
|
|
173
|
-
sessionId,
|
|
194
|
+
...issuedReceipt ? {} : { sessionId },
|
|
174
195
|
dryRun: this.dryRun
|
|
175
196
|
}
|
|
176
197
|
};
|
|
@@ -347,7 +368,67 @@ var InMemoryRateLimiter = class {
|
|
|
347
368
|
}
|
|
348
369
|
};
|
|
349
370
|
|
|
371
|
+
// src/detection/detectors/cdp.ts
|
|
372
|
+
var HIGH_CONFIDENCE_SIGNALS = ["chromedriver_cdc", "puppeteer_eval", "cdp_script_injection"];
|
|
373
|
+
var CORROBORATING_DRIVER_SIGNALS = /* @__PURE__ */ new Set([
|
|
374
|
+
...HIGH_CONFIDENCE_SIGNALS,
|
|
375
|
+
"selenium_evaluate",
|
|
376
|
+
"selenium_driver_evaluate",
|
|
377
|
+
"selenium_unwrapped",
|
|
378
|
+
"selenium_driver_unwrapped",
|
|
379
|
+
"selenium_direct",
|
|
380
|
+
"firefox_driver",
|
|
381
|
+
"firefox_driver_unwrapped"
|
|
382
|
+
]);
|
|
383
|
+
function hasDriverInjection(signals) {
|
|
384
|
+
return signals.some((signal) => HIGH_CONFIDENCE_SIGNALS.includes(signal));
|
|
385
|
+
}
|
|
386
|
+
function detectCDP(signals, corroborating) {
|
|
387
|
+
const detections = [];
|
|
388
|
+
const cdp = signals.environmental?.cdp ?? {};
|
|
389
|
+
if (!cdp.detected) return detections;
|
|
390
|
+
const signalList = cdp.signals ?? [];
|
|
391
|
+
const signalCount = signalList.length;
|
|
392
|
+
if (hasDriverInjection(signalList)) {
|
|
393
|
+
detections.push({
|
|
394
|
+
category: "cdp",
|
|
395
|
+
score: 0.9,
|
|
396
|
+
confidence: 0.95,
|
|
397
|
+
reason: `CDP automation detected: ${signalList.join(", ")}`
|
|
398
|
+
});
|
|
399
|
+
} else if (signalCount >= 2) {
|
|
400
|
+
detections.push({
|
|
401
|
+
category: "cdp",
|
|
402
|
+
score: 0.8,
|
|
403
|
+
confidence: 0.85,
|
|
404
|
+
reason: `Multiple CDP indicators: ${signalList.join(", ")}`
|
|
405
|
+
});
|
|
406
|
+
} else if (signalCount === 1) {
|
|
407
|
+
detections.push({
|
|
408
|
+
category: "cdp",
|
|
409
|
+
score: 0.6,
|
|
410
|
+
confidence: 0.7,
|
|
411
|
+
reason: `CDP indicator: ${signalList.join(", ")}`
|
|
412
|
+
});
|
|
413
|
+
}
|
|
414
|
+
if (corroborating && signalList.every((signal) => CORROBORATING_DRIVER_SIGNALS.has(signal))) {
|
|
415
|
+
corroborating.push(...detections);
|
|
416
|
+
}
|
|
417
|
+
return detections;
|
|
418
|
+
}
|
|
419
|
+
|
|
350
420
|
// src/detection/scoring.ts
|
|
421
|
+
function applyDecisiveScoreFloor(score, env, weights) {
|
|
422
|
+
const declaresWebDriver = (weights.headless ?? 0) > 0 && env?.webdriver === true && env.automationFlags?.webdriver === true;
|
|
423
|
+
const hasInjectedDriver = (weights.cdp ?? 0) > 0 && env?.cdp?.detected === true && hasDriverInjection(env.cdp.signals ?? []);
|
|
424
|
+
return declaresWebDriver || hasInjectedDriver ? Math.max(0.9, score) : score;
|
|
425
|
+
}
|
|
426
|
+
function applyCorroborationScoreFloor(score, detections, weights) {
|
|
427
|
+
if (score >= 0.6 || detections.length < 2) return score;
|
|
428
|
+
const support = calculateNoisyOrCategoryScores(detections, weights);
|
|
429
|
+
const agrees = (category) => (weights[category] ?? 0) > 0 && (support[category] ?? 0) >= 0.4 - 1e-9;
|
|
430
|
+
return agrees("cdp") && (agrees("vision_ai") || agrees("behavioral")) ? 0.6 : score;
|
|
431
|
+
}
|
|
351
432
|
function calculateCategoryScores(detections, weights) {
|
|
352
433
|
var _a;
|
|
353
434
|
const categoryData = {};
|
|
@@ -369,6 +450,19 @@ function calculateCategoryScores(detections, weights) {
|
|
|
369
450
|
}
|
|
370
451
|
return result;
|
|
371
452
|
}
|
|
453
|
+
function calculateNoisyOrCategoryScores(detections, weights) {
|
|
454
|
+
const remaining = /* @__PURE__ */ new Map();
|
|
455
|
+
for (const { category, score, confidence } of detections) {
|
|
456
|
+
if (!Number.isFinite(score) || !Number.isFinite(confidence) || score <= 0 || confidence <= 0) {
|
|
457
|
+
continue;
|
|
458
|
+
}
|
|
459
|
+
const evidence = Math.min(1, score * confidence);
|
|
460
|
+
remaining.set(category, (remaining.get(category) ?? 1) * (1 - evidence));
|
|
461
|
+
}
|
|
462
|
+
return Object.fromEntries(
|
|
463
|
+
Object.keys(weights).map((category) => [category, 1 - (remaining.get(category) ?? 1)])
|
|
464
|
+
);
|
|
465
|
+
}
|
|
372
466
|
function calculateFinalScore(categoryScores, weights) {
|
|
373
467
|
let total = 0;
|
|
374
468
|
for (const [cat, weight] of Object.entries(weights)) {
|
|
@@ -382,6 +476,39 @@ function recommend(finalScore) {
|
|
|
382
476
|
return "block";
|
|
383
477
|
}
|
|
384
478
|
|
|
479
|
+
// src/detection/detectors/corroborating-detection.ts
|
|
480
|
+
function addCorroboratingDetection(detections, corroborating, detection) {
|
|
481
|
+
detections.push(detection);
|
|
482
|
+
corroborating?.push(detection);
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
// src/detection/detectors/keyboard-input.ts
|
|
486
|
+
var MIN_KEY_HOLD_SAMPLES = 3;
|
|
487
|
+
var MAX_KEY_HOLD_SAMPLES = 200;
|
|
488
|
+
var SHORT_KEY_HOLD_MS = 20;
|
|
489
|
+
var MAX_KEY_HOLD_MS = 2e3;
|
|
490
|
+
function classifyKeyboardInput(signals) {
|
|
491
|
+
const behavioral = signals.behavioral ?? {};
|
|
492
|
+
const keyEvents = behavioral.keyEvents ?? 0;
|
|
493
|
+
if (!Number.isFinite(keyEvents) || keyEvents < 2 || (behavioral.totalPoints ?? 0) !== 0) {
|
|
494
|
+
return null;
|
|
495
|
+
}
|
|
496
|
+
const { keyHoldSamples = 0, keyHoldAvg = 0 } = behavioral;
|
|
497
|
+
const validSummary = Number.isInteger(keyHoldSamples) && keyHoldSamples > 0 && keyHoldSamples <= Math.min(MAX_KEY_HOLD_SAMPLES, Math.floor(keyEvents / 2)) && Number.isFinite(keyHoldAvg) && keyHoldAvg > 0 && keyHoldAvg < MAX_KEY_HOLD_MS;
|
|
498
|
+
let count = validSummary ? keyHoldSamples : 0;
|
|
499
|
+
let duration = count > 0 ? count * keyHoldAvg : 0;
|
|
500
|
+
for (const field of Object.values(signals.formAnalysis?.textareaKeyboard ?? {})) {
|
|
501
|
+
if (!Array.isArray(field?.dwellTimes)) continue;
|
|
502
|
+
for (const dwell of field.dwellTimes) {
|
|
503
|
+
if (typeof dwell === "number" && dwell > 0 && dwell < MAX_KEY_HOLD_MS) {
|
|
504
|
+
count++;
|
|
505
|
+
duration += dwell;
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
return count >= MIN_KEY_HOLD_SAMPLES && duration / count < SHORT_KEY_HOLD_MS ? "short-key-holds" : "keyboard-only";
|
|
510
|
+
}
|
|
511
|
+
|
|
385
512
|
// src/detection/detectors/pointer-evidence.ts
|
|
386
513
|
function isTouchModality(behavioral) {
|
|
387
514
|
const touchEvents = behavioral.touchEvents ?? 0;
|
|
@@ -389,14 +516,14 @@ function isTouchModality(behavioral) {
|
|
|
389
516
|
return touchEvents >= 3 || hasTouchSample && behavioral.pointerHasNonMouseType === true;
|
|
390
517
|
}
|
|
391
518
|
function hasHumanMovementMarkers(behavioral) {
|
|
392
|
-
const tremor = behavioral.microTremorScore ?? 0;
|
|
519
|
+
const tremor = behavioral.microTremorScore ?? 0.5;
|
|
393
520
|
const corrections = behavioral.overshootCorrections ?? 0;
|
|
394
521
|
const changes = behavioral.directionChanges ?? 0;
|
|
395
522
|
return tremor >= 0.5 && (corrections >= 1 || changes >= 10);
|
|
396
523
|
}
|
|
397
524
|
|
|
398
525
|
// src/detection/detectors/vision-ai.ts
|
|
399
|
-
function detectVisionAI(signals, widgetInteraction = true) {
|
|
526
|
+
function detectVisionAI(signals, widgetInteraction = true, corroborating) {
|
|
400
527
|
const detections = [];
|
|
401
528
|
const b = signals.behavioral ?? {};
|
|
402
529
|
const t = signals.temporal ?? {};
|
|
@@ -404,7 +531,7 @@ function detectVisionAI(signals, widgetInteraction = true) {
|
|
|
404
531
|
const trajectory = b.trajectoryLength ?? 0;
|
|
405
532
|
const approachPts = b.approachPoints ?? 0;
|
|
406
533
|
const isTouchUser = isTouchModality(b);
|
|
407
|
-
const isKeyboardUser = (
|
|
534
|
+
const isKeyboardUser = classifyKeyboardInput(signals) === "keyboard-only";
|
|
408
535
|
const usesMouse = !isTouchUser && !isKeyboardUser;
|
|
409
536
|
if (totalPoints < 5 && trajectory < 10 && usesMouse) {
|
|
410
537
|
detections.push({
|
|
@@ -444,7 +571,7 @@ function detectVisionAI(signals, widgetInteraction = true) {
|
|
|
444
571
|
}
|
|
445
572
|
const microTremor = b.microTremorScore ?? 0.5;
|
|
446
573
|
if (usesMouse && totalPoints >= 5 && microTremor < 0.15) {
|
|
447
|
-
detections
|
|
574
|
+
addCorroboratingDetection(detections, corroborating, {
|
|
448
575
|
category: "vision_ai",
|
|
449
576
|
score: 0.7,
|
|
450
577
|
confidence: 0.6,
|
|
@@ -452,7 +579,7 @@ function detectVisionAI(signals, widgetInteraction = true) {
|
|
|
452
579
|
});
|
|
453
580
|
}
|
|
454
581
|
if (widgetInteraction && usesMouse && approachPts >= 5 && (b.approachDirectness ?? 0) > 0.95 && !hasHumanMovementMarkers(b)) {
|
|
455
|
-
detections
|
|
582
|
+
addCorroboratingDetection(detections, corroborating, {
|
|
456
583
|
category: "vision_ai",
|
|
457
584
|
score: 0.5,
|
|
458
585
|
confidence: 0.5,
|
|
@@ -461,7 +588,7 @@ function detectVisionAI(signals, widgetInteraction = true) {
|
|
|
461
588
|
}
|
|
462
589
|
const exploration = b.explorationRatio ?? 0.3;
|
|
463
590
|
if (widgetInteraction && exploration < 0.05 && trajectory > 50) {
|
|
464
|
-
detections
|
|
591
|
+
addCorroboratingDetection(detections, corroborating, {
|
|
465
592
|
category: "vision_ai",
|
|
466
593
|
score: 0.4,
|
|
467
594
|
confidence: 0.4,
|
|
@@ -609,58 +736,31 @@ function detectAutomation(signals) {
|
|
|
609
736
|
return detections;
|
|
610
737
|
}
|
|
611
738
|
|
|
612
|
-
// src/detection/detectors/cdp.ts
|
|
613
|
-
var HIGH_CONFIDENCE_SIGNALS = ["chromedriver_cdc", "puppeteer_eval", "cdp_script_injection"];
|
|
614
|
-
function detectCDP(signals) {
|
|
615
|
-
const detections = [];
|
|
616
|
-
const cdp = signals.environmental?.cdp ?? {};
|
|
617
|
-
if (!cdp.detected) return detections;
|
|
618
|
-
const signalList = cdp.signals ?? [];
|
|
619
|
-
const signalCount = signalList.length;
|
|
620
|
-
const hasHighConf = signalList.some((s) => HIGH_CONFIDENCE_SIGNALS.includes(s));
|
|
621
|
-
if (hasHighConf) {
|
|
622
|
-
detections.push({
|
|
623
|
-
category: "cdp",
|
|
624
|
-
score: 0.9,
|
|
625
|
-
confidence: 0.95,
|
|
626
|
-
reason: `CDP automation detected: ${signalList.join(", ")}`
|
|
627
|
-
});
|
|
628
|
-
} else if (signalCount >= 2) {
|
|
629
|
-
detections.push({
|
|
630
|
-
category: "cdp",
|
|
631
|
-
score: 0.8,
|
|
632
|
-
confidence: 0.85,
|
|
633
|
-
reason: `Multiple CDP indicators: ${signalList.join(", ")}`
|
|
634
|
-
});
|
|
635
|
-
} else if (signalCount === 1) {
|
|
636
|
-
detections.push({
|
|
637
|
-
category: "cdp",
|
|
638
|
-
score: 0.6,
|
|
639
|
-
confidence: 0.7,
|
|
640
|
-
reason: `CDP indicator: ${signalList.join(", ")}`
|
|
641
|
-
});
|
|
642
|
-
}
|
|
643
|
-
return detections;
|
|
644
|
-
}
|
|
645
|
-
|
|
646
739
|
// src/detection/detectors/behavioral.ts
|
|
647
|
-
function detectBehavioral(signals, widgetInteraction = true) {
|
|
740
|
+
function detectBehavioral(signals, widgetInteraction = true, corroborating) {
|
|
648
741
|
const detections = [];
|
|
649
742
|
const b = signals.behavioral ?? {};
|
|
650
743
|
const t = signals.temporal ?? {};
|
|
651
744
|
const totalPoints = b.totalPoints ?? 0;
|
|
652
745
|
const trajectory = b.trajectoryLength ?? 0;
|
|
653
746
|
const isTouchUser = isTouchModality(b);
|
|
654
|
-
const
|
|
747
|
+
const keyboard = classifyKeyboardInput(signals);
|
|
655
748
|
const humanMovement = hasHumanMovementMarkers(b);
|
|
656
|
-
if (
|
|
749
|
+
if (keyboard === "short-key-holds" && !isTouchUser) {
|
|
750
|
+
addCorroboratingDetection(detections, corroborating, {
|
|
751
|
+
category: "behavioral",
|
|
752
|
+
score: 0.8,
|
|
753
|
+
confidence: 0.9,
|
|
754
|
+
reason: "Keyboard-only input has unusually short key holds"
|
|
755
|
+
});
|
|
756
|
+
} else if (totalPoints === 0 && !isTouchUser && keyboard === null) {
|
|
657
757
|
detections.push({
|
|
658
758
|
category: "behavioral",
|
|
659
759
|
score: 0.8,
|
|
660
760
|
confidence: 0.9,
|
|
661
761
|
reason: "Zero mouse, touch, or keyboard events recorded"
|
|
662
762
|
});
|
|
663
|
-
} else if (totalPoints < 10 && !isTouchUser &&
|
|
763
|
+
} else if (totalPoints < 10 && !isTouchUser && keyboard === null && trajectory < 30) {
|
|
664
764
|
detections.push({
|
|
665
765
|
category: "behavioral",
|
|
666
766
|
score: 0.6,
|
|
@@ -670,7 +770,7 @@ function detectBehavioral(signals, widgetInteraction = true) {
|
|
|
670
770
|
}
|
|
671
771
|
const velVar = b.velocityVariance ?? 1;
|
|
672
772
|
if (velVar < 0.02 && trajectory > 50 && !humanMovement) {
|
|
673
|
-
detections
|
|
773
|
+
addCorroboratingDetection(detections, corroborating, {
|
|
674
774
|
category: "behavioral",
|
|
675
775
|
score: 0.6,
|
|
676
776
|
confidence: 0.6,
|
|
@@ -729,7 +829,7 @@ function detectBehavioral(signals, widgetInteraction = true) {
|
|
|
729
829
|
}
|
|
730
830
|
const straight = b.straightLineRatio ?? 0;
|
|
731
831
|
if (straight > 0.8 && trajectory > 100) {
|
|
732
|
-
detections
|
|
832
|
+
addCorroboratingDetection(detections, corroborating, {
|
|
733
833
|
category: "behavioral",
|
|
734
834
|
score: 0.5,
|
|
735
835
|
confidence: 0.5,
|
|
@@ -738,7 +838,7 @@ function detectBehavioral(signals, widgetInteraction = true) {
|
|
|
738
838
|
}
|
|
739
839
|
const dirChanges = b.directionChanges ?? 10;
|
|
740
840
|
if (totalPoints > 50 && dirChanges < 3) {
|
|
741
|
-
detections
|
|
841
|
+
addCorroboratingDetection(detections, corroborating, {
|
|
742
842
|
category: "behavioral",
|
|
743
843
|
score: 0.4,
|
|
744
844
|
confidence: 0.4,
|
|
@@ -1700,16 +1800,24 @@ function analyzeAdvancedSignals(signals) {
|
|
|
1700
1800
|
// src/detection/engine.ts
|
|
1701
1801
|
var DetectionEngine = class {
|
|
1702
1802
|
constructor(options = {}) {
|
|
1803
|
+
if (options.scoringPolicy !== void 0 && options.scoringPolicy !== "weighted-mean-v1" && options.scoringPolicy !== "noisy-or-floors-v1") {
|
|
1804
|
+
throw new TypeError("Unknown detection scoring policy: " + String(options.scoringPolicy));
|
|
1805
|
+
}
|
|
1703
1806
|
this.weights = options.weights ?? DEFAULT_WEIGHTS;
|
|
1807
|
+
this.scoringPolicy = options.scoringPolicy ?? "weighted-mean-v1";
|
|
1704
1808
|
this.requirePoW = options.requirePoW ?? true;
|
|
1705
1809
|
this.fingerprintStore = options.fingerprintStore ?? new InMemoryFingerprintStore();
|
|
1706
1810
|
this.rateLimiter = options.rateLimiter ?? new InMemoryRateLimiter();
|
|
1811
|
+
this.onScoringComparison = options.onScoringComparison;
|
|
1707
1812
|
}
|
|
1708
1813
|
/** Score `signals` for the given request context and return a verdict. */
|
|
1709
1814
|
score(signals, context) {
|
|
1710
1815
|
const { ip, siteKey, userAgent } = context;
|
|
1711
1816
|
const headers = context.headers ?? {};
|
|
1712
1817
|
const detections = [];
|
|
1818
|
+
const onScoringComparison = this.onScoringComparison;
|
|
1819
|
+
const useCandidate = this.scoringPolicy === "noisy-or-floors-v1";
|
|
1820
|
+
const corroborating = onScoringComparison || useCandidate ? [] : void 0;
|
|
1713
1821
|
if (context.extraDetections) detections.push(...context.extraDetections);
|
|
1714
1822
|
const pow = context.pow;
|
|
1715
1823
|
if (pow && pow.provided) {
|
|
@@ -1750,11 +1858,11 @@ var DetectionEngine = class {
|
|
|
1750
1858
|
}
|
|
1751
1859
|
const widgetInteraction = context.widgetInteraction !== false;
|
|
1752
1860
|
detections.push(
|
|
1753
|
-
...detectVisionAI(signals, widgetInteraction),
|
|
1861
|
+
...detectVisionAI(signals, widgetInteraction, corroborating),
|
|
1754
1862
|
...detectHeadless(signals, userAgent),
|
|
1755
1863
|
...detectAutomation(signals),
|
|
1756
|
-
...detectCDP(signals),
|
|
1757
|
-
...detectBehavioral(signals, widgetInteraction),
|
|
1864
|
+
...detectCDP(signals, corroborating),
|
|
1865
|
+
...detectBehavioral(signals, widgetInteraction, corroborating),
|
|
1758
1866
|
...detectTouchAuthenticity(signals, userAgent),
|
|
1759
1867
|
...detectSensorEntropy(signals, userAgent),
|
|
1760
1868
|
...detectTouchKinematics(signals),
|
|
@@ -1782,13 +1890,42 @@ var DetectionEngine = class {
|
|
|
1782
1890
|
detections.push(...analyzeFormInteraction(signals.formAnalysis, signals.behavioral));
|
|
1783
1891
|
}
|
|
1784
1892
|
detections.push(...analyzeAdvancedSignals(signals));
|
|
1785
|
-
const
|
|
1786
|
-
const
|
|
1893
|
+
const currentCategoryScores = calculateCategoryScores(detections, this.weights);
|
|
1894
|
+
const currentScore = calculateFinalScore(currentCategoryScores, this.weights);
|
|
1895
|
+
const currentRecommendation = recommend(currentScore);
|
|
1787
1896
|
const scrollObservation = observeScrollJump(signals.behavioral);
|
|
1897
|
+
let candidateCategoryScores;
|
|
1898
|
+
let candidateScore;
|
|
1899
|
+
let candidateRecommendation;
|
|
1900
|
+
if (corroborating) {
|
|
1901
|
+
candidateCategoryScores = calculateNoisyOrCategoryScores(detections, this.weights);
|
|
1902
|
+
const decisiveScore = applyDecisiveScoreFloor(
|
|
1903
|
+
calculateFinalScore(candidateCategoryScores, this.weights),
|
|
1904
|
+
signals.environmental,
|
|
1905
|
+
this.weights
|
|
1906
|
+
);
|
|
1907
|
+
candidateScore = applyCorroborationScoreFloor(decisiveScore, corroborating, this.weights);
|
|
1908
|
+
candidateRecommendation = recommend(candidateScore);
|
|
1909
|
+
}
|
|
1910
|
+
if (onScoringComparison && candidateScore !== void 0 && candidateRecommendation !== void 0) {
|
|
1911
|
+
try {
|
|
1912
|
+
const pending = onScoringComparison({
|
|
1913
|
+
policy: "noisy-or-floors-v1",
|
|
1914
|
+
current: { score: currentScore, recommendation: currentRecommendation },
|
|
1915
|
+
candidate: { score: candidateScore, recommendation: candidateRecommendation }
|
|
1916
|
+
});
|
|
1917
|
+
void pending?.catch(() => {
|
|
1918
|
+
});
|
|
1919
|
+
} catch {
|
|
1920
|
+
}
|
|
1921
|
+
}
|
|
1922
|
+
const categoryScores = useCandidate ? candidateCategoryScores : currentCategoryScores;
|
|
1923
|
+
const score = useCandidate ? candidateScore : currentScore;
|
|
1924
|
+
const recommendation = useCandidate ? candidateRecommendation : currentRecommendation;
|
|
1788
1925
|
return {
|
|
1789
1926
|
success: score < 0.5,
|
|
1790
1927
|
score,
|
|
1791
|
-
recommendation
|
|
1928
|
+
recommendation,
|
|
1792
1929
|
categoryScores,
|
|
1793
1930
|
detections,
|
|
1794
1931
|
...scrollObservation ? { observations: [scrollObservation] } : {},
|
|
@@ -1999,9 +2136,11 @@ var Captcha = class {
|
|
|
1999
2136
|
const fingerprintStore = options.fingerprintStore ?? new InMemoryFingerprintStore();
|
|
2000
2137
|
this.engine = new DetectionEngine({
|
|
2001
2138
|
weights: options.weights,
|
|
2139
|
+
scoringPolicy: options.scoringPolicy,
|
|
2002
2140
|
requirePoW: true,
|
|
2003
2141
|
fingerprintStore,
|
|
2004
|
-
rateLimiter
|
|
2142
|
+
rateLimiter,
|
|
2143
|
+
onScoringComparison: options.onScoringComparison
|
|
2005
2144
|
});
|
|
2006
2145
|
this.pow = new PoWManager({ secret, store: options.challengeStore, rateLimiter });
|
|
2007
2146
|
this.tokens = new TokenManager({ secret, store: options.tokenStore });
|
|
@@ -2016,13 +2155,14 @@ var Captcha = class {
|
|
|
2016
2155
|
* signals, and issues a session token on success. Mirrors `runVerification`.
|
|
2017
2156
|
*/
|
|
2018
2157
|
async verify(input) {
|
|
2019
|
-
return this.verifySubmission(input, true);
|
|
2158
|
+
return (await this.verifySubmission(input, true)).result;
|
|
2020
2159
|
}
|
|
2021
2160
|
async verifySubmission(input, widgetInteraction) {
|
|
2022
2161
|
const { siteKey, ip, userAgent } = input;
|
|
2023
2162
|
const headers = input.headers ?? {};
|
|
2024
2163
|
let signals = input.signals ?? {};
|
|
2025
2164
|
const extraDetections = [];
|
|
2165
|
+
let committedSignals = false;
|
|
2026
2166
|
const clientSignalsHash = input.powSolution?.signalsHash ?? null;
|
|
2027
2167
|
if (input.signalsJson && clientSignalsHash) {
|
|
2028
2168
|
const computed = await sha256Hex(input.signalsJson);
|
|
@@ -2035,7 +2175,11 @@ var Captcha = class {
|
|
|
2035
2175
|
});
|
|
2036
2176
|
}
|
|
2037
2177
|
try {
|
|
2038
|
-
|
|
2178
|
+
const parsed = JSON.parse(input.signalsJson);
|
|
2179
|
+
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
|
|
2180
|
+
signals = parsed;
|
|
2181
|
+
committedSignals = computed === clientSignalsHash;
|
|
2182
|
+
}
|
|
2039
2183
|
} catch {
|
|
2040
2184
|
}
|
|
2041
2185
|
}
|
|
@@ -2070,18 +2214,28 @@ var Captcha = class {
|
|
|
2070
2214
|
const success = proofVerified && verdict.success;
|
|
2071
2215
|
const token = success ? await this.tokens.issue(ip, siteKey, verdict.score) : null;
|
|
2072
2216
|
const recommendation = !proofVerified && verdict.recommendation === "allow" ? "challenge" : verdict.recommendation;
|
|
2073
|
-
return {
|
|
2217
|
+
return {
|
|
2218
|
+
result: { ...verdict, success, recommendation, token },
|
|
2219
|
+
verifiedSignals: proofVerified && committedSignals && !!pow.nonce && signals.meta?.challengeNonce === pow.nonce
|
|
2220
|
+
};
|
|
2074
2221
|
}
|
|
2075
2222
|
/** Invisible-mode scoring — same pipeline, returns a compact result + action. */
|
|
2076
2223
|
async score(input) {
|
|
2077
|
-
|
|
2224
|
+
return (await this.scoreForSession(input)).result;
|
|
2225
|
+
}
|
|
2226
|
+
/** @internal Return the score and whether the server verified the committed signals. */
|
|
2227
|
+
async scoreForSession(input) {
|
|
2228
|
+
const { result, verifiedSignals } = await this.verifySubmission(input, false);
|
|
2078
2229
|
return {
|
|
2079
|
-
|
|
2080
|
-
|
|
2081
|
-
|
|
2082
|
-
|
|
2083
|
-
|
|
2084
|
-
|
|
2230
|
+
result: {
|
|
2231
|
+
success: result.success,
|
|
2232
|
+
score: result.score,
|
|
2233
|
+
token: result.token,
|
|
2234
|
+
action: input.action ?? "",
|
|
2235
|
+
recommendation: result.recommendation,
|
|
2236
|
+
...result.observations ? { observations: result.observations } : {}
|
|
2237
|
+
},
|
|
2238
|
+
verifiedSignals
|
|
2085
2239
|
};
|
|
2086
2240
|
}
|
|
2087
2241
|
/** Verify a previously issued token (single-use, optionally IP-bound). */
|
|
@@ -2131,8 +2285,9 @@ function createCaptchaEndpoints(options = {}) {
|
|
|
2131
2285
|
}
|
|
2132
2286
|
if (sub === "/score" && method === "POST") {
|
|
2133
2287
|
const b = req.body ?? {};
|
|
2134
|
-
const
|
|
2135
|
-
|
|
2288
|
+
const siteKey = b.siteKey ?? "default";
|
|
2289
|
+
const { result, verifiedSignals } = await captcha.scoreForSession({
|
|
2290
|
+
siteKey,
|
|
2136
2291
|
ip: req.ip,
|
|
2137
2292
|
userAgent,
|
|
2138
2293
|
headers: req.headers,
|
|
@@ -2143,20 +2298,28 @@ function createCaptchaEndpoints(options = {}) {
|
|
|
2143
2298
|
ja3Hash,
|
|
2144
2299
|
action: b.action
|
|
2145
2300
|
});
|
|
2146
|
-
|
|
2301
|
+
const legacyId = typeof b.sessionId === "string" && !b.sessionId.startsWith(CLIENT_RECEIPT_PREFIX) ? b.sessionId || null : null;
|
|
2302
|
+
const response = json(200, { ...result, sessionId: legacyId });
|
|
2303
|
+
if (!signalStore) return response;
|
|
2304
|
+
const at = Date.now();
|
|
2305
|
+
const verdict = { score: result.score, recommendation: result.recommendation, at };
|
|
2306
|
+
if (legacyId) {
|
|
2307
|
+
await signalStore.set({ ...verdict, sessionId: legacyId });
|
|
2308
|
+
return response;
|
|
2309
|
+
}
|
|
2310
|
+
const host = getClientSignalHost(req.headers);
|
|
2311
|
+
const origin = req.headers.origin;
|
|
2312
|
+
if (verifiedSignals && host && (!origin || origin === "https://" + host) && typeof siteKey === "string" && siteKey.length <= 128) {
|
|
2313
|
+
const receipt = randomHex(24);
|
|
2147
2314
|
await signalStore.set({
|
|
2148
|
-
|
|
2149
|
-
|
|
2150
|
-
|
|
2151
|
-
at: Date.now()
|
|
2315
|
+
...verdict,
|
|
2316
|
+
sessionId: CLIENT_RECEIPT_PREFIX + receipt,
|
|
2317
|
+
receipt: { host, siteKey, expiresAt: at + CLIENT_RECEIPT_TTL_MS }
|
|
2152
2318
|
});
|
|
2319
|
+
response.headers["set-cookie"] = `${CLIENT_RECEIPT_COOKIE}=${receipt}; Path=/; Max-Age=${CLIENT_RECEIPT_TTL_MS / 1e3}; Secure; HttpOnly; SameSite=Lax`;
|
|
2320
|
+
response.headers["cache-control"] = "private, no-store";
|
|
2153
2321
|
}
|
|
2154
|
-
return
|
|
2155
|
-
...result,
|
|
2156
|
-
// Echoed so the widget can set the cookie the rule reads, without the
|
|
2157
|
-
// developer wiring a second endpoint to hand it one.
|
|
2158
|
-
sessionId: b.sessionId ?? null
|
|
2159
|
-
});
|
|
2322
|
+
return response;
|
|
2160
2323
|
}
|
|
2161
2324
|
if (sub === "/token/verify" && method === "POST") {
|
|
2162
2325
|
const b = req.body ?? {};
|
package/dist/vercel.js
CHANGED
|
@@ -4524,12 +4524,14 @@ function createFetchGuard(options = {}) {
|
|
|
4524
4524
|
},
|
|
4525
4525
|
async check(request, peer) {
|
|
4526
4526
|
const url = new URL(request.url);
|
|
4527
|
+
const headers = headerRecord(request.headers);
|
|
4528
|
+
if (!headers.host && !headers[":authority"]) headers.host = url.host;
|
|
4527
4529
|
const metadata = {
|
|
4528
4530
|
method: request.method,
|
|
4529
4531
|
path: url.pathname,
|
|
4530
4532
|
ip: resolveIP(request, peer),
|
|
4531
4533
|
user_agent: request.headers.get("user-agent") ?? void 0,
|
|
4532
|
-
headers
|
|
4534
|
+
headers,
|
|
4533
4535
|
query: url.search ? url.search.slice(1) : void 0,
|
|
4534
4536
|
timestamp: Date.now()
|
|
4535
4537
|
};
|
package/dist/vercel.mjs
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webdecoy/node",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.30.0",
|
|
4
4
|
"description": "Bot detection and AI agent verification for Node.js and edge runtimes. Local honeypot tripwires, rate limiting and AI crawler policies, with optional WebDecoy dashboard reporting.",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|