diff --git a/packages/client/src/collectors/environment-fonts.test.ts b/packages/client/src/collectors/environment-fonts.test.ts new file mode 100644 index 0000000..ef20028 --- /dev/null +++ b/packages/client/src/collectors/environment-fonts.test.ts @@ -0,0 +1,26 @@ +import { EnvironmentalCollector } from './environment'; + +it('includes Menlo and Calibri in the existing font summary', () => { + const installed = new Set(['Arial', 'Menlo', 'Calibri']); + const canvas = { + font: '', + measureText(): { width: number } { + const family = /"([^"]+)"/.exec(this.font)?.[1]; + return { width: family && installed.has(family) ? 105 : 100 }; + }, + }; + const context = jest.spyOn(HTMLCanvasElement.prototype, 'getContext') + .mockReturnValue(canvas as unknown as CanvasRenderingContext2D); + try { + expect(new EnvironmentalCollector()._getFontsInfo()).toMatchObject({ + supported: true, + count: 3, + hasMenlo: true, + hasCalibri: true, + hasSFPro: false, + hasSegoeUI: false, + }); + } finally { + context.mockRestore(); + } +}); diff --git a/packages/client/src/collectors/environment.ts b/packages/client/src/collectors/environment.ts index 1960ca7..b8bc7df 100644 --- a/packages/client/src/collectors/environment.ts +++ b/packages/client/src/collectors/environment.ts @@ -707,7 +707,9 @@ export class EnvironmentalCollector { hasArial: detectedFonts.includes('Arial'), hasTimesNewRoman: detectedFonts.includes('Times New Roman'), hasSegoeUI: detectedFonts.includes('Segoe UI'), + hasCalibri: detectedFonts.includes('Calibri'), hasSFPro: detectedFonts.includes('SF Pro'), + hasMenlo: detectedFonts.includes('Menlo'), hasDejaVuSans: detectedFonts.includes('DejaVu Sans'), supported: true }; diff --git a/packages/webdecoy/src/detection/detectors/advanced.ts b/packages/webdecoy/src/detection/detectors/advanced.ts index 73276e0..8094e36 100644 --- a/packages/webdecoy/src/detection/detectors/advanced.ts +++ b/packages/webdecoy/src/detection/detectors/advanced.ts @@ -130,56 +130,37 @@ export function analyzeCSSMediaQueries(cssMedia: CSSMediaQueriesInfo | undefined return detections; } -export function analyzeFonts(fontsInfo: FontsInfo | undefined, userAgent: string): Detection[] { - if (!fontsInfo || !fontsInfo.supported) return []; - const detections: Detection[] = []; +export function analyzeFonts(fontsInfo: FontsInfo, platform?: string): Detection[] { const count = fontsInfo.count ?? 0; - - if (count < 3) { - detections.push({ - category: 'headless', - score: 0.5, - confidence: 0.5, - reason: `Very few fonts detected (${count})`, - }); + if (fontsInfo.supported !== true || !Number.isInteger(count) || count < 3 || typeof platform !== 'string') { + return []; } - const ua = (userAgent || '').toLowerCase(); + // A missing flag on an older payload means the font was not reported. + const hasWindowsFonts = fontsInfo.hasSegoeUI === true || fontsInfo.hasCalibri === true; + const hasMacFonts = fontsInfo.hasSFPro === true || fontsInfo.hasMenlo === true; + const noWindowsFonts = fontsInfo.hasSegoeUI === false && fontsInfo.hasCalibri === false; + const noMacFonts = fontsInfo.hasSFPro === false && fontsInfo.hasMenlo === false; - if (ua.includes('windows') && fontsInfo.hasSegoeUI === false && count > 5) { - detections.push({ - category: 'bot', + if (/^(mac|iphone|ipad|ipod)/i.test(platform) && hasWindowsFonts && noMacFonts) { + return [{ + category: 'automation', score: 0.5, - confidence: 0.6, - reason: 'Windows UA but Segoe UI font not detected', - }); - } - - if ( - (ua.includes('mac os x') || ua.includes('macintosh')) && - fontsInfo.hasSFPro === false && - !ua.includes('10_15') && - !ua.includes('10_14') && - count > 5 - ) { - detections.push({ - category: 'bot', - score: 0.3, - confidence: 0.4, - reason: 'Modern macOS UA but SF Pro font not detected', - }); + confidence: 0.5, + reason: `Font set contradicts platform (Windows faces while claiming ${platform})`, + }]; } - if (ua.includes('linux') && !ua.includes('android') && fontsInfo.hasDejaVuSans === false && count > 5) { - detections.push({ - category: 'bot', - score: 0.4, + if (/^win/i.test(platform) && hasMacFonts && noWindowsFonts) { + return [{ + category: 'automation', + score: 0.5, confidence: 0.5, - reason: 'Linux UA but DejaVu Sans font not detected', - }); + reason: `Font set contradicts platform (macOS faces while claiming ${platform})`, + }]; } - return detections; + return []; } const PERMISSION_API_KEYS = [ @@ -239,7 +220,7 @@ export function analyzeDOMRect(domRectInfo?: DOMRectInfo): Detection[] { } /** Run every advanced-fingerprint detector present in the signals. */ -export function analyzeAdvancedSignals(signals: Signals, userAgent: string): Detection[] { +export function analyzeAdvancedSignals(signals: Signals): Detection[] { const detections: Detection[] = []; const env = signals.environmental ?? {}; @@ -247,7 +228,7 @@ export function analyzeAdvancedSignals(signals: Signals, userAgent: string): Det if (env.speechInfo) detections.push(...analyzeSpeechAPI(env.speechInfo)); if (env.workerConsistency) detections.push(...analyzeWorkerConsistency(env.workerConsistency)); if (env.cssMediaQueries) detections.push(...analyzeCSSMediaQueries(env.cssMediaQueries, signals)); - if (env.fontsInfo) detections.push(...analyzeFonts(env.fontsInfo, userAgent)); + if (env.fontsInfo) detections.push(...analyzeFonts(env.fontsInfo, env.navigator?.platform)); if (env.permissionsInfo) detections.push(...analyzePermissions(env.permissionsInfo)); if (env.domRectFingerprint) detections.push(...analyzeDOMRect(env.domRectFingerprint)); if (env.audioInfo) detections.push(...analyzeAudioContext(env.audioInfo)); diff --git a/packages/webdecoy/src/detection/engine.test.ts b/packages/webdecoy/src/detection/engine.test.ts index 8c223a2..c1ae066 100644 --- a/packages/webdecoy/src/detection/engine.test.ts +++ b/packages/webdecoy/src/detection/engine.test.ts @@ -9,6 +9,7 @@ */ import { DetectionEngine } from './engine'; +import fontPlatformFixtures from './fixtures/font-platform.json'; import inputModalityFixtures from './fixtures/input-modality.json'; import type { DetectionContext, Signals, Verdict } from './types'; @@ -394,11 +395,21 @@ describe('advanced fingerprint detection', () => { expect(hasReasonIncluding(v, 'Worker/main thread mismatch')).toBe(true); }); - it('detects Windows UA without Segoe UI', () => { - const v = score({ - environmental: { fontsInfo: { supported: true, count: 10, hasSegoeUI: false, hasSFPro: false, hasDejaVuSans: false } }, + it.each(fontPlatformFixtures.cases)('matches the reference for $name', ({ fonts, platform, reasons }) => { + const verdict = score({ environmental: { fontsInfo: fonts, navigator: { platform } } }); + expect(verdict.detections.filter(({ reason }) => /font/i.test(reason))).toEqual( + reasons.map(reason => ({ category: 'automation', score: 0.5, confidence: 0.5, reason })), + ); + }); + + it('keeps legacy font summaries neutral when a native font flag was not reported', () => { + const verdict = score({ + environmental: { + fontsInfo: { supported: true, count: 12, hasSegoeUI: true, hasSFPro: false }, + navigator: { platform: 'MacIntel' }, + }, }); - expect(hasReasonIncluding(v, 'Segoe UI')).toBe(true); + expect(verdict.detections.filter(({ reason }) => /font/i.test(reason))).toEqual([]); }); it('detects CSS pointer/touch mismatch', () => { diff --git a/packages/webdecoy/src/detection/engine.ts b/packages/webdecoy/src/detection/engine.ts index 331f292..7438283 100644 --- a/packages/webdecoy/src/detection/engine.ts +++ b/packages/webdecoy/src/detection/engine.ts @@ -157,7 +157,7 @@ export class DetectionEngine { if (signals.formAnalysis) { detections.push(...analyzeFormInteraction(signals.formAnalysis)); } - detections.push(...analyzeAdvancedSignals(signals, userAgent)); + detections.push(...analyzeAdvancedSignals(signals)); // --- Aggregate ------------------------------------------------------------- const categoryScores = calculateCategoryScores(detections, this.weights); diff --git a/packages/webdecoy/src/detection/fixtures/font-platform.json b/packages/webdecoy/src/detection/fixtures/font-platform.json new file mode 100644 index 0000000..f91cfcb --- /dev/null +++ b/packages/webdecoy/src/detection/fixtures/font-platform.json @@ -0,0 +1,85 @@ +{ + "reference": { + "version": "v1.51.1", + "commit": "b769817ffa4069a80217dc5b4cef9ce597077c07" + }, + "cases": [ + { + "name": "a Windows font on a reported Mac platform", + "platform": "MacIntel", + "fonts": { + "supported": true, + "count": 12, + "hasSegoeUI": false, + "hasCalibri": true, + "hasSFPro": false, + "hasMenlo": false + }, + "reasons": [ + "Font set contradicts platform (Windows faces while claiming MacIntel)" + ] + }, + { + "name": "a Mac font on a reported Windows platform", + "platform": "Win32", + "fonts": { + "supported": true, + "count": 12, + "hasSegoeUI": false, + "hasCalibri": false, + "hasSFPro": false, + "hasMenlo": true + }, + "reasons": [ + "Font set contradicts platform (macOS faces while claiming Win32)" + ] + }, + { + "name": "a Mac with both native and cross-platform fonts", + "platform": "MacIntel", + "fonts": { + "supported": true, + "count": 12, + "hasSegoeUI": true, + "hasCalibri": true, + "hasSFPro": false, + "hasMenlo": true + }, + "reasons": [] + }, + { + "name": "a small visible font inventory", + "platform": "MacIntel", + "fonts": { + "supported": true, + "count": 2, + "hasSegoeUI": true, + "hasCalibri": false, + "hasSFPro": false, + "hasMenlo": false + }, + "reasons": [] + }, + { + "name": "a browser with font probing unavailable", + "platform": "MacIntel", + "fonts": { + "supported": false + }, + "reasons": [] + }, + { + "name": "a Windows device missing stock fonts", + "platform": "Win32", + "fonts": { + "supported": true, + "count": 8, + "hasSegoeUI": false, + "hasCalibri": false, + "hasSFPro": false, + "hasMenlo": false + }, + "reasons": [] + } + ] +} diff --git a/packages/webdecoy/src/detection/types.ts b/packages/webdecoy/src/detection/types.ts index 99e00d8..2177954 100644 --- a/packages/webdecoy/src/detection/types.ts +++ b/packages/webdecoy/src/detection/types.ts @@ -167,7 +167,9 @@ export interface FontsInfo { supported?: boolean; count?: number; hasSegoeUI?: boolean; + hasCalibri?: boolean; hasSFPro?: boolean; + hasMenlo?: boolean; hasDejaVuSans?: boolean; [key: string]: unknown; }