From 51b7000036d70a947255485d0c70a41e22e58774 Mon Sep 17 00:00:00 2001 From: Chris Portscheller Date: Mon, 23 Nov 2026 11:21:39 -0600 Subject: [PATCH] Compare CAPTCHA cadence reasons in CI --- .github/workflows/ci.yml | 9 ++- .../detection/fixtures/scoring-policy.json | 4 +- scripts/check-captcha-score-parity.cjs | 68 ++++++++++++++++++- 3 files changed, 76 insertions(+), 5 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4d9defe..27311ad 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -39,8 +39,15 @@ jobs: 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" + curl --fail --silent --show-error --retry 3 --max-time 30 \ + "https://raw.githubusercontent.com/WebDecoy/FCaptcha/$reference_commit/server-node/detection.js" \ + --output "$replay_dir/fcaptcha-detection.cjs" + curl --fail --silent --show-error --retry 3 --max-time 30 \ + "https://raw.githubusercontent.com/WebDecoy/FCaptcha/$reference_commit/test/fixtures/keystroke-cadence.json" \ + --output "$replay_dir/keystroke-cadence.json" 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 + node:22-alpine timeout -k 3s 30s node /replay/scripts/check-captcha-score-parity.cjs \ + /replay/fcaptcha-engine.cjs /replay/fcaptcha-detection.cjs /replay/keystroke-cadence.json diff --git a/packages/webdecoy/src/detection/fixtures/scoring-policy.json b/packages/webdecoy/src/detection/fixtures/scoring-policy.json index caa684d..2d21723 100644 --- a/packages/webdecoy/src/detection/fixtures/scoring-policy.json +++ b/packages/webdecoy/src/detection/fixtures/scoring-policy.json @@ -2,7 +2,9 @@ "reference": { "version": "v1.51.1", "commit": "b769817ffa4069a80217dc5b4cef9ce597077c07", - "engineSha256": "2da0e61e8efcc9022f0cdeaa22740dfc2ba1ee1fb2b0157c491294008616bd1a" + "engineSha256": "2da0e61e8efcc9022f0cdeaa22740dfc2ba1ee1fb2b0157c491294008616bd1a", + "detectionSha256": "8bfbd56c765ae47edee083e5e3c20512c16a1cca1dc18f7fb88d162f36d67682", + "cadenceFixtureSha256": "c11c199504aaae979acdaecaa8918615b08b4310c1e311c275b4c53a625c69ce" }, "cases": [ { diff --git a/scripts/check-captcha-score-parity.cjs b/scripts/check-captcha-score-parity.cjs index 79748b0..bcf81a3 100644 --- a/scripts/check-captcha-score-parity.cjs +++ b/scripts/check-captcha-score-parity.cjs @@ -1,6 +1,6 @@ /** * 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. + * Pass engine.js, detection.js, and keystroke-cadence.json from the pinned FCaptcha commit. * Build @webdecoy/node first so this uses its packaged scoring exports. */ const assert = require('node:assert/strict'); @@ -11,11 +11,24 @@ 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]; +const detectionPath = process.argv[3]; +const cadenceFixturePath = process.argv[4]; assert.ok(enginePath, 'Pass the pinned FCaptcha server-node/engine.js path'); -const hash = createHash('sha256').update(readFileSync(resolve(enginePath))).digest('hex'); -assert.equal(hash, fixtures.reference.engineSha256, 'Use FCaptcha ' + fixtures.reference.commit); +assert.ok(detectionPath, 'Pass the pinned FCaptcha server-node/detection.js path'); +assert.ok(cadenceFixturePath, 'Pass the pinned FCaptcha keystroke-cadence.json path'); + +function assertDigest(path, expected, label) { + const hash = createHash('sha256').update(readFileSync(resolve(path))).digest('hex'); + assert.equal(hash, expected, 'Use pinned ' + label + ' from FCaptcha ' + fixtures.reference.commit); +} + +assertDigest(enginePath, fixtures.reference.engineSha256, 'engine.js'); +assertDigest(detectionPath, fixtures.reference.detectionSha256, 'detection.js'); +assertDigest(cadenceFixturePath, fixtures.reference.cadenceFixtureSha256, 'cadence fixture'); const reference = require(resolve(enginePath)); +const referenceDetection = require(resolve(detectionPath)); +const cadenceFixtures = require(resolve(cadenceFixturePath)); const embedded = require('../packages/webdecoy/dist/index.js'); const reasonIds = { @@ -110,6 +123,55 @@ function assertOutcome(score, recommendation, expected, label) { assert.equal(score < 0.5, expected.success, label + ' success with accepted proof'); } +const cadenceReasonId = 'typing.non_human_cadence'; +const cadenceReason = 'Keystroke cadence analysis indicates non-human typing pattern'; +function cadenceDetection(detections, label) { + const matched = detections.filter(({ reason }) => reason === cadenceReason); + assert.ok(matched.length <= 1, label + ' emitted duplicate ' + cadenceReasonId); + return matched[0]; +} + +function assertClose(actual, expected, label) { + assert.ok(Math.abs(actual - expected) < 1e-9, label + ': ' + actual + ', expected ' + expected); +} + +for (const fixture of cadenceFixtures.cases) { + const form = { textareaKeyboard: { message: fixture.stats } }; + const embeddedSignals = withOrdinaryBrowser({ behavioral: ordinaryPointer, formAnalysis: form }); + const embeddedDetections = new embedded.DetectionEngine().score(embeddedSignals, context).detections; + const embeddedDetection = cadenceDetection(embeddedDetections, fixture.name + ' (Node)'); + const referenceResult = cadenceDetection( + referenceDetection.analyzeFormInteraction(form, { humanPresent: true }), + fixture.name + ' (FCaptcha)', + ); + + if (fixture.expected === null) { + assert.equal(embeddedDetection, undefined, fixture.name + ' (Node) reason IDs'); + assert.equal(referenceResult, undefined, fixture.name + ' (FCaptcha) reason IDs'); + console.log(fixture.name + ' — no cadence reason ID in either engine'); + continue; + } + + assert.ok(embeddedDetection, fixture.name + ' (Node) missing ' + cadenceReasonId); + assert.ok(referenceResult, fixture.name + ' (FCaptcha) missing ' + cadenceReasonId); + for (const [label, detection] of [['Node', embeddedDetection], ['FCaptcha', referenceResult]]) { + assert.equal(detection.category, 'bot', fixture.name + ' (' + label + ') category'); + assertClose(detection.score, fixture.expected.score, fixture.name + ' (' + label + ') score'); + assertClose(detection.confidence, fixture.expected.confidence, fixture.name + ' (' + label + ') confidence'); + assertClose( + detection.details.cadenceHumanScore, + fixture.expected.cadenceHumanScore, + fixture.name + ' (' + label + ') human score', + ); + assert.deepEqual( + Object.keys(detection.details.metrics).sort(), + Object.keys(fixture.expected.metrics).sort(), + fixture.name + ' (' + label + ') metric IDs', + ); + } + console.log(fixture.name + ' — matching ' + cadenceReasonId + ' and metric IDs'); +} + for (const fixture of fixtures.cases) { const { name, detections } = fixture; const embeddedScores = embedded.calculateCategoryScores(detections, embedded.DEFAULT_WEIGHTS);