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/models/inspection/page_inspection.dart b/lib/models/inspection/page_inspection.dart index 32cfabd..e7eecb7 100644 --- a/lib/models/inspection/page_inspection.dart +++ b/lib/models/inspection/page_inspection.dart @@ -6,6 +6,7 @@ import 'observed.dart'; import 'page_layout.dart'; import 'permission_request.dart'; import 'server_snapshot.dart'; +import 'technique_signals.dart'; /// [PageInspection.freshLoad] before the user asked for one. const kFreshLoadNotMeasured = Unavailable( @@ -73,6 +74,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; @@ -99,6 +104,7 @@ class PageInspection { this.primaryDocumentPreference, this.browserSettled = false, this.freshLoad = kFreshLoadNotMeasured, + this.freshTechniques = kTechniquesNotMeasured, this.freshEstimates = const {}, this.liveScreenshot = kNoScreenshot, this.freshScreenshot = kNoScreenshot, @@ -122,6 +128,7 @@ class PageInspection { primaryDocumentPreference = null, browserSettled = false, freshLoad = kFreshLoadNotMeasured, + freshTechniques = kTechniquesNotMeasured, freshEstimates = const {}, liveScreenshot = kNoScreenshot, freshScreenshot = kNoScreenshot, @@ -138,6 +145,7 @@ class PageInspection { Object? primaryDocumentPreference = _keep, bool? browserSettled, Observed? freshLoad, + Observed? freshTechniques, Map>? freshEstimates, Observed? liveScreenshot, Observed? freshScreenshot, @@ -158,6 +166,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..7fc0f2b --- /dev/null +++ b/lib/models/inspection/technique_signals.dart @@ -0,0 +1,157 @@ +import 'dart:convert'; + +import 'package:flutter/foundation.dart' show debugPrint; + +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 {}, + }); +} + +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) => + 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) { + debugPrint('Technique report had an unexpected shape'); + return const Failed(_kReportUnreadable); + } + 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) { + debugPrint('Technique report could not be read: $e'); + return const Failed(_kReportUnreadable); + } +} diff --git a/lib/services/cname_cloaking_service.dart b/lib/services/cname_cloaking_service.dart new file mode 100644 index 0000000..4549a40 --- /dev/null +++ b/lib/services/cname_cloaking_service.dart @@ -0,0 +1,116 @@ +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); + +/// [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); + +/// 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; + +/// 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 && kCloakingTrackerCategories.contains(e.category)) { + aliases.add(CnameAlias(h, t, e.name)); + break; + } + } + } + return CloakingResult( + aliases: aliases, checked: checked, notChecked: notChecked); +} 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/collectors/vsv_techniques_js.dart b/lib/services/collectors/vsv_techniques_js.dart new file mode 100644 index 0000000..425a74e --- /dev/null +++ b/lib/services/collectors/vsv_techniques_js.dart @@ -0,0 +1,293 @@ +// 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() { + try { + if (window.__vsv_tech_report) return; + 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: {}, 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: '' }; + var counts = { enumerateDevices: 0, battery: 0, clipboardRead: 0 }; + 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) { + if (m[1].split('#')[0] === String(location.href).split('#')[0]) return ''; + 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 --- + // 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 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) { + 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) { + 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 e = ctxEntry(ctx); + if (!e) return; + var text = String(args[0]); + // 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) { + 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 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 e = ctxEntry(ctx); + if (e) read(e.el); + }); + wrap(ctxProto, 'measureText', function(ctx) { + fonts.measureCalls++; + 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(); + }); + } + + // --- 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) { + 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, + fonts: { measureCalls: fonts.measureCalls, distinctFonts: fonts.seenCount, + script: fonts.script }, + webgl: webgl, audio: audio, rtc: rtc, + enumerateDevices: counts.enumerateDevices, battery: counts.battery, + clipboardRead: counts.clipboardRead, prompts: prompts }); + }; + } catch (e) {} +})(); +'''; diff --git a/lib/services/derive/techniques.dart b/lib/services/derive/techniques.dart new file mode 100644 index 0000000..57c148e --- /dev/null +++ b/lib/services/derive/techniques.dart @@ -0,0 +1,429 @@ +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 +// 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 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; +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; +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 } + +/// 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, + }); + + /// 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, + 'title': title, + 'detail': detail, + 'scripts': scripts, + 'source': source.name, + }; +} + +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 + }.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 rarely read back a canvas they ' + 'drew text on.', + 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.', + )); + } + // 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, + 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; +} + +/// 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 (_isConsentCookie(c.name) || + v.length < kIdMinLength || + v.split('').toSet().length < kIdMinDistinctChars || + !seen.add(v)) { + 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(); + 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 $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.', + ); +} + +/// 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; + + /// 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, + required this.callChecksCanRun, + required this.notChecked, + required this.cloakingNote, + required this.complete, + this.sharedIdsNote, + }); +} + +TechniquesModel deriveTechniques(PageInspection p, + {required Observed cloaking, + Map? metadata, + bool freshLoadSupported = true}) { + 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; + if (!freshLoadSupported) { + callNote = + unavailableMessage(UnavailableReason.unsupportedPlatform, ''); + canRun = false; + } + 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; + 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 []; + 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, + 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 5f7c6d1..33e7ff3 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'; @@ -28,6 +29,7 @@ import 'package:view_source_vibe/models/inspection/page_inspection.dart'; import 'package:view_source_vibe/models/inspection/page_layout.dart'; import 'package:view_source_vibe/models/inspection/permission_request.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'; @@ -50,6 +52,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'; @@ -236,6 +239,15 @@ class HtmlService extends ChangeNotifier { 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; @@ -1085,6 +1097,114 @@ class HtmlService extends ChangeNotifier { if (watching) _scheduleSovereignty(); } + /// 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 browser = current.browser; + 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') { + return const Unavailable(UnavailableReason.notApplicable, + 'Only web pages have hostnames to check.'); + } + final state = _cloakingState; + if (state == null || _cloakingResultGen != current.generation) { + return const Pending(); + } + 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; + 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, + freshLoadSupported: freshLoadSupported, + 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) { + _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 _cloakingFinder(hosts, + pageHost: url.host, lookup: _cnameLookup); + if (!_disposed && gen == store.current.generation) { + _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 { @@ -1715,7 +1835,11 @@ class HtmlService extends ChangeNotifier { NetworkQuietPolicy browserPoll = NetworkQuietPolicy.live, Duration lateCollectDelay = const Duration(seconds: 2), Duration spaSettleDelay = const Duration(milliseconds: 400), - }) : _sovereigntyDebounce = sovereigntyDebounce, + CnameLookup? cnameLookup, + CloakingFinder? cloakingFinder, + }) : _cnameLookup = cnameLookup ?? dohCnameLookup, + _cloakingFinder = cloakingFinder ?? findCloakedTrackers, + _sovereigntyDebounce = sovereigntyDebounce, _sovereigntyAnalyzer = sovereigntyAnalyzer ?? SovereigntyService.analyze, _detectionScanner = detectionScanner, @@ -2464,6 +2588,7 @@ Technical details: $message'''; if (_disposed) return; super.notifyListeners(); _scheduleSovereignty(); + _scheduleCloaking(); } @override @@ -4401,7 +4526,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_export_service.dart b/lib/services/inspection_export_service.dart index bc1c29b..653c518 100644 --- a/lib/services/inspection_export_service.dart +++ b/lib/services/inspection_export_service.dart @@ -7,6 +7,8 @@ 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/cname_cloaking_service.dart'; import 'package:view_source_vibe/services/derive/cookies_view.dart'; import 'package:view_source_vibe/services/derive/inspection_summary.dart'; import 'package:view_source_vibe/services/derive/metadata_view.dart'; @@ -181,6 +183,20 @@ 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), + // 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 structureJson = unlessBlocked( InspectionExportData.structure(htmlService.structureView)); final sovereigntyJson = observedJson(sovereignty, (r) => r.toJson(), @@ -226,6 +242,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/summary.json', summary.toJson()); addJson('analysis/metadata.json', metadataJson); diff --git a/lib/services/inspection_store.dart b/lib/services/inspection_store.dart index 5c711bc..4c97cce 100644 --- a/lib/services/inspection_store.dart +++ b/lib/services/inspection_store.dart @@ -10,6 +10,7 @@ import '../models/inspection/page_inspection.dart'; import '../models/inspection/page_layout.dart'; import '../models/inspection/permission_request.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; @@ -168,14 +169,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/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); + }); + }); + + 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('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: + 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 fdbe1e9..13654c5 100644 --- a/test/inspection_export_service_test.dart +++ b/test/inspection_export_service_test.dart @@ -125,6 +125,30 @@ 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); + expect(json['hostnameCheck']['status'], isA()); + expect(json['complete'], isFalse); + }); + test('no debug payload without a browser sync', () async { final htmlService = HtmlService(); addTearDown(htmlService.dispose); diff --git a/test/services/cname_cloaking_service_test.dart b/test/services/cname_cloaking_service_test.dart new file mode 100644 index 0000000..4bcf8db --- /dev/null +++ b/test/services/cname_cloaking_service_test.dart @@ -0,0 +1,89 @@ +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('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'], + 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..0623cdc --- /dev/null +++ b/test/services/cname_cloaking_state_test.dart @@ -0,0 +1,146 @@ +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'; +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; + var hang = false; + var finderThrows = false; + + 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 => {}, + 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 []; + }, + ); + }); + 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)); + }); + + 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>()); + }); + + 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; + expect(state.detail, 'Needs the in-app browser'); + 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, + 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/services/html_service_fresh_load_test.dart b/test/services/html_service_fresh_load_test.dart index 0afecac..559df52 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'; @@ -157,4 +158,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); + }); } 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); }); diff --git a/test/support/collector_node.dart b/test/support/collector_node.dart index 9b97747..8fe395c 100644 --- a/test/support/collector_node.dart +++ b/test/support/collector_node.dart @@ -30,31 +30,16 @@ Future?> runCollector({ String opts = '{dom: false}', String probe = 'null', }) async { - try { - await Process.run('node', ['--version']); - } on ProcessException { - return null; - } - 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 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; - } finally { - dir.deleteSync(recursive: true); - } + 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; } /// Runs [source] under Node and returns what it printed to stdout (trimmed). diff --git a/test/widgets/techniques_section_test.dart b/test/widgets/techniques_section_test.dart new file mode 100644 index 0000000..1de21a1 --- /dev/null +++ b/test/widgets/techniques_section_test.dart @@ -0,0 +1,111 @@ +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/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'; + +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, + FreshLoadMeasurer? freshLoadMeasurer}) 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 => {}, + freshLoadMeasurer: freshLoadMeasurer, + 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 { + // 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); + 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, + '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); + }); +}