From 1b5e3c49d016cd043c06d47a325ab93c3157c6bb Mon Sep 17 00:00:00 2001 From: Chris Portscheller Date: Mon, 23 Nov 2026 09:32:25 -0600 Subject: [PATCH] Add pinned fixtures for CAPTCHA scoring policy comparison --- .../detection/fixtures/scoring-policy.json | 202 ++++++++++++++++++ .../webdecoy/src/detection/scoring.test.ts | 13 ++ scripts/check-captcha-score-parity.cjs | 40 ++++ 3 files changed, 255 insertions(+) create mode 100644 packages/webdecoy/src/detection/fixtures/scoring-policy.json create mode 100644 packages/webdecoy/src/detection/scoring.test.ts create mode 100644 scripts/check-captcha-score-parity.cjs diff --git a/packages/webdecoy/src/detection/fixtures/scoring-policy.json b/packages/webdecoy/src/detection/fixtures/scoring-policy.json new file mode 100644 index 0000000..5996118 --- /dev/null +++ b/packages/webdecoy/src/detection/fixtures/scoring-policy.json @@ -0,0 +1,202 @@ +{ + "reference": { + "version": "v1.51.1", + "commit": "b769817ffa4069a80217dc5b4cef9ce597077c07", + "engineSha256": "2da0e61e8efcc9022f0cdeaa22740dfc2ba1ee1fb2b0157c491294008616bd1a" + }, + "cases": [ + { + "name": "ordinary input with no findings", + "detections": [], + "embedded": { + "score": 0, + "recommendation": "allow", + "success": true + }, + "reference": { + "score": 0, + "recommendation": "allow", + "success": true + } + }, + { + "name": "one weak behavior finding", + "detections": [ + { + "reason": "weak behavior fixture observation", + "category": "behavioral", + "score": 0.3, + "confidence": 0.4 + } + ], + "embedded": { + "score": 0.054, + "recommendation": "allow", + "success": true + }, + "reference": { + "score": 0.0216, + "recommendation": "allow", + "success": true + } + }, + { + "name": "one stronger observation", + "detections": [ + { + "reason": "primary fixture observation", + "category": "vision_ai", + "score": 0.75, + "confidence": 0.85 + } + ], + "embedded": { + "score": 0.1125, + "recommendation": "allow", + "success": true + }, + "reference": { + "score": 0.095625, + "recommendation": "allow", + "success": true + } + }, + { + "name": "two observations in one category", + "detections": [ + { + "reason": "primary fixture observation", + "category": "vision_ai", + "score": 0.75, + "confidence": 0.85 + }, + { + "reason": "secondary fixture observation", + "category": "vision_ai", + "score": 0.25, + "confidence": 0.4 + } + ], + "embedded": { + "score": 0.0885, + "recommendation": "allow", + "success": true + }, + "reference": { + "score": 0.1010625, + "recommendation": "allow", + "success": true + } + }, + { + "name": "decisive evidence", + "detections": [ + { + "reason": "decisive fixture observation", + "category": "headless", + "score": 0.9, + "confidence": 1, + "dispositive": true + } + ], + "embedded": { + "score": 0.135, + "recommendation": "allow", + "success": true + }, + "reference": { + "score": 0.9, + "recommendation": "block", + "success": false + } + }, + { + "name": "corroborated evidence", + "detections": [ + { + "reason": "behavior fixture observation", + "category": "behavioral", + "score": 0.75, + "confidence": 0.8 + }, + { + "reason": "separate vision fixture observation", + "category": "vision_ai", + "score": 0.7, + "confidence": 0.7 + } + ], + "embedded": { + "score": 0.24, + "recommendation": "allow", + "success": true + }, + "reference": { + "score": 0.6, + "recommendation": "block", + "success": false + } + }, + { + "name": "debug context contributes without corroborating", + "detections": [ + { + "reason": "behavior fixture observation", + "category": "behavioral", + "score": 0.75, + "confidence": 0.8 + }, + { + "reason": "developer context fixture observation", + "category": "cdp", + "score": 0.72, + "confidence": 0.8, + "nonCorroborating": true + } + ], + "embedded": { + "score": 0.2214, + "recommendation": "allow", + "success": true + }, + "reference": { + "score": 0.17712, + "recommendation": "allow", + "success": true + } + }, + { + "name": "challenge boundary with independent ambient categories", + "detections": [ + { + "reason": "environment fixture observation", + "category": "headless", + "score": 0.9, + "confidence": 0.95 + }, + { + "reason": "device fixture observation", + "category": "fingerprint", + "score": 0.9, + "confidence": 0.95 + }, + { + "reason": "request fixture observation", + "category": "bot", + "score": 0.9, + "confidence": 0.95 + } + ], + "embedded": { + "score": 0.342, + "recommendation": "challenge", + "success": true + }, + "reference": { + "score": 0.3078, + "recommendation": "challenge", + "success": true + } + } + ] +} diff --git a/packages/webdecoy/src/detection/scoring.test.ts b/packages/webdecoy/src/detection/scoring.test.ts new file mode 100644 index 0000000..dc2d0e5 --- /dev/null +++ b/packages/webdecoy/src/detection/scoring.test.ts @@ -0,0 +1,13 @@ +import fixtures from './fixtures/scoring-policy.json'; +import { calculateCategoryScores, calculateFinalScore, recommend } from './scoring'; +import { DEFAULT_WEIGHTS } from './weights'; + +describe('embedded CAPTCHA score policy', () => { + it.each(fixtures.cases)('$name', ({ detections, embedded }) => { + const categories = calculateCategoryScores(detections, DEFAULT_WEIGHTS); + const score = calculateFinalScore(categories, DEFAULT_WEIGHTS); + expect(score).toBeCloseTo(embedded.score, 10); + expect(recommend(score)).toBe(embedded.recommendation); + expect(score < 0.5).toBe(embedded.success); + }); +}); diff --git a/scripts/check-captcha-score-parity.cjs b/scripts/check-captcha-score-parity.cjs new file mode 100644 index 0000000..99fff9b --- /dev/null +++ b/scripts/check-captcha-score-parity.cjs @@ -0,0 +1,40 @@ +/** + * Compare scoring policy with synthetic detector evidence 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. + */ +const assert = require('node:assert/strict'); +const { createHash } = require('node:crypto'); +const { readFileSync } = require('node:fs'); +const { resolve } = require('node:path'); + +const fixtures = require('../packages/webdecoy/src/detection/fixtures/scoring-policy.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'); +assert.equal(hash, fixtures.reference.engineSha256, 'Use FCaptcha ' + fixtures.reference.commit); + +const reference = require(resolve(enginePath)); +const embedded = require('../packages/webdecoy/dist/index.js'); + +function assertOutcome(score, recommendation, expected, label) { + assert.ok(Math.abs(score - expected.score) < 1e-10, label + ' score'); + assert.equal(recommendation, expected.recommendation, label + ' recommendation'); + assert.equal(score < 0.5, expected.success, label + ' success with accepted proof'); +} + +for (const fixture of fixtures.cases) { + const { name, detections } = fixture; + const embeddedScores = embedded.calculateCategoryScores(detections, embedded.DEFAULT_WEIGHTS); + 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'; + assertOutcome(referenceScore, recommendation, fixture.reference, name + ' (FCaptcha)'); + console.log(name + ' — Node ' + fixture.embedded.recommendation + ', FCaptcha ' + recommendation); +}