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
18 changes: 9 additions & 9 deletions packages/webdecoy/src/detection/detectors/behavioral.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/** Core behavioral detection: mouse data sufficiency, velocity, timing. */

import type { Detection, Signals } from '../types';
import { hasHumanMovementMarkers, isTouchModality } from './pointer-evidence';

export function detectBehavioral(signals: Signals, widgetInteraction = true): Detection[] {
const detections: Detection[] = [];
Expand All @@ -10,19 +11,18 @@ export function detectBehavioral(signals: Signals, widgetInteraction = true): De
// Exempt touch and keyboard-only users from mouse-movement heuristics.
const totalPoints = b.totalPoints ?? 0;
const trajectory = b.trajectoryLength ?? 0;
const touchEvts = b.touchEvents ?? 0;
const keyEvts = b.keyEvents ?? 0;
const isTouchUsr = touchEvts >= 3;
const isKbdUser = keyEvts >= 2 && totalPoints === 0;
const isTouchUser = isTouchModality(b);
const isKeyboardUser = (b.keyEvents ?? 0) >= 2 && totalPoints === 0;
const humanMovement = hasHumanMovementMarkers(b);

if (totalPoints === 0 && !isTouchUsr && !isKbdUser) {
if (totalPoints === 0 && !isTouchUser && !isKeyboardUser) {
detections.push({
category: 'behavioral',
score: 0.8,
confidence: 0.9,
reason: 'Zero mouse, touch, or keyboard events recorded',
});
} else if (totalPoints < 10 && !isTouchUsr && !isKbdUser && trajectory < 30) {
} else if (totalPoints < 10 && !isTouchUser && !isKeyboardUser && trajectory < 30) {
detections.push({
category: 'behavioral',
score: 0.6,
Expand All @@ -32,7 +32,7 @@ export function detectBehavioral(signals: Signals, widgetInteraction = true): De
}

const velVar = b.velocityVariance ?? 1;
if (velVar < 0.02 && trajectory > 50) {
if (velVar < 0.02 && trajectory > 50 && !humanMovement) {
detections.push({
category: 'behavioral',
score: 0.6,
Expand All @@ -42,7 +42,7 @@ export function detectBehavioral(signals: Signals, widgetInteraction = true): De
}

const overshoots = b.overshootCorrections ?? 0;
if (widgetInteraction && overshoots === 0 && trajectory > 200) {
if (widgetInteraction && overshoots === 0 && trajectory > 200 && !humanMovement) {
detections.push({
category: 'behavioral',
score: 0.4,
Expand Down Expand Up @@ -86,7 +86,7 @@ export function detectBehavioral(signals: Signals, widgetInteraction = true): De
confidence: 0.5,
reason: 'Mouse event rate abnormally high',
});
} else if (eventRate > 0 && eventRate < 10) {
} else if (eventRate > 0 && eventRate < 10 && !humanMovement) {
detections.push({
category: 'behavioral',
score: 0.4,
Expand Down
16 changes: 16 additions & 0 deletions packages/webdecoy/src/detection/detectors/pointer-evidence.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { BehavioralSignals } from '../types';

/** Match a single touch sample with non-mouse pointer evidence when available. */
export function isTouchModality(behavioral: BehavioralSignals): boolean {
const touchEvents = behavioral.touchEvents ?? 0;
const hasTouchSample = touchEvents > 0 || (behavioral.touchTotalPoints ?? 0) > 0;
return touchEvents >= 3 || (hasTouchSample && behavioral.pointerHasNonMouseType === true);
}

/** Use measured movement to put slow or steady mouse input in context. */
export function hasHumanMovementMarkers(behavioral: BehavioralSignals): boolean {
const tremor = behavioral.microTremorScore ?? 0;
const corrections = behavioral.overshootCorrections ?? 0;
const changes = behavioral.directionChanges ?? 0;
return tremor >= 0.5 && (corrections >= 1 || changes >= 10);
}
32 changes: 14 additions & 18 deletions packages/webdecoy/src/detection/detectors/vision-ai.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/** Vision-AI agent detection: missing natural mouse behavior + PoW timing. */

import type { Detection, Signals } from '../types';
import { hasHumanMovementMarkers, isTouchModality } from './pointer-evidence';

export function detectVisionAI(signals: Signals, widgetInteraction = true): Detection[] {
const detections: Detection[] = [];
Expand All @@ -11,12 +12,11 @@ export function detectVisionAI(signals: Signals, widgetInteraction = true): Dete
const totalPoints = b.totalPoints ?? 0;
const trajectory = b.trajectoryLength ?? 0;
const approachPts = b.approachPoints ?? 0;
const touchEvents = b.touchEvents ?? 0;
const keyEvents = b.keyEvents ?? 0;
const isTouchUser = touchEvents >= 3;
const isKeyboardUser = keyEvents >= 2 && totalPoints === 0;
const isTouchUser = isTouchModality(b);
const isKeyboardUser = (b.keyEvents ?? 0) >= 2 && totalPoints === 0;
const usesMouse = !isTouchUser && !isKeyboardUser;

if (totalPoints < 5 && trajectory < 10 && !isTouchUser && !isKeyboardUser) {
if (totalPoints < 5 && trajectory < 10 && usesMouse) {
detections.push({
category: 'vision_ai',
score: 0.9,
Expand All @@ -25,7 +25,7 @@ export function detectVisionAI(signals: Signals, widgetInteraction = true): Dete
});
}

if (widgetInteraction && approachPts === 0 && !isTouchUser && !isKeyboardUser) {
if (widgetInteraction && approachPts === 0 && usesMouse) {
detections.push({
category: 'vision_ai',
score: 0.7,
Expand Down Expand Up @@ -57,7 +57,7 @@ export function detectVisionAI(signals: Signals, widgetInteraction = true): Dete
}

const microTremor = b.microTremorScore ?? 0.5;
if (microTremor < 0.15) {
if (usesMouse && totalPoints >= 5 && microTremor < 0.15) {
detections.push({
category: 'vision_ai',
score: 0.7,
Expand All @@ -66,7 +66,13 @@ export function detectVisionAI(signals: Signals, widgetInteraction = true): Dete
});
}

if (widgetInteraction && (b.approachDirectness ?? 0) > 0.95) {
if (
widgetInteraction &&
usesMouse &&
approachPts >= 5 &&
(b.approachDirectness ?? 0) > 0.95 &&
!hasHumanMovementMarkers(b)
) {
detections.push({
category: 'vision_ai',
score: 0.5,
Expand All @@ -75,16 +81,6 @@ export function detectVisionAI(signals: Signals, widgetInteraction = true): Dete
});
}

const precision = b.clickPrecision ?? 10;
if (widgetInteraction && precision > 0 && precision < 2) {
detections.push({
category: 'vision_ai',
score: 0.4,
confidence: 0.5,
reason: 'Click precision is unnaturally accurate',
});
}

const exploration = b.explorationRatio ?? 0.3;
if (widgetInteraction && exploration < 0.05 && trajectory > 50) {
detections.push({
Expand Down
31 changes: 26 additions & 5 deletions packages/webdecoy/src/detection/engine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/

import { DetectionEngine } from './engine';
import inputModalityFixtures from './fixtures/input-modality.json';
import type { DetectionContext, Signals, Verdict } from './types';

const UA_CHROME_WIN = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/120.0.0.0';
Expand Down Expand Up @@ -195,19 +196,17 @@ describe('widget and invisible interaction modes', () => {
const signals: Signals = {
behavioral: {
...HUMAN_BEHAVIOR,
approachPoints: 0,
approachPoints: 12,
approachDirectness: 0.98,
clickPrecision: 1,
microTremorScore: 0.3,
explorationRatio: 0.01,
overshootCorrections: 0,
interactionDuration: 90_000,
velocityVariance: 0.01,
},
};
const widgetOnly = [
'No approach trajectory to target',
'Mouse path to target is unnaturally direct',
'Click precision is unnaturally accurate',
'No exploratory mouse movement before click',
'No overshoot corrections on long trajectory',
'Unusually long interaction time',
Expand All @@ -219,6 +218,9 @@ describe('widget and invisible interaction modes', () => {
expect(hasReasonIncluding(widget, reason)).toBe(true);
expect(hasReasonIncluding(invisible, reason)).toBe(false);
}
const withoutApproach = { behavioral: { ...signals.behavioral, approachPoints: 0 } };
expect(hasReasonIncluding(score(withoutApproach), 'No approach trajectory to target')).toBe(true);
expect(hasReasonIncluding(score(withoutApproach, { widgetInteraction: false }), 'No approach trajectory to target')).toBe(false);
expect(hasReasonIncluding(widget, 'Mouse velocity too consistent')).toBe(true);
expect(hasReasonIncluding(invisible, 'Mouse velocity too consistent')).toBe(true);
});
Expand All @@ -236,7 +238,7 @@ describe('vision AI detection', () => {
});

it('detects lack of micro-tremor', () => {
const v = score({ behavioral: { microTremorScore: 0.05, trajectoryLength: 200 } });
const v = score({ behavioral: { totalPoints: 40, microTremorScore: 0.05, trajectoryLength: 200 } });
expect(hasReasonIncluding(v, 'micro-tremor')).toBe(true);
});
});
Expand Down Expand Up @@ -487,6 +489,25 @@ describe('PoW outcome', () => {
});
});

describe('input modality controls from FCaptcha ' + inputModalityFixtures.reference.version, () => {
it.each(inputModalityFixtures.cases)('$name', ({ behavioral, reasons }) => {
const engine = new DetectionEngine();
const verdict = engine.score({ behavioral }, {
ip: '203.0.113.9',
siteKey: 'test',
userAgent: UA_CHROME_MAC,
headers: GOOD_HEADERS,
pow: { provided: true, valid: true, serverElapsed: 3000 },
});
const inputReasons = verdict.detections
.filter(({ category }) => category === 'vision_ai' || category === 'behavioral')
.map(({ reason }) => reason)
.sort();
expect(inputReasons).toEqual([...reasons].sort());
expect(verdict.recommendation).toBe('allow');
});
});

describe('accessibility exemptions', () => {
it('does not exempt touchEvents=1 from detection', () => {
const v = score({ behavioral: { totalPoints: 0, trajectoryLength: 0, approachPoints: 0, touchEvents: 1, keyEvents: 0 } });
Expand Down
88 changes: 88 additions & 0 deletions packages/webdecoy/src/detection/fixtures/input-modality.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
{
"reference": {
"version": "v1.51.1",
"commit": "b769817ffa4069a80217dc5b4cef9ce597077c07"
},
"cases": [
{
"name": "mobile user taps once without scrolling",
"behavioral": {
"totalPoints": 0,
"trajectoryLength": 0,
"approachPoints": 0,
"approachDirectness": 1,
"touchEvents": 1,
"touchTotalPoints": 1,
"pointerHasNonMouseType": true,
"interactionDuration": 1500
},
"reasons": []
},
{
"name": "keyboard user activates the widget with no mouse path",
"behavioral": {
"totalPoints": 0,
"trajectoryLength": 0,
"approachPoints": 0,
"approachDirectness": 1,
"keyEvents": 8,
"interactionDuration": 4000
},
"reasons": []
},
{
"name": "screen reader user navigates before activation",
"behavioral": {
"totalPoints": 0,
"trajectoryLength": 0,
"approachPoints": 0,
"approachDirectness": 1,
"keyEvents": 12,
"interactionDuration": 12000
},
"reasons": []
},
{
"name": "trackpad user clicks near the center",
"behavioral": {
"totalPoints": 45,
"trajectoryLength": 150,
"approachPoints": 12,
"approachDirectness": 0.8,
"clickPrecision": 1.62,
"mouseEventRate": 30,
"interactionDuration": 4000
},
"reasons": []
},
{
"name": "slow pointer movement has natural corrections",
"behavioral": {
"totalPoints": 46,
"trajectoryLength": 270,
"approachPoints": 14,
"approachDirectness": 0.97,
"microTremorScore": 0.8,
"directionChanges": 18,
"mouseEventRate": 7,
"velocityVariance": 0.01,
"overshootCorrections": 0,
"interactionDuration": 4500
},
"reasons": []
},
{
"name": "measured straight path still produces a directness finding",
"behavioral": {
"totalPoints": 40,
"trajectoryLength": 180,
"approachPoints": 20,
"approachDirectness": 0.99,
"interactionDuration": 2000
},
"reasons": [
"Mouse path to target is unnaturally direct"
]
}
]
}
1 change: 1 addition & 0 deletions packages/webdecoy/src/detection/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export interface BehavioralSignals {
trajectoryLength?: number;
approachPoints?: number;
touchEvents?: number;
pointerHasNonMouseType?: boolean;
keyEvents?: number;
microTremorScore?: number;
approachDirectness?: number;
Expand Down
Loading