From b777618068d8b2c69f848316fb53f25510c167d6 Mon Sep 17 00:00:00 2001 From: Wouter van Wijk Date: Tue, 17 Nov 2026 22:34:47 +0100 Subject: [PATCH 01/12] Add fingerprinting instrumentation script (fresh load only) Co-Authored-By: Claude Opus 5.5 --- .../collectors/vsv_techniques_js.dart | 254 ++++++++++++++++++ test/collectors/vsv_techniques_js_test.dart | 123 +++++++++ test/support/collector_node.dart | 46 ++-- 3 files changed, 403 insertions(+), 20 deletions(-) create mode 100644 lib/services/collectors/vsv_techniques_js.dart create mode 100644 test/collectors/vsv_techniques_js_test.dart diff --git a/lib/services/collectors/vsv_techniques_js.dart b/lib/services/collectors/vsv_techniques_js.dart new file mode 100644 index 0000000..02ec2e1 --- /dev/null +++ b/lib/services/collectors/vsv_techniques_js.dart @@ -0,0 +1,254 @@ +// Browser-side instrumentation for the "suspicious techniques" check. +// +// Fresh load only: this script is injected into the headless measurement +// WebView, never into the live browser (`browser_view.dart`). It observes and +// never blocks or alters: every wrapper records inside try/catch and then calls +// the original with the original `this` and arguments. Keep this ES5-ish (no +// optional chaining, no arrow functions, no let/const). + +/// Must run at document start. Defines `window.__vsv_tech_report()`, which +/// returns a JSON string (see the plan for the shape). +const String vsvTechniquesJs = r''' +(function() { + if (window.__vsv_tech_report) return; + var MAX_CANVAS = 20, MAX_PROMPTS = 20, MAX_SEEN = 100; + var started = Date.now(); + var canvases = []; // {el, w, h, chars, colors, reads, script} + var contexts = []; // {ctx, el, fill} + var fonts = { measureCalls: 0, seen: {}, script: '' }; + var webgl = { debugInfo: false, script: '' }; + var audio = { offline: 0, oscillators: 0, rendered: 0, script: '' }; + var rtc = { connections: 0, dataChannels: 0, offers: 0, getUserMedia: 0, script: '' }; + var counts = { enumerateDevices: 0, battery: 0, clipboardRead: 0 }; + var prompts = []; + var offlineSeen = [], rtcSeen = []; + + function script() { + try { + var lines = String(new Error().stack || '').split('\n'); + for (var i = 0; i < lines.length; i++) { + var m = /(https?:\/\/[^\s)]+?)(?::\d+){0,2}\)?\s*$/.exec(lines[i]); + if (m && m[1] !== String(location.href)) return m[1].slice(0, 300); + } + } catch (e) {} + return ''; + } + function seenNew(list, obj) { + if (list.indexOf(obj) >= 0) return false; + if (list.length < MAX_SEEN) list.push(obj); + return true; + } + function ownerOf(obj, name) { + while (obj && !Object.prototype.hasOwnProperty.call(obj, name)) { + obj = Object.getPrototypeOf(obj); + } + return obj; + } + // Replaces obj[name] with a wrapper that records, then calls the original + // unchanged. Skipped when the API is missing or cannot be redefined. + function wrap(obj, name, before, after) { + try { + var owner = ownerOf(obj, name); + if (!owner) return; + var d = Object.getOwnPropertyDescriptor(owner, name); + if (!d || typeof d.value !== 'function' || !d.configurable) return; + var orig = d.value; + Object.defineProperty(owner, name, { + value: function() { + if (before) { try { before(this, arguments); } catch (e) {} } + var result = orig.apply(this, arguments); + if (after) { try { after(this, arguments, result); } catch (e) {} } + return result; + }, + writable: d.writable, enumerable: d.enumerable, configurable: true }); + } catch (e) {} + } + function wrapSetter(obj, name, record) { + try { + var owner = ownerOf(obj, name); + if (!owner) return; + var d = Object.getOwnPropertyDescriptor(owner, name); + if (!d || !d.set || !d.configurable) return; + var set = d.set; + Object.defineProperty(owner, name, { + get: d.get, enumerable: d.enumerable, configurable: true, + set: function(v) { + try { record(this, v); } catch (e) {} + return set.call(this, v); + } }); + } catch (e) {} + } + function addPrompt(api) { + if (prompts.length >= MAX_PROMPTS) return; + var active = null; + try { + if (navigator.userActivation) active = !!navigator.userActivation.isActive; + } catch (e) {} + prompts.push({ api: api, atMs: Date.now() - started, userActive: active, script: script() }); + } + function proto(name) { + try { + var c = (typeof window !== 'undefined' && window[name]) || null; + return c && c.prototype ? c.prototype : null; + } catch (e) { return null; } + } + + // --- canvas --- + function ctxRecord(ctx) { + for (var i = 0; i < contexts.length; i++) if (contexts[i].ctx === ctx) return contexts[i]; + return null; + } + function canvasRecord(el) { + for (var i = 0; i < canvases.length; i++) if (canvases[i].el === el) return canvases[i]; + if (canvases.length >= MAX_CANVAS) return null; + var r = { el: el, w: 0, h: 0, chars: {}, colors: {}, reads: 0, script: '' }; + canvases.push(r); + return r; + } + function read(el) { + var r = canvasRecord(el); + if (!r) return; + r.reads++; + r.w = el.width; r.h = el.height; + if (!r.script) r.script = script(); + } + function drewText(ctx, args) { + var cr = ctxRecord(ctx); + if (!cr) return; + var r = canvasRecord(cr.el); + if (!r) return; + var text = String(args[0]); + for (var i = 0; i < text.length; i++) r.chars[text.charAt(i)] = 1; + if (cr.fill !== null) r.colors[cr.fill] = 1; + } + var canvasProto = proto('HTMLCanvasElement'), ctxProto = proto('CanvasRenderingContext2D'); + if (canvasProto) { + wrap(canvasProto, 'getContext', null, function(el, args, ctx) { + if (args[0] === '2d' && ctx && !ctxRecord(ctx) && contexts.length < MAX_SEEN) { + contexts.push({ ctx: ctx, el: el, fill: null }); + } + }); + wrap(canvasProto, 'toDataURL', function(el) { read(el); }); + wrap(canvasProto, 'toBlob', function(el) { read(el); }); + } + if (ctxProto) { + wrapSetter(ctxProto, 'fillStyle', function(ctx, v) { + var cr = ctxRecord(ctx); + if (cr) cr.fill = typeof v === 'string' ? v : null; + }); + wrap(ctxProto, 'fillText', function(ctx, args) { drewText(ctx, args); }); + wrap(ctxProto, 'strokeText', function(ctx, args) { drewText(ctx, args); }); + wrap(ctxProto, 'getImageData', function(ctx, args) { + if (!(args[2] >= 16 && args[3] >= 16)) return; + var cr = ctxRecord(ctx); + if (cr) read(cr.el); + }); + wrap(ctxProto, 'measureText', function(ctx) { + fonts.measureCalls++; + fonts.seen[String(ctx.font)] = 1; + if (!fonts.script) fonts.script = script(); + }); + } + + // --- webgl --- + var glNames = ['WebGLRenderingContext', 'WebGL2RenderingContext']; + for (var g = 0; g < glNames.length; g++) { + var gp = proto(glNames[g]); + if (!gp) continue; + wrap(gp, 'getExtension', function(gl, args) { + if (args[0] === 'WEBGL_debug_renderer_info') { + webgl.debugInfo = true; + if (!webgl.script) webgl.script = script(); + } + }); + wrap(gp, 'getParameter', function(gl, args) { + if (args[0] === 37445 || args[0] === 37446) { + webgl.debugInfo = true; + if (!webgl.script) webgl.script = script(); + } + }); + } + + // --- audio --- + var offlineProto = proto('OfflineAudioContext'); + function offlineCall(ctx) { + try { + if (typeof OfflineAudioContext !== 'undefined' && !(ctx instanceof OfflineAudioContext)) { + return false; + } + } catch (e) {} + if (seenNew(offlineSeen, ctx)) audio.offline++; + if (!audio.script) audio.script = script(); + return true; + } + if (offlineProto) { + wrap(offlineProto, 'createOscillator', function(ctx) { + if (offlineCall(ctx)) audio.oscillators++; + }); + wrap(offlineProto, 'startRendering', function(ctx) { + if (offlineCall(ctx)) audio.rendered++; + }); + } + + // --- webrtc and media --- + var rtcProto = proto('RTCPeerConnection'); + function rtcCall(pc) { + if (seenNew(rtcSeen, pc)) rtc.connections++; + if (!rtc.script) rtc.script = script(); + } + if (rtcProto) { + wrap(rtcProto, 'createDataChannel', function(pc) { rtcCall(pc); rtc.dataChannels++; }); + wrap(rtcProto, 'createOffer', function(pc) { rtcCall(pc); rtc.offers++; }); + } + try { + if (typeof navigator !== 'undefined' && navigator.mediaDevices) { + var md = navigator.mediaDevices; + wrap(md, 'getUserMedia', function() { + rtc.getUserMedia++; + if (!rtc.script) rtc.script = script(); + addPrompt('camera-microphone'); + }); + wrap(md, 'enumerateDevices', function() { counts.enumerateDevices++; }); + } + } catch (e) {} + + // --- other APIs --- + try { + if (typeof navigator !== 'undefined') { + wrap(navigator, 'getBattery', function() { counts.battery++; }); + wrap(navigator, 'requestMIDIAccess', function() { addPrompt('midi'); }); + if (navigator.geolocation) { + wrap(navigator.geolocation, 'getCurrentPosition', function() { addPrompt('geolocation'); }); + wrap(navigator.geolocation, 'watchPosition', function() { addPrompt('geolocation'); }); + } + if (navigator.clipboard) { + wrap(navigator.clipboard, 'read', function() { counts.clipboardRead++; addPrompt('clipboard'); }); + wrap(navigator.clipboard, 'readText', function() { counts.clipboardRead++; addPrompt('clipboard'); }); + } + } + if (typeof Notification !== 'undefined') { + wrap(Notification, 'requestPermission', function() { addPrompt('notifications'); }); + } + if (typeof document !== 'undefined') { + wrap(document, 'requestStorageAccess', function() { addPrompt('storage-access'); }); + } + } catch (e) {} + + window.__vsv_tech_report = function() { + var out = []; + for (var i = 0; i < canvases.length; i++) { + var c = canvases[i]; + if (c.reads > 0) { + out.push({ w: c.w, h: c.h, chars: Object.keys(c.chars).length, + colors: Object.keys(c.colors).length, reads: c.reads, script: c.script }); + } + } + return JSON.stringify({ v: 1, canvas: out, + fonts: { measureCalls: fonts.measureCalls, distinctFonts: Object.keys(fonts.seen).length, + script: fonts.script }, + webgl: webgl, audio: audio, rtc: rtc, + enumerateDevices: counts.enumerateDevices, battery: counts.battery, + clipboardRead: counts.clipboardRead, prompts: prompts }); + }; +})(); +'''; diff --git a/test/collectors/vsv_techniques_js_test.dart b/test/collectors/vsv_techniques_js_test.dart new file mode 100644 index 0000000..72b9419 --- /dev/null +++ b/test/collectors/vsv_techniques_js_test.dart @@ -0,0 +1,123 @@ +import 'dart:convert'; + +import 'package:flutter_test/flutter_test.dart'; +import 'package:view_source_vibe/services/collectors/vsv_techniques_js.dart'; + +import '../support/collector_node.dart'; + +const _fakeBrowser = r''' +var window = {}; window.top = window; +var location = { href: 'https://example.com/' }; +var navigator = { userActivation: { isActive: false }, + mediaDevices: { getUserMedia: function() { return 'gum'; }, + enumerateDevices: function() { return 'devs'; } }, + geolocation: { getCurrentPosition: function() { return 'pos'; } } }; +function HTMLCanvasElement() { this.width = 300; this.height = 150; } +HTMLCanvasElement.prototype.getContext = function() { + return this._x || (this._x = new CanvasRenderingContext2D()); +}; +HTMLCanvasElement.prototype.toDataURL = function() { return 'data:,x'; }; +HTMLCanvasElement.prototype.toBlob = function() { return 'blob'; }; +function CanvasRenderingContext2D() {} +CanvasRenderingContext2D.prototype = { + fillText: function() {}, strokeText: function() {}, + measureText: function() { return { width: 1 }; }, + getImageData: function(x, y, w, h) { return { width: w, height: h }; } +}; +(function() { + var fill = '#000', font = '10px a'; + Object.defineProperty(CanvasRenderingContext2D.prototype, 'fillStyle', { + configurable: true, get: function() { return fill; }, set: function(v) { fill = v; } }); + Object.defineProperty(CanvasRenderingContext2D.prototype, 'font', { + configurable: true, get: function() { return font; }, set: function(v) { font = v; } }); +})(); +function WebGLRenderingContext() {} +WebGLRenderingContext.prototype.getExtension = function(n) { return { n: n }; }; +WebGLRenderingContext.prototype.getParameter = function(p) { return p; }; +function OfflineAudioContext() {} +OfflineAudioContext.prototype.createOscillator = function() { return {}; }; +OfflineAudioContext.prototype.startRendering = function() { return 'r'; }; +function RTCPeerConnection() {} +RTCPeerConnection.prototype.createDataChannel = function() { return {}; }; +RTCPeerConnection.prototype.createOffer = function() { return 'o'; }; +function Notification() {} +Notification.requestPermission = function() { return 'granted'; }; +[HTMLCanvasElement, CanvasRenderingContext2D, WebGLRenderingContext, + OfflineAudioContext, RTCPeerConnection, Notification].forEach(function(c) { + window[c.name] = c; }); +'''; + +Future?> report(String page, {String before = ''}) async { + final out = await runJs([ + _fakeBrowser, + before, + vsvTechniquesJs, + page, + 'console.log(window.__vsv_tech_report());', + ].join('\n')); + return out == null ? null : jsonDecode(out) as Map; +} + +void main() { + test('text drawn in two colours and read back is recorded', () async { + final r = await report(''' + var c = new HTMLCanvasElement(); c.width = 300; c.height = 150; + var x = c.getContext('2d'); + x.fillStyle = '#f60'; x.fillText('Cwm fjordbank glyphs vext quiz', 2, 15); + x.fillStyle = 'rgba(102,204,0,0.7)'; x.fillText('Cwm fjordbank glyphs vext quiz', 4, 17); + c.toDataURL();'''); + if (r == null) return markTestSkipped('node not installed'); + expect(r['v'], 1); + expect((r['canvas'] as List).single, containsPair('reads', 1)); + expect((r['canvas'] as List).single['colors'], 2); + expect((r['canvas'] as List).single['chars'], greaterThanOrEqualTo(10)); + }); + + test('the original result is returned unchanged', () async { + final out = await runJs([ + _fakeBrowser, + vsvTechniquesJs, + 'var c = new HTMLCanvasElement();' + 'console.log(c.toDataURL() + "|" + (c.getContext("2d") === c.getContext("2d")));', + ].join('\n')); + if (out == null) return markTestSkipped('node not installed'); + expect(out.trim(), 'data:,x|true'); + }); + + test('webgl debug info and audio rendering are recorded', () async { + final r = await report(''' + var g = new WebGLRenderingContext(); + g.getExtension('WEBGL_debug_renderer_info'); g.getParameter(37445); + var a = new OfflineAudioContext(); a.createOscillator(); a.startRendering();'''); + if (r == null) return markTestSkipped('node not installed'); + expect(r['webgl'], containsPair('debugInfo', true)); + expect(r['audio'], + allOf(containsPair('offline', 1), containsPair('oscillators', 1), + containsPair('rendered', 1))); + }); + + test('a notification prompt without a click is recorded', () async { + final r = await report('Notification.requestPermission();'); + if (r == null) return markTestSkipped('node not installed'); + final p = (r['prompts'] as List).single as Map; + expect(p['api'], 'notifications'); + expect(p['userActive'], false); + }); + + test('a frozen prototype does not break the page', () async { + final r = await report(''' + var c = new HTMLCanvasElement(); + window.__res = c.toDataURL();''', + before: 'Object.freeze(HTMLCanvasElement.prototype);'); + if (r == null) return markTestSkipped('node not installed'); + expect(r['canvas'], isEmpty); + }); + + test('nothing called gives an empty report', () async { + final r = await report(''); + if (r == null) return markTestSkipped('node not installed'); + expect(r['canvas'], isEmpty); + expect(r['fonts']['measureCalls'], 0); + expect(r['prompts'], isEmpty); + }); +} diff --git a/test/support/collector_node.dart b/test/support/collector_node.dart index ccbd7c3..04794ef 100644 --- a/test/support/collector_node.dart +++ b/test/support/collector_node.dart @@ -21,15 +21,9 @@ XMLHttpRequest.prototype.send = function() {}; var setTimeout = function() { return 0; }; '''; -/// Runs the collector JS under Node with browser stubs and returns the decoded -/// result of `window.__vsv_collect(opts)`, plus the value of [probe] under -/// `__probe`. Returns null when Node is not installed. -Future?> runCollector({ - String before = '', - String after = '', - String opts = '{dom: false}', - String probe = 'null', -}) async { +/// Runs [source] under Node and returns its stdout. Returns null when Node is +/// not installed. +Future runJs(String source) async { try { await Process.run('node', ['--version']); } on ProcessException { @@ -37,22 +31,34 @@ Future?> runCollector({ } final dir = Directory.systemTemp.createTempSync('vsv_collector'); try { - final file = File('${dir.path}/run.js'); - file.writeAsStringSync([ - _stubs, - before, - vsvResourceBootstrapJs, - vsvCollectJs, - after, - 'var o = JSON.parse(window.__vsv_collect($opts)); ' - 'o.__probe = ($probe); console.log(JSON.stringify(o));', - ].join('\n')); + final file = File('${dir.path}/run.js')..writeAsStringSync(source); final r = await Process.run('node', [file.path]); if (r.exitCode != 0) { throw StateError('collector run failed: ${r.stderr}'); } - return jsonDecode(r.stdout as String) as Map; + return r.stdout as String; } finally { dir.deleteSync(recursive: true); } } + +/// Runs the collector JS under Node with browser stubs and returns the decoded +/// result of `window.__vsv_collect(opts)`, plus the value of [probe] under +/// `__probe`. Returns null when Node is not installed. +Future?> runCollector({ + String before = '', + String after = '', + String opts = '{dom: false}', + String probe = 'null', +}) async { + final out = await runJs([ + _stubs, + before, + vsvResourceBootstrapJs, + vsvCollectJs, + after, + 'var o = JSON.parse(window.__vsv_collect($opts)); ' + 'o.__probe = ($probe); console.log(JSON.stringify(o));', + ].join('\n')); + return out == null ? null : jsonDecode(out) as Map; +} From 711d33f8e17ac4ba086de5f3a75e9bf3211c143a Mon Sep 17 00:00:00 2001 From: Wouter van Wijk Date: Tue, 17 Nov 2026 22:37:52 +0100 Subject: [PATCH 02/12] Fix: techniques script slot exhaustion and untracked contexts Co-Authored-By: Claude Opus 5.5 --- .../collectors/vsv_techniques_js.dart | 68 ++++++++++++------- test/collectors/vsv_techniques_js_test.dart | 28 ++++++-- 2 files changed, 66 insertions(+), 30 deletions(-) diff --git a/lib/services/collectors/vsv_techniques_js.dart b/lib/services/collectors/vsv_techniques_js.dart index 02ec2e1..217741e 100644 --- a/lib/services/collectors/vsv_techniques_js.dart +++ b/lib/services/collectors/vsv_techniques_js.dart @@ -10,11 +10,14 @@ /// returns a JSON string (see the plan for the shape). const String vsvTechniquesJs = r''' (function() { + try { if (window.__vsv_tech_report) return; var MAX_CANVAS = 20, MAX_PROMPTS = 20, MAX_SEEN = 100; var started = Date.now(); var canvases = []; // {el, w, h, chars, colors, reads, script} - var contexts = []; // {ctx, el, fill} + var entries = []; // fallback without WeakMap: {ctx, el, fill, chars, colors} + var byCtx = typeof WeakMap === 'function' ? new WeakMap() : null; + var byEl = byCtx ? new WeakMap() : null; var fonts = { measureCalls: 0, seen: {}, script: '' }; var webgl = { debugInfo: false, script: '' }; var audio = { offline: 0, oscillators: 0, rendered: 0, script: '' }; @@ -94,54 +97,65 @@ const String vsvTechniquesJs = r''' } // --- canvas --- - function ctxRecord(ctx) { - for (var i = 0; i < contexts.length; i++) if (contexts[i].ctx === ctx) return contexts[i]; + // Text and colours are kept per context; a canvas record exists only once + // the canvas was read back, so unread chart canvases cannot use up the slots. + function ctxEntry(ctx) { + if (byCtx) return byCtx.get(ctx) || null; + for (var i = 0; i < entries.length; i++) if (entries[i].ctx === ctx) return entries[i]; + return null; + } + function elEntry(el) { + if (byEl) return byEl.get(el) || null; + for (var i = 0; i < entries.length; i++) if (entries[i].el === el) return entries[i]; return null; } - function canvasRecord(el) { - for (var i = 0; i < canvases.length; i++) if (canvases[i].el === el) return canvases[i]; - if (canvases.length >= MAX_CANVAS) return null; - var r = { el: el, w: 0, h: 0, chars: {}, colors: {}, reads: 0, script: '' }; - canvases.push(r); - return r; + function addEntry(ctx, el) { + var e = { ctx: ctx, el: el, fill: null, chars: {}, colors: {} }; + if (byCtx) { + byCtx.set(ctx, e); + byEl.set(el, e); + } else if (entries.length < MAX_SEEN) { + entries.push(e); + } } function read(el) { - var r = canvasRecord(el); - if (!r) return; + var r = null; + for (var i = 0; i < canvases.length; i++) if (canvases[i].el === el) r = canvases[i]; + if (!r) { + if (canvases.length >= MAX_CANVAS) return; + r = { el: el, w: 0, h: 0, reads: 0, script: '' }; + canvases.push(r); + } r.reads++; r.w = el.width; r.h = el.height; if (!r.script) r.script = script(); } function drewText(ctx, args) { - var cr = ctxRecord(ctx); - if (!cr) return; - var r = canvasRecord(cr.el); - if (!r) return; + var e = ctxEntry(ctx); + if (!e) return; var text = String(args[0]); - for (var i = 0; i < text.length; i++) r.chars[text.charAt(i)] = 1; - if (cr.fill !== null) r.colors[cr.fill] = 1; + for (var i = 0; i < text.length; i++) e.chars[text.charAt(i)] = 1; + if (e.fill !== null) e.colors[e.fill] = 1; } var canvasProto = proto('HTMLCanvasElement'), ctxProto = proto('CanvasRenderingContext2D'); if (canvasProto) { wrap(canvasProto, 'getContext', null, function(el, args, ctx) { - if (args[0] === '2d' && ctx && !ctxRecord(ctx) && contexts.length < MAX_SEEN) { - contexts.push({ ctx: ctx, el: el, fill: null }); - } + if (args[0] === '2d' && ctx && !ctxEntry(ctx)) addEntry(ctx, el); }); wrap(canvasProto, 'toDataURL', function(el) { read(el); }); wrap(canvasProto, 'toBlob', function(el) { read(el); }); } if (ctxProto) { wrapSetter(ctxProto, 'fillStyle', function(ctx, v) { - var cr = ctxRecord(ctx); - if (cr) cr.fill = typeof v === 'string' ? v : null; + var e = ctxEntry(ctx); + if (e) e.fill = typeof v === 'string' ? v : null; }); wrap(ctxProto, 'fillText', function(ctx, args) { drewText(ctx, args); }); wrap(ctxProto, 'strokeText', function(ctx, args) { drewText(ctx, args); }); wrap(ctxProto, 'getImageData', function(ctx, args) { if (!(args[2] >= 16 && args[3] >= 16)) return; - var cr = ctxRecord(ctx); - if (cr) read(cr.el); + var e = ctxEntry(ctx); + if (e) read(e.el); }); wrap(ctxProto, 'measureText', function(ctx) { fonts.measureCalls++; @@ -239,8 +253,9 @@ const String vsvTechniquesJs = r''' for (var i = 0; i < canvases.length; i++) { var c = canvases[i]; if (c.reads > 0) { - out.push({ w: c.w, h: c.h, chars: Object.keys(c.chars).length, - colors: Object.keys(c.colors).length, reads: c.reads, script: c.script }); + var e = elEntry(c.el); + out.push({ w: c.w, h: c.h, chars: e ? Object.keys(e.chars).length : 0, + colors: e ? Object.keys(e.colors).length : 0, reads: c.reads, script: c.script }); } } return JSON.stringify({ v: 1, canvas: out, @@ -250,5 +265,6 @@ const String vsvTechniquesJs = r''' enumerateDevices: counts.enumerateDevices, battery: counts.battery, clipboardRead: counts.clipboardRead, prompts: prompts }); }; + } catch (e) {} })(); '''; diff --git a/test/collectors/vsv_techniques_js_test.dart b/test/collectors/vsv_techniques_js_test.dart index 72b9419..2286764 100644 --- a/test/collectors/vsv_techniques_js_test.dart +++ b/test/collectors/vsv_techniques_js_test.dart @@ -105,12 +105,32 @@ void main() { }); test('a frozen prototype does not break the page', () async { + final out = await runJs([ + _fakeBrowser, + 'Object.freeze(HTMLCanvasElement.prototype);', + vsvTechniquesJs, + 'var c = new HTMLCanvasElement();' + 'var res = c.toDataURL();' + 'var g = new WebGLRenderingContext(); g.getParameter(37445);' + 'var r = JSON.parse(window.__vsv_tech_report());' + 'console.log(res + "|" + r.canvas.length + "|" + r.webgl.debugInfo);', + ].join('\n')); + if (out == null) return markTestSkipped('node not installed'); + expect(out.trim(), 'data:,x|0|true'); + }); + + test('many text-drawing canvases that are never read do not hide a later one', + () async { final r = await report(''' - var c = new HTMLCanvasElement(); - window.__res = c.toDataURL();''', - before: 'Object.freeze(HTMLCanvasElement.prototype);'); + for (var i = 0; i < 25; i++) { + var d = new HTMLCanvasElement(); var y = d.getContext('2d'); + y.fillStyle = '#123'; y.fillText('chart label ' + i, 1, 1); + } + var c = new HTMLCanvasElement(); var x = c.getContext('2d'); + x.fillStyle = '#f60'; x.fillText('Cwm fjordbank glyphs', 2, 15); + c.toDataURL();'''); if (r == null) return markTestSkipped('node not installed'); - expect(r['canvas'], isEmpty); + expect((r['canvas'] as List).single['chars'], greaterThanOrEqualTo(10)); }); test('nothing called gives an empty report', () async { From 2e0551fa4f551cf44b35d040bb2e1bbea43175d6 Mon Sep 17 00:00:00 2001 From: Wouter van Wijk Date: Tue, 17 Nov 2026 22:52:14 +0100 Subject: [PATCH 03/12] Fresh load records fingerprinting signals; rules that judge them Co-Authored-By: Claude Opus 5.5 --- lib/models/inspection/page_inspection.dart | 9 + lib/models/inspection/technique_signals.dart | 151 ++++++++++++ .../collectors/fresh_load_measurer.dart | 22 +- lib/services/derive/techniques.dart | 157 ++++++++++++ lib/services/html_service.dart | 4 +- lib/services/inspection_store.dart | 17 +- test/derive/techniques_test.dart | 227 ++++++++++++++++++ .../html_service_fresh_load_test.dart | 51 +++- 8 files changed, 630 insertions(+), 8 deletions(-) create mode 100644 lib/models/inspection/technique_signals.dart create mode 100644 lib/services/derive/techniques.dart create mode 100644 test/derive/techniques_test.dart diff --git a/lib/models/inspection/page_inspection.dart b/lib/models/inspection/page_inspection.dart index d5cdf7a..b99fcb1 100644 --- a/lib/models/inspection/page_inspection.dart +++ b/lib/models/inspection/page_inspection.dart @@ -4,6 +4,7 @@ import 'browser_snapshot.dart'; import 'file_snapshot.dart'; import 'observed.dart'; import 'server_snapshot.dart'; +import 'technique_signals.dart'; /// [PageInspection.freshLoad] before the user asked for one. const kFreshLoadNotMeasured = Unavailable( @@ -67,6 +68,10 @@ class PageInspection { /// screen, wait for a quiet network), only when the user asks for it. final Observed freshLoad; + /// What the page called during the fresh load (fingerprinting and similar). + /// Only the fresh load is instrumented, never the live browser. + final Observed freshTechniques; + /// The app's own size lookups for [freshLoad] (see [resourceEstimates]). final Map> freshEstimates; @@ -87,6 +92,7 @@ class PageInspection { this.primaryDocumentPreference, this.browserSettled = false, this.freshLoad = kFreshLoadNotMeasured, + this.freshTechniques = kTechniquesNotMeasured, this.freshEstimates = const {}, this.liveScreenshot = kNoScreenshot, this.freshScreenshot = kNoScreenshot, @@ -108,6 +114,7 @@ class PageInspection { primaryDocumentPreference = null, browserSettled = false, freshLoad = kFreshLoadNotMeasured, + freshTechniques = kTechniquesNotMeasured, freshEstimates = const {}, liveScreenshot = kNoScreenshot, freshScreenshot = kNoScreenshot; @@ -122,6 +129,7 @@ class PageInspection { Object? primaryDocumentPreference = _keep, bool? browserSettled, Observed? freshLoad, + Observed? freshTechniques, Map>? freshEstimates, Observed? liveScreenshot, Observed? freshScreenshot, @@ -140,6 +148,7 @@ class PageInspection { : primaryDocumentPreference as DataSource?, browserSettled: browserSettled ?? this.browserSettled, freshLoad: freshLoad ?? this.freshLoad, + freshTechniques: freshTechniques ?? this.freshTechniques, freshEstimates: freshEstimates ?? this.freshEstimates, liveScreenshot: liveScreenshot ?? this.liveScreenshot, freshScreenshot: freshScreenshot ?? this.freshScreenshot, diff --git a/lib/models/inspection/technique_signals.dart b/lib/models/inspection/technique_signals.dart new file mode 100644 index 0000000..322bfa3 --- /dev/null +++ b/lib/models/inspection/technique_signals.dart @@ -0,0 +1,151 @@ +import 'dart:convert'; + +import 'observed.dart'; + +/// [PageInspection.freshTechniques] before a fresh load was asked for. Not +/// measured is not clean: it is never shown as "none found". +const kTechniquesNotMeasured = Unavailable( + UnavailableReason.notApplicable, 'Run a fresh load to check'); + +/// A canvas the page read back (toDataURL / toBlob), with what was drawn on +/// its 2d context. Counts only: no pixels, no text. +class CanvasRead { + final int width, height, chars, colors, reads; + final String script; + const CanvasRead({ + required this.width, + required this.height, + required this.chars, + required this.colors, + required this.reads, + required this.script, + }); +} + +/// A permission or similar prompt asked for by the page. +class PromptCall { + final String api; + + /// Time since the page started loading. + final Duration at; + + /// Whether the user had clicked or typed recently; null when unknown. + final bool? userActive; + final String script; + const PromptCall({ + required this.api, + required this.at, + required this.userActive, + required this.script, + }); +} + +/// What the instrumentation script saw the page call during a fresh load. +class TechniqueSignals { + final List canvas; + final int fontMeasureCalls, distinctFonts; + final bool webglDebugInfo; + final int audioOffline, audioOscillators, audioRendered; + final int rtcConnections, rtcDataChannels, rtcOffers, rtcGetUserMedia; + final int enumerateDevices, battery, clipboardRead; + final List prompts; + + /// Signal group (`fonts`, `webgl`, `audio`, `rtc`) to the script URL that + /// made the first call; '' when unknown. + final Map scripts; + + const TechniqueSignals({ + this.canvas = const [], + this.fontMeasureCalls = 0, + this.distinctFonts = 0, + this.webglDebugInfo = false, + this.audioOffline = 0, + this.audioOscillators = 0, + this.audioRendered = 0, + this.rtcConnections = 0, + this.rtcDataChannels = 0, + this.rtcOffers = 0, + this.rtcGetUserMedia = 0, + this.enumerateDevices = 0, + this.battery = 0, + this.clipboardRead = 0, + this.prompts = const [], + this.scripts = const {}, + }); +} + +int _int(Object? v) => v is num ? v.toInt() : 0; +String _str(Object? v) => v is String ? v : ''; +Map _map(Object? v) => + v is Map ? v.cast() : const {}; + +/// Reads the JSON string returned by `window.__vsv_tech_report()`. No report +/// is Unavailable (the script did not run); a malformed one is Failed. Never +/// an empty, clean-looking value. +Observed parseTechniqueReport(Object? raw) { + if (raw == null) { + return const Unavailable(UnavailableReason.notExposedToPage, + 'The page gave no technique report'); + } + try { + final decoded = raw is String ? jsonDecode(raw) : raw; + if (decoded is! Map) { + return const Failed('The technique report had an unexpected shape'); + } + final r = decoded.cast(); + final fonts = _map(r['fonts']); + final webgl = _map(r['webgl']); + final audio = _map(r['audio']); + final rtc = _map(r['rtc']); + final canvas = [ + for (final c in (r['canvas'] is List ? r['canvas'] as List : const [])) + if (c is Map) + CanvasRead( + width: _int(c['w']), + height: _int(c['h']), + chars: _int(c['chars']), + colors: _int(c['colors']), + reads: _int(c['reads']), + script: _str(c['script']), + ), + ]; + final prompts = [ + for (final p in (r['prompts'] is List ? r['prompts'] as List : const [])) + if (p is Map) + PromptCall( + api: _str(p['api']), + at: Duration(milliseconds: _int(p['atMs'])), + userActive: + p['userActive'] is bool ? p['userActive'] as bool : null, + script: _str(p['script']), + ), + ]; + return Present( + TechniqueSignals( + canvas: canvas, + fontMeasureCalls: _int(fonts['measureCalls']), + distinctFonts: _int(fonts['distinctFonts']), + webglDebugInfo: webgl['debugInfo'] == true, + audioOffline: _int(audio['offline']), + audioOscillators: _int(audio['oscillators']), + audioRendered: _int(audio['rendered']), + rtcConnections: _int(rtc['connections']), + rtcDataChannels: _int(rtc['dataChannels']), + rtcOffers: _int(rtc['offers']), + rtcGetUserMedia: _int(rtc['getUserMedia']), + enumerateDevices: _int(r['enumerateDevices']), + battery: _int(r['battery']), + clipboardRead: _int(r['clipboardRead']), + prompts: prompts, + scripts: { + 'fonts': _str(fonts['script']), + 'webgl': _str(webgl['script']), + 'audio': _str(audio['script']), + 'rtc': _str(rtc['script']), + }, + ), + source: DataSource.browser); + } catch (e) { + return Failed('The technique report could not be read: $e'); + } +} diff --git a/lib/services/collectors/fresh_load_measurer.dart b/lib/services/collectors/fresh_load_measurer.dart index c9bacbb..1686f5a 100644 --- a/lib/services/collectors/fresh_load_measurer.dart +++ b/lib/services/collectors/fresh_load_measurer.dart @@ -10,6 +10,7 @@ import 'package:flutter_inappwebview/flutter_inappwebview.dart'; import '../derive/network_quiet.dart'; import 'collector_world.dart'; import 'frame_relay.dart'; +import 'vsv_techniques_js.dart'; /// Loads a page once more, apart from the live browser, the way Lighthouse /// does: empty cache, phone-sized screen, waiting until the network is @@ -24,7 +25,11 @@ class FreshLoadRaw { /// Small JPEG; null when the platform gave none. final Uint8List? screenshot; - const FreshLoadRaw(this.payload, {this.screenshot}); + + /// What `window.__vsv_tech_report()` returned (a JSON string); null when + /// the script did not run or the call failed. + final Object? techniques; + const FreshLoadRaw(this.payload, {this.screenshot, this.techniques}); } /// Thumbnail settings for screenshots of a load (small JPEG). @@ -89,6 +94,12 @@ class HeadlessFreshLoadMeasurer implements FreshLoadMeasurer { ), initialUserScripts: UnmodifiableListView([ collectorUserScript(defaultTargetPlatform), + // Page world: it has to see the page's own calls. Fresh load only. + UserScript( + source: vsvTechniquesJs, + injectionTime: UserScriptInjectionTime.AT_DOCUMENT_START, + forMainFrameOnly: true, + ), ]), onWebViewCreated: (controller) { controller.addJavaScriptHandler( @@ -140,6 +151,13 @@ class HeadlessFreshLoadMeasurer implements FreshLoadMeasurer { } catch (_) {} await waitQuiet(clock.elapsed + _scrollSettleBudget); + Object? techniques; + try { + techniques = await controller.evaluateJavascript( + source: 'window.__vsv_tech_report && window.__vsv_tech_report()'); + } catch (_) { + // No report is said as unavailable, never as clean. + } final payload = await controller.evaluateJavascript( source: 'window.__vsv_collect()', contentWorld: world); Uint8List? shot; @@ -149,7 +167,7 @@ class HeadlessFreshLoadMeasurer implements FreshLoadMeasurer { } catch (_) { // No screenshot is fine: the sizes are what was measured. } - return FreshLoadRaw(payload, screenshot: shot); + return FreshLoadRaw(payload, screenshot: shot, techniques: techniques); } finally { await view.dispose(); } diff --git a/lib/services/derive/techniques.dart b/lib/services/derive/techniques.dart new file mode 100644 index 0000000..47158a7 --- /dev/null +++ b/lib/services/derive/techniques.dart @@ -0,0 +1,157 @@ +import '../../models/inspection/observed.dart'; +import '../../models/inspection/technique_signals.dart'; + +// Thresholds. Canvas and font numbers follow the detection heuristics of +// Englehardt and Narayanan, "Online Tracking: A 1-million-site Measurement +// and Analysis" (ACM CCS 2016), section 6.1 (canvas: at least 16x16 px, text +// with two colours or ten distinct characters, read back) and section 6.2 +// (fonts: at least 50 distinct font values measured). The rest are this +// app's own choices. +const int kCanvasMinSide = 16; +const int kCanvasMinChars = 10; +const int kCanvasMinColors = 2; +const int kFontProbeMinFonts = 50; + +enum TechniqueSeverity { high, medium, low } + +/// An indication from what a page called, not proof of intent. +class TechniqueFinding { + final String id; + final TechniqueSeverity severity; + final String title; + final String detail; + + /// Script URLs involved (empty when the script could not be told). + final List scripts; + final DataSource source; + + const TechniqueFinding({ + required this.id, + required this.severity, + required this.title, + required this.detail, + this.scripts = const [], + this.source = DataSource.browser, + }); + + Map toJson() => { + 'id': id, + 'severity': severity.name, + 'title': title, + 'detail': detail, + 'scripts': scripts, + 'source': source.name, + }; +} + +List _scripts(Iterable urls) => { + for (final u in urls) + if (u.isNotEmpty) u + }.toList(); + +/// Rules that turn recorded calls into findings. No findings means nothing of +/// this was seen in the fresh load; the caller decides when it was not measured. +List judgeSignals(TechniqueSignals s) { + final out = []; + + final fp = [ + for (final c in s.canvas) + if (c.width >= kCanvasMinSide && + c.height >= kCanvasMinSide && + c.reads >= 1 && + (c.chars >= kCanvasMinChars || c.colors >= kCanvasMinColors)) + c + ]; + if (fp.isNotEmpty) { + out.add(TechniqueFinding( + id: 'canvas-fingerprint', + severity: TechniqueSeverity.high, + title: 'Reads back a canvas it drew text on (canvas fingerprinting)', + detail: 'The page drew text on a canvas and read the pixels back. ' + 'Rendering differs slightly between devices, so this can tell ' + 'visitors apart. Charts and image editors do not draw text this way.', + scripts: _scripts(fp.map((c) => c.script)), + )); + } + if (s.distinctFonts >= kFontProbeMinFonts) { + out.add(TechniqueFinding( + id: 'font-probing', + severity: TechniqueSeverity.medium, + title: 'Measures text in ${s.distinctFonts} different fonts ' + '(font probing)', + detail: 'Which fonts are installed differs per device, so measuring ' + 'text in many fonts can tell visitors apart.', + scripts: _scripts([s.scripts['fonts'] ?? '']), + )); + } + if (s.webglDebugInfo) { + out.add(TechniqueFinding( + id: 'webgl-fingerprint', + severity: TechniqueSeverity.medium, + title: "Reads the graphics card's vendor and model", + detail: 'The page asked WebGL for the unmasked renderer, which names ' + 'the graphics hardware.', + scripts: _scripts([s.scripts['webgl'] ?? '']), + )); + } + if (s.audioOffline >= 1 && s.audioOscillators >= 1 && s.audioRendered >= 1) { + out.add(TechniqueFinding( + id: 'audio-fingerprint', + severity: TechniqueSeverity.high, + title: 'Renders a silent audio signal and reads it back ' + '(audio fingerprinting)', + detail: 'An offline audio context rendered an oscillator without ' + 'playing it. The result differs slightly between devices.', + scripts: _scripts([s.scripts['audio'] ?? '']), + )); + } + if (s.rtcConnections >= 1 && + (s.rtcDataChannels >= 1 || s.rtcOffers >= 1) && + s.rtcGetUserMedia == 0) { + out.add(TechniqueFinding( + id: 'webrtc-probe', + severity: TechniqueSeverity.medium, + title: 'Opens a WebRTC connection without a call ' + '(can reveal local addresses)', + detail: 'A peer connection was created and an offer or data channel ' + 'started, but no camera or microphone was requested.', + scripts: _scripts([s.scripts['rtc'] ?? '']), + )); + } + if (s.enumerateDevices >= 1) { + out.add(const TechniqueFinding( + id: 'device-enumeration', + severity: TechniqueSeverity.low, + title: 'Lists cameras and microphones', + detail: 'The page asked which media devices are connected.', + )); + } + if (s.battery >= 1) { + out.add(const TechniqueFinding( + id: 'battery', + severity: TechniqueSeverity.low, + title: 'Reads the battery status', + detail: 'The page asked for the battery level or charging state.', + )); + } + if (s.clipboardRead >= 1) { + out.add(const TechniqueFinding( + id: 'clipboard-read', + severity: TechniqueSeverity.high, + title: 'Reads the clipboard', + detail: 'The page tried to read what you copied last.', + )); + } + for (final p in s.prompts) { + if (p.userActive != false) continue; + out.add(TechniqueFinding( + id: 'prompt-on-load', + severity: TechniqueSeverity.medium, + title: 'Asks for ${p.api} without a click, ' + '${p.at.inSeconds} s after loading', + detail: 'The request came while you had not clicked or typed.', + scripts: _scripts([p.script]), + )); + } + return out; +} diff --git a/lib/services/html_service.dart b/lib/services/html_service.dart index 4a982b2..b185000 100644 --- a/lib/services/html_service.dart +++ b/lib/services/html_service.dart @@ -33,6 +33,7 @@ import 'package:view_source_vibe/services/collectors/server_fetcher.dart'; import 'package:view_source_vibe/services/derive/cookies_view.dart'; import 'package:view_source_vibe/services/derive/detections.dart'; import 'package:view_source_vibe/services/derive/detections_view.dart'; +import 'package:view_source_vibe/models/inspection/technique_signals.dart'; import 'package:view_source_vibe/services/derive/divergences.dart'; import 'package:view_source_vibe/services/derive/block_page.dart'; import 'package:view_source_vibe/services/derive/header_insights.dart'; @@ -4279,7 +4280,8 @@ Technical details: $message'''; return; } store.applyFreshLoad(gen, Present(snapshot, source: DataSource.browser), - screenshot: _screenshotOf(raw.screenshot)); + screenshot: _screenshotOf(raw.screenshot), + techniques: parseTechniqueReport(raw.techniques)); // Hidden sizes, like the live load (shared lookup cache per page). final urls = estimateTargetsOf(store.current.freshLoad, const {}, diff --git a/lib/services/inspection_store.dart b/lib/services/inspection_store.dart index c232e88..6072924 100644 --- a/lib/services/inspection_store.dart +++ b/lib/services/inspection_store.dart @@ -8,6 +8,7 @@ import '../models/inspection/file_snapshot.dart'; import '../models/inspection/observed.dart'; import '../models/inspection/page_inspection.dart'; import '../models/inspection/server_snapshot.dart'; +import '../models/inspection/technique_signals.dart'; import 'collectors/browser_payload.dart'; /// The central reactive data point. Every navigation gets a new generation; @@ -156,14 +157,28 @@ class InspectionStore extends ChangeNotifier { /// result and screenshot. A new result drops the lookups and screenshot of /// the previous one. void applyFreshLoad(int gen, Observed result, - {Observed screenshot = kNoScreenshot}) { + {Observed screenshot = kNoScreenshot, + Observed? techniques}) { if (!_accepts(gen)) return; _set(_current.copyWith( freshLoad: result, + freshTechniques: techniques ?? _techniquesFor(result), freshEstimates: const {}, freshScreenshot: screenshot)); } + /// No signals given: Pending while the load runs, Unavailable where there + /// is no headless browser, otherwise "run a fresh load". + Observed _techniquesFor(Observed r) => + switch (r) { + Pending() => const Pending(), + Unavailable(reason: UnavailableReason.unsupportedPlatform) => + const Unavailable(UnavailableReason.unsupportedPlatform, + 'Not supported on this platform'), + Failed(error: final e) => Failed(e), + _ => kTechniquesNotMeasured, + }; + /// A screenshot of the live browser; a missing one never replaces a /// screenshot already there. void applyLiveScreenshot(int gen, Observed shot) { diff --git a/test/derive/techniques_test.dart b/test/derive/techniques_test.dart new file mode 100644 index 0000000..e09a44d --- /dev/null +++ b/test/derive/techniques_test.dart @@ -0,0 +1,227 @@ +import 'dart:convert'; + +import 'package:flutter_test/flutter_test.dart'; +import 'package:view_source_vibe/models/inspection/observed.dart'; +import 'package:view_source_vibe/models/inspection/technique_signals.dart'; +import 'package:view_source_vibe/services/derive/techniques.dart'; + +TechniqueSignals signals({ + List canvas = const [], + int distinctFonts = 0, + bool webglDebugInfo = false, + int audioOffline = 0, + int audioOscillators = 0, + int audioRendered = 0, + int rtcConnections = 0, + int rtcDataChannels = 0, + int rtcOffers = 0, + int rtcGetUserMedia = 0, + int enumerateDevices = 0, + int battery = 0, + int clipboardRead = 0, + List prompts = const [], + Map scripts = const {}, +}) => + TechniqueSignals( + canvas: canvas, + fontMeasureCalls: distinctFonts, + distinctFonts: distinctFonts, + webglDebugInfo: webglDebugInfo, + audioOffline: audioOffline, + audioOscillators: audioOscillators, + audioRendered: audioRendered, + rtcConnections: rtcConnections, + rtcDataChannels: rtcDataChannels, + rtcOffers: rtcOffers, + rtcGetUserMedia: rtcGetUserMedia, + enumerateDevices: enumerateDevices, + battery: battery, + clipboardRead: clipboardRead, + prompts: prompts, + scripts: scripts, + ); + +void main() { + test('text in two colours read back is a canvas fingerprint', () { + final f = judgeSignals(signals(canvas: [ + const CanvasRead( + width: 300, + height: 150, + chars: 34, + colors: 2, + reads: 1, + script: 'https://cdn.fp.test/fp.js') + ])); + expect(f.single.id, 'canvas-fingerprint'); + expect(f.single.severity, TechniqueSeverity.high); + expect(f.single.scripts, ['https://cdn.fp.test/fp.js']); + }); + + test('ten distinct characters in one colour is enough; nine is not', () { + CanvasRead c(int chars) => CanvasRead( + width: 300, height: 150, chars: chars, colors: 1, reads: 1, script: ''); + expect( + judgeSignals(signals(canvas: [c(10)])).single.id, 'canvas-fingerprint'); + expect(judgeSignals(signals(canvas: [c(9)])), isEmpty); + }); + + test('a chart is not a fingerprint', () { + expect( + judgeSignals(signals(canvas: [ + const CanvasRead( + width: 800, + height: 400, + chars: 0, + colors: 1, + reads: 3, + script: '') + ])), + isEmpty); + }); + + test('a tiny canvas is not a fingerprint', () { + expect( + judgeSignals(signals(canvas: [ + const CanvasRead( + width: 8, height: 8, chars: 30, colors: 3, reads: 1, script: '') + ])), + isEmpty); + }); + + test('50 fonts measured is font probing; 49 is not', () { + final f = judgeSignals(signals(distinctFonts: 96)); + expect(f.single.id, 'font-probing'); + expect(f.single.title, contains('96 different fonts')); + expect(judgeSignals(signals(distinctFonts: 49)), isEmpty); + }); + + test('audio needs offline context, oscillator and rendering', () { + expect( + judgeSignals( + signals(audioOffline: 1, audioOscillators: 1, audioRendered: 1)) + .single + .id, + 'audio-fingerprint'); + expect( + judgeSignals(signals(audioOffline: 1, audioOscillators: 1)), isEmpty); + }); + + test('a WebRTC connection without a call is a probe', () { + expect(judgeSignals(signals(rtcConnections: 1, rtcOffers: 1)).single.id, + 'webrtc-probe'); + }); + + test('a video call is not a WebRTC probe', () { + expect( + judgeSignals( + signals(rtcConnections: 1, rtcOffers: 1, rtcGetUserMedia: 1)), + isEmpty); + }); + + test('single-call signals', () { + final f = judgeSignals(signals( + webglDebugInfo: true, + enumerateDevices: 1, + battery: 2, + clipboardRead: 1)); + expect(f.map((e) => e.id).toSet(), { + 'webgl-fingerprint', + 'device-enumeration', + 'battery', + 'clipboard-read' + }); + }); + + test('a prompt without a click is reported, with its delay', () { + final f = judgeSignals(signals(prompts: [ + const PromptCall( + api: 'notifications', + at: Duration(milliseconds: 2400), + userActive: false, + script: 'https://x.test/p.js') + ])); + expect(f.single.id, 'prompt-on-load'); + expect(f.single.title, + 'Asks for notifications without a click, 2 s after loading'); + expect(f.single.scripts, ['https://x.test/p.js']); + }); + + test('a prompt after a click, or of unknown activity, is not reported', () { + expect( + judgeSignals(signals(prompts: [ + const PromptCall( + api: 'geolocation', + at: Duration(seconds: 3), + userActive: true, + script: ''), + const PromptCall( + api: 'geolocation', + at: Duration(seconds: 3), + userActive: null, + script: ''), + ])), + isEmpty); + }); + + test('toJson carries no more than the finding', () { + final j = judgeSignals(signals(clipboardRead: 1)).single.toJson(); + expect(j['id'], 'clipboard-read'); + expect(j['severity'], 'high'); + }); + + test('report parsing', () { + expect(parseTechniqueReport(null), isA>()); + expect(parseTechniqueReport('not json'), isA>()); + final r = parseTechniqueReport(jsonEncode({ + 'v': 1, + 'canvas': [ + { + 'w': 300, + 'h': 150, + 'chars': 34, + 'colors': 2, + 'reads': 1, + 'script': 'https://a.test/x.js' + } + ], + 'fonts': { + 'measureCalls': 200, + 'distinctFonts': 96, + 'script': 'https://a.test/f.js' + }, + 'webgl': {'debugInfo': true, 'script': ''}, + 'audio': {'offline': 1, 'oscillators': 1, 'rendered': 1, 'script': ''}, + 'rtc': { + 'connections': 1, + 'dataChannels': 1, + 'offers': 1, + 'getUserMedia': 0, + 'script': '' + }, + 'enumerateDevices': 1, + 'battery': 0, + 'clipboardRead': 0, + 'prompts': [ + { + 'api': 'notifications', + 'atMs': 1500, + 'userActive': false, + 'script': 's' + } + ], + })) as Present; + final s = r.value; + expect(r.source, DataSource.browser); + expect(s.distinctFonts, 96); + expect(s.canvas.single.chars, 34); + expect(s.prompts.single.at, const Duration(milliseconds: 1500)); + expect(s.prompts.single.userActive, false); + expect(s.scripts['fonts'], 'https://a.test/f.js'); + expect(s.webglDebugInfo, true); + }); + + test('a report with the wrong shape fails instead of reading as clean', () { + expect(parseTechniqueReport('[1,2]'), isA>()); + expect(parseTechniqueReport(42), isA>()); + }); +} diff --git a/test/services/html_service_fresh_load_test.dart b/test/services/html_service_fresh_load_test.dart index 0afecac..22cdddd 100644 --- a/test/services/html_service_fresh_load_test.dart +++ b/test/services/html_service_fresh_load_test.dart @@ -4,6 +4,7 @@ import 'dart:async'; import 'package:flutter_test/flutter_test.dart'; import 'package:view_source_vibe/models/inspection/browser_snapshot.dart'; import 'package:view_source_vibe/models/inspection/observed.dart'; +import 'package:view_source_vibe/models/inspection/technique_signals.dart'; import 'package:view_source_vibe/models/settings.dart'; import 'package:view_source_vibe/services/collectors/fresh_load_measurer.dart'; import 'package:view_source_vibe/services/derive/network_quiet.dart'; @@ -44,8 +45,8 @@ void main() { serverFetcher: fetcher, browserLoader: FakeBrowserLoader(), metadataExtractor: (html, baseUrl, {headers}) async => {}, - browserCollect: (url) async => payload( - url: url, observer: [obs('https://a.test/app.js', tx: 1000)]), + browserCollect: (url) async => + payload(url: url, observer: [obs('https://a.test/app.js', tx: 1000)]), nativeCookies: (u) async => [], browserPlatform: BrowserPlatform.ios, browserPoll: NetworkQuietPolicy.off, @@ -106,8 +107,7 @@ void main() { expect(page.differenceNote, contains('cookie or consent wall')); }); - test('the live browser screenshot is taken once the page loaded', - () async { + test('the live browser screenshot is taken once the page loaded', () async { service = make(const NoFreshLoad(), screenshot: () async => Uint8List.fromList([9])); await load('https://a.test/'); @@ -157,4 +157,47 @@ void main() { expect(f, isA>()); expect((f as Unavailable).reason, UnavailableReason.unsupportedPlatform); }); + + test('the fresh load stores the technique signals', () async { + final m = FakeMeasurer((url) async => FreshLoadRaw( + payload( + url: '$url', observer: [obs('https://a.test/app.js', tx: 1000)]), + techniques: '{"v":1,"canvas":[],"fonts":{"measureCalls":0,' + '"distinctFonts":0},"clipboardRead":1}')); + service = make(m); + await load('https://a.test/'); + await service.measureFreshLoad(); + final t = store.current.freshTechniques; + expect(t, isA>()); + expect((t as Present).value.clipboardRead, 1); + }); + + test('before a fresh load the signals are unavailable with the hint', + () async { + service = make(const NoFreshLoad()); + await load('https://a.test/'); + final t = store.current.freshTechniques as Unavailable; + expect(t.reason, UnavailableReason.notApplicable); + expect(t.detail, 'Run a fresh load to check'); + }); + + test('pending while the load runs, unsupported where there is no browser', + () async { + final gate = Completer(); + service = make(FakeMeasurer((_) => gate.future)); + await load('https://a.test/'); + final run = service.measureFreshLoad(); + await pumpEventQueue(); + expect(store.current.freshTechniques, isA>()); + gate.completeError(StateError('offline')); + await run; + expect(store.current.freshTechniques, isA>()); + service.dispose(); + store = InspectionStore(); + service = make(const NoFreshLoad()); + await load('https://a.test/'); + await service.measureFreshLoad(); + expect((store.current.freshTechniques as Unavailable).reason, + UnavailableReason.unsupportedPlatform); + }); } From ee165ce30b44db5cd562bbc39c2dc9ff15c9963d Mon Sep 17 00:00:00 2001 From: Wouter van Wijk Date: Tue, 17 Nov 2026 22:59:37 +0100 Subject: [PATCH 04/12] Detect identifiers shared between parties and third-party files without SRI Co-Authored-By: Claude Opus 5.5 --- lib/services/derive/techniques.dart | 85 ++++++++++++++++++++++ lib/services/metadata_parser.dart | 45 ++++++++++++ test/derive/techniques_test.dart | 106 ++++++++++++++++++++++++++++ 3 files changed, 236 insertions(+) diff --git a/lib/services/derive/techniques.dart b/lib/services/derive/techniques.dart index 47158a7..e16e874 100644 --- a/lib/services/derive/techniques.dart +++ b/lib/services/derive/techniques.dart @@ -1,5 +1,7 @@ +import '../../models/inspection/browser_snapshot.dart'; import '../../models/inspection/observed.dart'; import '../../models/inspection/technique_signals.dart'; +import '../../utils/site.dart'; // Thresholds. Canvas and font numbers follow the detection heuristics of // Englehardt and Narayanan, "Online Tracking: A 1-million-site Measurement @@ -12,6 +14,15 @@ const int kCanvasMinChars = 10; const int kCanvasMinColors = 2; const int kFontProbeMinFonts = 50; +// Shared identifiers: this app's own choices. A cookie value counts as an +// identifier only when it is long and varied enough not to be a flag, a +// language, a timestamp or a counter. +const int kIdMinLength = 16; +const int kIdMinDistinctChars = 8; +const int kIdMinParties = 2; +const int kIdMaxFindings = 10; +const int kListedItems = 5; + enum TechniqueSeverity { high, medium, low } /// An indication from what a page called, not proof of intent. @@ -155,3 +166,77 @@ List judgeSignals(TechniqueSignals s) { } return out; } + +/// Cookies whose value shows up in requests to other registrable domains. +/// Values are compared in memory only: no finding holds a value or part of one. +List findSharedIdentifiers(BrowserSnapshot b) { + final cookies = b.cookies; + final resources = b.resources; + if (cookies is! Present> || + resources is! Present>) { + return const []; + } + final page = registrableDomain(b.url.host); + // Each request to another site: its registrable domain and url. + final requests = <(String, String)>[]; + for (final r in resources.value) { + final host = Uri.tryParse(r.url)?.host ?? ''; + if (host.isEmpty) continue; + final domain = registrableDomain(host); + if (domain != page) requests.add((domain, r.url)); + } + final out = []; + final seen = {}; + for (final c in cookies.value) { + if (out.length >= kIdMaxFindings) break; + final v = c.value; + if (v.length < kIdMinLength || + v.split('').toSet().length < kIdMinDistinctChars || + !seen.add(c.name)) { + continue; + } + final encoded = Uri.encodeComponent(v); + final domains = { + for (final (domain, url) in requests) + if (url.contains(v) || url.contains(encoded)) domain + }.toList() + ..sort(); + if (domains.length < kIdMinParties) continue; + final listed = domains.take(kListedItems).toList(); + out.add(TechniqueFinding( + id: 'id-sharing', + severity: TechniqueSeverity.medium, + title: 'The identifier in cookie ${c.name} is sent to ' + '${domains.length} other parties', + detail: 'The value of this cookie appears in requests to ' + '${listed.join(', ')}' + '${domains.length > listed.length ? ' and more' : ''}. ' + 'That can let them recognise the same visitor.', + scripts: listed, + )); + } + return out; +} + +/// Third-party scripts and stylesheets without an integrity attribute, from +/// `metadata['subresourceIntegrity']`. +TechniqueFinding? findMissingIntegrity(Map metadata) { + final sri = metadata['subresourceIntegrity']; + if (sri is! Map) return null; + final without = sri['without']; + final total = sri['thirdParty']; + if (without is! List || without.isEmpty || total is! int) return null; + final hosts = {}; + for (final u in without) { + final h = Uri.tryParse('$u')?.host ?? ''; + if (h.isNotEmpty) hosts.add(h); + } + return TechniqueFinding( + id: 'no-sri', + severity: TechniqueSeverity.low, + title: '${without.length} of $total third-party scripts and stylesheets ' + 'have no integrity check', + detail: 'Files loaded from ${hosts.take(kListedItems).join(', ')} are ' + 'not checked against a hash, so a changed file would run unnoticed.', + ); +} diff --git a/lib/services/metadata_parser.dart b/lib/services/metadata_parser.dart index 3c8400b..2bf1218 100644 --- a/lib/services/metadata_parser.dart +++ b/lib/services/metadata_parser.dart @@ -3,6 +3,7 @@ import 'dart:convert'; import 'package:html/parser.dart' as html_parser; import 'package:html/dom.dart' as dom; import 'package:xml/xml.dart' as xml; +import 'package:view_source_vibe/utils/site.dart'; /// Extract metadata from the file content. Service/tracker detection is not /// part of it (see derive/detections.dart); [headers] is kept for callers. @@ -43,6 +44,10 @@ Map _extractMetadataInternal(Map args) { 'videos': >[], }, 'article': {}, + 'subresourceIntegrity': { + 'thirdParty': 0, + 'without': [], + }, }; // Determine if we should use HTML or XML parser @@ -243,6 +248,8 @@ void _extractFromHtml( } } + _collectIntegrity(doc, metadata, baseUrl); + // Scripts final scripts = doc.querySelectorAll('script[src]'); for (final script in scripts) { @@ -574,6 +581,44 @@ void _processJsonLdObject( } } +/// Third-party ` + + + +'''; + final m = extractMetadataSync(html, 'https://shop.test/page'); + expect(m['subresourceIntegrity'], { + 'thirdParty': 3, + 'without': [ + 'https://cdn.one.test/b.js', + 'https://cdn.two.test/c.css', + ], + }); + final f = findMissingIntegrity(m)!; + expect(f.id, 'no-sri'); + expect(f.severity, TechniqueSeverity.low); + expect(f.title, + '2 of 3 third-party scripts and stylesheets have no integrity check'); + expect(f.detail, contains('cdn.one.test')); + }); + + test('a sibling subdomain is the same site', () { + expect( + judge(''), isNull); + }); + + test('nothing without integrity gives no finding', () { + expect( + judge(''), + isNull); + expect(findMissingIntegrity(const {}), isNull); + }); + }); } From 16d4bbac827f706139231875c58e6cb3e517bb38 Mon Sep 17 00:00:00 2001 From: Wouter van Wijk Date: Tue, 17 Nov 2026 23:06:32 +0100 Subject: [PATCH 05/12] Detect trackers hidden behind a first-party hostname (CNAME cloaking) Co-Authored-By: Claude Opus 5.5 --- PRIVACY.md | 2 +- lib/services/cname_cloaking_service.dart | 101 ++++++++++++++++++ lib/services/html_service.dart | 74 ++++++++++++- lib/services/sovereignty_service.dart | 8 +- .../services/cname_cloaking_service_test.dart | 80 ++++++++++++++ test/services/cname_cloaking_state_test.dart | 71 ++++++++++++ test/services/sovereignty_service_test.dart | 15 +++ 7 files changed, 347 insertions(+), 4 deletions(-) create mode 100644 lib/services/cname_cloaking_service.dart create mode 100644 test/services/cname_cloaking_service_test.dart create mode 100644 test/services/cname_cloaking_state_test.dart diff --git a/PRIVACY.md b/PRIVACY.md index d974d8f..df69eb7 100644 --- a/PRIVACY.md +++ b/PRIVACY.md @@ -21,7 +21,7 @@ The application contacts: 1. **The site you inspect**: the page itself (in the in-app browser and with a separate request by the app), its redirects, `robots.txt`, `sitemap.xml` and, once the Sovereignty tab has been opened, `/.well-known/security.txt`. 2. **The hosts that page loads files from**: the in-app browser loads them like any browser. For files whose size the browser does not report, the app requests the file once more to measure it. Known tracking pixels, and third-party images that look like pings, are not requested again. If you start a "fresh load", the app loads the page and its third-party files again in an off-screen browser. 3. **RIPEstat (stat.ripe.net, RIPE NCC, Netherlands)**: the IP addresses of the hosts a page uses, to look up their network and country. Only after you open the Sovereignty tab. Once opened, the analysis also runs for the pages you open next during that session. -4. **A DNS-over-HTTPS resolver** (doh.libredns.gr, Greece; dns.google, United States, as fallback): the domain name of the site, to read its DNS records. Only after you open the Sovereignty tab, as above. +4. **A DNS-over-HTTPS resolver** (doh.libredns.gr, Greece; dns.google, United States, as fallback): the domain name of the site, to read its DNS records, and the host names on the site's own domain that the page loads (at most 20), to see whether any is an alias for a tracker. Only after you open the Sovereignty tab, as above, or the Privacy tab. 5. **Your device's DNS resolver** (usually your internet provider's or the one set in your operating system): the app looks up the IP address of every host a page uses, so this resolver sees those host names. The in-app browser also uses it for every page it loads, whether or not you open the Sovereignty tab; the app's own lookups of all hosts a page uses happen only after you open it, as above. 6. **DuckDuckGo's icon service (icons.duckduckgo.com)**: the domain names of services detected on a page, to show their logos. Logos are cached for 30 days. diff --git a/lib/services/cname_cloaking_service.dart b/lib/services/cname_cloaking_service.dart new file mode 100644 index 0000000..e30aa52 --- /dev/null +++ b/lib/services/cname_cloaking_service.dart @@ -0,0 +1,101 @@ +import 'dart:async'; + +import '../utils/site.dart'; +import 'detection/catalog.dart'; +import 'sovereignty_service.dart'; + +/// A host on the page's own site whose DNS name is an alias for a tracker. +class CnameAlias { + final String host; // metrics.shop.test + final String target; // shop.sc.omtrdc.net + final String tracker; // catalogue entity name + const CnameAlias(this.host, this.target, this.tracker); + + Map toJson() => + {'host': host, 'target': target, 'tracker': tracker}; +} + +class CloakingResult { + final List aliases; + final List checked; + + /// Hosts whose lookup failed or timed out. + final List notChecked; + const CloakingResult({ + this.aliases = const [], + this.checked = const [], + this.notChecked = const [], + }); +} + +/// CNAME targets of a host: null when the lookup failed, empty when it has none. +typedef CnameLookup = Future?> Function(String host); + +/// The app's own DNS-over-HTTPS lookup of CNAME records. +Future?> dohCnameLookup(String host) => + SovereigntyService.dnsQuery(host, 5); + +const int kCloakingMaxHosts = 20; +const int kCloakingParallel = 4; + +/// Hosts of [resourceHosts] on the page's own site (same registrable domain, +/// not the page host itself), at most [max]; each is looked up once. A lookup +/// that fails, throws or exceeds [lookupTimeout] lists that host as not +/// checked and does not stop the others. +Future findCloakedTrackers( + Iterable resourceHosts, { + required String pageHost, + required CnameLookup lookup, + int max = kCloakingMaxHosts, + Duration lookupTimeout = const Duration(seconds: 12), +}) async { + final page = pageHost.toLowerCase(); + final hosts = []; + for (final raw in resourceHosts) { + final h = raw.toLowerCase(); + if (h.isEmpty || h == page || !sameSite(h, page) || hosts.contains(h)) { + continue; + } + hosts.add(h); + if (hosts.length == max) break; + } + + Future?> safeLookup(String h) async { + try { + return await lookup(h).timeout(lookupTimeout); + } catch (_) { + return null; + } + } + + final results = ?>{}; + for (var i = 0; i < hosts.length; i += kCloakingParallel) { + final batch = hosts.skip(i).take(kCloakingParallel).toList(); + final answers = await Future.wait(batch.map(safeLookup)); + for (var j = 0; j < batch.length; j++) { + results[batch[j]] = answers[j]; + } + } + + final aliases = []; + final checked = []; + final notChecked = []; + for (final h in hosts) { + final targets = results[h]; + if (targets == null) { + notChecked.add(h); + continue; + } + checked.add(h); + for (final t in targets) { + if (sameSite(t, page)) continue; + final e = entityForHost(t); + if (e != null && e.category.isTracking) { + aliases.add(CnameAlias(h, t, e.name)); + break; + } + } + } + return CloakingResult( + aliases: aliases, checked: checked, notChecked: notChecked); +} diff --git a/lib/services/html_service.dart b/lib/services/html_service.dart index b185000..4685fdd 100644 --- a/lib/services/html_service.dart +++ b/lib/services/html_service.dart @@ -14,6 +14,7 @@ import 'package:view_source_vibe/utils/font_url.dart'; import 'package:view_source_vibe/services/source_viewer_editor.dart'; import 'package:view_source_vibe/services/jurisdiction.dart'; import 'package:view_source_vibe/services/sovereignty_service.dart'; +import 'package:view_source_vibe/services/cname_cloaking_service.dart'; import 'package:view_source_vibe/services/file_type_detector.dart'; import 'package:view_source_vibe/services/app_state_service.dart'; import 'package:view_source_vibe/models/settings.dart'; @@ -224,6 +225,13 @@ class HtmlService extends ChangeNotifier { String? _sovereigntyError; int _sovereigntyWatchers = 0; bool _sovereigntyScheduled = false; + + final CnameLookup _cnameLookup; + int _cloakingWatchers = 0; + bool _cloakingRunning = false; + int? _cloakingGen; + CloakingResult? _cloakingResult; + Observed? _cloakingPresent; String? _sovereigntyInputKey; Timer? _sovereigntyTimer; final Duration _sovereigntyDebounce; @@ -1011,6 +1019,67 @@ class HtmlService extends ChangeNotifier { /// The URL the analysis is about: where the browser is, else where the /// app's own request ended, else what was asked for. From the store only. + /// CNAME cloaking check of the current page: Pending until a view watches + /// and the page has settled. + Observed get cnameCloaking { + final current = store.current; + if (current.target is! UrlTarget) { + return const Unavailable(UnavailableReason.notApplicable, + 'Load a URL to check its hostnames.'); + } + final result = _cloakingResult; + if (result == null || _cloakingGen != current.generation) { + return const Pending(); + } + final cached = _cloakingPresent; + if (cached is Present && identical(cached.value, result)) { + return cached; + } + return _cloakingPresent = Present(result, + source: DataSource.server, provenance: Provenance.derived); + } + + /// A view calls this when it appears (+1) and disappears (-1). The lookups + /// run once per navigation, after the page settled, only while watched. + void watchCloaking(bool watching) { + _cloakingWatchers = + (_cloakingWatchers + (watching ? 1 : -1)).clamp(0, 1 << 20); + if (watching) _scheduleCloaking(); + } + + void _scheduleCloaking() { + if (_disposed || _cloakingWatchers == 0 || _cloakingRunning) return; + final current = store.current; + final b = current.browser; + if (!current.browserSettled || + b is! Present || + _cloakingGen == current.generation || + (b.value.url.scheme != 'http' && b.value.url.scheme != 'https')) { + return; + } + _cloakingRunning = true; + _cloakingGen = current.generation; + scheduleMicrotask(() => _runCloaking(current.generation, b.value.url)); + } + + Future _runCloaking(int gen, Uri url) async { + try { + final hosts = SovereigntyService.browserResources(store.current) + .map((r) => Uri.tryParse(r.url)?.host ?? '') + .where((h) => h.isNotEmpty); + final result = await findCloakedTrackers(hosts, + pageHost: url.host, lookup: _cnameLookup); + if (!_disposed && gen == store.current.generation) { + _cloakingResult = result; + } + } catch (e) { + debugPrint('CNAME cloaking check failed: $e'); + } finally { + _cloakingRunning = false; + notifyListeners(); // also starts the check for a newer navigation + } + } + Uri? get _sovereigntyUri { final current = store.current; final target = current.target; @@ -1629,7 +1698,9 @@ class HtmlService extends ChangeNotifier { NetworkQuietPolicy browserPoll = NetworkQuietPolicy.live, Duration lateCollectDelay = const Duration(seconds: 2), Duration spaSettleDelay = const Duration(milliseconds: 400), - }) : _sovereigntyDebounce = sovereigntyDebounce, + CnameLookup? cnameLookup, + }) : _cnameLookup = cnameLookup ?? dohCnameLookup, + _sovereigntyDebounce = sovereigntyDebounce, _sovereigntyAnalyzer = sovereigntyAnalyzer ?? SovereigntyService.analyze, _detectionScanner = detectionScanner, @@ -2377,6 +2448,7 @@ Technical details: $message'''; if (_disposed) return; super.notifyListeners(); _scheduleSovereignty(); + _scheduleCloaking(); } @override diff --git a/lib/services/sovereignty_service.dart b/lib/services/sovereignty_service.dart index 10963ac..d7ac16c 100644 --- a/lib/services/sovereignty_service.dart +++ b/lib/services/sovereignty_service.dart @@ -705,7 +705,9 @@ class SovereigntyService { final domain = registrableDomain(mainHost); Future?> dnsQuery(String name, int type) => - domain.isEmpty ? Future.value(null) : _dnsQuery(name, type); + domain.isEmpty + ? Future.value(null) + : SovereigntyService.dnsQuery(name, type); final lookups = await Future.wait([ dnsQuery(domain, 2), // NS dnsQuery(domain, 15), // MX @@ -1532,7 +1534,8 @@ class SovereigntyService { /// DNS-over-HTTPS (RFC 8484 wire format). /// Returns parsed records (see [parseDnsRecords]), null on failure. - static Future?> _dnsQuery(String domain, int type) async { + /// Public so other analyses (CNAME cloaking) reuse the same resolvers. + static Future?> dnsQuery(String domain, int type) async { final dns = base64Url.encode(buildDnsQuery(domain, type)).replaceAll('=', ''); for (final resolver in _dohResolvers) { @@ -1640,6 +1643,7 @@ class SovereigntyService { 16 => _txt(d.sublist(rdata, rdata + rdlen)), 257 => _caa(d.sublist(rdata, rdata + rdlen)), 43 => 'key tag ${u16(rdata)}', + 5 => readName(rdata).$1.toLowerCase(), _ => readName(rdata).$1, }); } diff --git a/test/services/cname_cloaking_service_test.dart b/test/services/cname_cloaking_service_test.dart new file mode 100644 index 0000000..d33beb9 --- /dev/null +++ b/test/services/cname_cloaking_service_test.dart @@ -0,0 +1,80 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:view_source_vibe/services/cname_cloaking_service.dart'; + +void main() { + test('a first-party host that is an alias for a tracker is found', () async { + final r = await findCloakedTrackers([ + 'metrics.shop.test', + 'cdn.shop.test', + 'www.shop.test', + 'ads.other.test' + ], + pageHost: 'www.shop.test', + lookup: (h) async => switch (h) { + 'metrics.shop.test' => ['shop.sc.omtrdc.net'], + 'cdn.shop.test' => ['d111.cloudfront.net'], + _ => const [], + }); + expect(r.checked, ['metrics.shop.test', 'cdn.shop.test']); + expect(r.notChecked, isEmpty); + expect(r.aliases.single.host, 'metrics.shop.test'); + expect(r.aliases.single.target, 'shop.sc.omtrdc.net'); + expect(r.aliases.single.tracker, 'Adobe Analytics'); + }); + + test('a failed lookup is listed as not checked, the rest still runs', + () async { + final r = await findCloakedTrackers(['a.shop.test', 'b.shop.test'], + pageHost: 'www.shop.test', + lookup: (h) async => h == 'a.shop.test' ? null : ['x.omtrdc.net']); + expect(r.notChecked, ['a.shop.test']); + expect(r.checked, ['b.shop.test']); + expect(r.aliases.single.host, 'b.shop.test'); + }); + + test('a lookup that throws is also not checked, not fatal', () async { + final r = await findCloakedTrackers(['a.shop.test', 'b.shop.test'], + pageHost: 'www.shop.test', + lookup: (h) async => + h == 'a.shop.test' ? throw Exception('boom') : ['x.omtrdc.net']); + expect(r.notChecked, ['a.shop.test']); + expect(r.aliases.single.host, 'b.shop.test'); + }); + + test('a lookup that hangs does not stop the others', () async { + final r = await findCloakedTrackers(['a.shop.test', 'b.shop.test'], + pageHost: 'www.shop.test', + lookupTimeout: const Duration(milliseconds: 20), + lookup: (h) => h == 'a.shop.test' + ? Future.delayed(const Duration(seconds: 5), () => []) + : Future.value(['x.omtrdc.net'])); + expect(r.notChecked, ['a.shop.test']); + expect(r.aliases.single.host, 'b.shop.test'); + }); + + test('an alias to the same site or to an unknown host is not reported', + () async { + final r = await findCloakedTrackers(['a.shop.test', 'b.shop.test'], + pageHost: 'www.shop.test', + lookup: (h) async => + h == 'a.shop.test' ? ['edge.shop.test'] : ['x.unknown-cdn.test']); + expect(r.aliases, isEmpty); + expect(r.checked, hasLength(2)); + }); + + test('at most 20 hosts are looked up, each once', () async { + final looked = []; + final hosts = [ + for (var i = 0; i < 30; i++) 'h$i.shop.test', + 'h0.shop.test', + ]; + final r = await findCloakedTrackers(hosts, pageHost: 'www.shop.test', + lookup: (h) async { + looked.add(h); + return const []; + }); + expect(r.checked, hasLength(20)); + expect(looked, hasLength(20)); + expect(looked.toSet(), hasLength(20)); + }); +} diff --git a/test/services/cname_cloaking_state_test.dart b/test/services/cname_cloaking_state_test.dart new file mode 100644 index 0000000..7e4f1f1 --- /dev/null +++ b/test/services/cname_cloaking_state_test.dart @@ -0,0 +1,71 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:view_source_vibe/models/inspection/observed.dart'; +import 'package:view_source_vibe/services/cname_cloaking_service.dart'; +import 'package:view_source_vibe/services/html_service.dart'; +import 'package:view_source_vibe/services/inspection_store.dart'; + +import '../fixtures/inspection/builders.dart'; +import '../fixtures/inspection/full_page.dart'; +import '../fixtures/inspection/pump.dart'; + +void main() { + late InspectionStore store; + late HtmlService service; + late List looked; + + final page = fullPage().copyWith( + browser: browser(browserSnapshot( + url: 'https://www.example.com/', + dom: fullPageDomHtml, + resources: [ + resource('https://metrics.example.com/s.js', 'script'), + resource('https://static.example.com/a.css', 'css'), + ], + )), + ); + + setUp(() { + store = InspectionStore(); + looked = []; + service = HtmlService( + store: store, + serverFetcher: SilentServerFetcher(), + browserLoader: SilentBrowserLoader(), + metadataExtractor: (html, baseUrl, {headers}) async => {}, + cnameLookup: (h) async { + looked.add(h); + return h.startsWith('metrics') ? ['x.sc.omtrdc.net'] : const []; + }, + ); + }); + tearDown(() => service.dispose()); + + test('no lookup while no view watches', () async { + seedStore(store, page); + await pumpEventQueue(); + expect(looked, isEmpty); + expect(service.cnameCloaking, isA>()); + }); + + test('watched and settled: one lookup run, a Present result', () async { + seedStore(store, page); + service.watchCloaking(true); + await pumpEventQueue(); + final state = service.cnameCloaking as Present; + expect(state.value.aliases.single.host, 'metrics.example.com'); + expect(looked.toSet(), {'metrics.example.com', 'static.example.com'}); + final n = looked.length; + store.applyRobots(store.current.generation, const Pending()); + await pumpEventQueue(); + expect(looked.length, n); + }); + + test('a result for a page we navigated away from is dropped', () async { + seedStore(store, page); + service.watchCloaking(true); + seedStore(store, page); // new generation before the lookups answer + await pumpEventQueue(); + final state = service.cnameCloaking as Present; + expect(state.value.aliases, hasLength(1)); + }); +} diff --git a/test/services/sovereignty_service_test.dart b/test/services/sovereignty_service_test.dart index 5ca3c64..b66d43b 100644 --- a/test/services/sovereignty_service_test.dart +++ b/test/services/sovereignty_service_test.dart @@ -208,6 +208,21 @@ void main() { expect(SovereigntyService.parseDnsRecords(data, 15), isEmpty); }); + test('CNAME answers (type 5) are lower-case host names without the dot', + () { + // metrics.shop.test CNAME Shop.SC.Omtrdc.net. + final data = Uint8List.fromList([ + 0, 0, 0x81, 0x80, 0, 1, 0, 1, 0, 0, 0, 0, + 7, 109, 101, 116, 114, 105, 99, 115, 4, 115, 104, 111, 112, 4, 116, + 101, 115, 116, 0, 0, 5, 0, 1, // question + 0xC0, 0x0C, 0, 5, 0, 1, 0, 0, 0, 60, 0, 0x14, // answer header + 4, 83, 104, 111, 112, 2, 83, 67, 6, 79, 109, 116, 114, 100, 99, 3, + 110, 101, 116, 0, + ]); + expect(SovereigntyService.parseDnsRecords(data, 5), + ['shop.sc.omtrdc.net']); + }); + test('tolerates garbage', () { expect(SovereigntyService.parseDnsRecords(Uint8List(3), 2), isEmpty); }); From c1ab4292cc6655d528832fc7e1d452c4c3401e14 Mon Sep 17 00:00:00 2001 From: Wouter van Wijk Date: Tue, 17 Nov 2026 23:10:49 +0100 Subject: [PATCH 06/12] Fix: CNAME cloaking shows no stale result for the next page; a failed check is Failed Co-Authored-By: Claude Opus 5.5 --- lib/services/cname_cloaking_service.dart | 6 +++ lib/services/html_service.dart | 43 +++++++++++--------- test/services/cname_cloaking_state_test.dart | 28 +++++++++++++ 3 files changed, 58 insertions(+), 19 deletions(-) diff --git a/lib/services/cname_cloaking_service.dart b/lib/services/cname_cloaking_service.dart index e30aa52..e1f8815 100644 --- a/lib/services/cname_cloaking_service.dart +++ b/lib/services/cname_cloaking_service.dart @@ -31,6 +31,12 @@ class CloakingResult { /// CNAME targets of a host: null when the lookup failed, empty when it has none. typedef CnameLookup = Future?> Function(String host); +/// [findCloakedTrackers] without its tuning knobs (a seam for tests). +typedef CloakingFinder = Future Function( + Iterable resourceHosts, + {required String pageHost, + required CnameLookup lookup}); + /// The app's own DNS-over-HTTPS lookup of CNAME records. Future?> dohCnameLookup(String host) => SovereigntyService.dnsQuery(host, 5); diff --git a/lib/services/html_service.dart b/lib/services/html_service.dart index 4685fdd..7d65491 100644 --- a/lib/services/html_service.dart +++ b/lib/services/html_service.dart @@ -225,18 +225,20 @@ class HtmlService extends ChangeNotifier { String? _sovereigntyError; int _sovereigntyWatchers = 0; bool _sovereigntyScheduled = false; - - final CnameLookup _cnameLookup; - int _cloakingWatchers = 0; - bool _cloakingRunning = false; - int? _cloakingGen; - CloakingResult? _cloakingResult; - Observed? _cloakingPresent; String? _sovereigntyInputKey; Timer? _sovereigntyTimer; final Duration _sovereigntyDebounce; Observed? _sovereigntyPresent; + // CNAME cloaking check (see watchCloaking). + final CnameLookup _cnameLookup; + final CloakingFinder _cloakingFinder; + int _cloakingWatchers = 0; + bool _cloakingRunning = false; + int? _cloakingGen; // navigation the check was started for + int? _cloakingResultGen; // navigation the stored result belongs to + Observed? _cloakingState; + // Metadata state Map? _pageMetadata; @@ -1017,8 +1019,6 @@ class HtmlService extends ChangeNotifier { if (watching) _scheduleSovereignty(); } - /// The URL the analysis is about: where the browser is, else where the - /// app's own request ended, else what was asked for. From the store only. /// CNAME cloaking check of the current page: Pending until a view watches /// and the page has settled. Observed get cnameCloaking { @@ -1027,16 +1027,11 @@ class HtmlService extends ChangeNotifier { return const Unavailable(UnavailableReason.notApplicable, 'Load a URL to check its hostnames.'); } - final result = _cloakingResult; - if (result == null || _cloakingGen != current.generation) { + final state = _cloakingState; + if (state == null || _cloakingResultGen != current.generation) { return const Pending(); } - final cached = _cloakingPresent; - if (cached is Present && identical(cached.value, result)) { - return cached; - } - return _cloakingPresent = Present(result, - source: DataSource.server, provenance: Provenance.derived); + return state; } /// A view calls this when it appears (+1) and disappears (-1). The lookups @@ -1067,19 +1062,27 @@ class HtmlService extends ChangeNotifier { final hosts = SovereigntyService.browserResources(store.current) .map((r) => Uri.tryParse(r.url)?.host ?? '') .where((h) => h.isNotEmpty); - final result = await findCloakedTrackers(hosts, + final result = await _cloakingFinder(hosts, pageHost: url.host, lookup: _cnameLookup); if (!_disposed && gen == store.current.generation) { - _cloakingResult = result; + _cloakingResultGen = gen; + _cloakingState = Present(result, + source: DataSource.server, provenance: Provenance.derived); } } catch (e) { debugPrint('CNAME cloaking check failed: $e'); + if (!_disposed && gen == store.current.generation) { + _cloakingResultGen = gen; + _cloakingState = Failed('$e'); + } } finally { _cloakingRunning = false; notifyListeners(); // also starts the check for a newer navigation } } + /// The URL the analysis is about: where the browser is, else where the + /// app's own request ended, else what was asked for. From the store only. Uri? get _sovereigntyUri { final current = store.current; final target = current.target; @@ -1699,7 +1702,9 @@ class HtmlService extends ChangeNotifier { Duration lateCollectDelay = const Duration(seconds: 2), Duration spaSettleDelay = const Duration(milliseconds: 400), CnameLookup? cnameLookup, + CloakingFinder? cloakingFinder, }) : _cnameLookup = cnameLookup ?? dohCnameLookup, + _cloakingFinder = cloakingFinder ?? findCloakedTrackers, _sovereigntyDebounce = sovereigntyDebounce, _sovereigntyAnalyzer = sovereigntyAnalyzer ?? SovereigntyService.analyze, diff --git a/test/services/cname_cloaking_state_test.dart b/test/services/cname_cloaking_state_test.dart index 7e4f1f1..7334568 100644 --- a/test/services/cname_cloaking_state_test.dart +++ b/test/services/cname_cloaking_state_test.dart @@ -1,3 +1,5 @@ +import 'dart:async'; + import 'package:flutter_test/flutter_test.dart'; import 'package:view_source_vibe/models/inspection/observed.dart'; import 'package:view_source_vibe/services/cname_cloaking_service.dart'; @@ -12,6 +14,8 @@ void main() { late InspectionStore store; late HtmlService service; late List looked; + var hang = false; + var finderThrows = false; final page = fullPage().copyWith( browser: browser(browserSnapshot( @@ -32,8 +36,13 @@ void main() { serverFetcher: SilentServerFetcher(), browserLoader: SilentBrowserLoader(), metadataExtractor: (html, baseUrl, {headers}) async => {}, + cloakingFinder: (hosts, {required pageHost, required lookup}) { + if (finderThrows) throw StateError('boom'); + return findCloakedTrackers(hosts, pageHost: pageHost, lookup: lookup); + }, cnameLookup: (h) async { looked.add(h); + if (hang) return Completer?>().future; return h.startsWith('metrics') ? ['x.sc.omtrdc.net'] : const []; }, ); @@ -68,4 +77,23 @@ void main() { final state = service.cnameCloaking as Present; expect(state.value.aliases, hasLength(1)); }); + + test('the next page does not show the previous page result', () async { + seedStore(store, page); + service.watchCloaking(true); + await pumpEventQueue(); + expect(service.cnameCloaking, isA>()); + hang = true; + seedStore(store, page); + await pumpEventQueue(); + expect(service.cnameCloaking, isA>()); + }); + + test('a check that throws is Failed, not Pending forever', () async { + finderThrows = true; + seedStore(store, page); + service.watchCloaking(true); + await pumpEventQueue(); + expect(service.cnameCloaking, isA>()); + }); } From 267c62cef555ed3ba338a8dc32b43f16c65bf92f Mon Sep 17 00:00:00 2001 From: Wouter van Wijk Date: Tue, 17 Nov 2026 23:17:28 +0100 Subject: [PATCH 07/12] Privacy tab: suspicious techniques (fingerprinting, shared identifiers, cloaking, SRI) Co-Authored-By: Claude Opus 5.5 --- lib/services/derive/techniques.dart | 127 ++++++++++++++++++ lib/services/html_service.dart | 28 ++++ lib/services/inspection_export_service.dart | 11 ++ lib/widgets/privacy_view.dart | 27 +++- lib/widgets/techniques_section.dart | 136 ++++++++++++++++++++ test/derive/techniques_test.dart | 124 ++++++++++++++++++ test/inspection_export_service_test.dart | 22 ++++ test/widgets/techniques_section_test.dart | 98 ++++++++++++++ 8 files changed, 568 insertions(+), 5 deletions(-) create mode 100644 lib/widgets/techniques_section.dart create mode 100644 test/widgets/techniques_section_test.dart diff --git a/lib/services/derive/techniques.dart b/lib/services/derive/techniques.dart index e16e874..95cc69b 100644 --- a/lib/services/derive/techniques.dart +++ b/lib/services/derive/techniques.dart @@ -1,7 +1,9 @@ import '../../models/inspection/browser_snapshot.dart'; import '../../models/inspection/observed.dart'; +import '../../models/inspection/page_inspection.dart'; import '../../models/inspection/technique_signals.dart'; import '../../utils/site.dart'; +import '../cname_cloaking_service.dart'; // Thresholds. Canvas and font numbers follow the detection heuristics of // Englehardt and Narayanan, "Online Tracking: A 1-million-site Measurement @@ -45,6 +47,11 @@ class TechniqueFinding { this.source = DataSource.browser, }); + /// Hosts of [scripts] (a plain host stays as it is), without repeats. + List get hosts => { + for (final u in scripts) _hostOf(u) + }.toList(); + Map toJson() => { 'id': id, 'severity': severity.name, @@ -55,6 +62,11 @@ class TechniqueFinding { }; } +String _hostOf(String url) { + final host = Uri.tryParse(url)?.host ?? ''; + return host.isEmpty ? url : host; +} + List _scripts(Iterable urls) => { for (final u in urls) if (u.isNotEmpty) u @@ -240,3 +252,118 @@ TechniqueFinding? findMissingIntegrity(Map metadata) { 'not checked against a hash, so a changed file would run unnoticed.', ); } + +/// What the Privacy tab shows about suspicious techniques. +class TechniquesModel { + /// High first, then in rule order. + final List findings; + + /// Why the call-based checks have no result (not measured, running, + /// unsupported, failed); null when they ran. + final String? callChecksNote; + + /// Whether the 'Run fresh load' button is offered. + final bool callChecksCanRun; + + /// Hosts whose DNS lookup failed. + final List notChecked; + + /// Why the hostname (CNAME) check has no result yet; null when it ran or + /// does not apply. + final String? cloakingNote; + + /// Every check ran: "no findings" then means none were seen. + final bool complete; + + const TechniquesModel({ + required this.findings, + required this.callChecksNote, + required this.callChecksCanRun, + required this.notChecked, + required this.cloakingNote, + required this.complete, + }); +} + +TechniquesModel deriveTechniques(PageInspection p, + {required Observed cloaking, + Map? metadata}) { + final findings = []; + final isUrl = p.target is UrlTarget; + + final calls = p.freshTechniques; + String? callNote; + var canRun = false; + switch (calls) { + case Present(value: final signals): + findings.addAll(judgeSignals(signals)); + case Pending(): + callNote = 'Fresh load running'; + case Unavailable( + reason: final r, + detail: final d + ): + callNote = d.isNotEmpty ? d : unavailableMessage(r, d); + canRun = isUrl && r != UnavailableReason.unsupportedPlatform; + case Failed(error: final e): + callNote = e; + canRun = isUrl; + } + if (!isUrl && callNote != null) { + callNote = 'A fresh load needs a web address'; + canRun = false; + } + + final browser = p.browser; + if (browser is Present) { + findings.addAll(findSharedIdentifiers(browser.value)); + } + + var notChecked = const []; + String? cloakingNote; + switch (cloaking) { + case Present(value: final r): + notChecked = r.notChecked; + for (final a in r.aliases) { + findings.add(TechniqueFinding( + id: 'cname-cloaking', + severity: TechniqueSeverity.medium, + title: '${a.host} is an alias for ${a.tracker}', + detail: 'The name ${a.host} looks like part of this site but its ' + 'DNS record points to ${a.target}, which belongs to a tracker. ' + 'Blockers that look at hostnames would miss it.', + scripts: [a.host], + source: DataSource.server, + )); + } + case Pending(): + cloakingNote = 'Checking hostnames'; + case Unavailable(reason: final r, detail: final d): + // A local file has no hostnames: nothing to check. + if (r != UnavailableReason.notApplicable) { + cloakingNote = d.isNotEmpty ? d : unavailableMessage(r, d); + } + case Failed(error: final e): + cloakingNote = 'Hostname check failed: $e'; + } + + final sri = metadata == null ? null : findMissingIntegrity(metadata); + if (sri != null) findings.add(sri); + + // Stable: high before medium before low, rule order within a severity. + final ordered = [ + for (final sev in TechniqueSeverity.values) + ...findings.where((f) => f.severity == sev) + ]; + return TechniquesModel( + findings: ordered, + callChecksNote: callNote, + callChecksCanRun: canRun, + notChecked: notChecked, + cloakingNote: cloakingNote, + complete: callNote == null && + cloakingNote == null && + notChecked.isEmpty && + metadata != null, + ); +} diff --git a/lib/services/html_service.dart b/lib/services/html_service.dart index 7d65491..28850ba 100644 --- a/lib/services/html_service.dart +++ b/lib/services/html_service.dart @@ -47,6 +47,7 @@ import 'package:view_source_vibe/services/derive/media.dart'; import 'package:view_source_vibe/services/derive/network_quiet.dart'; import 'package:view_source_vibe/services/derive/metadata.dart'; import 'package:view_source_vibe/services/derive/metadata_view.dart'; +import 'package:view_source_vibe/services/derive/techniques.dart'; import 'package:view_source_vibe/services/derive/estimate_targets.dart'; import 'package:view_source_vibe/services/derive/page_size.dart'; import 'package:view_source_vibe/services/derive/dom_tree.dart'; @@ -1034,6 +1035,33 @@ class HtmlService extends ChangeNotifier { return state; } + TechniquesModel? _techniquesView; + PageInspection? _techniquesViewFor; + Observed? _techniquesViewMetadata; + Observed? _techniquesViewCloaking; + + /// The Privacy tab's suspicious techniques (see [deriveTechniques]); cached + /// until the inspection, the metadata or the cloaking check changes. + TechniquesModel get techniquesView { + final current = store.current; + final metadata = metadataState; + final cloaking = cnameCloaking; + if (_techniquesView == null || + !identical(_techniquesViewFor, current) || + !identical(_techniquesViewMetadata, metadata) || + !identical(_techniquesViewCloaking, cloaking)) { + _techniquesViewFor = current; + _techniquesViewMetadata = metadata; + _techniquesViewCloaking = cloaking; + _techniquesView = deriveTechniques(current, + cloaking: cloaking, + metadata: metadata is Present + ? metadata.value.data + : null); + } + return _techniquesView!; + } + /// A view calls this when it appears (+1) and disappears (-1). The lookups /// run once per navigation, after the page settled, only while watched. void watchCloaking(bool watching) { diff --git a/lib/services/inspection_export_service.dart b/lib/services/inspection_export_service.dart index ea74b2d..fcaee84 100644 --- a/lib/services/inspection_export_service.dart +++ b/lib/services/inspection_export_service.dart @@ -7,6 +7,7 @@ import 'package:view_source_vibe/models/html_file.dart'; import 'package:view_source_vibe/models/inspection/browser_snapshot.dart'; import 'package:view_source_vibe/models/inspection/observed.dart'; import 'package:view_source_vibe/models/inspection/page_inspection.dart'; +import 'package:view_source_vibe/models/inspection/technique_signals.dart'; import 'package:view_source_vibe/services/derive/cookies_view.dart'; import 'package:view_source_vibe/services/derive/metadata_view.dart'; import 'package:view_source_vibe/services/derive/observed_text.dart'; @@ -179,6 +180,15 @@ class InspectionExportService { 'checked': detections.sources.line, 'value': [for (final d in detections.detections) d.toMap()], }); + final techniques = htmlService.techniquesView; + final techniquesJson = unlessBlocked({ + // Whether the fresh-load checks ran, not their raw counts. + 'callChecks': observedJson( + p.freshTechniques, (_) => 'measured', + source: DataSource.browser), + 'findings': [for (final f in techniques.findings) f.toJson()], + 'notChecked': techniques.notChecked, + }); final sovereigntyJson = observedJson(sovereignty, (r) => r.toJson(), source: DataSource.server, provenance: Provenance.derived); @@ -219,6 +229,7 @@ class InspectionExportService { addJson('network/redirects.json', redirectsJson); addJson('network/timeline.json', timelineJson); addJson('analysis/sovereignty.json', sovereigntyJson); + addJson('analysis/techniques.json', techniquesJson); addJson('analysis/metadata.json', metadataJson); addJson('analysis/detections.json', detectionsJson); diff --git a/lib/widgets/privacy_view.dart b/lib/widgets/privacy_view.dart index 3935b5d..25e7579 100644 --- a/lib/widgets/privacy_view.dart +++ b/lib/widgets/privacy_view.dart @@ -7,6 +7,7 @@ import 'package:view_source_vibe/models/detection_evidence.dart'; import 'package:view_source_vibe/services/detection/engine.dart'; import 'package:view_source_vibe/widgets/service_logo.dart'; import 'package:view_source_vibe/widgets/evidence_view.dart'; +import 'package:view_source_vibe/widgets/techniques_section.dart'; /// Who is tracking you on this page: the tracking categories of /// [HtmlService.detections], with logos. @@ -42,11 +43,25 @@ class PrivacyView extends StatelessWidget { final others = seesVisit(view.detections); if (hits.isEmpty && others.isEmpty) { - return DetectionEmptyState( - view: view, - icon: Icons.verified_user_outlined, - noneTitle: 'No Trackers Detected', - noneText: 'No known trackers found on this page.', + return Scrollbar( + child: SingleChildScrollView( + primary: true, + padding: const EdgeInsets.all(16), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + DetectionEmptyState( + view: view, + icon: Icons.verified_user_outlined, + noneTitle: 'No Trackers Detected', + noneText: 'No known trackers found on this page.', + ), + const SizedBox(height: 24), + const TechniquesSection(), + const SizedBox(height: 80), + ], + ), + ), ); } @@ -111,6 +126,8 @@ class PrivacyView extends StatelessWidget { const SizedBox(height: 8), ...others.map((d) => _TrackerTile(hit: d)), ], + const SizedBox(height: 32), + const TechniquesSection(), const SizedBox(height: 24), DetectionNote(view: view), const SizedBox(height: 80), diff --git a/lib/widgets/techniques_section.dart b/lib/widgets/techniques_section.dart new file mode 100644 index 0000000..67ff13e --- /dev/null +++ b/lib/widgets/techniques_section.dart @@ -0,0 +1,136 @@ +import 'package:material_ui/material_ui.dart'; +import 'package:provider/provider.dart'; +import 'package:view_source_vibe/services/derive/techniques.dart'; +import 'package:view_source_vibe/services/html_service.dart'; + +/// Suspicious techniques the page used (see [deriveTechniques]). Findings are +/// indications from what the page called, not proof of intent. +class TechniquesSection extends StatefulWidget { + const TechniquesSection({super.key}); + + @override + State createState() => _TechniquesSectionState(); +} + +class _TechniquesSectionState extends State { + late final HtmlService _service = context.read(); + bool _watching = false; + + @override + void initState() { + super.initState(); + // The hostname lookups run only while a view shows them. Not from build. + WidgetsBinding.instance.addPostFrameCallback((_) { + if (!mounted) return; + _watching = true; + _service.watchCloaking(true); + }); + } + + @override + void dispose() { + if (_watching) _service.watchCloaking(false); + super.dispose(); + } + + static Color _color(TechniqueSeverity s) => switch (s) { + TechniqueSeverity.high => Colors.red, + TechniqueSeverity.medium => Colors.orange, + TechniqueSeverity.low => Colors.blueGrey, + }; + + @override + Widget build(BuildContext context) { + final model = + context.select((s) => s.techniquesView); + final scheme = Theme.of(context).colorScheme; + final muted = TextStyle(color: scheme.onSurfaceVariant, fontSize: 12); + return Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text('Techniques', + style: Theme.of(context) + .textTheme + .titleMedium + ?.copyWith(fontWeight: FontWeight.bold)), + const SizedBox(height: 8), + for (final f in model.findings) _FindingTile(finding: f), + if (model.callChecksNote != null) + Row( + children: [ + Expanded(child: Text(model.callChecksNote!, style: muted)), + if (model.callChecksCanRun) + FilledButton.tonal( + onPressed: _service.measureFreshLoad, + child: const Text('Run fresh load'), + ), + ], + ), + if (model.cloakingNote != null) Text(model.cloakingNote!, style: muted), + if (model.notChecked.isNotEmpty) + Text('Not checked (lookup failed): ${model.notChecked.join(', ')}', + style: muted), + if (model.findings.isEmpty && model.complete) + const Text('No suspicious techniques seen by the checks that ran.'), + ], + ); + } +} + +class _FindingTile extends StatelessWidget { + final TechniqueFinding finding; + const _FindingTile({required this.finding}); + + @override + Widget build(BuildContext context) { + final scheme = Theme.of(context).colorScheme; + final color = _TechniquesSectionState._color(finding.severity); + return Card( + elevation: 0, + margin: const EdgeInsets.only(bottom: 8), + shape: RoundedRectangleBorder( + side: BorderSide(color: scheme.outlineVariant), + borderRadius: BorderRadius.circular(12), + ), + child: Padding( + padding: const EdgeInsets.all(12), + child: Row( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Padding( + padding: const EdgeInsets.only(top: 5, right: 10), + child: Semantics( + label: '${finding.severity.name} severity', + child: Container( + width: 10, + height: 10, + decoration: + BoxDecoration(color: color, shape: BoxShape.circle), + ), + ), + ), + Expanded( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text(finding.title, + style: const TextStyle(fontWeight: FontWeight.bold)), + const SizedBox(height: 4), + Text(finding.detail, style: const TextStyle(fontSize: 13)), + if (finding.scripts.isNotEmpty) ...[ + const SizedBox(height: 4), + Text( + finding.hosts.join(', '), + style: TextStyle( + fontSize: 12, color: scheme.onSurfaceVariant), + ), + ], + ], + ), + ), + ], + ), + ), + ); + } +} diff --git a/test/derive/techniques_test.dart b/test/derive/techniques_test.dart index 8b21169..2d4fbb7 100644 --- a/test/derive/techniques_test.dart +++ b/test/derive/techniques_test.dart @@ -3,6 +3,7 @@ import 'dart:convert'; import 'package:flutter_test/flutter_test.dart'; import 'package:view_source_vibe/models/inspection/observed.dart'; import 'package:view_source_vibe/models/inspection/technique_signals.dart'; +import 'package:view_source_vibe/services/cname_cloaking_service.dart'; import 'package:view_source_vibe/services/derive/techniques.dart'; import 'package:view_source_vibe/services/metadata_parser.dart'; @@ -330,4 +331,127 @@ void main() { expect(findMissingIntegrity(const {}), isNull); }); }); + + group('deriveTechniques', () { + const id = 'a1b2c3d4e5f6a7b8c9'; + final pageWithSharedId = inspection( + browser: browserSnap( + url: 'https://shop.test/', + cookies: [cookie('_uid', value: id)], + resources: [ + res('https://sync.ads-a.test/p?u=$id'), + res('https://px.ads-b.test/m/$id'), + ])); + + test('without a fresh load the call checks say so, data checks still run', + () { + final m = deriveTechniques(pageWithSharedId, + cloaking: const Pending(), metadata: null); + expect(m.callChecksNote, 'Run a fresh load to check'); + expect(m.callChecksCanRun, isTrue); + expect(m.findings.map((f) => f.id), ['id-sharing']); + }); + + test('unsupported platform: note without a button, data checks still run', + () { + final m = deriveTechniques( + pageWithSharedId.copyWith( + freshTechniques: const Unavailable( + UnavailableReason.unsupportedPlatform, + 'Not supported on this platform')), + cloaking: const Pending(), + metadata: null); + expect(m.callChecksNote, 'Not supported on this platform'); + expect(m.callChecksCanRun, isFalse); + expect(m.findings.map((f) => f.id), ['id-sharing']); + }); + + test('a running fresh load has a note and no button', () { + final m = deriveTechniques( + pageWithSharedId.copyWith(freshTechniques: const Pending()), + cloaking: const Pending(), + metadata: null); + expect(m.callChecksNote, isNotNull); + expect(m.callChecksCanRun, isFalse); + }); + + test('a failed fresh load can be run again', () { + final m = deriveTechniques( + pageWithSharedId.copyWith(freshTechniques: const Failed('boom')), + cloaking: const Pending(), + metadata: null); + expect(m.callChecksNote, contains('boom')); + expect(m.callChecksCanRun, isTrue); + }); + + test('a local file cannot run a fresh load', () { + final m = deriveTechniques(inspection(file: fileSnap('')), + cloaking: const Unavailable(UnavailableReason.notApplicable), + metadata: const {}); + expect(m.callChecksCanRun, isFalse); + }); + + test('findings are ordered by severity, then rule order', () { + final m = deriveTechniques( + pageWithSharedId.copyWith( + freshTechniques: Present( + signals(battery: 1, clipboardRead: 1, distinctFonts: 60), + source: DataSource.browser)), + cloaking: const Pending(), + metadata: { + 'subresourceIntegrity': { + 'thirdParty': 1, + 'without': ['https://cdn.one.test/a.js'] + } + }); + expect(m.callChecksNote, isNull); + expect(m.findings.map((f) => f.id), + ['clipboard-read', 'font-probing', 'id-sharing', 'battery', 'no-sri']); + }); + + test('cloaked hostnames are findings; failed lookups are not checked', () { + final m = deriveTechniques(pageWithSharedId, + cloaking: Present(const CloakingResult( + aliases: [ + CnameAlias( + 'metrics.shop.test', 'shop.sc.omtrdc.net', 'Adobe') + ], + checked: ['metrics.shop.test'], + notChecked: ['x.shop.test']), + source: DataSource.server), + metadata: null); + expect(m.findings.map((f) => f.id), contains('cname-cloaking')); + final f = m.findings.firstWhere((f) => f.id == 'cname-cloaking'); + expect(f.title, contains('metrics.shop.test')); + expect(f.detail, contains('shop.sc.omtrdc.net')); + expect(m.notChecked, ['x.shop.test']); + }); + + test('a failed cloaking check is listed, not silently clean', () { + final m = deriveTechniques(pageWithSharedId, + cloaking: const Failed('dns down'), metadata: null); + expect(m.cloakingNote, contains('dns down')); + }); + + test('complete only when every check ran', () { + final ran = pageWithSharedId.copyWith( + freshTechniques: + Present(signals(), source: DataSource.browser)); + expect( + deriveTechniques(ran, + cloaking: const Present(CloakingResult(), source: DataSource.server), metadata: const {}) + .complete, + isTrue); + expect( + deriveTechniques(ran, + cloaking: const Pending(), metadata: const {}) + .complete, + isFalse); + expect( + deriveTechniques(pageWithSharedId, + cloaking: const Present(CloakingResult(), source: DataSource.server), metadata: const {}) + .complete, + isFalse); + }); + }); } diff --git a/test/inspection_export_service_test.dart b/test/inspection_export_service_test.dart index 53c729c..f14dca6 100644 --- a/test/inspection_export_service_test.dart +++ b/test/inspection_export_service_test.dart @@ -98,6 +98,28 @@ void main() { expect(report, contains('unknown')); }); + test('techniques.json: status of the call checks and the findings', + () async { + final htmlService = HtmlService(); + await htmlService.loadFile(HtmlFile( + name: 'example.html', + path: 'https://example.com', + content: '', + lastModified: DateTime.utc(2026, 4, 29), + size: 13, + isUrl: true, + )); + final bytes = InspectionExportService().buildPackageBytes(htmlService); + final archive = ZipDecoder().decodeBytes(bytes); + final text = + utf8.decode(archive.findFile('analysis/techniques.json')!.readBytes()!); + final json = jsonDecode(text) as Map; + expect(json['callChecks']['status'], 'unavailable'); + expect(json['callChecks']['detail'], 'Run a fresh load to check'); + expect(json['findings'], isEmpty); + expect(json['notChecked'], isEmpty); + }); + test('no debug payload without a browser sync', () async { final htmlService = HtmlService(); addTearDown(htmlService.dispose); diff --git a/test/widgets/techniques_section_test.dart b/test/widgets/techniques_section_test.dart new file mode 100644 index 0000000..ffce22d --- /dev/null +++ b/test/widgets/techniques_section_test.dart @@ -0,0 +1,98 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:view_source_vibe/models/inspection/observed.dart'; +import 'package:view_source_vibe/models/inspection/page_inspection.dart'; +import 'package:view_source_vibe/models/inspection/technique_signals.dart'; +import 'package:view_source_vibe/services/cname_cloaking_service.dart'; +import 'package:view_source_vibe/services/html_service.dart'; +import 'package:view_source_vibe/services/inspection_store.dart'; +import 'package:view_source_vibe/widgets/privacy_view.dart'; + +import '../derive/fixtures.dart' as fx; +import '../fixtures/inspection/pump.dart'; +import '../fixtures/inspection/trackers.dart'; + +void main() { + late InspectionStore store; + late HtmlService service; + + Future show(WidgetTester tester, PageInspection p, + {Observed? techniques}) async { + store = seedStore(InspectionStore(), p); + if (techniques != null) { + store.applyFreshLoad(store.current.generation, + Present(fx.browserSnap(), source: DataSource.browser), + techniques: techniques); + } + service = HtmlService( + store: store, + serverFetcher: SilentServerFetcher(), + browserLoader: SilentBrowserLoader(), + metadataExtractor: (html, baseUrl, {headers}) async => {}, + cloakingFinder: (hosts, {required pageHost, required lookup}) async => + const CloakingResult(), + ); + addTearDown(service.dispose); + // The metadata the page's own checks (SRI) run on. + await tester.runAsync(() => service.loadFile(htmlFileFor(p, content: 'x'))); + await pumpWithProviders(tester, + service: service, child: const PrivacyView()); + await tester.pumpAndSettle(); + } + + testWidgets('the section offers the fresh load when it has not run', + (tester) async { + await show(tester, trackerPage()); + expect(find.text('Techniques'), findsOneWidget); + expect(find.text('Run a fresh load to check'), findsOneWidget); + expect(find.text('Run fresh load'), findsOneWidget); + expect(find.textContaining('No suspicious techniques'), findsNothing); + }); + + testWidgets('unsupported platform: says so, no button', (tester) async { + await show(tester, trackerPage(), + techniques: const Unavailable(UnavailableReason.unsupportedPlatform, + 'Not supported on this platform')); + expect(find.text('Not supported on this platform'), findsOneWidget); + expect(find.text('Run fresh load'), findsNothing); + // The data-derived trackers still show. + expect(find.text('Meta Pixel'), findsOneWidget); + }); + + testWidgets('no findings after all checks ran', (tester) async { + await show(tester, trackerPage(), + techniques: + Present(const TechniqueSignals(), source: DataSource.browser)); + expect(find.text('No suspicious techniques seen by the checks that ran.'), + findsOneWidget); + expect(find.text('Run fresh load'), findsNothing); + }); + + testWidgets('a finding is shown with its title and hosts', (tester) async { + await show(tester, trackerPage(), + techniques: Present(const TechniqueSignals(clipboardRead: 1), + source: DataSource.browser)); + expect(find.text('Reads the clipboard'), findsOneWidget); + expect(find.textContaining('No suspicious techniques'), findsNothing); + }); + + testWidgets('the cloaking check runs while the section shows', + (tester) async { + var calls = 0; + store = seedStore(InspectionStore(), trackerPage()); + service = HtmlService( + store: store, + serverFetcher: SilentServerFetcher(), + browserLoader: SilentBrowserLoader(), + metadataExtractor: (html, baseUrl, {headers}) async => {}, + cloakingFinder: (hosts, {required pageHost, required lookup}) async { + calls++; + return const CloakingResult(); + }, + ); + addTearDown(service.dispose); + await pumpWithProviders(tester, + service: service, child: const PrivacyView()); + await tester.pumpAndSettle(); + expect(calls, 1); + }); +} From c81cc98490679e317952249a5c8c4317d02eb661 Mon Sep 17 00:00:00 2001 From: Wouter van Wijk Date: Tue, 17 Nov 2026 23:27:41 +0100 Subject: [PATCH 08/12] Fix: instrumentation ignores emoji checks, counts font families, names the real script Emoji support checks no longer count as canvas text, fonts are counted by family (capped at 500), the script is the first http frame unless it is the page itself, and canvases under 16 px are not recorded. Co-Authored-By: Claude Opus 5.5 --- .../collectors/vsv_techniques_js.dart | 35 ++++++++-- test/collectors/vsv_techniques_js_test.dart | 65 +++++++++++++++++++ 2 files changed, 94 insertions(+), 6 deletions(-) diff --git a/lib/services/collectors/vsv_techniques_js.dart b/lib/services/collectors/vsv_techniques_js.dart index 217741e..425a74e 100644 --- a/lib/services/collectors/vsv_techniques_js.dart +++ b/lib/services/collectors/vsv_techniques_js.dart @@ -12,13 +12,13 @@ const String vsvTechniquesJs = r''' (function() { try { if (window.__vsv_tech_report) return; - var MAX_CANVAS = 20, MAX_PROMPTS = 20, MAX_SEEN = 100; + var MAX_CANVAS = 20, MAX_PROMPTS = 20, MAX_SEEN = 100, MAX_FONTS = 500; var started = Date.now(); var canvases = []; // {el, w, h, chars, colors, reads, script} var entries = []; // fallback without WeakMap: {ctx, el, fill, chars, colors} var byCtx = typeof WeakMap === 'function' ? new WeakMap() : null; var byEl = byCtx ? new WeakMap() : null; - var fonts = { measureCalls: 0, seen: {}, script: '' }; + var fonts = { measureCalls: 0, seen: {}, seenCount: 0, script: '' }; var webgl = { debugInfo: false, script: '' }; var audio = { offline: 0, oscillators: 0, rendered: 0, script: '' }; var rtc = { connections: 0, dataChannels: 0, offers: 0, getUserMedia: 0, script: '' }; @@ -26,12 +26,17 @@ const String vsvTechniquesJs = r''' var prompts = []; var offlineSeen = [], rtcSeen = []; + // The first frame with an http(s) URL is the code that made the call. When + // that is the page itself (inline script) there is no separate file to name. function script() { try { var lines = String(new Error().stack || '').split('\n'); for (var i = 0; i < lines.length; i++) { var m = /(https?:\/\/[^\s)]+?)(?::\d+){0,2}\)?\s*$/.exec(lines[i]); - if (m && m[1] !== String(location.href)) return m[1].slice(0, 300); + if (m) { + if (m[1].split('#')[0] === String(location.href).split('#')[0]) return ''; + return m[1].slice(0, 300); + } } } catch (e) {} return ''; @@ -119,6 +124,7 @@ const String vsvTechniquesJs = r''' } } function read(el) { + if (el.width < 16 || el.height < 16) return; var r = null; for (var i = 0; i < canvases.length; i++) if (canvases[i].el === el) r = canvases[i]; if (!r) { @@ -134,9 +140,22 @@ const String vsvTechniquesJs = r''' var e = ctxEntry(ctx); if (!e) return; var text = String(args[0]); - for (var i = 0; i < text.length; i++) e.chars[text.charAt(i)] = 1; + // Emoji support checks (WordPress and others) draw surrogate pairs, joiners + // and variation selectors and read them back; those are not counted. + for (var i = 0; i < text.length; i++) { + var code = text.charCodeAt(i); + if ((code >= 0xD800 && code <= 0xDFFF) || (code >= 0x2000 && code <= 0x206F) || + (code >= 0xFE00 && code <= 0xFE0F)) continue; + e.chars[text.charAt(i)] = 1; + } if (e.fill !== null) e.colors[e.fill] = 1; } + // The font family list of a CSS font string: style, variant, weight, size and + // line height dropped, so "12px Arial" and "13px Arial" are one font. + function fontFamily(f) { + var m = /(?:^|\s)(?:[0-9.]+(?:px|pt|em|rem|%|ex|ch|vw|vh|vmin|vmax|cm|mm|in|pc|q)|xx-small|x-small|small|medium|large|x-large|xx-large|xxx-large|smaller|larger)(?:\/\S+)?\s+(.+)$/i.exec(f); + return String(m ? m[1] : f).toLowerCase().replace(/\s+/g, ' '); + } var canvasProto = proto('HTMLCanvasElement'), ctxProto = proto('CanvasRenderingContext2D'); if (canvasProto) { wrap(canvasProto, 'getContext', null, function(el, args, ctx) { @@ -159,7 +178,11 @@ const String vsvTechniquesJs = r''' }); wrap(ctxProto, 'measureText', function(ctx) { fonts.measureCalls++; - fonts.seen[String(ctx.font)] = 1; + var fam = fontFamily(String(ctx.font)); + if (!Object.prototype.hasOwnProperty.call(fonts.seen, fam) && fonts.seenCount < MAX_FONTS) { + fonts.seen[fam] = 1; + fonts.seenCount++; + } if (!fonts.script) fonts.script = script(); }); } @@ -259,7 +282,7 @@ const String vsvTechniquesJs = r''' } } return JSON.stringify({ v: 1, canvas: out, - fonts: { measureCalls: fonts.measureCalls, distinctFonts: Object.keys(fonts.seen).length, + fonts: { measureCalls: fonts.measureCalls, distinctFonts: fonts.seenCount, script: fonts.script }, webgl: webgl, audio: audio, rtc: rtc, enumerateDevices: counts.enumerateDevices, battery: counts.battery, diff --git a/test/collectors/vsv_techniques_js_test.dart b/test/collectors/vsv_techniques_js_test.dart index 2286764..76d8b73 100644 --- a/test/collectors/vsv_techniques_js_test.dart +++ b/test/collectors/vsv_techniques_js_test.dart @@ -133,6 +133,71 @@ void main() { expect((r['canvas'] as List).single['chars'], greaterThanOrEqualTo(10)); }); + test('emoji support checks are not a canvas fingerprint', () async { + final r = await report(r""" + var strings = ['🏳️‍⚧️', '🇨🇭', + '🏳️‍🌈', '👋🏻', 'a​b⁠']; + var c = new HTMLCanvasElement(); var x = c.getContext('2d'); + x.fillStyle = '#fff'; x.fillStyle = '#000'; + for (var i = 0; i < strings.length; i++) x.fillText(strings[i], 0, 0); + c.toDataURL();"""); + if (r == null) return markTestSkipped('node not installed'); + // Only U+26A7, a and b count: surrogates, joiners and selectors do not. + expect((r['canvas'] as List).single['chars'], 3); + }); + + test('fonts are counted by family, not by size or style', () async { + final r = await report(r""" + var x = new HTMLCanvasElement().getContext('2d'); + var fonts = ['12px Arial', '13px Arial', 'bold 14px/20px Arial', + 'italic small-caps 700 12px "Times New Roman", serif', '12px Courier']; + for (var i = 0; i < fonts.length; i++) { x.font = fonts[i]; x.measureText('m'); }"""); + if (r == null) return markTestSkipped('node not installed'); + expect(r['fonts']['measureCalls'], 5); + expect(r['fonts']['distinctFonts'], 3); + }); + + test('the number of remembered fonts is capped', () async { + final r = await report(r""" + var x = new HTMLCanvasElement().getContext('2d'); + for (var i = 0; i < 800; i++) { x.font = '12px f' + i; x.measureText('m'); }"""); + if (r == null) return markTestSkipped('node not installed'); + expect(r['fonts']['distinctFonts'], 500); + }); + + test('the first http frame names the script; the page itself is none', + () async { + const fakeError = r""" + var __stack = ''; + Error = function() { this.stack = __stack; }; +"""; + Future scriptFor(String stack) async { + final r = await report(""" + __stack = ${jsonEncode(stack)}; + Notification.requestPermission();""", before: fakeError); + return (r!['prompts'] as List).single['script'] as String; + } + + if (await report('') == null) return markTestSkipped('node not installed'); + expect( + await scriptFor('Error\n at a (https://cdn.fp.test/fp.js:3:4)\n' + ' at b (https://example.com/:1:1)'), + 'https://cdn.fp.test/fp.js'); + expect( + await scriptFor('Error\n at a (https://example.com/#top:3:4)\n' + ' at b (https://cdn.fp.test/fp.js:1:1)'), + ''); + }); + + test('a canvas smaller than 16 px read back is not recorded', () async { + final r = await report(r""" + var c = new HTMLCanvasElement(); c.width = 8; c.height = 8; + c.getContext('2d').fillText('Cwm fjordbank glyphs', 0, 0); + c.toDataURL();"""); + if (r == null) return markTestSkipped('node not installed'); + expect(r['canvas'], isEmpty); + }); + test('nothing called gives an empty report', () async { final r = await report(''); if (r == null) return markTestSkipped('node not installed'); From 38f58592fede0afca46c19d94a3ffd8c83bb9cc2 Mon Sep 17 00:00:00 2001 From: Wouter van Wijk Date: Tue, 17 Nov 2026 23:27:47 +0100 Subject: [PATCH 09/12] Fix: cloaking matches only advertising, analytics and behavior trackers Marketing entities also own hosting domains (force.com). Also puts back the lines of the fresh load test that were only reformatted. Co-Authored-By: Claude Opus 5.5 --- lib/services/cname_cloaking_service.dart | 11 ++++++++++- test/services/cname_cloaking_service_test.dart | 9 +++++++++ test/services/html_service_fresh_load_test.dart | 7 ++++--- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/lib/services/cname_cloaking_service.dart b/lib/services/cname_cloaking_service.dart index e1f8815..4549a40 100644 --- a/lib/services/cname_cloaking_service.dart +++ b/lib/services/cname_cloaking_service.dart @@ -41,6 +41,15 @@ typedef CloakingFinder = Future Function( Future?> dohCnameLookup(String host) => SovereigntyService.dnsQuery(host, 5); +/// Categories whose services exist to follow visitors. Marketing and CRM +/// entities also own hosting domains (force.com), so an alias for those says +/// nothing about tracking. +const Set kCloakingTrackerCategories = { + DetectionCategory.advertising, + DetectionCategory.behavior, + DetectionCategory.analytics, +}; + const int kCloakingMaxHosts = 20; const int kCloakingParallel = 4; @@ -96,7 +105,7 @@ Future findCloakedTrackers( for (final t in targets) { if (sameSite(t, page)) continue; final e = entityForHost(t); - if (e != null && e.category.isTracking) { + if (e != null && kCloakingTrackerCategories.contains(e.category)) { aliases.add(CnameAlias(h, t, e.name)); break; } diff --git a/test/services/cname_cloaking_service_test.dart b/test/services/cname_cloaking_service_test.dart index d33beb9..4bcf8db 100644 --- a/test/services/cname_cloaking_service_test.dart +++ b/test/services/cname_cloaking_service_test.dart @@ -22,6 +22,15 @@ void main() { expect(r.aliases.single.tracker, 'Adobe Analytics'); }); + test('an alias for a hosting domain in a marketing entity is no finding', + () async { + final r = await findCloakedTrackers(['help.shop.test'], + pageHost: 'www.shop.test', + lookup: (h) async => ['shop.my.force.com']); + expect(r.checked, ['help.shop.test']); + expect(r.aliases, isEmpty); + }); + test('a failed lookup is listed as not checked, the rest still runs', () async { final r = await findCloakedTrackers(['a.shop.test', 'b.shop.test'], diff --git a/test/services/html_service_fresh_load_test.dart b/test/services/html_service_fresh_load_test.dart index 22cdddd..559df52 100644 --- a/test/services/html_service_fresh_load_test.dart +++ b/test/services/html_service_fresh_load_test.dart @@ -45,8 +45,8 @@ void main() { serverFetcher: fetcher, browserLoader: FakeBrowserLoader(), metadataExtractor: (html, baseUrl, {headers}) async => {}, - browserCollect: (url) async => - payload(url: url, observer: [obs('https://a.test/app.js', tx: 1000)]), + browserCollect: (url) async => payload( + url: url, observer: [obs('https://a.test/app.js', tx: 1000)]), nativeCookies: (u) async => [], browserPlatform: BrowserPlatform.ios, browserPoll: NetworkQuietPolicy.off, @@ -107,7 +107,8 @@ void main() { expect(page.differenceNote, contains('cookie or consent wall')); }); - test('the live browser screenshot is taken once the page loaded', () async { + test('the live browser screenshot is taken once the page loaded', + () async { service = make(const NoFreshLoad(), screenshot: () async => Uint8List.fromList([9])); await load('https://a.test/'); From edc1c241e23cf9770305c6b4d5334c41a101bc39 Mon Sep 17 00:00:00 2001 From: Wouter van Wijk Date: Tue, 17 Nov 2026 23:27:47 +0100 Subject: [PATCH 10/12] Fix: techniques tab review findings (consent cookies, completeness, export, platform support, cloaking state) Consent cookies are not shared identifiers; identifiers are deduped by value; one prompt finding per api; complete needs browser, cookies and resources; unsupported platforms say so up front; export gains hostnameCheck and complete; a missing in-app browser or non-http page no longer leaves the hostname check on Checking; parse failures show a plain message. Co-Authored-By: Claude Opus 5.5 --- lib/models/inspection/technique_signals.dart | 10 +- lib/services/derive/techniques.dart | 74 +++++++++-- lib/services/html_service.dart | 18 ++- lib/services/inspection_export_service.dart | 6 + lib/widgets/techniques_section.dart | 2 + test/derive/techniques_test.dart | 126 +++++++++++++++++++ test/inspection_export_service_test.dart | 2 + test/services/cname_cloaking_state_test.dart | 22 ++++ test/widgets/techniques_section_test.dart | 12 +- 9 files changed, 261 insertions(+), 11 deletions(-) diff --git a/lib/models/inspection/technique_signals.dart b/lib/models/inspection/technique_signals.dart index 322bfa3..7fc0f2b 100644 --- a/lib/models/inspection/technique_signals.dart +++ b/lib/models/inspection/technique_signals.dart @@ -1,5 +1,7 @@ import 'dart:convert'; +import 'package:flutter/foundation.dart' show debugPrint; + import 'observed.dart'; /// [PageInspection.freshTechniques] before a fresh load was asked for. Not @@ -74,6 +76,8 @@ class TechniqueSignals { }); } +const _kReportUnreadable = 'The technique report could not be read'; + int _int(Object? v) => v is num ? v.toInt() : 0; String _str(Object? v) => v is String ? v : ''; Map _map(Object? v) => @@ -90,7 +94,8 @@ Observed parseTechniqueReport(Object? raw) { try { final decoded = raw is String ? jsonDecode(raw) : raw; if (decoded is! Map) { - return const Failed('The technique report had an unexpected shape'); + debugPrint('Technique report had an unexpected shape'); + return const Failed(_kReportUnreadable); } final r = decoded.cast(); final fonts = _map(r['fonts']); @@ -146,6 +151,7 @@ Observed parseTechniqueReport(Object? raw) { ), source: DataSource.browser); } catch (e) { - return Failed('The technique report could not be read: $e'); + debugPrint('Technique report could not be read: $e'); + return const Failed(_kReportUnreadable); } } diff --git a/lib/services/derive/techniques.dart b/lib/services/derive/techniques.dart index 95cc69b..57c148e 100644 --- a/lib/services/derive/techniques.dart +++ b/lib/services/derive/techniques.dart @@ -9,8 +9,9 @@ import '../cname_cloaking_service.dart'; // Englehardt and Narayanan, "Online Tracking: A 1-million-site Measurement // and Analysis" (ACM CCS 2016), section 6.1 (canvas: at least 16x16 px, text // with two colours or ten distinct characters, read back) and section 6.2 -// (fonts: at least 50 distinct font values measured). The rest are this -// app's own choices. +// (fonts: at least 50 distinct font property values measured). The rest are +// this app's own choices; in particular the instrumentation counts font +// families, not whole font strings, so a size change is not a new font. const int kCanvasMinSide = 16; const int kCanvasMinChars = 10; const int kCanvasMinColors = 2; @@ -24,6 +25,29 @@ const int kIdMinDistinctChars = 8; const int kIdMinParties = 2; const int kIdMaxFindings = 10; const int kListedItems = 5; +const int kIdNameMaxLength = 40; + +// Cookies that hold a consent string. These are meant to be passed to other +// parties (IAB TCF `euconsent-v2` / `addtl_consent`, IAB US Privacy +// `usprivacy`, OneTrust `OptanonConsent` / `OptanonAlertBoxClosed`, Cookiebot +// `CookieConsent`, Didomi `didomi_token`, the `__cmp` family of consent +// platform cookies), so seeing one in requests to several parties is expected, +// not an identifier being shared. Matched on the lower-case name prefix. +const List kConsentCookiePrefixes = [ + 'euconsent', + 'addtl_consent', + 'optanonconsent', + 'optanonalertboxclosed', + 'cookieconsent', + 'didomi_token', + 'usprivacy', + '__cmp', +]; + +bool _isConsentCookie(String name) { + final n = name.toLowerCase(); + return kConsentCookiePrefixes.any(n.startsWith); +} enum TechniqueSeverity { high, medium, low } @@ -92,7 +116,8 @@ List judgeSignals(TechniqueSignals s) { title: 'Reads back a canvas it drew text on (canvas fingerprinting)', detail: 'The page drew text on a canvas and read the pixels back. ' 'Rendering differs slightly between devices, so this can tell ' - 'visitors apart. Charts and image editors do not draw text this way.', + 'visitors apart. Charts and image editors rarely read back a canvas they ' + 'drew text on.', scripts: _scripts(fp.map((c) => c.script)), )); } @@ -165,8 +190,14 @@ List judgeSignals(TechniqueSignals s) { detail: 'The page tried to read what you copied last.', )); } + // One finding per kind of prompt: the earliest call. + final firstPrompt = {}; for (final p in s.prompts) { if (p.userActive != false) continue; + final seen = firstPrompt[p.api]; + if (seen == null || p.at < seen.at) firstPrompt[p.api] = p; + } + for (final p in firstPrompt.values) { out.add(TechniqueFinding( id: 'prompt-on-load', severity: TechniqueSeverity.medium, @@ -202,9 +233,10 @@ List findSharedIdentifiers(BrowserSnapshot b) { for (final c in cookies.value) { if (out.length >= kIdMaxFindings) break; final v = c.value; - if (v.length < kIdMinLength || + if (_isConsentCookie(c.name) || + v.length < kIdMinLength || v.split('').toSet().length < kIdMinDistinctChars || - !seen.add(c.name)) { + !seen.add(v)) { continue; } final encoded = Uri.encodeComponent(v); @@ -215,10 +247,13 @@ List findSharedIdentifiers(BrowserSnapshot b) { ..sort(); if (domains.length < kIdMinParties) continue; final listed = domains.take(kListedItems).toList(); + final name = c.name.length > kIdNameMaxLength + ? '${c.name.substring(0, kIdNameMaxLength)}…' + : c.name; out.add(TechniqueFinding( id: 'id-sharing', severity: TechniqueSeverity.medium, - title: 'The identifier in cookie ${c.name} is sent to ' + title: 'The identifier in cookie $name is sent to ' '${domains.length} other parties', detail: 'The value of this cookie appears in requests to ' '${listed.join(', ')}' @@ -275,6 +310,10 @@ class TechniquesModel { /// Every check ran: "no findings" then means none were seen. final bool complete; + /// Why the shared-identifier check could not run; null when it ran or does + /// not apply. + final String? sharedIdsNote; + const TechniquesModel({ required this.findings, required this.callChecksNote, @@ -282,12 +321,14 @@ class TechniquesModel { required this.notChecked, required this.cloakingNote, required this.complete, + this.sharedIdsNote, }); } TechniquesModel deriveTechniques(PageInspection p, {required Observed cloaking, - Map? metadata}) { + Map? metadata, + bool freshLoadSupported = true}) { final findings = []; final isUrl = p.target is UrlTarget; @@ -305,6 +346,11 @@ TechniquesModel deriveTechniques(PageInspection p, ): callNote = d.isNotEmpty ? d : unavailableMessage(r, d); canRun = isUrl && r != UnavailableReason.unsupportedPlatform; + if (!freshLoadSupported) { + callNote = + unavailableMessage(UnavailableReason.unsupportedPlatform, ''); + canRun = false; + } case Failed(error: final e): callNote = e; canRun = isUrl; @@ -315,8 +361,20 @@ TechniquesModel deriveTechniques(PageInspection p, } final browser = p.browser; + String? sharedIdsNote; + var sharedIdsChecked = false; if (browser is Present) { findings.addAll(findSharedIdentifiers(browser.value)); + if (browser.value.cookies is! Present>) { + sharedIdsNote = 'Cookies not available: shared identifiers not checked.'; + } else if (browser.value.resources is! Present>) { + sharedIdsNote = 'Requests not available: shared identifiers not checked.'; + } else { + sharedIdsChecked = true; + } + } else if (isUrl) { + sharedIdsNote = 'Browser data not available: ' + 'shared identifiers not checked.'; } var notChecked = const []; @@ -361,7 +419,9 @@ TechniquesModel deriveTechniques(PageInspection p, callChecksCanRun: canRun, notChecked: notChecked, cloakingNote: cloakingNote, + sharedIdsNote: sharedIdsNote, complete: callNote == null && + sharedIdsChecked && cloakingNote == null && notChecked.isEmpty && metadata != null, diff --git a/lib/services/html_service.dart b/lib/services/html_service.dart index 28850ba..2870774 100644 --- a/lib/services/html_service.dart +++ b/lib/services/html_service.dart @@ -27,6 +27,7 @@ import 'package:view_source_vibe/models/inspection/file_snapshot.dart'; import 'package:view_source_vibe/models/inspection/observed.dart'; import 'package:view_source_vibe/models/inspection/page_inspection.dart'; import 'package:view_source_vibe/models/inspection/server_snapshot.dart'; +import 'package:view_source_vibe/models/inspection/technique_signals.dart'; import 'package:view_source_vibe/services/collectors/browser_loader.dart'; import 'package:view_source_vibe/services/collectors/collector_world.dart'; import 'package:view_source_vibe/services/collectors/file_loader.dart'; @@ -34,7 +35,6 @@ import 'package:view_source_vibe/services/collectors/server_fetcher.dart'; import 'package:view_source_vibe/services/derive/cookies_view.dart'; import 'package:view_source_vibe/services/derive/detections.dart'; import 'package:view_source_vibe/services/derive/detections_view.dart'; -import 'package:view_source_vibe/models/inspection/technique_signals.dart'; import 'package:view_source_vibe/services/derive/divergences.dart'; import 'package:view_source_vibe/services/derive/block_page.dart'; import 'package:view_source_vibe/services/derive/header_insights.dart'; @@ -1028,6 +1028,18 @@ class HtmlService extends ChangeNotifier { return const Unavailable(UnavailableReason.notApplicable, 'Load a URL to check its hostnames.'); } + final browser = current.browser; + if (browser is Unavailable || + browser is Failed) { + return const Unavailable( + UnavailableReason.blockedByPlatform, 'Needs the in-app browser'); + } + if (browser is Present && + browser.value.url.scheme != 'http' && + browser.value.url.scheme != 'https') { + return const Unavailable(UnavailableReason.notApplicable, + 'Only web pages have hostnames to check.'); + } final state = _cloakingState; if (state == null || _cloakingResultGen != current.generation) { return const Pending(); @@ -1035,6 +1047,9 @@ class HtmlService extends ChangeNotifier { return state; } + /// Whether this platform can load a page apart from the live browser. + bool get freshLoadSupported => _freshMeasurer is! NoFreshLoad; + TechniquesModel? _techniquesView; PageInspection? _techniquesViewFor; Observed? _techniquesViewMetadata; @@ -1055,6 +1070,7 @@ class HtmlService extends ChangeNotifier { _techniquesViewCloaking = cloaking; _techniquesView = deriveTechniques(current, cloaking: cloaking, + freshLoadSupported: freshLoadSupported, metadata: metadata is Present ? metadata.value.data : null); diff --git a/lib/services/inspection_export_service.dart b/lib/services/inspection_export_service.dart index fcaee84..cdaac23 100644 --- a/lib/services/inspection_export_service.dart +++ b/lib/services/inspection_export_service.dart @@ -8,6 +8,7 @@ import 'package:view_source_vibe/models/inspection/browser_snapshot.dart'; import 'package:view_source_vibe/models/inspection/observed.dart'; import 'package:view_source_vibe/models/inspection/page_inspection.dart'; import 'package:view_source_vibe/models/inspection/technique_signals.dart'; +import 'package:view_source_vibe/services/cname_cloaking_service.dart'; import 'package:view_source_vibe/services/derive/cookies_view.dart'; import 'package:view_source_vibe/services/derive/metadata_view.dart'; import 'package:view_source_vibe/services/derive/observed_text.dart'; @@ -186,8 +187,13 @@ class InspectionExportService { 'callChecks': observedJson( p.freshTechniques, (_) => 'measured', source: DataSource.browser), + // Whether the hostname check ran; the hosts are in `findings` only. + 'hostnameCheck': observedJson( + htmlService.cnameCloaking, (_) => 'checked', + source: DataSource.server), 'findings': [for (final f in techniques.findings) f.toJson()], 'notChecked': techniques.notChecked, + 'complete': techniques.complete, }); final sovereigntyJson = observedJson(sovereignty, (r) => r.toJson(), source: DataSource.server, provenance: Provenance.derived); diff --git a/lib/widgets/techniques_section.dart b/lib/widgets/techniques_section.dart index 67ff13e..951eaaf 100644 --- a/lib/widgets/techniques_section.dart +++ b/lib/widgets/techniques_section.dart @@ -66,6 +66,8 @@ class _TechniquesSectionState extends State { ), ], ), + if (model.sharedIdsNote != null) + Text(model.sharedIdsNote!, style: muted), if (model.cloakingNote != null) Text(model.cloakingNote!, style: muted), if (model.notChecked.isNotEmpty) Text('Not checked (lookup failed): ${model.notChecked.join(', ')}', diff --git a/test/derive/techniques_test.dart b/test/derive/techniques_test.dart index 2d4fbb7..d1c4bd1 100644 --- a/test/derive/techniques_test.dart +++ b/test/derive/techniques_test.dart @@ -167,6 +167,34 @@ void main() { isEmpty); }); + test('the same prompt asked for twice is one finding, the earliest', () { + PromptCall p(String api, int ms, String script) => PromptCall( + api: api, + at: Duration(milliseconds: ms), + userActive: false, + script: script); + final f = judgeSignals(signals(prompts: [ + p('geolocation', 3000, 'https://b.test/b.js'), + p('geolocation', 1000, 'https://a.test/a.js'), + p('notifications', 2000, ''), + ])); + expect(f.length, 2); + final geo = f.firstWhere((e) => e.title.contains('geolocation')); + expect(geo.title, contains('1 s after loading')); + expect(geo.scripts, ['https://a.test/a.js']); + }); + + test('canvas text does not claim charts never draw text', () { + final f = judgeSignals(signals(canvas: [ + const CanvasRead( + width: 300, height: 150, chars: 34, colors: 2, reads: 1, script: '') + ])); + expect( + f.single.detail, + contains('Charts and image editors rarely read back a canvas they ' + 'drew text on.')); + }); + test('toJson carries no more than the finding', () { final j = judgeSignals(signals(clipboardRead: 1)).single.toJson(); expect(j['id'], 'clipboard-read'); @@ -176,6 +204,8 @@ void main() { test('report parsing', () { expect(parseTechniqueReport(null), isA>()); expect(parseTechniqueReport('not json'), isA>()); + expect((parseTechniqueReport('not json') as Failed).error, + 'The technique report could not be read'); final r = parseTechniqueReport(jsonEncode({ 'v': 1, 'canvas': [ @@ -277,6 +307,65 @@ void main() { } }); + test('consent strings are not identifiers', () { + for (final n in [ + 'euconsent-v2', + 'addtl_consent', + 'OptanonConsent', + 'OptanonAlertBoxClosed', + 'CookieConsent', + 'didomi_token', + 'usprivacy', + '__cmpconsent15', + ]) { + final f = findSharedIdentifiers(browserSnap( + url: 'https://shop.test/', + cookies: [cookie(n, value: id)], + resources: [ + res('https://a.one.test/p?u=$id'), + res('https://b.two.test/p?u=$id'), + ])); + expect(f.isEmpty, isTrue, reason: '$n was reported'); + } + }); + + test('the same value in two cookies is one finding', () { + final f = findSharedIdentifiers(browserSnap( + url: 'https://shop.test/', + cookies: [cookie('_a', value: id), cookie('_b', value: id)], + resources: [ + res('https://a.one.test/p?u=$id'), + res('https://b.two.test/p?u=$id'), + ])); + expect(f.length, 1); + }); + + test('two cookies with one name and different values are two findings', + () { + const other = 'z9y8x7w6v5u4t3s2r1'; + final f = findSharedIdentifiers(browserSnap( + url: 'https://shop.test/', + cookies: [cookie('_a', value: id), cookie('_a', value: other)], + resources: [ + res('https://a.one.test/p?u=$id&v=$other'), + res('https://b.two.test/p?u=$id&v=$other'), + ])); + expect(f.length, 2); + }); + + test('a long cookie name is cut in the title', () { + final name = 'n' * 60; + final f = findSharedIdentifiers(browserSnap( + url: 'https://shop.test/', + cookies: [cookie(name, value: id)], + resources: [ + res('https://a.one.test/p?u=$id'), + res('https://b.two.test/p?u=$id'), + ])); + expect(f.single.title.contains('${'n' * 40}…'), isTrue); + expect(f.single.title.contains('n' * 41), isFalse); + }); + test('one other party is not sharing', () { final f = findSharedIdentifiers(browserSnap( url: 'https://shop.test/', @@ -433,6 +522,43 @@ void main() { expect(m.cloakingNote, contains('dns down')); }); + test('without cookies the shared identifiers are not checked, not clean', + () { + final p = inspection( + browser: browserSnap(url: 'https://shop.test/', cookies: null)) + .copyWith( + freshTechniques: Present(signals(), source: DataSource.browser)); + final m = deriveTechniques(p, + cloaking: const Present(CloakingResult(), source: DataSource.server), + metadata: const {}); + expect(m.sharedIdsNote, + 'Cookies not available: shared identifiers not checked.'); + expect(m.complete, isFalse); + }); + + test('not supported here: the note says so, no button', () { + final m = deriveTechniques(pageWithSharedId, + cloaking: const Pending(), + metadata: null, + freshLoadSupported: false); + expect(m.callChecksNote, 'Not supported on this platform'); + expect(m.callChecksCanRun, isFalse); + expect(m.findings.map((f) => f.id), ['id-sharing']); + }); + + test('a measured load is shown even where a fresh load is unsupported', + () { + final m = deriveTechniques( + pageWithSharedId.copyWith( + freshTechniques: + Present(signals(battery: 1), source: DataSource.browser)), + cloaking: const Pending(), + metadata: null, + freshLoadSupported: false); + expect(m.callChecksNote, isNull); + expect(m.findings.map((f) => f.id), contains('battery')); + }); + test('complete only when every check ran', () { final ran = pageWithSharedId.copyWith( freshTechniques: diff --git a/test/inspection_export_service_test.dart b/test/inspection_export_service_test.dart index f14dca6..94e7aab 100644 --- a/test/inspection_export_service_test.dart +++ b/test/inspection_export_service_test.dart @@ -118,6 +118,8 @@ void main() { expect(json['callChecks']['detail'], 'Run a fresh load to check'); expect(json['findings'], isEmpty); expect(json['notChecked'], isEmpty); + expect(json['hostnameCheck']['status'], isA()); + expect(json['complete'], isFalse); }); test('no debug payload without a browser sync', () async { diff --git a/test/services/cname_cloaking_state_test.dart b/test/services/cname_cloaking_state_test.dart index 7334568..b30686e 100644 --- a/test/services/cname_cloaking_state_test.dart +++ b/test/services/cname_cloaking_state_test.dart @@ -96,4 +96,26 @@ void main() { await pumpEventQueue(); expect(service.cnameCloaking, isA>()); }); + + test('no in-app browser result: says so instead of checking forever', + () async { + seedStore(store, page.copyWith(browser: const Failed('no webview'))); + service.watchCloaking(true); + await pumpEventQueue(); + final state = service.cnameCloaking as Unavailable; + expect(state.detail, 'Needs the in-app browser'); + expect(looked, isEmpty); + }); + + test('a page that is not http(s) has no hostnames to check', () async { + seedStore( + store, + page.copyWith( + browser: browser(browserSnapshot( + url: 'about:blank', dom: fullPageDomHtml, resources: [])))); + service.watchCloaking(true); + await pumpEventQueue(); + final state = service.cnameCloaking as Unavailable; + expect(state.reason, UnavailableReason.notApplicable); + }); } diff --git a/test/widgets/techniques_section_test.dart b/test/widgets/techniques_section_test.dart index ffce22d..43b1392 100644 --- a/test/widgets/techniques_section_test.dart +++ b/test/widgets/techniques_section_test.dart @@ -3,6 +3,7 @@ import 'package:view_source_vibe/models/inspection/observed.dart'; import 'package:view_source_vibe/models/inspection/page_inspection.dart'; import 'package:view_source_vibe/models/inspection/technique_signals.dart'; import 'package:view_source_vibe/services/cname_cloaking_service.dart'; +import 'package:view_source_vibe/services/collectors/fresh_load_measurer.dart'; import 'package:view_source_vibe/services/html_service.dart'; import 'package:view_source_vibe/services/inspection_store.dart'; import 'package:view_source_vibe/widgets/privacy_view.dart'; @@ -16,7 +17,8 @@ void main() { late HtmlService service; Future show(WidgetTester tester, PageInspection p, - {Observed? techniques}) async { + {Observed? techniques, + FreshLoadMeasurer? freshLoadMeasurer}) async { store = seedStore(InspectionStore(), p); if (techniques != null) { store.applyFreshLoad(store.current.generation, @@ -28,6 +30,7 @@ void main() { serverFetcher: SilentServerFetcher(), browserLoader: SilentBrowserLoader(), metadataExtractor: (html, baseUrl, {headers}) async => {}, + freshLoadMeasurer: freshLoadMeasurer, cloakingFinder: (hosts, {required pageHost, required lookup}) async => const CloakingResult(), ); @@ -48,6 +51,13 @@ void main() { expect(find.textContaining('No suspicious techniques'), findsNothing); }); + testWidgets('a platform without a fresh load says so from the start', + (tester) async { + await show(tester, trackerPage(), freshLoadMeasurer: const NoFreshLoad()); + expect(find.text('Not supported on this platform'), findsOneWidget); + expect(find.text('Run fresh load'), findsNothing); + }); + testWidgets('unsupported platform: says so, no button', (tester) async { await show(tester, trackerPage(), techniques: const Unavailable(UnavailableReason.unsupportedPlatform, From d54eaafb87741c461124b57ef0a5df2ac99cde52 Mon Sep 17 00:00:00 2001 From: Wouter van Wijk Date: Tue, 17 Nov 2026 23:30:47 +0100 Subject: [PATCH 11/12] Fix: hostname check says "needs the in-app browser" only when there is none A failed browser read is Failed, and a page not loaded yet stays Pending. Co-Authored-By: Claude Opus 5.5 --- lib/services/html_service.dart | 7 +++-- test/services/cname_cloaking_state_test.dart | 31 ++++++++++++++++++-- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/lib/services/html_service.dart b/lib/services/html_service.dart index 2870774..068ef80 100644 --- a/lib/services/html_service.dart +++ b/lib/services/html_service.dart @@ -1029,11 +1029,14 @@ class HtmlService extends ChangeNotifier { 'Load a URL to check its hostnames.'); } final browser = current.browser; - if (browser is Unavailable || - browser is Failed) { + if (browser is Unavailable && + browser.reason != UnavailableReason.notApplicable) { return const Unavailable( UnavailableReason.blockedByPlatform, 'Needs the in-app browser'); } + if (browser is Failed) { + return const Failed('the browser did not report this page'); + } if (browser is Present && browser.value.url.scheme != 'http' && browser.value.url.scheme != 'https') { diff --git a/test/services/cname_cloaking_state_test.dart b/test/services/cname_cloaking_state_test.dart index b30686e..0623cdc 100644 --- a/test/services/cname_cloaking_state_test.dart +++ b/test/services/cname_cloaking_state_test.dart @@ -1,6 +1,7 @@ import 'dart:async'; import 'package:flutter_test/flutter_test.dart'; +import 'package:view_source_vibe/models/inspection/browser_snapshot.dart'; import 'package:view_source_vibe/models/inspection/observed.dart'; import 'package:view_source_vibe/services/cname_cloaking_service.dart'; import 'package:view_source_vibe/services/html_service.dart'; @@ -97,9 +98,12 @@ void main() { expect(service.cnameCloaking, isA>()); }); - test('no in-app browser result: says so instead of checking forever', - () async { - seedStore(store, page.copyWith(browser: const Failed('no webview'))); + test('no in-app browser: says so instead of checking forever', () async { + seedStore( + store, + page.copyWith( + browser: const Unavailable( + UnavailableReason.unsupportedPlatform, 'no webview'))); service.watchCloaking(true); await pumpEventQueue(); final state = service.cnameCloaking as Unavailable; @@ -107,6 +111,27 @@ void main() { expect(looked, isEmpty); }); + test('a failed browser read is Failed, not "needs the browser"', () async { + seedStore(store, page.copyWith(browser: const Failed('no webview'))); + service.watchCloaking(true); + await pumpEventQueue(); + expect(service.cnameCloaking, isA>()); + expect(looked, isEmpty); + }); + + test('a browser not loaded yet keeps the check pending', () async { + for (final b in const >[ + Pending(), + Unavailable(UnavailableReason.notApplicable, 'nothing loaded'), + ]) { + seedStore(store, page.copyWith(browser: b)); + service.watchCloaking(true); + await pumpEventQueue(); + expect(service.cnameCloaking, isA>()); + service.watchCloaking(false); + } + }); + test('a page that is not http(s) has no hostnames to check', () async { seedStore( store, From e6edfe117a5411deb925e9852a6c423da77d2bcf Mon Sep 17 00:00:00 2001 From: Wouter van Wijk Date: Wed, 18 Nov 2026 12:22:05 +0100 Subject: [PATCH 12/12] Test: techniques section fresh-load test uses an explicit supported measurer The default measurer is NoFreshLoad on platforms without a headless WebView (Linux CI), where the 'not run yet' state cannot occur. Pass HeadlessFreshLoadMeasurer explicitly so the test is platform-independent. Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe --- test/widgets/techniques_section_test.dart | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/widgets/techniques_section_test.dart b/test/widgets/techniques_section_test.dart index 43b1392..1de21a1 100644 --- a/test/widgets/techniques_section_test.dart +++ b/test/widgets/techniques_section_test.dart @@ -44,7 +44,10 @@ void main() { testWidgets('the section offers the fresh load when it has not run', (tester) async { - await show(tester, trackerPage()); + // Explicit supported measurer: the default is NoFreshLoad on platforms + // without a headless WebView (e.g. Linux CI), where this state cannot occur. + await show(tester, trackerPage(), + freshLoadMeasurer: const HeadlessFreshLoadMeasurer()); expect(find.text('Techniques'), findsOneWidget); expect(find.text('Run a fresh load to check'), findsOneWidget); expect(find.text('Run fresh load'), findsOneWidget);