From abb7d75f582f589b7bdbfc3bf59ece9268ac1941 Mon Sep 17 00:00:00 2001 From: Chris Portscheller Date: Mon, 23 Nov 2026 11:11:40 -0600 Subject: [PATCH] Replay pinned CAPTCHA reasons in CI --- .github/workflows/ci.yml | 20 +++ .../detection/detectors/pointer-evidence.ts | 3 +- .../detection/fixtures/input-modality.json | 31 ++++- .../detection/fixtures/scoring-policy.json | 30 ++++- scripts/check-captcha-score-parity.cjs | 116 ++++++++++++++++-- 5 files changed, 181 insertions(+), 19 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8e38afb..4d9defe 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -24,3 +24,23 @@ jobs: - run: npm run lint - run: npm test - run: npm run check:edge + - name: Compare embedded CAPTCHA with pinned FCaptcha + if: matrix.node-version == 22 + run: | + replay_dir=$(mktemp -d) + chmod 755 "$replay_dir" + mkdir -p "$replay_dir/scripts" "$replay_dir/packages/webdecoy/dist" "$replay_dir/packages/webdecoy/src/detection/fixtures" + cp scripts/check-captcha-score-parity.cjs "$replay_dir/scripts/" + cp packages/webdecoy/dist/index.js "$replay_dir/packages/webdecoy/dist/" + cp packages/webdecoy/src/detection/fixtures/{scoring-policy,input-modality}.json "$replay_dir/packages/webdecoy/src/detection/fixtures/" + + reference_commit=$(node -p "require('./packages/webdecoy/src/detection/fixtures/scoring-policy.json').reference.commit") + [[ "$reference_commit" =~ ^[0-9a-f]{40}$ ]] + curl --fail --silent --show-error --retry 3 --max-time 30 \ + "https://raw.githubusercontent.com/WebDecoy/FCaptcha/$reference_commit/server-node/engine.js" \ + --output "$replay_dir/fcaptcha-engine.cjs" + + docker run --rm --network none --read-only --cap-drop=ALL --security-opt no-new-privileges \ + --pids-limit 128 --cpus 2 --memory 384m --memory-swap 384m --user 1000:1000 --log-driver none \ + --mount "type=bind,source=$replay_dir,target=/replay,readonly" \ + node:22-alpine timeout -k 3s 30s node /replay/scripts/check-captcha-score-parity.cjs /replay/fcaptcha-engine.cjs diff --git a/packages/webdecoy/src/detection/detectors/pointer-evidence.ts b/packages/webdecoy/src/detection/detectors/pointer-evidence.ts index a40048f..9758561 100644 --- a/packages/webdecoy/src/detection/detectors/pointer-evidence.ts +++ b/packages/webdecoy/src/detection/detectors/pointer-evidence.ts @@ -9,7 +9,8 @@ export function isTouchModality(behavioral: BehavioralSignals): boolean { /** Use measured movement to put slow or steady mouse input in context. */ export function hasHumanMovementMarkers(behavioral: BehavioralSignals): boolean { - const tremor = behavioral.microTremorScore ?? 0; + // The collector uses 0.5 as the neutral tremor value when it has no measurement. + const tremor = behavioral.microTremorScore ?? 0.5; const corrections = behavioral.overshootCorrections ?? 0; const changes = behavioral.directionChanges ?? 0; return tremor >= 0.5 && (corrections >= 1 || changes >= 10); diff --git a/packages/webdecoy/src/detection/fixtures/input-modality.json b/packages/webdecoy/src/detection/fixtures/input-modality.json index 0d2ac0f..0e4fb8b 100644 --- a/packages/webdecoy/src/detection/fixtures/input-modality.json +++ b/packages/webdecoy/src/detection/fixtures/input-modality.json @@ -16,7 +16,8 @@ "pointerHasNonMouseType": true, "interactionDuration": 1500 }, - "reasons": [] + "reasons": [], + "reasonIds": [] }, { "name": "keyboard user activates the widget with no mouse path", @@ -28,7 +29,8 @@ "keyEvents": 8, "interactionDuration": 4000 }, - "reasons": [] + "reasons": [], + "reasonIds": [] }, { "name": "screen reader user navigates before activation", @@ -40,7 +42,8 @@ "keyEvents": 12, "interactionDuration": 12000 }, - "reasons": [] + "reasons": [], + "reasonIds": [] }, { "name": "trackpad user clicks near the center", @@ -53,7 +56,8 @@ "mouseEventRate": 30, "interactionDuration": 4000 }, - "reasons": [] + "reasons": [], + "reasonIds": [] }, { "name": "slow pointer movement has natural corrections", @@ -69,7 +73,21 @@ "overshootCorrections": 0, "interactionDuration": 4500 }, - "reasons": [] + "reasons": [], + "reasonIds": [] + }, + { + "name": "pointer makes a natural correction while tremor data is unavailable", + "behavioral": { + "totalPoints": 80, + "trajectoryLength": 350, + "approachPoints": 12, + "approachDirectness": 0.97, + "overshootCorrections": 1, + "interactionDuration": 4500 + }, + "reasons": [], + "reasonIds": [] }, { "name": "measured straight path still produces a directness finding", @@ -82,7 +100,8 @@ }, "reasons": [ "Mouse path to target is unnaturally direct" - ] + ], + "reasonIds": ["pointer.direct_approach"] } ] } diff --git a/packages/webdecoy/src/detection/fixtures/scoring-policy.json b/packages/webdecoy/src/detection/fixtures/scoring-policy.json index 78131a8..caa684d 100644 --- a/packages/webdecoy/src/detection/fixtures/scoring-policy.json +++ b/packages/webdecoy/src/detection/fixtures/scoring-policy.json @@ -204,36 +204,48 @@ "name": "an explicit WebDriver declaration", "environmental": { "webdriver": true, "automationFlags": { "webdriver": true } }, "candidate": { "score": 0.9, "recommendation": "block" }, + "reasonIds": ["webdriver.detected"], + "decisions": { "node": "allow", "fcaptcha": "block" }, "referenceFloor": 0.9 }, { "name": "an older aggregate WebDriver flag", "environmental": { "webdriver": true }, "candidate": { "score": 0.135375, "recommendation": "allow" }, + "reasonIds": ["webdriver.detected"], + "decisions": { "node": "allow", "fcaptcha": "block" }, "referenceFloor": 0.9 }, { "name": "an indirect WebDriver hint with a false explicit flag", "environmental": { "webdriver": true, "automationFlags": { "webdriver": false } }, "candidate": { "score": 0.135375, "recommendation": "allow" }, + "reasonIds": ["webdriver.detected"], + "decisions": { "node": "allow", "fcaptcha": "block" }, "referenceFloor": 0.9 }, { "name": "a known injected driver marker", "environmental": { "cdp": { "detected": true, "signals": ["chromedriver_cdc"] } }, "candidate": { "score": 0.9, "recommendation": "block" }, + "reasonIds": ["cdp.known_driver"], + "decisions": { "node": "allow", "fcaptcha": "block" }, "referenceFloor": 0.9 }, { "name": "a generic CDP finding", "environmental": { "cdp": { "detected": true, "signals": ["selenium_evaluate"] } }, "candidate": { "score": 0.0504, "recommendation": "allow" }, + "reasonIds": ["cdp.single_indicator"], + "decisions": { "node": "allow", "fcaptcha": "allow" }, "referenceFloor": 0 }, { "name": "a DevTools console observation", "environmental": { "cdpRuntime": { "consoleAttached": true } }, "candidate": { "score": 0, "recommendation": "allow" }, + "reasonIds": [], + "decisions": { "node": "allow", "fcaptcha": "allow" }, "referenceFloor": 0 }, { @@ -261,15 +273,19 @@ "environmental": { "cdp": { "detected": true, "signals": ["selenium_evaluate"] } } }, "candidate": { "score": 0.6, "recommendation": "block" }, + "reasonIds": ["cdp.single_indicator", "pointer.low_tremor"], + "decisions": { "node": "allow", "fcaptcha": "block" }, "referenceFloor": 0.6 }, { "name": "a weak movement finding with an independent driver finding", "signals": { - "behavioral": { "totalPoints": 80, "trajectoryLength": 350, "approachPoints": 12, "approachDirectness": 0.97, "overshootCorrections": 1 }, + "behavioral": { "totalPoints": 80, "trajectoryLength": 350, "approachPoints": 12, "approachDirectness": 0.97, "microTremorScore": 0.3, "overshootCorrections": 1 }, "environmental": { "cdp": { "detected": true, "signals": ["selenium_evaluate"] } } }, "candidate": { "score": 0.0879, "recommendation": "allow" }, + "reasonIds": ["cdp.single_indicator", "pointer.direct_approach"], + "decisions": { "node": "allow", "fcaptcha": "allow" }, "referenceFloor": 0 }, { @@ -278,12 +294,16 @@ "behavioral": { "totalPoints": 80, "trajectoryLength": 350, "approachPoints": 12, "microTremorScore": 0.1, "overshootCorrections": 1, "velocityVariance": 0.01, "straightLineRatio": 0.9 } }, "candidate": { "score": 0.1566, "recommendation": "allow" }, + "reasonIds": ["pointer.consistent_velocity", "pointer.low_tremor", "pointer.straight_motion"], + "decisions": { "node": "allow", "fcaptcha": "block" }, "referenceFloor": 0.6 }, { "name": "missing interaction data with a driver finding", "signals": { "environmental": { "cdp": { "detected": true, "signals": ["selenium_evaluate"] } } }, "candidate": { "score": 0.31449, "recommendation": "challenge" }, + "reasonIds": ["cdp.single_indicator", "input.missing", "pointer.approach_missing", "pointer.missing"], + "decisions": { "node": "challenge", "fcaptcha": "block" }, "referenceFloor": 0.6 }, { @@ -293,6 +313,8 @@ "environmental": { "cdpRuntime": { "consoleAttached": true } } }, "candidate": { "score": 0.063, "recommendation": "allow" }, + "reasonIds": ["pointer.low_tremor"], + "decisions": { "node": "allow", "fcaptcha": "allow" }, "referenceFloor": 0 }, { @@ -302,6 +324,8 @@ "environmental": { "cdp": { "detected": true, "signals": ["selenium_evaluate", "webdriver_getter_modified"] } } }, "candidate": { "score": 0.1446, "recommendation": "allow" }, + "reasonIds": ["cdp.multiple_indicators", "pointer.low_tremor"], + "decisions": { "node": "allow", "fcaptcha": "block" }, "referenceFloor": 0.6 }, { @@ -311,6 +335,8 @@ "environmental": { "cdp": { "detected": true, "signals": ["selenium_evaluate"] } } }, "candidate": { "score": 0.0504, "recommendation": "allow" }, + "reasonIds": ["cdp.single_indicator"], + "decisions": { "node": "allow", "fcaptcha": "allow" }, "referenceFloor": 0 }, { @@ -329,6 +355,8 @@ "environmental": { "cdp": { "detected": true, "signals": ["chromedriver_cdc"] } } }, "candidate": { "score": 0.9, "recommendation": "block" }, + "reasonIds": ["cdp.known_driver", "pointer.low_tremor"], + "decisions": { "node": "allow", "fcaptcha": "block" }, "referenceFloor": 0.6 } ] diff --git a/scripts/check-captcha-score-parity.cjs b/scripts/check-captcha-score-parity.cjs index a7f81a4..79748b0 100644 --- a/scripts/check-captcha-score-parity.cjs +++ b/scripts/check-captcha-score-parity.cjs @@ -1,5 +1,5 @@ /** - * Compare scoring policy with synthetic detector evidence and an accepted proof. + * Compare scoring policy and detector reason IDs using fixtures and an accepted proof. * Pass the engine.js file from the FCaptcha commit recorded in the fixture. * Build @webdecoy/node first so this uses its packaged scoring exports. */ @@ -9,6 +9,7 @@ const { readFileSync } = require('node:fs'); const { resolve } = require('node:path'); const fixtures = require('../packages/webdecoy/src/detection/fixtures/scoring-policy.json'); +const inputModality = require('../packages/webdecoy/src/detection/fixtures/input-modality.json'); const enginePath = process.argv[2]; assert.ok(enginePath, 'Pass the pinned FCaptcha server-node/engine.js path'); const hash = createHash('sha256').update(readFileSync(resolve(enginePath))).digest('hex'); @@ -17,6 +18,92 @@ assert.equal(hash, fixtures.reference.engineSha256, 'Use FCaptcha ' + fixtures.r const reference = require(resolve(enginePath)); const embedded = require('../packages/webdecoy/dist/index.js'); +const reasonIds = { + vision_ai: { + 'No mouse movement detected before click (AI agent pattern)': 'pointer.missing', + 'No approach trajectory to target': 'pointer.approach_missing', + 'Mouse movement lacks natural micro-tremor': 'pointer.low_tremor', + 'Mouse path to target is unnaturally direct': 'pointer.direct_approach', + }, + behavioral: { + 'Zero mouse, touch, or keyboard events recorded': 'input.missing', + 'Mouse velocity too consistent': 'pointer.consistent_velocity', + 'Mouse movements too straight': 'pointer.straight_motion', + }, + headless: { + 'WebDriver detected': 'webdriver.detected', + }, + // CDP reasons append the reported driver labels after a colon. + cdp: { + 'CDP automation detected': 'cdp.known_driver', + 'Multiple CDP indicators': 'cdp.multiple_indicators', + 'CDP indicator': 'cdp.single_indicator', + }, +}; + +const userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 Chrome/120.0.0.0 Safari/537.36'; +const context = { + ip: '203.0.113.9', + siteKey: 'parity-fixture', + userAgent, + headers: { + 'user-agent': userAgent, + accept: 'text/html', + 'accept-language': 'en-US,en;q=0.9', + 'accept-encoding': 'gzip, deflate, br', + }, + pow: { provided: true, valid: true, serverElapsed: 3000 }, +}; +const ordinaryPointer = { + totalPoints: 80, + trajectoryLength: 350, + approachPoints: 12, + microTremorScore: 0.6, + overshootCorrections: 3, +}; + +function getReasonIds(detections, label) { + return detections.flatMap(({ category, reason }) => { + if (!Object.hasOwn(reasonIds, category)) return []; + const message = category === 'cdp' ? reason.split(': ', 1)[0] : reason; + assert.ok(Object.hasOwn(reasonIds[category], message), label + ' unmapped reason: ' + reason); + return [reasonIds[category][message]]; + }).sort(); +} + +function scoreReference(detections) { + const categories = reference.calculateCategoryScores(detections); + return reference.applyCorroborationFloor( + reference.applyDispositiveFloor(reference.calculateFinalScore(categories), detections), + detections, + ); +} + +function referenceRecommendation(score) { + return score < 0.3 ? 'allow' : score < 0.6 ? 'challenge' : 'block'; +} + +function withOrdinaryBrowser(signals) { + return { + ...signals, + environmental: { + navigator: { platform: 'MacIntel', maxTouchPoints: 0 }, + ...signals.environmental, + automationFlags: { chrome: true, plugins: 5, platform: 'MacIntel', ...signals.environmental?.automationFlags }, + }, + }; +} + +function compareSignalFixture(name, signals, referenceDetections, expectedIds, decisions) { + const verdict = new embedded.DetectionEngine().score(signals, context); + const expectedOrder = [...expectedIds].sort(); + + assert.deepEqual(getReasonIds(verdict.detections, name + ' (Node)'), expectedOrder, name + ' (Node) reason IDs'); + assert.deepEqual(getReasonIds(referenceDetections, name + ' (FCaptcha)'), expectedOrder, name + ' (FCaptcha) reason IDs'); + assert.equal(verdict.recommendation, decisions.node, name + ' (Node) recommendation'); + assert.equal(referenceRecommendation(scoreReference(referenceDetections)), decisions.fcaptcha, name + ' (FCaptcha) recommendation'); +} + function assertOutcome(score, recommendation, expected, label) { assert.ok(Math.abs(score - expected.score) < 1e-10, label + ' score'); assert.equal(recommendation, expected.recommendation, label + ' recommendation'); @@ -29,27 +116,33 @@ for (const fixture of fixtures.cases) { const embeddedScore = embedded.calculateFinalScore(embeddedScores, embedded.DEFAULT_WEIGHTS); assertOutcome(embeddedScore, embedded.recommend(embeddedScore), fixture.embedded, name + ' (Node)'); - const referenceScores = reference.calculateCategoryScores(detections); - const referenceScore = reference.applyCorroborationFloor( - reference.applyDispositiveFloor(reference.calculateFinalScore(referenceScores), detections), - detections, - ); - const recommendation = referenceScore < 0.3 ? 'allow' : referenceScore < 0.6 ? 'challenge' : 'block'; + const referenceScore = scoreReference(detections); + const recommendation = referenceRecommendation(referenceScore); assertOutcome(referenceScore, recommendation, fixture.reference, name + ' (FCaptcha)'); console.log(name + ' — Node ' + fixture.embedded.recommendation + ', FCaptcha ' + recommendation); } -for (const { name, environmental, referenceFloor } of fixtures.decisiveFloorCases) { +assert.equal(inputModality.reference.commit, fixtures.reference.commit, 'Use the same FCaptcha commit for input-modality fixtures'); +for (const { name, behavioral, reasonIds: expectedIds } of inputModality.cases) { + const signals = withOrdinaryBrowser({ behavioral }); + const detections = [...reference.detectVisionAI(signals), ...reference.detectBehavioral(signals)]; + compareSignalFixture(name, signals, detections, expectedIds, { node: 'allow', fcaptcha: 'allow' }); + console.log(name + ' — matching reason IDs and allow outcomes'); +} + +for (const { name, environmental, referenceFloor, reasonIds: expectedIds, decisions } of fixtures.decisiveFloorCases) { if (referenceFloor === undefined) continue; - const signals = { environmental }; - const detections = [...reference.detectHeadless(signals, ''), ...reference.detectCDP(signals)]; + const signals = withOrdinaryBrowser({ environmental, behavioral: ordinaryPointer }); + const detections = [...reference.detectHeadless(signals, userAgent), ...reference.detectCDP(signals)]; const floor = reference.applyDispositiveFloor(0, detections); assert.equal(floor, referenceFloor, name + ' (FCaptcha decisive floor)'); + compareSignalFixture(name, signals, detections, expectedIds, decisions); console.log(name + ' — FCaptcha decisive floor ' + floor); } -for (const { name, signals, referenceFloor } of fixtures.corroborationFloorCases) { +for (const { name, signals: input, referenceFloor, reasonIds: expectedIds, decisions } of fixtures.corroborationFloorCases) { if (referenceFloor === undefined) continue; + const signals = withOrdinaryBrowser(input); const detections = [ ...reference.detectVisionAI(signals), ...reference.detectBehavioral(signals), @@ -57,5 +150,6 @@ for (const { name, signals, referenceFloor } of fixtures.corroborationFloorCases ]; const floor = reference.applyCorroborationFloor(0, detections); assert.equal(floor, referenceFloor, name + ' (FCaptcha corroboration floor)'); + compareSignalFixture(name, signals, detections, expectedIds, decisions); console.log(name + ' — FCaptcha corroboration floor ' + floor); }