diff --git a/packages/webdecoy/src/detection/detectors/behavioral.ts b/packages/webdecoy/src/detection/detectors/behavioral.ts index 69a4e46..f578856 100644 --- a/packages/webdecoy/src/detection/detectors/behavioral.ts +++ b/packages/webdecoy/src/detection/detectors/behavioral.ts @@ -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[] { @@ -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, diff --git a/packages/webdecoy/src/detection/detectors/keyboard-input.ts b/packages/webdecoy/src/detection/detectors/keyboard-input.ts new file mode 100644 index 0000000..f6de568 --- /dev/null +++ b/packages/webdecoy/src/detection/detectors/keyboard-input.ts @@ -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'; +} diff --git a/packages/webdecoy/src/detection/detectors/vision-ai.ts b/packages/webdecoy/src/detection/detectors/vision-ai.ts index b971edc..c0d4065 100644 --- a/packages/webdecoy/src/detection/detectors/vision-ai.ts +++ b/packages/webdecoy/src/detection/detectors/vision-ai.ts @@ -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[] { @@ -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) { diff --git a/packages/webdecoy/src/detection/engine.test.ts b/packages/webdecoy/src/detection/engine.test.ts index 8c223a2..ba11d4c 100644 --- a/packages/webdecoy/src/detection/engine.test.ts +++ b/packages/webdecoy/src/detection/engine.test.ts @@ -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(