Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
202 changes: 202 additions & 0 deletions packages/webdecoy/src/detection/fixtures/scoring-policy.json
Original file line number Diff line number Diff line change
@@ -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
}
}
]
}
13 changes: 13 additions & 0 deletions packages/webdecoy/src/detection/scoring.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
40 changes: 40 additions & 0 deletions scripts/check-captcha-score-parity.cjs
Original file line number Diff line number Diff line change
@@ -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);
}
Loading