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
14 changes: 11 additions & 3 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 { classifyKeyboardInput } from './keyboard-input';
import { hasHumanMovementMarkers, isTouchModality } from './pointer-evidence';

export function detectBehavioral(signals: Signals, widgetInteraction = true): Detection[] {
Expand All @@ -12,17 +13,24 @@ export function detectBehavioral(signals: Signals, widgetInteraction = true): De
const totalPoints = b.totalPoints ?? 0;
const trajectory = b.trajectoryLength ?? 0;
const isTouchUser = isTouchModality(b);
const isKeyboardUser = (b.keyEvents ?? 0) >= 2 && totalPoints === 0;
const keyboard = classifyKeyboardInput(signals);
const humanMovement = hasHumanMovementMarkers(b);

if (totalPoints === 0 && !isTouchUser && !isKeyboardUser) {
if (keyboard === 'short-key-holds' && !isTouchUser) {
detections.push({
category: 'behavioral',
score: 0.8,
confidence: 0.9,
reason: 'Keyboard-only input has unusually short key holds',
});
} else if (totalPoints === 0 && !isTouchUser && keyboard === null) {
detections.push({
category: 'behavioral',
score: 0.8,
confidence: 0.9,
reason: 'Zero mouse, touch, or keyboard events recorded',
});
} else if (totalPoints < 10 && !isTouchUser && !isKeyboardUser && trajectory < 30) {
} else if (totalPoints < 10 && !isTouchUser && keyboard === null && trajectory < 30) {
detections.push({
category: 'behavioral',
score: 0.6,
Expand Down
32 changes: 32 additions & 0 deletions packages/webdecoy/src/detection/detectors/keyboard-input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { Signals } from '../types';

const MIN_KEY_HOLD_SAMPLES = 3;
const SHORT_KEY_HOLD_MS = 20;
const MAX_KEY_HOLD_MS = 2_000;

type KeyboardModality = 'keyboard-only' | 'short-key-holds';

/** Classify pointerless keyboard input using any dwell times already collected in textareas. */
export function classifyKeyboardInput(signals: Signals): KeyboardModality | null {
const behavioral = signals.behavioral ?? {};
const keyEvents = behavioral.keyEvents ?? 0;
if (!Number.isFinite(keyEvents) || keyEvents < 2 || (behavioral.totalPoints ?? 0) !== 0) {
return null;
}

let count = 0;
let duration = 0;
for (const field of Object.values(signals.formAnalysis?.textareaKeyboard ?? {})) {
if (!Array.isArray(field?.dwellTimes)) continue;
for (const dwell of field.dwellTimes) {
if (typeof dwell === 'number' && dwell > 0 && dwell < MAX_KEY_HOLD_MS) {
count++;
duration += dwell;
}
}
}

return count >= MIN_KEY_HOLD_SAMPLES && duration / count < SHORT_KEY_HOLD_MS
? 'short-key-holds'
: 'keyboard-only';
}
3 changes: 2 additions & 1 deletion 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 { classifyKeyboardInput } from './keyboard-input';
import { hasHumanMovementMarkers, isTouchModality } from './pointer-evidence';

export function detectVisionAI(signals: Signals, widgetInteraction = true): Detection[] {
Expand All @@ -13,7 +14,7 @@ export function detectVisionAI(signals: Signals, widgetInteraction = true): Dete
const trajectory = b.trajectoryLength ?? 0;
const approachPts = b.approachPoints ?? 0;
const isTouchUser = isTouchModality(b);
const isKeyboardUser = (b.keyEvents ?? 0) >= 2 && totalPoints === 0;
const isKeyboardUser = classifyKeyboardInput(signals) === 'keyboard-only';
const usesMouse = !isTouchUser && !isKeyboardUser;

if (totalPoints < 5 && trajectory < 10 && usesMouse) {
Expand Down
42 changes: 42 additions & 0 deletions packages/webdecoy/src/detection/engine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,48 @@ describe('accessibility exemptions', () => {
});
});

describe('keyboard-only textarea timing', () => {
const behavioral = { keyEvents: 12, totalPoints: 0, interactionDuration: 1500 };

it.each([
{ name: 'ordinary holds', dwellTimes: [65, 80, 75] },
{ name: 'sparse short holds', dwellTimes: [8, 11] },
{ name: 'out-of-range durations mixed with sparse samples', dwellTimes: [8, 11, 0, -1, Infinity, 2_000] },
])('keeps the keyboard exemption with $name', ({ dwellTimes }) => {
const verdict = score({
behavioral,
formAnalysis: { textareaKeyboard: { message: { dwellTimes } } },
});

expect(hasCategory(verdict, 'behavioral')).toBe(false);
expect(hasCategory(verdict, 'vision_ai')).toBe(false);
});

it.each([true, false])('reports repeated short holds with widget mode %s', (widgetInteraction) => {
const verdict = score({
behavioral,
formAnalysis: { textareaKeyboard: {
message: { dwellTimes: [4, 6] },
notes: { dwellTimes: [5, 8] },
} },
}, { widgetInteraction });

expect(hasReasonIncluding(verdict, 'Keyboard-only input has unusually short key holds')).toBe(true);
expect(hasReasonIncluding(verdict, 'No mouse movement detected')).toBe(true);
expect(hasReasonIncluding(verdict, 'Zero mouse, touch, or keyboard events')).toBe(false);
});

it('keeps the touch exemption when keyboard and touch are both used', () => {
const verdict = score({
behavioral: { ...behavioral, touchEvents: 1, pointerHasNonMouseType: true },
formAnalysis: { textareaKeyboard: { message: { dwellTimes: [4, 6, 8] } } },
});

expect(hasCategory(verdict, 'behavioral')).toBe(false);
expect(hasCategory(verdict, 'vision_ai')).toBe(false);
});
});

describe('mobile touch authenticity', () => {
it('flags synthetic force=1 touches on mobile UA', () => {
const v = score(
Expand Down
Loading