diff --git a/README.md b/README.md index dac4c8b..bad90ac 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ In a nice user interface you can easily view: - Http Headers - Robots.txt - Security Score +- Permissions (what the page may use, hands to frames and stores on your device) - DOM Tree - Plain Html @@ -77,6 +78,7 @@ Each page opens into a set of tabs: - **Timeline:** The request timeline of the page load. - **Probe & Headers:** HTTP details and response headers. - **Security:** Security headers and TLS details. + - **Permissions:** Permissions policy, access handed to embedded frames, denied permission requests and site storage. - **🛠️ Everything else** - **Bundles:** Save and organize favorite sites and snippets. diff --git a/lib/models/app_tab.dart b/lib/models/app_tab.dart index 7fecdcd..0acbbd2 100644 --- a/lib/models/app_tab.dart +++ b/lib/models/app_tab.dart @@ -28,6 +28,8 @@ enum AppTab { privacy(TabGroup.trust, 'Privacy', Icons.privacy_tip_outlined, 'privacy'), sovereignty(TabGroup.trust, 'Sovereignty', Icons.public, 'sovereignty'), security(TabGroup.trust, 'Security', Icons.security, 'security'), + permissions(TabGroup.trust, 'Permissions', + Icons.admin_panel_settings_outlined, 'permissions'), cookies(TabGroup.trust, 'Cookies', Icons.cookie_outlined, 'cookies'); final TabGroup group; @@ -66,6 +68,7 @@ List visibleTabsFor({ AppTab.probe || AppTab.sovereignty || AppTab.security || + AppTab.permissions || AppTab.cookies => server, }) diff --git a/lib/models/inspection/browser_snapshot.dart b/lib/models/inspection/browser_snapshot.dart index 4af0395..543f66f 100644 --- a/lib/models/inspection/browser_snapshot.dart +++ b/lib/models/inspection/browser_snapshot.dart @@ -45,8 +45,8 @@ class RenderedDom { other.shadowRootCount == shadowRootCount; @override - int get hashCode => Object.hash( - text, unwrappedRawDocument, iframeCount, shadowRootCount); + int get hashCode => + Object.hash(text, unwrappedRawDocument, iframeCount, shadowRootCount); /// Parts of the page that [text] does not contain. List get limitations => [ @@ -130,8 +130,8 @@ class BrowserCookie { sameObserved(other.sameSite, sameSite); @override - int get hashCode => Object.hash(name, value, domain, path, expires, isSecure, - isHttpOnly, sameSite); + int get hashCode => Object.hash( + name, value, domain, path, expires, isSecure, isHttpOnly, sameSite); } class StorageItem { @@ -230,6 +230,85 @@ class NavigationTiming { domContentLoaded, loadEvent, protocol, documentTransfer, documentDecoded); } +/// One iframe as the page declares it. +class FrameInfo { + /// Absolute URL; '' for srcdoc / about:blank frames. + final String src; + + /// The raw `allow` attribute, '' when absent. + final String allow; + + /// The raw `sandbox` attribute; null = no sandbox attribute. + final String? sandbox; + const FrameInfo({required this.src, this.allow = '', this.sandbox}); + + @override + bool operator ==(Object other) => + other is FrameInfo && + other.src == src && + other.allow == allow && + other.sandbox == sandbox; + + @override + int get hashCode => Object.hash(src, allow, sandbox); +} + +class ServiceWorkerInfo { + final String scope; + final String script; + const ServiceWorkerInfo({required this.scope, required this.script}); + + @override + bool operator ==(Object other) => + other is ServiceWorkerInfo && + other.scope == scope && + other.script == script; + + @override + int get hashCode => Object.hash(scope, script); +} + +/// What the site keeps on the device besides cookies and Web Storage. +class SiteStorage { + final Observed usageBytes; + final Observed quotaBytes; + final Observed> indexedDbNames; + final Observed> cacheNames; + final Observed> serviceWorkers; + const SiteStorage({ + required this.usageBytes, + required this.quotaBytes, + required this.indexedDbNames, + required this.cacheNames, + required this.serviceWorkers, + }); + + @override + bool operator ==(Object other) => + other is SiteStorage && + sameObserved(other.usageBytes, usageBytes) && + sameObserved(other.quotaBytes, quotaBytes) && + sameObserved(other.indexedDbNames, indexedDbNames) && + sameObserved(other.cacheNames, cacheNames) && + sameObserved(other.serviceWorkers, serviceWorkers); + + @override + int get hashCode => Object.hash( + usageBytes.runtimeType, + quotaBytes.runtimeType, + indexedDbNames.runtimeType, + cacheNames.runtimeType, + serviceWorkers.runtimeType); +} + +const kFramesNotCollected = Unavailable>( + UnavailableReason.notReportedByPlatform, 'not collected'); +const kSiteStorageNotCollected = Unavailable( + UnavailableReason.notReportedByPlatform, 'not collected'); + +const kCapabilityNotCollected = Unavailable( + UnavailableReason.notReportedByPlatform, 'not collected'); + /// Everything the WebView told us about the current page, one section each. class BrowserSnapshot { final Uri url; @@ -255,6 +334,18 @@ class BrowserSnapshot { final bool resourcesTruncated; final int resourcesDropped; + /// The iframes the page declares (at most 200) and how many there are. + final Observed> frames; + final int frameCount; + + /// Quota usage, IndexedDB, Cache Storage and service workers. + final Observed siteStorage; + + /// Whether the page can ask for camera/microphone and for location at all + /// (navigator.mediaDevices.getUserMedia / navigator.geolocation exist). + final Observed mediaDevices; + final Observed geolocation; + /// The raw collector payload (debug export / fixture recording). final String? rawPayload; @@ -271,6 +362,11 @@ class BrowserSnapshot { this.mainStatus = mainStatusNotReported, this.resourcesTruncated = false, this.resourcesDropped = 0, + this.frames = kFramesNotCollected, + this.frameCount = 0, + this.siteStorage = kSiteStorageNotCollected, + this.mediaDevices = kCapabilityNotCollected, + this.geolocation = kCapabilityNotCollected, this.rawPayload, }); @@ -298,7 +394,9 @@ class BrowserSnapshot { BrowserSnapshot copyWith( {Observed>? cookies, - Observed? mainStatus}) => + Observed? mainStatus, + Observed? mediaDevices, + Observed? geolocation}) => BrowserSnapshot( url: url, platform: platform, @@ -312,6 +410,11 @@ class BrowserSnapshot { mainStatus: mainStatus ?? this.mainStatus, resourcesTruncated: resourcesTruncated, resourcesDropped: resourcesDropped, + frames: frames, + frameCount: frameCount, + siteStorage: siteStorage, + mediaDevices: mediaDevices ?? this.mediaDevices, + geolocation: geolocation ?? this.geolocation, rawPayload: rawPayload, ); @@ -333,6 +436,8 @@ class BrowserSnapshot { storage: Unavailable(reason, detail), navigation: Unavailable(reason, detail), snapshotTimeMs: Unavailable(reason, detail), + frames: Unavailable(reason, detail), + siteStorage: Unavailable(reason, detail), rawPayload: rawPayload, ); @@ -353,6 +458,8 @@ class BrowserSnapshot { storage: Failed(error), navigation: Failed(error), snapshotTimeMs: Failed(error), + frames: Failed(error), + siteStorage: Failed(error), rawPayload: rawPayload, ); } diff --git a/lib/models/inspection/page_inspection.dart b/lib/models/inspection/page_inspection.dart index d5cdf7a..f5a6fd1 100644 --- a/lib/models/inspection/page_inspection.dart +++ b/lib/models/inspection/page_inspection.dart @@ -3,6 +3,7 @@ import 'dart:typed_data'; import 'browser_snapshot.dart'; import 'file_snapshot.dart'; import 'observed.dart'; +import 'permission_request.dart'; import 'server_snapshot.dart'; /// [PageInspection.freshLoad] before the user asked for one. @@ -75,6 +76,9 @@ class PageInspection { final Observed liveScreenshot; final Observed freshScreenshot; + /// Native permission requests the page made (all denied), oldest first. + final List permissionRequests; + const PageInspection({ required this.generation, required this.target, @@ -90,6 +94,7 @@ class PageInspection { this.freshEstimates = const {}, this.liveScreenshot = kNoScreenshot, this.freshScreenshot = kNoScreenshot, + this.permissionRequests = const [], }); const PageInspection.empty() @@ -110,7 +115,8 @@ class PageInspection { freshLoad = kFreshLoadNotMeasured, freshEstimates = const {}, liveScreenshot = kNoScreenshot, - freshScreenshot = kNoScreenshot; + freshScreenshot = kNoScreenshot, + permissionRequests = const []; PageInspection copyWith({ Observed? server, @@ -125,6 +131,7 @@ class PageInspection { Map>? freshEstimates, Observed? liveScreenshot, Observed? freshScreenshot, + List? permissionRequests, }) => PageInspection( generation: generation, @@ -143,6 +150,7 @@ class PageInspection { freshEstimates: freshEstimates ?? this.freshEstimates, liveScreenshot: liveScreenshot ?? this.liveScreenshot, freshScreenshot: freshScreenshot ?? this.freshScreenshot, + permissionRequests: permissionRequests ?? this.permissionRequests, ); bool _has(DataSource s) => switch (s) { diff --git a/lib/models/inspection/permission_request.dart b/lib/models/inspection/permission_request.dart new file mode 100644 index 0000000..f3b8e01 --- /dev/null +++ b/lib/models/inspection/permission_request.dart @@ -0,0 +1,18 @@ +/// A native permission request (camera, microphone, location, ...) a page +/// made. The app always denies it; this only records that it was asked. +class PermissionRequestEvent { + /// Who asked, e.g. https://maps.test + final String origin; + + /// Lower-case names: 'camera', 'microphone', 'geolocation', ... + final List resources; + + /// Time since the last main-frame load start; null when none was seen. + final Duration? sinceLoadStart; + + const PermissionRequestEvent({ + required this.origin, + required this.resources, + required this.sinceLoadStart, + }); +} diff --git a/lib/screens/about_screen.dart b/lib/screens/about_screen.dart index ddcb707..54445e1 100644 --- a/lib/screens/about_screen.dart +++ b/lib/screens/about_screen.dart @@ -43,6 +43,10 @@ class AboutScreen extends StatelessWidget { Icons.timeline, 'Network: request timeline, headers, cookies, probe and security (incl. TLS)' ), + ( + Icons.admin_panel_settings_outlined, + 'Permissions: what the page may use, hands to embedded frames and stores on your device' + ), ( Icons.folder_open, 'Open local files, share URLs/files/text into the app, URL history' diff --git a/lib/screens/home_screen.dart b/lib/screens/home_screen.dart index a014ff0..5b54423 100644 --- a/lib/screens/home_screen.dart +++ b/lib/screens/home_screen.dart @@ -18,6 +18,7 @@ import 'package:view_source_vibe/widgets/services_view.dart'; import 'package:view_source_vibe/widgets/media_view.dart'; import 'package:view_source_vibe/widgets/privacy_view.dart'; import 'package:view_source_vibe/widgets/probe_views.dart'; +import 'package:view_source_vibe/widgets/permissions_view.dart'; import 'package:view_source_vibe/widgets/request_timeline_view.dart'; import 'package:view_source_vibe/widgets/sovereignty_view.dart'; import 'package:view_source_vibe/widgets/dom_tree_view.dart'; @@ -428,6 +429,7 @@ class HomeScreenState extends State with TickerProviderStateMixin { AppTab.privacy => page(tab, const PrivacyView()), AppTab.sovereignty => page(tab, const SovereigntyView()), AppTab.security => page(tab, const ProbeSecurityView()), + AppTab.permissions => page(tab, const PermissionsView()), AppTab.cookies => page(tab, const ProbeCookiesView()), }, ]; diff --git a/lib/services/collectors/browser_payload.dart b/lib/services/collectors/browser_payload.dart index b13da6f..17775b8 100644 --- a/lib/services/collectors/browser_payload.dart +++ b/lib/services/collectors/browser_payload.dart @@ -104,6 +104,13 @@ BrowserSnapshot parseBrowserPayload(Object? raw, BrowserPlatform platform, storage: _parseStorage(p['storage']), navigation: _section(p['navigation'], (m) => _parseNavigation(m, _isWebPage(effectiveUrl))), mainStatus: parseMainStatus(p['navigation']), + frames: _section(p['frames'], (m) => _parseFrames(m, effectiveUrl)), + frameCount: p['frames'] is Map && p['frames']['total'] is num + ? (p['frames']['total'] as num).toInt() + : 0, + siteStorage: _parseSiteStorage(p['siteStorage']), + mediaDevices: _capability(p['capabilities'], 'mediaDevices'), + geolocation: _capability(p['capabilities'], 'geolocation'), snapshotTimeMs: _section(p['timing'], (m) { final n = m['now']; if (n is! num) throw const FormatException('timing.now missing'); @@ -541,6 +548,76 @@ Observed _parseArea(Object? a) { } } +// ---- Frames and site storage ---------------------------------------------- + +List _parseFrames(Map m, Uri pageUrl) { + final out = []; + for (final f in (m['items'] as List? ?? const [])) { + if (f is! Map) continue; + final raw = f['src']; + var src = ''; + if (raw is String && raw.isNotEmpty && raw != 'about:blank') { + try { + src = pageUrl.resolve(raw).toString(); + } catch (_) { + src = raw; + } + } + final sandbox = f['sandbox']; + out.add(FrameInfo( + src: src, + allow: f['allow'] is String ? f['allow'] as String : '', + sandbox: sandbox is String ? sandbox : null, + )); + } + return out; +} + +/// A missing or malformed flag is Unavailable, never false. +Observed _capability(Object? sec, String key) { + final v = sec is Map && sec['ok'] == true ? sec[key] : null; + return v is bool ? _measured(v) : kCapabilityNotCollected; +} + +Observed _parseSiteStorage(Object? sec) { + if (sec == null) return kSiteStorageNotCollected; + if (sec is! Map) return const Failed('section is not an object'); + if (sec['ok'] != true) return Failed('${sec['error'] ?? 'unknown error'}'); + if (sec['pending'] == true) return const Pending(); + + Observed bytes(Object? v) => v is num + ? _measured(v.toInt()) + : const Unavailable(UnavailableReason.notReportedByPlatform, + 'navigator.storage.estimate() reported nothing'); + + Observed> list(Object? part, String key, T Function(Object?) conv) { + if (part is! Map) { + return const Unavailable( + UnavailableReason.notReportedByPlatform, 'not collected'); + } + if (part['ok'] != true) { + return Unavailable(UnavailableReason.notReportedByPlatform, + '${part['error'] ?? 'not reported by this browser'}'); + } + return _measured([ + for (final e in (part[key] as List? ?? const [])) conv(e), + ]); + } + + return _measured(SiteStorage( + usageBytes: bytes(sec['usage']), + quotaBytes: bytes(sec['quota']), + indexedDbNames: list(sec['idb'], 'names', (e) => '$e'), + cacheNames: list(sec['caches'], 'names', (e) => '$e'), + serviceWorkers: list( + sec['sw'], + 'items', + (e) => ServiceWorkerInfo( + scope: e is Map ? '${e['scope'] ?? ''}' : '', + script: e is Map ? '${e['script'] ?? ''}' : '')), + )); +} + // ---- Cookies ------------------------------------------------------------- /// Thrown inside the resources section when nothing was ever measured. diff --git a/lib/services/collectors/vsv_collect_js.dart b/lib/services/collectors/vsv_collect_js.dart index bbb8090..2f3c2f9 100644 --- a/lib/services/collectors/vsv_collect_js.dart +++ b/lib/services/collectors/vsv_collect_js.dart @@ -440,6 +440,102 @@ window.__vsv_collect = function(opts) { out.cookies = section(function() { return {raw: document.cookie || ''}; }); + // Every iframe's delegation attributes (the cap keeps the payload small). + out.frames = section(function() { + var list = document.getElementsByTagName('iframe'); + var items = []; + for (var i = 0; i < list.length && i < 200; i++) { + var f = list[i]; + items.push({ + src: f.getAttribute('src') || '', + allow: f.getAttribute('allow') || '', + sandbox: f.getAttribute('sandbox') + }); + } + return {total: list.length, items: items}; + }); + + // What the page can ask for at all: a WebView may lack these APIs. + out.capabilities = section(function() { + var n = typeof navigator !== 'undefined' ? navigator : {}; + return { + mediaDevices: !!(n.mediaDevices && n.mediaDevices.getUserMedia), + geolocation: !!n.geolocation + }; + }); + + // What the site stored on the device. The lookups are promises, so they + // start once per document and a later collect picks the results up. + function startSiteStorage() { + if (window.__vsv_site) return; + var site = window.__vsv_site = {done: false, started: Date.now()}; + var left = 4; + function settle() { if (--left === 0) site.done = true; } + function track(key, api, call, shape) { + var p; + try { p = call(); } catch (e) { + site[key] = {ok: false, error: describe(e)}; settle(); return; + } + if (!p || typeof p.then !== 'function') { + site[key] = {ok: false, error: api + ' is not supported'}; settle(); return; + } + try { + p.then(function(v) { + try { site[key] = shape(v); } + catch (e) { site[key] = {ok: false, error: describe(e)}; } + settle(); + }).catch(function(e) { + site[key] = {ok: false, error: describe(e)}; settle(); + }); + } catch (e) { + site[key] = {ok: false, error: describe(e)}; settle(); + } + } + var nav = typeof navigator !== 'undefined' ? navigator : {}; + site.usage = null; site.quota = null; + track('est', 'navigator.storage.estimate', + function() { return nav.storage && nav.storage.estimate ? nav.storage.estimate() : null; }, + function(e) { site.usage = num(e.usage); site.quota = num(e.quota); return {ok: true}; }); + track('idb', 'indexedDB.databases', + function() { return typeof indexedDB !== 'undefined' && indexedDB.databases ? indexedDB.databases() : null; }, + function(dbs) { + var names = []; + for (var i = 0; i < dbs.length && i < 100; i++) names.push(String(dbs[i].name)); + return {ok: true, names: names}; + }); + track('caches', 'caches.keys', + function() { return typeof caches !== 'undefined' && caches.keys ? caches.keys() : null; }, + function(keys) { + var names = []; + for (var i = 0; i < keys.length && i < 100; i++) names.push(String(keys[i])); + return {ok: true, names: names}; + }); + track('sw', 'navigator.serviceWorker.getRegistrations', + function() { return nav.serviceWorker && nav.serviceWorker.getRegistrations ? nav.serviceWorker.getRegistrations() : null; }, + function(regs) { + var items = []; + for (var i = 0; i < regs.length && i < 50; i++) { + var w = regs[i].active || regs[i].waiting || regs[i].installing; + items.push({scope: String(regs[i].scope), script: w ? String(w.scriptURL) : ''}); + } + return {ok: true, items: items}; + }); + } + out.siteStorage = section(function() { + startSiteStorage(); + var s = window.__vsv_site; + if (!s) return {pending: true}; + if (!s.done) { + // One API that never answers must not keep the section pending. + if (!(Date.now() - s.started > 4000)) return {pending: true}; + var late = ['idb', 'caches', 'sw']; + for (var i = 0; i < late.length; i++) { + if (!s[late[i]]) s[late[i]] = {ok: false, error: 'no answer within 4 s'}; + } + } + return {usage: s.usage, quota: s.quota, idb: s.idb, caches: s.caches, sw: s.sw}; + }); + try { return JSON.stringify(out); } catch (e) { return JSON.stringify({v: 1, fatal: describe(e)}); } }; diff --git a/lib/services/derive/permissions_policy.dart b/lib/services/derive/permissions_policy.dart new file mode 100644 index 0000000..9711c3b --- /dev/null +++ b/lib/services/derive/permissions_policy.dart @@ -0,0 +1,197 @@ +/// Parsers for `Permissions-Policy`, `Feature-Policy` and iframe `allow`. +library; + +class Allowlist { + final bool all; // * + final bool self; // self / 'self' + final bool src; // 'src' (iframe allow only) + final List origins; + bool get none => !all && !self && !src && origins.isEmpty; + const Allowlist( + {this.all = false, + this.self = false, + this.src = false, + this.origins = const []}); +} + +class ParsedPolicy { + final Map features; // header order + final List invalid; // members that could not be parsed + const ParsedPolicy(this.features, this.invalid); +} + +/// Features that reach the device or the user: shown first, flagged when +/// delegated to a third party. +const Set powerfulFeatures = { + 'camera', + 'microphone', + 'geolocation', + 'display-capture', + 'payment', + 'usb', + 'serial', + 'hid', + 'bluetooth', + 'midi', + 'clipboard-read', + 'publickey-credentials-get', + 'screen-wake-lock', + 'idle-detection', + 'accelerometer', + 'gyroscope', + 'magnetometer', + 'browsing-topics', + 'attribution-reporting', + 'local-fonts', +}; + +final _featureName = RegExp(r'^[a-z][a-z0-9_.-]*$'); +final _origin = RegExp(r'^[a-z][a-z0-9+.-]*://[^\s/"]+$', caseSensitive: false); + +/// Splits [s] on [sep] outside double quotes; empty parts are dropped. +List _split(String s, Pattern sep, {bool quotes = true}) { + final parts = []; + final buf = StringBuffer(); + var inQuote = false; + for (final rune in s.split('')) { + if (quotes && rune == '"') inQuote = !inQuote; + if (!inQuote && _isSep(rune, sep)) { + parts.add(buf.toString()); + buf.clear(); + } else { + buf.write(rune); + } + } + parts.add(buf.toString()); + return parts.map((p) => p.trim()).where((p) => p.isNotEmpty).toList(); +} + +bool _isSep(String ch, Pattern sep) => + sep is String ? ch == sep : sep.allMatches(ch).isNotEmpty; + +/// Drops structured-field parameters (`;report-to=x`) outside quotes. +String _dropParams(String s) { + var inQuote = false; + for (var i = 0; i < s.length; i++) { + final c = s[i]; + if (c == '"') inQuote = !inQuote; + if (c == ';' && !inQuote) return s.substring(0, i).trim(); + } + return s.trim(); +} + +/// Adds [name] -> [list]; a repeat keeps the first and is reported invalid. +void _add(Map features, List invalid, String name, + Allowlist list) { + if (features.containsKey(name)) { + invalid.add('$name (repeated)'); + } else { + features[name] = list; + } +} + +/// `Permissions-Policy: geolocation=(), camera=(self "https://a.test"), fullscreen=*` +ParsedPolicy parsePermissionsPolicy(String header) { + final features = {}; + final invalid = []; + // Commas are only valid between members; a stray unclosed "(" must not + // swallow the members after it, so parentheses are not tracked here. + for (final member in _split(header, ',')) { + final eq = member.indexOf('='); + final name = + (eq < 0 ? member : member.substring(0, eq)).trim().toLowerCase(); + if (eq < 0 || !_featureName.hasMatch(name)) { + invalid.add(member); + continue; + } + final list = _parseValue(member.substring(eq + 1).trim()); + if (list == null) { + invalid.add(member); + } else { + _add(features, invalid, name, list); + } + } + return ParsedPolicy(features, invalid); +} + +Allowlist? _parseValue(String value) { + List items; + if (value.startsWith('(')) { + final close = value.lastIndexOf(')'); + if (close < 0) return null; + // Parameters may only follow the closing parenthesis. + final rest = value.substring(close + 1).trim(); + if (rest.isNotEmpty && !rest.startsWith(';')) return null; + items = _split(value.substring(1, close), RegExp(r'\s')); + } else { + items = [_dropParams(value)]; + } + var all = false, self = false; + final origins = []; + for (final raw in items) { + final item = _dropParams(raw); + if (item == '*') { + all = true; + } else if (item == 'self') { + self = true; + } else if (item.length > 1 && + item.startsWith('"') && + item.endsWith('"') && + _origin.hasMatch(item.substring(1, item.length - 1))) { + origins.add(item.substring(1, item.length - 1)); + } else { + return null; + } + } + return Allowlist(all: all, self: self, origins: origins); +} + +/// `Feature-Policy` header and iframe `allow` attribute: +/// `geolocation 'none'; camera 'self' https://a.test; fullscreen *`. +/// A feature without a value means 'src' for [iframeAllow], 'self' otherwise. +ParsedPolicy parseFeaturePolicy(String value, {bool iframeAllow = false}) { + final features = {}; + final invalid = []; + for (final directive in _split(value, ';', quotes: false)) { + final tokens = directive.split(RegExp(r'\s+')); + final name = tokens.first.toLowerCase(); + if (!_featureName.hasMatch(name)) { + invalid.add(directive); + continue; + } + final values = tokens.skip(1); + if (values.isEmpty) { + _add( + features, + invalid, + name, + iframeAllow + ? const Allowlist(src: true) + : const Allowlist(self: true)); + continue; + } + var all = false, self = false, src = false; + final origins = []; + for (final t in values) { + switch (t) { + case '*': + all = true; + case "'self'": + self = true; + case "'src'": + src = true; + case "'none'": + break; + default: + if (_origin.hasMatch(t)) { + origins.add(t); + } else { + invalid.add('$name: $t (invalid token)'); + } + } + } + _add(features, invalid, name, + Allowlist(all: all, self: self, src: src, origins: origins)); + } + return ParsedPolicy(features, invalid); +} diff --git a/lib/services/derive/permissions_view.dart b/lib/services/derive/permissions_view.dart new file mode 100644 index 0000000..91570d0 --- /dev/null +++ b/lib/services/derive/permissions_view.dart @@ -0,0 +1,261 @@ +import '../../models/inspection/browser_snapshot.dart'; +import '../../models/inspection/observed.dart'; +import '../../models/inspection/page_inspection.dart'; +import '../../models/inspection/permission_request.dart'; +import '../../models/inspection/server_snapshot.dart'; +import '../../utils/site.dart'; +import 'permissions_policy.dart'; + +/// One feature the page's own policy header speaks about. +class DeclaredFeature { + final String feature; + + /// 'nobody' | 'this site' | 'everyone' | 'this site, https://a.test' + final String allowed; + final bool powerful; + const DeclaredFeature(this.feature, this.allowed, this.powerful); +} + +/// An iframe the page gave an `allow` attribute. +class Delegation { + /// '' for srcdoc / about:blank frames. + final String frameHost; + final bool thirdParty; + + /// Features the frame may use; powerful ones first. + final List features; + final bool sandboxed; + const Delegation({ + required this.frameHost, + required this.thirdParty, + required this.features, + required this.sandboxed, + }); +} + +class PermissionsModel { + /// Present: the header was sent (possibly with no features). Unavailable + /// (notReportedByServer): no Permissions-Policy or Feature-Policy header. + final Observed> declared; + final List invalidDeclarations; + + /// Only Feature-Policy was sent. + final bool legacyHeaderOnly; + + /// Only frames with an allow attribute. + final Observed> delegations; + /// How many iframes the page has; unknown when the browser reported none. + final Observed frameCount; + final List requests; + final Observed stored; + + /// Local and session storage: counts and sizes only, never values. + final Observed webStorage; + + /// Plain sentences for the top of the tab. + final List notes; + + const PermissionsModel({ + required this.declared, + required this.invalidDeclarations, + required this.legacyHeaderOnly, + required this.delegations, + required this.frameCount, + required this.requests, + required this.stored, + required this.webStorage, + required this.notes, + }); +} + +/// Same state, another value type (never a value). +Observed _carry(Observed o, String missing) => switch (o) { + Pending() => Pending(o.detail), + Failed() => Failed(o.error), + Unavailable() => Unavailable(o.reason, o.detail), + Present() => Unavailable(UnavailableReason.notApplicable, missing), + }; + +List _ordered(Iterable features) { + final set = features.toSet(); + return [ + ...powerfulFeatures.where(set.contains), + ...(set.difference(powerfulFeatures).toList()..sort()), + ]; +} + +String _allowedText(Allowlist a) { + if (a.none) return 'nobody'; + if (a.all) return 'everyone'; + return [ + if (a.self) 'this site', + if (a.src) "the frame's own origin", + ...a.origins, + ].join(', '); +} + +String? _header(Map> headers, String name) { + return headers[name]?.join(', '); +} + +/// '2.3 s' / '12 s' after the load start; 'time unknown' when no load start +/// was seen. Rounded to tenths below 10 s, whole seconds above. +String formatSinceLoadStart(Duration? d) { + if (d == null) return 'time unknown'; + final tenths = (d.inMilliseconds / 100).round(); + return tenths < 100 + ? '${(tenths / 10).toStringAsFixed(1)} s' + : '${(tenths / 10).round()} s'; +} + +Observed derivePermissions(PageInspection p) { + if (p.target is! UrlTarget) { + return const Unavailable( + UnavailableReason.notApplicable, 'only for web pages'); + } + + final server = p.server, browser = p.browser; + final serverSnap = server is Present ? server.value : null; + final browserSnap = + browser is Present ? browser.value : null; + if (serverSnap == null && browserSnap == null) { + if (server is Pending || browser is Pending) return const Pending(); + return const Unavailable(UnavailableReason.notApplicable, + 'neither the server nor the browser answered'); + } + + final pageUrl = (p.target as UrlTarget).url; + final pageHost = browserSnap?.url.host ?? pageUrl.host; + + // Declared policy, from the final response's headers. + Observed> declared; + var invalid = []; + var legacyOnly = false; + var noHeader = false; + if (serverSnap == null || serverSnap.hops.isEmpty) { + declared = server is Pending + ? const Pending() + : const Unavailable(UnavailableReason.notReportedByServer, + 'the server response is not available'); + } else { + final headers = serverSnap.hops.last.headers; + final pp = _header(headers, 'permissions-policy'); + final fp = _header(headers, 'feature-policy'); + if (pp == null && fp == null) { + noHeader = true; + declared = const Unavailable(UnavailableReason.notReportedByServer, + 'no Permissions-Policy or Feature-Policy header'); + } else { + legacyOnly = pp == null; + final features = {}; + if (fp != null) { + // Each header value on its own (a ',' is not a separator here); + // for a feature named twice the first header wins. + for (final value in headers['feature-policy']!) { + final parsed = parseFeaturePolicy(value); + for (final e in parsed.features.entries) { + features.putIfAbsent(e.key, () => e.value); + } + invalid = [...invalid, ...parsed.invalid]; + } + } + if (pp != null) { + final parsed = parsePermissionsPolicy(pp); + features.addAll(parsed.features); // the newer header wins + invalid = [...parsed.invalid, ...invalid]; + } + declared = Present([ + for (final f in _ordered(features.keys)) + DeclaredFeature( + f, _allowedText(features[f]!), powerfulFeatures.contains(f)), + ], source: DataSource.server, provenance: Provenance.derived); + } + } + + // Delegation to iframes. + Observed> delegations; + final frames = browserSnap?.frames; + if (frames is Present>) { + final list = []; + for (final f in frames.value) { + if (f.allow.trim().isEmpty) continue; + final allowed = parseFeaturePolicy(f.allow, iframeAllow: true) + .features + .entries + .where((e) => !e.value.none) + .map((e) => e.key); + final host = f.src.isEmpty ? '' : (Uri.tryParse(f.src)?.host ?? ''); + list.add(Delegation( + frameHost: host, + thirdParty: host.isNotEmpty && !sameSite(host, pageHost), + features: _ordered(allowed), + sandboxed: f.sandbox != null, + )); + } + delegations = Present(list, + source: DataSource.browser, provenance: Provenance.derived); + } else { + delegations = _carry>( + frames ?? browser, 'the browser did not report frames'); + } + + final stored = browserSnap?.siteStorage ?? + _carry(browser, 'the browser did not report storage'); + + final notes = [ + if (noHeader) + "No Permissions-Policy header: embedded frames keep the browser's defaults.", + ]; + if (browserSnap?.mediaDevices case Present(value: false)) { + notes.add( + 'This browser does not offer camera or microphone to pages, so a page cannot ask for them here.'); + } + if (browserSnap?.geolocation case Present(value: false)) { + notes.add( + 'This browser does not offer location to pages, so a page cannot ask for it here.'); + } + if (delegations is Present>) { + final risky = delegations.value + .where((d) => d.thirdParty && d.features.any(powerfulFeatures.contains)) + .toList(); + if (risky.isNotEmpty) { + final union = _ordered( + risky.expand((d) => d.features.where(powerfulFeatures.contains))); + notes.add( + '${risky.length} third-party frame(s) may use: ${union.join(', ')}'); + } + } + for (final r in p.permissionRequests) { + notes.add( + r.sinceLoadStart == null + ? 'The page asked for ${r.resources.join(', ')} (time since load start unknown).' + : 'The page asked for ${r.resources.join(', ')} ${formatSinceLoadStart(r.sinceLoadStart)} after it started loading.'); + } + if (stored is Present) { + final sw = stored.value.serviceWorkers; + if (sw is Present> && sw.value.isNotEmpty) { + notes.add( + 'A service worker is installed: the site can run code in the background.'); + } + } + + return Present( + PermissionsModel( + declared: declared, + invalidDeclarations: invalid, + legacyHeaderOnly: legacyOnly, + delegations: delegations, + frameCount: frames is Present> + ? Present(browserSnap!.frameCount, + source: DataSource.browser, provenance: Provenance.derived) + : _carry(frames ?? browser, 'the browser did not report frames'), + requests: p.permissionRequests, + stored: stored, + webStorage: browserSnap?.storage ?? + _carry( + browser, 'the browser did not report web storage'), + notes: notes, + ), + source: DataSource.browser, + provenance: Provenance.derived); +} diff --git a/lib/services/html_service.dart b/lib/services/html_service.dart index 4a982b2..45f1fb1 100644 --- a/lib/services/html_service.dart +++ b/lib/services/html_service.dart @@ -25,6 +25,7 @@ import 'package:xml/xml.dart' as xml; 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/permission_request.dart'; import 'package:view_source_vibe/models/inspection/server_snapshot.dart'; import 'package:view_source_vibe/services/collectors/browser_loader.dart'; import 'package:view_source_vibe/services/collectors/collector_world.dart'; @@ -38,6 +39,7 @@ import 'package:view_source_vibe/services/derive/block_page.dart'; import 'package:view_source_vibe/services/derive/header_insights.dart'; import 'package:view_source_vibe/services/derive/probe_general.dart'; import 'package:view_source_vibe/services/derive/request_timeline.dart'; +import 'package:view_source_vibe/services/derive/permissions_view.dart'; import 'package:view_source_vibe/services/derive/security_audit.dart'; import 'package:view_source_vibe/services/collectors/fresh_load_measurer.dart'; import 'package:view_source_vibe/services/collectors/transfer_size_lookup.dart'; @@ -114,6 +116,7 @@ class HtmlService extends ChangeNotifier { /// Removes the browser's data for one site (long-press Reload); a seam /// for tests. final Future Function(Uri url) _clearSiteData; + /// Null: the default lookup on the per-navigation shared client. final ResourceSizeLookup? _sizeLookup; final FreshLoadMeasurer _freshMeasurer; @@ -881,6 +884,32 @@ class HtmlService extends ChangeNotifier { return _security!; } + Observed? _permissions; + ({ + InspectionTarget target, + Observed browser, + Observed server, + List requests + })? _permissionsFor; + + /// The Permissions tab: declared policy, delegation to frames, requests + /// the app denied, what is stored. Cached on the inputs, not on the + /// PageInspection object. + Observed get permissionsView { + final current = store.current; + final key = ( + target: current.target, + browser: current.browser, + server: current.server, + requests: current.permissionRequests + ); + if (_permissions == null || _permissionsFor != key) { + _permissionsFor = key; + _permissions = derivePermissions(current); + } + return _permissions!; + } + Observed? _cookiesView; ({ Observed browser, @@ -1482,7 +1511,10 @@ class HtmlService extends ChangeNotifier { // In low-resource mode the Browser WebView is paused once its tab // is hidden, but extraction evaluates JS in it — so pause only // after the extraction has finished. - reader.extractIfStale().whenComplete(_applyWebViewResourceMode).ignore(); + reader + .extractIfStale() + .whenComplete(_applyWebViewResourceMode) + .ignore(); } else { _applyWebViewResourceMode(); } @@ -4370,10 +4402,9 @@ Technical details: $message'''; /// Our own download of a resource, measured like the browser's transfer /// size (compressed bytes plus headers); see [lookupTransferSize]. - Future _lookupResourceSize(int gen, String url) => lookupTransferSize( - url, - userAgent: kServerUserAgent, - shared: _clientFor(gen)); + Future _lookupResourceSize(int gen, String url) => + lookupTransferSize(url, + userAgent: kServerUserAgent, shared: _clientFor(gen)); /// The browser reported a URL change (history API / SPA route, redirect). /// The same document as the current navigation (R20) is nothing new; any @@ -4597,8 +4628,55 @@ Technical details: $message'''; /// load, for engines whose page cannot read its own status (WebKit). ({int gen, int status})? _browserHttpError; - /// The browser started loading a new main-frame document. - void onBrowserLoadStart() => _browserHttpError = null; + /// The browser started loading a new main-frame document ([url] when + /// known: an in-view navigation starts its generation only later, so the + /// load start is matched to the page by URL). + void onBrowserLoadStart([Uri? url]) { + _browserHttpError = null; + _loadStartGen = store.current.generation; + _loadStartUrl = url; + _loadStartAt = DateTime.now(); + _loadStartSeq++; + } + + /// Counts main-frame load starts. A native callback reads it on entry and + /// hands it back with the request, so a request that waited (for the + /// WebView's URL) across a navigation or reload is dropped. + int get loadStartSeq => _loadStartSeq; + int _loadStartSeq = 0; + + int? _loadStartGen; + Uri? _loadStartUrl; + DateTime? _loadStartAt; + + /// The page asked for a native permission (the browser denied it). Only + /// recorded when the WebView's main frame ([mainFrameUrl]) is the page + /// being inspected; a request from a page already left is dropped. The + /// request's own [origin] may differ (embedded frames). + void onBrowserPermissionRequest( + String origin, List resources, Uri? mainFrameUrl, + {required int loadSeq}) { + if (_disposed || + loadSeq != _loadStartSeq || + mainFrameUrl == null || + !_storeCovers(mainFrameUrl)) { + return; + } + final start = _loadStartAt; + final startUrl = _loadStartUrl; + final startBelongs = start != null && + (startUrl != null + ? _storeCovers(startUrl) + : _loadStartGen == store.current.generation); + store.addPermissionRequest( + store.current.generation, + PermissionRequestEvent( + origin: origin, + resources: resources, + sinceLoadStart: + startBelongs ? DateTime.now().difference(start) : null, + )); + } /// The WebView received an HTTP error for the main frame. void onBrowserMainFrameHttpError(int status) => diff --git a/lib/services/inspection_export_data.dart b/lib/services/inspection_export_data.dart index f8137ed..9ecb052 100644 --- a/lib/services/inspection_export_data.dart +++ b/lib/services/inspection_export_data.dart @@ -8,6 +8,7 @@ import 'derive/cookies_view.dart'; import 'derive/observed_text.dart' show notPresentAs; import 'derive/page_weight.dart'; import 'derive/probe_general.dart'; +import 'derive/permissions_view.dart'; import 'derive/security_audit.dart'; import 'observed_json.dart'; @@ -345,6 +346,91 @@ class InspectionExportData { }; } + static Object? _area(StorageArea a) => { + 'count': a.count, + 'bytes': observedPlain(a.bytes, source: DataSource.browser), + }; + + /// analysis/permissions.json: names, counts and sizes; never a stored + /// value. + static Map permissions(Observed p) => + observedJson( + p, + (m) => { + 'notes': m.notes, + 'declared': observedJson( + m.declared, + (list) => [ + for (final f in list) + { + 'feature': f.feature, + 'allowed': f.allowed, + 'powerful': f.powerful, + }, + ], + source: DataSource.server, + provenance: Provenance.derived, + ), + 'invalidDeclarations': m.invalidDeclarations, + 'legacyHeaderOnly': m.legacyHeaderOnly, + 'delegations': observedJson( + m.delegations, + (list) => [ + for (final d in list) + { + 'frameHost': d.frameHost, + 'thirdParty': d.thirdParty, + 'features': d.features, + 'sandboxed': d.sandboxed, + }, + ], + source: DataSource.browser, + provenance: Provenance.derived, + ), + 'frameCount': observedPlain(m.frameCount, source: DataSource.browser), + 'requests': [ + for (final r in m.requests) + { + 'origin': r.origin, + 'resources': r.resources, + 'sinceLoadStartMs': r.sinceLoadStart?.inMilliseconds, + }, + ], + 'stored': observedJson( + m.stored, + (s) => { + 'usageBytes': + observedPlain(s.usageBytes, source: DataSource.browser), + 'quotaBytes': + observedPlain(s.quotaBytes, source: DataSource.browser), + 'indexedDbNames': + observedPlain(s.indexedDbNames, source: DataSource.browser), + 'cacheNames': + observedPlain(s.cacheNames, source: DataSource.browser), + 'serviceWorkers': observedJson( + s.serviceWorkers, + (list) => [ + for (final w in list) {'scope': w.scope, 'script': w.script}, + ], + source: DataSource.browser, + ), + }, + source: DataSource.browser, + ), + 'webStorage': observedJson( + m.webStorage, + (w) => { + 'local': observedJson(w.local, _area, source: DataSource.browser), + 'session': + observedJson(w.session, _area, source: DataSource.browser), + }, + source: DataSource.browser, + ), + }, + source: DataSource.browser, + provenance: Provenance.derived, + ); + /// analysis/security-scorecard.json static Map securityScorecard(Observed s) => observedJson( diff --git a/lib/services/inspection_export_service.dart b/lib/services/inspection_export_service.dart index ea74b2d..4100344 100644 --- a/lib/services/inspection_export_service.dart +++ b/lib/services/inspection_export_service.dart @@ -224,6 +224,8 @@ class InspectionExportService { addJson('analysis/detections.json', detectionsJson); addJson('analysis/security-scorecard.json', scorecardJson); addJson('analysis/certificate.json', certificateJson); + addJson('analysis/permissions.json', + InspectionExportData.permissions(htmlService.permissionsView)); addJson('analysis/robots-sitemap.json', robotsJson); addJson( 'analysis/structured-data.json', diff --git a/lib/services/inspection_store.dart b/lib/services/inspection_store.dart index c232e88..e4a9b2a 100644 --- a/lib/services/inspection_store.dart +++ b/lib/services/inspection_store.dart @@ -7,6 +7,7 @@ import '../models/inspection/browser_snapshot.dart'; import '../models/inspection/file_snapshot.dart'; import '../models/inspection/observed.dart'; import '../models/inspection/page_inspection.dart'; +import '../models/inspection/permission_request.dart'; import '../models/inspection/server_snapshot.dart'; import 'collectors/browser_payload.dart'; @@ -146,6 +147,16 @@ class InspectionStore extends ChangeNotifier { _set(_current.copyWith(declaredMedia: urls)); } + static const _maxPermissionRequests = 50; + + /// A denied native permission request, kept for the page that asked only. + void addPermissionRequest(int gen, PermissionRequestEvent e) { + if (!_accepts(gen)) return; + if (_current.permissionRequests.length >= _maxPermissionRequests) return; + _set(_current + .copyWith(permissionRequests: [..._current.permissionRequests, e])); + } + /// The live load stopped requesting (see [PageInspection.browserSettled]). void markBrowserSettled(int gen) { if (!_accepts(gen) || _current.browserSettled) return; @@ -280,6 +291,11 @@ bool _sameBrowser(BrowserSnapshot a, BrowserSnapshot b) { sameObserved(a.navigation, b.navigation) && sameObserved(a.snapshotTimeMs, b.snapshotTimeMs) && sameObserved(a.mainStatus, b.mainStatus) && + sameObserved(a.frames, b.frames) && + a.frameCount == b.frameCount && + sameObserved(a.siteStorage, b.siteStorage) && + sameObserved(a.mediaDevices, b.mediaDevices) && + sameObserved(a.geolocation, b.geolocation) && a.resourcesTruncated == b.resourcesTruncated && a.resourcesDropped == b.resourcesDropped && a.rawPayload == b.rawPayload; @@ -304,6 +320,13 @@ BrowserSnapshot _merge(BrowserSnapshot old, BrowserSnapshot n) { storage: _fresher(old.storage, n.storage), navigation: _fresher(old.navigation, n.navigation), snapshotTimeMs: _fresher(old.snapshotTimeMs, n.snapshotTimeMs), + frames: _fresher(old.frames, n.frames), + frameCount: old.frames is Present && n.frames is! Present + ? old.frameCount + : n.frameCount, + siteStorage: _fresher(old.siteStorage, n.siteStorage), + mediaDevices: _fresher(old.mediaDevices, n.mediaDevices), + geolocation: _fresher(old.geolocation, n.geolocation), // Per document: the newest collect knows best (a reload after a // challenge page must not keep the challenge's 403). mainStatus: sameObserved(old.mainStatus, n.mainStatus) diff --git a/lib/services/permission_resources.dart b/lib/services/permission_resources.dart new file mode 100644 index 0000000..3c8cc5c --- /dev/null +++ b/lib/services/permission_resources.dart @@ -0,0 +1,17 @@ +import 'package:flutter_inappwebview/flutter_inappwebview.dart' + show PermissionResourceType; + +/// Lower-case names for the resources of a native permission request. +/// CAMERA_AND_MICROPHONE is reported as both; others by their own name. +List permissionResourceNames(List resources) { + final names = []; + for (final r in resources) { + final value = r.toValue(); + if (value == PermissionResourceType.CAMERA_AND_MICROPHONE.toValue()) { + names.addAll(const ['camera', 'microphone']); + } else { + names.add(value.toLowerCase()); + } + } + return names; +} diff --git a/lib/widgets/browser_view.dart b/lib/widgets/browser_view.dart index eadff29..92c08ff 100644 --- a/lib/widgets/browser_view.dart +++ b/lib/widgets/browser_view.dart @@ -1,3 +1,4 @@ +import 'dart:async'; import 'package:flutter/gestures.dart'; import 'package:material_ui/material_ui.dart'; import 'package:flutter/foundation.dart'; @@ -10,6 +11,7 @@ import 'package:view_source_vibe/services/collectors/collector_world.dart'; import 'package:view_source_vibe/services/collectors/frame_relay.dart'; import 'package:view_source_vibe/services/html_service.dart'; import 'package:view_source_vibe/services/navigation_policy.dart'; +import 'package:view_source_vibe/services/permission_resources.dart'; import 'package:view_source_vibe/services/rss_template_service.dart'; import 'package:view_source_vibe/utils/low_resource_webview.dart'; import 'package:view_source_vibe/widgets/scroll_through_banner.dart'; @@ -224,6 +226,25 @@ class _BrowserViewState extends State { } } + int _loadStartSeq() => + Provider.of(context, listen: false).loadStartSeq; + + /// Records a (denied) permission request for the page showing now. Never + /// throws: the caller must always answer DENY. + Future _recordPermission(InAppWebViewController controller, + int loadSeq, String origin, List resources) async { + try { + final mainFrame = await controller.getUrl(); + if (!mounted) return; + Provider.of(context, listen: false) + .onBrowserPermissionRequest(origin, resources, + mainFrame == null ? null : Uri.tryParse(mainFrame.toString()), + loadSeq: loadSeq); + } catch (_) { + // Recording is best effort. + } + } + @override Widget build(BuildContext context) { final appSettings = context.watch(); @@ -287,9 +308,26 @@ class _BrowserViewState extends State { _loadContent(); } }, + // The app never grants a permission: deny, and record who asked. + onPermissionRequest: (controller, request) async { + // Answer at once; recording must not delay the page (or the answer). + unawaited(_recordPermission(controller, _loadStartSeq(), + request.origin.toString(), + permissionResourceNames(request.resources))); + return PermissionResponse( + resources: request.resources, + action: PermissionResponseAction.DENY); + }, + onGeolocationPermissionsShowPrompt: (controller, origin) async { + unawaited(_recordPermission( + controller, _loadStartSeq(), origin, const ['geolocation'])); + return GeolocationPermissionShowPromptResponse( + origin: origin, allow: false, retain: false); + }, onLoadStart: (controller, url) { _lastSyncedUrl = null; - Provider.of(context, listen: false).onBrowserLoadStart(); + Provider.of(context, listen: false).onBrowserLoadStart( + url == null ? null : Uri.tryParse(url.toString())); }, onLoadStop: (controller, url) async { if (mounted && url != null) { diff --git a/lib/widgets/permissions_view.dart b/lib/widgets/permissions_view.dart new file mode 100644 index 0000000..0580c7f --- /dev/null +++ b/lib/widgets/permissions_view.dart @@ -0,0 +1,206 @@ +import 'package:material_ui/material_ui.dart'; +import 'package:provider/provider.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/derive/observed_text.dart'; +import 'package:view_source_vibe/services/derive/page_size.dart'; +import 'package:view_source_vibe/services/derive/permissions_view.dart'; +import 'package:view_source_vibe/services/html_service.dart'; +import 'package:view_source_vibe/utils/format_utils.dart'; +import 'package:view_source_vibe/widgets/observed_value.dart'; +import 'package:view_source_vibe/widgets/section_box.dart'; + +/// The Permissions tab: what the page's policy allows, what it hands to +/// embedded frames, what it asked for while loading (the app denies all of +/// it) and what it keeps on this device. +class PermissionsView extends StatelessWidget { + const PermissionsView({super.key}); + + @override + Widget build(BuildContext context) { + final state = context.select>( + (s) => s.permissionsView); + return switch (state) { + Present(:final value) => _content(context, value), + Pending() => + const Center(child: CircularProgressIndicator()), + Failed(:final error) => _message('Error: $error'), + Unavailable() => _message(notPresentText(state)), + }; + } + + Widget _message(String text) => Center( + child: Padding( + padding: const EdgeInsets.all(24), + child: Text(text, textAlign: TextAlign.center), + ), + ); + + Widget _content(BuildContext context, PermissionsModel m) { + final theme = Theme.of(context); + return Scrollbar( + child: ListView( + primary: true, + padding: const EdgeInsets.fromLTRB(16, 16, 16, 66), + children: [ + for (final n in m.notes) + Padding( + padding: const EdgeInsets.only(bottom: 6), + child: Row( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + const Icon(Icons.info_outline, size: 16), + const SizedBox(width: 8), + Expanded(child: Text(n)), + ], + ), + ), + _title(theme, 'Allowed by the site'), + _declared(m), + _title(theme, 'Handed to embedded frames'), + _delegations(context, m), + _title(theme, 'Asked while loading'), + _requests(context, m), + _title(theme, 'Stored on this device'), + _stored(m), + ], + ), + ); + } + + Widget _title(ThemeData theme, String text) => Padding( + padding: const EdgeInsets.only(top: 16, bottom: 8), + child: Text(text, + style: theme.textTheme.titleSmall + ?.copyWith(fontWeight: FontWeight.bold)), + ); + + Widget _pad(Widget child) => + Padding(padding: const EdgeInsets.all(12), child: child); + + Widget _line(String label, Widget value) => _pad(Row( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + SizedBox( + width: 140, + child: Text(label, + style: const TextStyle(fontWeight: FontWeight.w600))), + Expanded(child: value), + ], + )); + + /// Why a section has no data (never an empty box). + Widget _why(Observed o) => + _pad(ObservedValue(value: o, format: (_) => '')); + + Widget _declared(PermissionsModel m) { + final declared = m.declared; + if (declared is! Present>) { + return SectionBox(children: [_why(declared)]); + } + return SectionBox(children: [ + if (m.legacyHeaderOnly) + _pad(const Text( + 'Only the older Feature-Policy header was sent, not Permissions-Policy.')), + if (declared.value.isEmpty) + _pad(const Text('The header is present and lists no features.')), + for (final f in declared.value) + _line(f.feature, + Text(f.allowed + (f.powerful ? ' (device access)' : ''))), + if (m.invalidDeclarations.isNotEmpty) + _pad(Text('Could not read: ${m.invalidDeclarations.join(', ')}')), + ]); + } + + Widget _delegations(BuildContext context, PermissionsModel m) { + final d = m.delegations; + if (d is! Present>) { + return SectionBox(children: [_why(d)]); + } + if (d.value.isEmpty) { + return SectionBox(children: [ + _pad(Text(switch (m.frameCount) { + Present(:final value) when value == 0 => + 'The page has no embedded frames.', + Present(:final value) => + 'None of the $value embedded frames got an allow attribute.', + _ => 'None of the embedded frames got an allow attribute.', + })), + ]); + } + return SectionBox(children: [ + for (final f in d.value) + _line( + f.frameHost.isEmpty ? 'inline frame' : f.frameHost, + Wrap( + spacing: 8, + runSpacing: 4, + children: [ + if (f.thirdParty) _badge(context, 'third-party'), + if (f.sandboxed) _badge(context, 'sandboxed'), + Text(f.features.isEmpty ? 'no features' : f.features.join(', ')), + ], + ), + ), + ]); + } + + Widget _badge(BuildContext context, String text) { + final theme = Theme.of(context); + return Container( + padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 1), + decoration: BoxDecoration( + border: Border.all(color: theme.colorScheme.outlineVariant), + borderRadius: BorderRadius.circular(8), + ), + child: Text(text, style: theme.textTheme.bodySmall), + ); + } + + Widget _requests(BuildContext context, PermissionsModel m) { + final theme = Theme.of(context); + final muted = theme.textTheme.bodySmall + ?.copyWith(color: theme.colorScheme.onSurfaceVariant); + return SectionBox(children: [ + _pad(Text('Denied by this app', style: muted)), + if (m.requests.isEmpty) _pad(const Text('No requests.')), + for (final r in m.requests) + _line(r.resources.join(', '), + Text('${r.origin}, ${formatSinceLoadStart(r.sinceLoadStart)}')), + ]); + } + + Widget _stored(PermissionsModel m) { + final s = m.stored; + String size(Observed o) => + presentOrUnknown(o, FormatUtils.formatBytes); + String names(Observed> o) => + presentOrUnknown(o, (v) => v.isEmpty ? 'none' : v.join(', ')); + return SectionBox(children: [ + if (s is Present) ...[ + _line('Used', + Text('${size(s.value.usageBytes)} of ${size(s.value.quotaBytes)}')), + _line('IndexedDB', Text(names(s.value.indexedDbNames))), + _line('Caches', Text(names(s.value.cacheNames))), + _line( + 'Service workers', + Text(presentOrUnknown(s.value.serviceWorkers, + (v) => v.isEmpty ? 'none' : v.map((w) => w.scope).join(', ')))), + ] else + _why(s), + _line('localStorage', Text(_area(m.webStorage, (w) => w.local))), + _line('sessionStorage', Text(_area(m.webStorage, (w) => w.session))), + ]); + } + + String _area(Observed o, + Observed Function(BrowserStorage) pick) { + if (o is! Present) return unknownText(o); + return presentOrUnknown(pick(o.value), (a) { + final bytes = a.bytes; + final size = + bytes is Present ? ', ${formatDownloadSize(bytes.value)}' : ''; + return '${a.count} items$size'; + }); + } +} diff --git a/test/collectors/browser_payload_test.dart b/test/collectors/browser_payload_test.dart index b5c38c9..31860eb 100644 --- a/test/collectors/browser_payload_test.dart +++ b/test/collectors/browser_payload_test.dart @@ -914,4 +914,99 @@ void main() { 200); }); }); + + group('frames and site storage', () { + BrowserSnapshot withSections(Map extra) => + parseBrowserPayload( + jsonEncode({ + 'v': 1, + 'url': 'https://a.test/x', + 'timing': {'ok': true, 'now': 5}, + ...extra, + }), + BrowserPlatform.macos, + pageUrl: Uri.parse('https://a.test/x')); + + test('frames resolve against the page URL', () { + final snap = withSections({ + 'frames': { + 'ok': true, + 'total': 3, + 'items': [ + {'src': '/embed', 'allow': 'camera; microphone', 'sandbox': ''}, + {'src': '', 'allow': '', 'sandbox': null}, + ], + }, + }); + final frames = present(snap.frames); + expect(frames.first.src, 'https://a.test/embed'); + expect(frames.first.allow, 'camera; microphone'); + expect(frames.first.sandbox, ''); + expect(frames.last.src, ''); + expect(frames.last.sandbox, isNull); + expect(snap.frameCount, 3); + }); + + test('capabilities map to Observed, missing is never false', () { + final snap = withSections({ + 'capabilities': {'ok': true, 'mediaDevices': false, 'geolocation': true}, + }); + expect(present(snap.mediaDevices), isFalse); + expect(present(snap.geolocation), isTrue); + final old = withSections({}); + expect(unavailable(old.mediaDevices), + UnavailableReason.notReportedByPlatform); + expect(unavailable(old.geolocation), + UnavailableReason.notReportedByPlatform); + }); + + test('sections missing from an old payload are Unavailable', () { + final snap = withSections({}); + expect(unavailable(snap.frames), UnavailableReason.notReportedByPlatform); + expect((snap.siteStorage as Unavailable).detail, 'not collected'); + }); + + test('site storage sections map to Observed', () { + expect(withSections({'siteStorage': {'ok': true, 'pending': true}}) + .siteStorage, isA>()); + + final s = present(withSections({ + 'siteStorage': { + 'ok': true, + 'usage': 10, + 'quota': 100, + 'idb': {'ok': false, 'error': 'indexedDB.databases is not supported'}, + 'caches': {'ok': true, 'names': ['v1']}, + 'sw': { + 'ok': true, + 'items': [ + {'scope': 'https://a.test/', 'script': 'https://a.test/sw.js'} + ] + }, + }, + }).siteStorage); + expect(present(s.usageBytes), 10); + expect(present(s.cacheNames), ['v1']); + expect(unavailable(s.indexedDbNames), + UnavailableReason.notReportedByPlatform); + expect((s.indexedDbNames as Unavailable).detail, + 'indexedDB.databases is not supported'); + expect(present(s.serviceWorkers).single.script, 'https://a.test/sw.js'); + }); + + test('usage null is Unavailable', () { + final s = present(withSections({ + 'siteStorage': { + 'ok': true, + 'usage': null, + 'quota': null, + 'idb': {'ok': true, 'names': []}, + 'caches': {'ok': true, 'names': []}, + 'sw': {'ok': true, 'items': []}, + }, + }).siteStorage); + expect(unavailable(s.usageBytes), UnavailableReason.notReportedByPlatform); + expect(present(s.indexedDbNames), isEmpty); + }); + }); } diff --git a/test/collectors/vsv_collect_js_test.dart b/test/collectors/vsv_collect_js_test.dart index 9eb1e28..5a0657a 100644 --- a/test/collectors/vsv_collect_js_test.dart +++ b/test/collectors/vsv_collect_js_test.dart @@ -62,4 +62,111 @@ void main() { expect(local['bytes'], 3 + 15); // 'a'+'é' = 1+2, 'tok'+'secret-token' = 3+12 expect(local['items'][0], {'k': 'tok', 'b': 15}); }); + + test('iframe attributes are collected, capped at 200', () async { + final out = await runCollector( + before: "var fr = []; for (var i = 0; i < 250; i++)" + " fr.push({src: 'https://f.test/' + i, getAttribute: function(n) {" + " return n === 'src' ? this.src" + " : n === 'allow' ? 'camera' : null; }});" + "document.getElementsByTagName = function(t) {" + " return t === 'iframe' ? fr : []; };"); + if (out == null) return markTestSkipped('node not installed'); + expect(out['frames']['total'], 250); + expect(out['frames']['items'], hasLength(200)); + expect(out['frames']['items'][0], + {'src': 'https://f.test/0', 'allow': 'camera', 'sandbox': null}); + }); + + test('an empty sandbox attribute stays empty, a missing one is null', + () async { + final out = await runCollector( + before: "var fr = [{getAttribute: function(n) {" + " return n === 'sandbox' ? '' : null; }}];" + "document.getElementsByTagName = function(t) {" + " return t === 'iframe' ? fr : []; };"); + if (out == null) return markTestSkipped('node not installed'); + expect(out['frames']['items'].single, + {'src': '', 'allow': '', 'sandbox': ''}); + }); + + test('site storage is pending until the promises settle', () async { + // A promise that never settles; the other three APIs are absent. + final out = await runCollector( + before: "var navigator = {storage: {estimate:" + " function() { return {then: function() {" + " return {catch: function() {}}; }}; }}};"); + if (out == null) return markTestSkipped('node not installed'); + expect(out['siteStorage'], {'ok': true, 'pending': true}); + }); + + test('capabilities say what the page can ask for', () async { + final out = await runCollector( + before: "var navigator = {mediaDevices: {getUserMedia: function() {}}," + " geolocation: {}};"); + if (out == null) return markTestSkipped('node not installed'); + expect(out['capabilities'], + {'ok': true, 'mediaDevices': true, 'geolocation': true}); + }); + + test('capabilities are false when the APIs are absent', () async { + final out = await runCollector( + before: "var navigator = {mediaDevices: undefined};"); + if (out == null) return markTestSkipped('node not installed'); + expect(out['capabilities'], + {'ok': true, 'mediaDevices': false, 'geolocation': false}); + }); + + test('a missing API is an error for that field only', () async { + final out = await runCollector( + before: "window.__vsv_site = {done: true, usage: 10, quota: 100," + " idb: {ok: false, error: 'indexedDB.databases is not supported'}," + " caches: {ok: true, names: ['v1']}, sw: {ok: true, items: []}};"); + if (out == null) return markTestSkipped('node not installed'); + final s = out['siteStorage']; + expect(s['idb']['ok'], isFalse); + expect(s['caches']['names'], ['v1']); + expect(s['usage'], 10); + }); + + test('site storage lookups that are absent are recorded per field', + () async { + // Node has none of navigator.storage / indexedDB / caches here. + final out = await runCollector( + before: "var navigator = {};", + probe: "window.__vsv_site"); + if (out == null) return markTestSkipped('node not installed'); + final s = out['siteStorage']; + expect(s['idb'], + {'ok': false, 'error': 'indexedDB.databases is not supported'}); + expect(s['sw']['ok'], isFalse); + expect(s['usage'], isNull); + }); + + test('a storage API that never answers is reported after 4 s', () async { + // caches.keys() hangs; the clock jumps 5 s once it has been called. + final out = await runCollector( + before: "var __real = Date.now, __skew = 0;" + " Date.now = function() { return __real() + __skew; };" + " var navigator = {};" + " var caches = {keys: function() { return {then: function() {" + " __skew = 5000; return {catch: function() {}}; }}; }};"); + if (out == null) return markTestSkipped('node not installed'); + final s = out['siteStorage']; + expect(s['pending'], isNull); + expect(s['caches'], {'ok': false, 'error': 'no answer within 4 s'}); + expect(s['idb']['ok'], isFalse); + expect(s['usage'], isNull); + }); + + test('a synchronous throw from then() settles that field only', () async { + final out = await runCollector( + before: "var navigator = {};" + " var caches = {keys: function() { return {then: function() {" + " throw new Error('boom'); }}; }};", + probe: "window.__vsv_site.caches"); + if (out == null) return markTestSkipped('node not installed'); + expect(out['__probe']['ok'], isFalse); + expect(out['__probe']['error'], contains('boom')); + }); } diff --git a/test/derive/permissions_policy_test.dart b/test/derive/permissions_policy_test.dart new file mode 100644 index 0000000..b8e24ab --- /dev/null +++ b/test/derive/permissions_policy_test.dart @@ -0,0 +1,74 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:view_source_vibe/services/derive/permissions_policy.dart'; + +void main() { + test('permissions-policy', () { + final p = parsePermissionsPolicy( + 'geolocation=(), camera=(self "https://a.test"), fullscreen=*, microphone=self'); + expect( + p.features.keys, ['geolocation', 'camera', 'fullscreen', 'microphone']); + expect(p.features['geolocation']!.none, isTrue); + expect(p.features['camera']!.self, isTrue); + expect(p.features['camera']!.origins, ['https://a.test']); + expect(p.features['fullscreen']!.all, isTrue); + expect(p.features['microphone']!.self, isTrue); + expect(p.invalid, isEmpty); + }); + test('a broken member does not lose the others', () { + final p = parsePermissionsPolicy('camera=(self, geolocation=(), ,x'); + expect(p.features.keys, contains('geolocation')); + expect(p.invalid, isNotEmpty); + }); + test('repeated header values joined by a comma parse as one', () { + expect(parsePermissionsPolicy('camera=(), microphone=()').features, + hasLength(2)); + }); + test('iframe allow', () { + final p = parseFeaturePolicy( + "camera; microphone 'none'; geolocation https://m.test *", + iframeAllow: true); + expect(p.features['camera']!.src, isTrue); + expect(p.features['microphone']!.none, isTrue); + expect(p.features['geolocation']!.all, isTrue); + expect(p.features['geolocation']!.origins, ['https://m.test']); + }); + test('empty input', () { + expect(parsePermissionsPolicy('').features, isEmpty); + expect(parseFeaturePolicy(' ').features, isEmpty); + }); + test('names are lower-cased, repeats keep the first', () { + final p = parsePermissionsPolicy('Camera=(), camera=*'); + expect(p.features.keys, ['camera']); + expect(p.features['camera']!.none, isTrue); + expect(p.invalid, ['camera (repeated)']); + }); + test('quoted self is not a keyword; params and commas in strings', () { + final p = parsePermissionsPolicy( + 'camera=("self"), mic=(self;x=1 "https://a.test,b"), usb=(self);report-to=x'); + expect(p.features.keys, ['mic', 'usb']); + expect(p.features['mic']!.self, isTrue); + expect(p.features['mic']!.origins, ['https://a.test,b']); + expect(p.features['usb']!.self, isTrue); + expect(p.invalid, hasLength(1)); + }); + test('feature-policy header defaults to self', () { + final p = parseFeaturePolicy( + "geolocation 'none'; camera 'self' https://a.test; fullscreen *; usb"); + expect(p.features['geolocation']!.none, isTrue); + expect(p.features['camera']!.self, isTrue); + expect(p.features['camera']!.origins, ['https://a.test']); + expect(p.features['fullscreen']!.all, isTrue); + expect(p.features['usb']!.self, isTrue); + expect(p.features['usb']!.src, isFalse); + }); + test('invalid tokens are reported and not counted', () { + for (final t in ['none', 'self', "'foo'"]) { + final p = parseFeaturePolicy('camera $t', iframeAllow: true); + expect(p.features['camera']!.none, isTrue, reason: t); + expect(p.invalid, ['camera: $t (invalid token)']); + } + final p = parseFeaturePolicy('camera self https://a.test'); + expect(p.features['camera']!.origins, ['https://a.test']); + expect(p.features['camera']!.none, isFalse); + }); +} diff --git a/test/derive/permissions_view_test.dart b/test/derive/permissions_view_test.dart new file mode 100644 index 0000000..df1bab2 --- /dev/null +++ b/test/derive/permissions_view_test.dart @@ -0,0 +1,244 @@ +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/page_inspection.dart'; +import 'package:view_source_vibe/models/inspection/permission_request.dart'; +import 'package:view_source_vibe/services/derive/permissions_view.dart'; + +import '../fixtures/inspection/local_file.dart'; +import 'fixtures.dart'; + +PageInspection page({ + Map> headers = const {}, + List? frames, + SiteStorage? storage, + List requests = const [], + bool? mediaDevices, + bool? geolocation, +}) { + final snap = browserSnap(url: 'https://a.test/'); + final withExtras = BrowserSnapshot( + url: snap.url, + platform: snap.platform, + contentType: snap.contentType, + renderedDom: snap.renderedDom, + resources: snap.resources, + cookies: snap.cookies, + storage: snap.storage, + navigation: snap.navigation, + snapshotTimeMs: snap.snapshotTimeMs, + frames: frames == null ? kFramesNotCollected : b(frames), + frameCount: frames?.length ?? 0, + siteStorage: storage == null ? kSiteStorageNotCollected : b(storage), + mediaDevices: mediaDevices == null ? kCapabilityNotCollected : b(mediaDevices), + geolocation: geolocation == null ? kCapabilityNotCollected : b(geolocation), + ); + return inspection( + server: serverSnap(url: 'https://a.test/', headers: headers), + browser: withExtras) + .copyWith(permissionRequests: requests); +} + +PermissionsModel present(Observed o) => + (o as Present).value; + +void main() { + test('notes say when the browser cannot offer camera or location', () { + final m = present(derivePermissions( + page(mediaDevices: false, geolocation: false))); + expect(m.notes, contains( + 'This browser does not offer camera or microphone to pages, so a page cannot ask for them here.')); + expect(m.notes, contains( + 'This browser does not offer location to pages, so a page cannot ask for it here.')); + }); + + test('no capability note when offered or unknown', () { + for (final m in [ + present(derivePermissions(page(mediaDevices: true, geolocation: true))), + present(derivePermissions(page())), + ]) { + expect(m.notes.where((n) => n.startsWith('This browser does not offer')), + isEmpty); + } + }); + + test('declared features, powerful ones first', () { + final m = present(derivePermissions(page(headers: { + 'permissions-policy': ['fullscreen=*, camera=(), geolocation=(self)'] + }))); + final d = (m.declared as Present>).value; + expect(d.map((f) => '${f.feature}: ${f.allowed}'), + ['camera: nobody', 'geolocation: this site', 'fullscreen: everyone']); + expect(m.legacyHeaderOnly, isFalse); + }); + + test('no header is unavailable, not an empty list', () { + final m = present(derivePermissions(page(headers: {}))); + expect(m.declared, isA>>()); + expect(m.notes.first, startsWith('No Permissions-Policy header')); + }); + + test('origins are listed after this site', () { + final m = present(derivePermissions(page(headers: { + 'permissions-policy': ['camera=(self "https://b.test")'] + }))); + final d = (m.declared as Present>).value; + expect(d.single.allowed, 'this site, https://b.test'); + }); + + test('Feature-Policy alone is legacy; Permissions-Policy wins when both', () { + var m = present(derivePermissions(page(headers: { + 'feature-policy': ["camera 'none'"] + }))); + expect(m.legacyHeaderOnly, isTrue); + expect((m.declared as Present>).value.single.allowed, + 'nobody'); + m = present(derivePermissions(page(headers: { + 'feature-policy': ["camera 'none'"], + 'permissions-policy': ['camera=*'] + }))); + expect(m.legacyHeaderOnly, isFalse); + expect((m.declared as Present>).value.single.allowed, + 'everyone'); + }); + + test('a header with no features is present and empty; bad members listed', + () { + final m = present(derivePermissions(page(headers: { + 'permissions-policy': ['camera=(self'] + }))); + expect((m.declared as Present>).value, isEmpty); + expect(m.invalidDeclarations, ['camera=(self']); + expect(m.notes, isNot(contains(startsWith('No Permissions-Policy')))); + }); + + test('a third-party frame with camera access is flagged', () { + final m = present(derivePermissions(page(frames: const [ + FrameInfo(src: 'https://meet.other.test/x', allow: 'camera; microphone'), + FrameInfo(src: 'https://static.a.test/y', allow: 'fullscreen'), + FrameInfo(src: 'https://nothing.other.test/'), + FrameInfo(src: '', allow: 'geolocation', sandbox: ''), + ]))); + final d = (m.delegations as Present>).value; + expect(d.length, 3); + expect(d[0].thirdParty, isTrue); + expect(d[0].features, ['camera', 'microphone']); + expect(d[0].frameHost, 'meet.other.test'); + expect(d[1].thirdParty, isFalse); + expect(d[2].frameHost, ''); + expect(d[2].thirdParty, isFalse); + expect(d[2].sandboxed, isTrue); + expect((m.frameCount as Present).value, 4); + expect(m.notes, + contains('1 third-party frame(s) may use: camera, microphone')); + }); + + test('requests become notes with one decimal below ten seconds', () { + final m = present(derivePermissions(page(requests: const [ + PermissionRequestEvent( + origin: 'https://a.test', + resources: ['camera'], + sinceLoadStart: Duration(milliseconds: 2340)), + PermissionRequestEvent( + origin: 'https://a.test', + resources: ['geolocation'], + sinceLoadStart: Duration(milliseconds: 12400)), + ]))); + expect(m.requests.length, 2); + expect(m.notes, [ + 'No Permissions-Policy header: embedded frames keep the browser\'s defaults.', + 'The page asked for camera 2.3 s after it started loading.', + 'The page asked for geolocation 12 s after it started loading.', + ]); + }); + + test('a service worker is noted', () { + final m = present(derivePermissions(page( + storage: const SiteStorage( + usageBytes: Unavailable(UnavailableReason.notReportedByPlatform), + quotaBytes: Unavailable(UnavailableReason.notReportedByPlatform), + indexedDbNames: + Unavailable(UnavailableReason.notReportedByPlatform), + cacheNames: Unavailable(UnavailableReason.notReportedByPlatform), + serviceWorkers: Present( + [ServiceWorkerInfo(scope: 'https://a.test/', script: 'sw.js')], + source: DataSource.browser))))); + expect(m.notes.last, + 'A service worker is installed: the site can run code in the background.'); + expect(m.stored, isA>()); + }); + + test('states: browser pending, nothing answered', () { + expect(derivePermissions(page().copyWith(browser: const Pending())), + isA>()); + final m = + present(derivePermissions(page().copyWith(browser: const Pending()))); + expect(m.delegations, isA>>()); + expect( + derivePermissions(const PageInspection.empty() + .copyWith(server: const Pending(), browser: const Pending())), + isA>()); + final both = + page().copyWith(server: const Pending(), browser: const Pending()); + expect(derivePermissions(both), isA>()); + }); + + test('local files have no permissions to check', () { + expect( + derivePermissions(localFile()), isA>()); + expect(derivePermissions(const PageInspection.empty()), + isA>()); + }); + test('allow="camera none" delegates nothing and raises no note', () { + final m = present(derivePermissions(page(frames: const [ + FrameInfo(src: 'https://meet.other.test/x', allow: 'camera none'), + ]))); + expect((m.delegations as Present>).value.single.features, + isEmpty); + expect(m.notes.where((n) => n.contains('third-party')), isEmpty); + }); + + test('9.96 s prints as 10 s', () { + final m = present(derivePermissions(page(requests: const [ + PermissionRequestEvent( + origin: 'https://a.test', + resources: ['camera'], + sinceLoadStart: Duration(milliseconds: 9960)), + ]))); + expect(m.notes.last, + 'The page asked for camera 10 s after it started loading.'); + }); + + test('an unknown load start says so', () { + final m = present(derivePermissions(page(requests: const [ + PermissionRequestEvent( + origin: 'https://a.test', + resources: ['camera'], + sinceLoadStart: null), + ]))); + expect(m.notes.last, + 'The page asked for camera (time since load start unknown).'); + expect(formatSinceLoadStart(null), 'time unknown'); + }); + + test('frames not reported: the count is unknown, not 0', () { + final m = present(derivePermissions(page())); + expect(m.frameCount, isA>()); + }); + + test('several Feature-Policy headers are parsed one by one', () { + final m = present(derivePermissions(page(headers: { + 'feature-policy': ["camera 'none'", "geolocation 'none'; microphone 'self'"] + }))); + expect(m.invalidDeclarations, isEmpty); + final d = (m.declared as Present>).value; + expect(d.map((f) => '${f.feature}: ${f.allowed}'), + ['camera: nobody', 'microphone: this site', 'geolocation: nobody']); + }); + + test('unavailable detail is pinned', () { + final u = derivePermissions(localFile()) as Unavailable; + expect(u.reason, UnavailableReason.notApplicable); + expect(u.detail, 'only for web pages'); + }); +} diff --git a/test/models/app_tab_test.dart b/test/models/app_tab_test.dart index ba3e9e9..4c7ff56 100644 --- a/test/models/app_tab_test.dart +++ b/test/models/app_tab_test.dart @@ -15,7 +15,7 @@ void main() { 'Browser', 'Metadata', 'Media', 'Services', 'Reader', // 'DOM Tree', 'Source', // 'Size', 'Headers', 'Timeline', 'Probe', // - 'Privacy', 'Sovereignty', 'Security', 'Cookies', + 'Privacy', 'Sovereignty', 'Security', 'Permissions', 'Cookies', ]); // Groups are contiguous, so each group's chips are one run of pages. final groups = tabs.map((t) => t.group).toList(); diff --git a/test/services/html_service_browser_sync_test.dart b/test/services/html_service_browser_sync_test.dart index 4acb3af..4b11972 100644 --- a/test/services/html_service_browser_sync_test.dart +++ b/test/services/html_service_browser_sync_test.dart @@ -2,6 +2,8 @@ import 'dart:async'; import 'dart:convert'; import 'package:flutter/foundation.dart'; +import 'package:flutter_inappwebview/flutter_inappwebview.dart' + show PermissionResourceType; import 'package:flutter_test/flutter_test.dart'; import 'package:view_source_vibe/services/derive/network_quiet.dart'; import 'package:view_source_vibe/models/inspection/browser_snapshot.dart'; @@ -12,6 +14,7 @@ import 'package:view_source_vibe/services/collectors/browser_loader.dart'; import 'package:view_source_vibe/services/derive/cookies.dart'; import 'package:view_source_vibe/services/html_service.dart'; import 'package:view_source_vibe/services/inspection_store.dart'; +import 'package:view_source_vibe/services/permission_resources.dart'; import 'html_service_lifecycle_test.dart' show @@ -150,8 +153,8 @@ void main() { test('cookies are deleted before every URL load when enabled', () async { var deletions = 0; service.dispose(); - service = make( - cleanCookies: true, deleteAllCookies: () async => deletions++); + service = + make(cleanCookies: true, deleteAllCookies: () async => deletions++); await service.loadUrl('https://a.test/'); fetcher.pending.last .complete(okSnapshot('https://a.test/', 'S')); @@ -395,12 +398,12 @@ void main() { // initial sync + first poll + post-scroll poll, then settled expect(collectCalls.length, greaterThanOrEqualTo(3)); expect(store.current.browserSettled, isTrue); - final names = - service.resourceTimelineData.map((r) => r['name']).toSet(); + final names = service.resourceTimelineData.map((r) => r['name']).toSet(); expect(names, contains('https://a.test/lazy.gif')); // The banner explains the scroll and how to disable it. expect(HtmlService.scrollThroughBannerText, contains('Settings')); - expect(HtmlService.scrollThroughBannerText, contains('Load lazy content')); + expect( + HtmlService.scrollThroughBannerText, contains('Load lazy content')); // The scroll-through raised the banner for the current page. expect(service.showScrollThroughBanner, isTrue); // Back at the top, the page reports it: the banner goes away. @@ -899,4 +902,85 @@ void main() { expect(browserOf(service).mainStatus, isA>()); }); }); + + group('permission requests', () { + test('a request is recorded for the current page', () async { + await loadAndAnswer('https://a.test/'); + service.onBrowserLoadStart(); + service.onBrowserPermissionRequest('https://a.test', + ['camera', 'microphone'], Uri.parse('https://a.test/'), + loadSeq: service.loadStartSeq); + final r = store.current.permissionRequests.single; + expect(r.origin, 'https://a.test'); + expect(r.resources, ['camera', 'microphone']); + expect(r.sinceLoadStart, greaterThanOrEqualTo(Duration.zero)); + }); + + test('a request that waited for getUrl across a load start is dropped', + () async { + await loadAndAnswer('https://a.test/'); + final seq = service.loadStartSeq; // read at native-callback entry + service.onBrowserLoadStart(Uri.parse('https://a.test/')); // reload + service.onBrowserPermissionRequest( + 'https://a.test', ['camera'], Uri.parse('https://a.test/'), + loadSeq: seq); + expect(store.current.permissionRequests, isEmpty); + }); + + test('without a load start the time is unknown', () async { + await loadAndAnswer('https://a.test/'); + service.onBrowserPermissionRequest( + 'https://a.test', ['geolocation'], Uri.parse('https://a.test/'), + loadSeq: service.loadStartSeq); + expect(store.current.permissionRequests.single.sinceLoadStart, isNull); + }); + + test( + 'a late request from the page left is dropped; one from the new page is kept', + () async { + await loadAndAnswer('https://a.test/'); + service.onBrowserPermissionRequest( + 'https://a.test', ['camera'], Uri.parse('https://a.test/'), + loadSeq: service.loadStartSeq); + await loadAndAnswer('https://b.test/'); + expect(store.current.permissionRequests, isEmpty); + service.onBrowserPermissionRequest( + 'https://a.test', ['camera'], Uri.parse('https://a.test/'), + loadSeq: service.loadStartSeq); + expect(store.current.permissionRequests, isEmpty); + // A third-party frame on page B: origin differs, main frame is B. + service.onBrowserPermissionRequest( + 'https://maps.test', ['geolocation'], Uri.parse('https://b.test/'), + loadSeq: service.loadStartSeq); + expect( + store.current.permissionRequests.single.origin, 'https://maps.test'); + }); + + test('a load start of the previous page gives unknown', () async { + await loadAndAnswer('https://a.test/'); + service.onBrowserLoadStart(Uri.parse('https://a.test/')); + await loadAndAnswer('https://b.test/'); + service.onBrowserPermissionRequest( + 'https://b.test', ['camera'], Uri.parse('https://b.test/'), + loadSeq: service.loadStartSeq); + expect(store.current.permissionRequests.single.sinceLoadStart, isNull); + }); + }); + + group('permission resource names', () { + test('camera, microphone, both, geolocation and others', () { + expect( + permissionResourceNames([PermissionResourceType.CAMERA]), ['camera']); + expect(permissionResourceNames([PermissionResourceType.MICROPHONE]), + ['microphone']); + expect( + permissionResourceNames( + [PermissionResourceType.CAMERA_AND_MICROPHONE]), + ['camera', 'microphone']); + expect(permissionResourceNames([PermissionResourceType.GEOLOCATION]), + ['geolocation']); + expect(permissionResourceNames([PermissionResourceType.NOTIFICATIONS]), + ['notifications']); + }); + }); } diff --git a/test/services/inspection_export_data_test.dart b/test/services/inspection_export_data_test.dart index 9f40196..fc8f3e7 100644 --- a/test/services/inspection_export_data_test.dart +++ b/test/services/inspection_export_data_test.dart @@ -4,6 +4,8 @@ import 'package:archive/archive.dart'; 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/services/derive/permissions_view.dart'; +import 'package:view_source_vibe/services/inspection_export_data.dart'; import 'package:view_source_vibe/services/inspection_export_service.dart'; import 'package:view_source_vibe/services/inspection_store.dart'; import 'package:view_source_vibe/services/observed_json.dart'; @@ -16,6 +18,7 @@ import '../fixtures/inspection/full_page.dart'; import '../fixtures/inspection/ios_partial.dart'; import '../fixtures/inspection/local_file.dart'; import '../fixtures/inspection/pump.dart'; +import '../widgets/permissions_data_test.dart' show pageWithPermissions; void main() { group('observedJson', () { @@ -242,4 +245,19 @@ void main() { isNot('unavailable')); }); }); + + test('the permissions export has names and sizes, never storage values', () { + final text = jsonEncode(InspectionExportData.permissions( + derivePermissions(pageWithPermissions))); + expect(text, contains('keyval-store')); + expect(text, contains('camera')); + expect(text, contains('sinceLoadStartMs')); + expect(text, contains('"count":3')); + // The observed envelope uses 'value'; no storage item (key or value) is + // exported, only counts, sizes and names. + expect(text, isNot(contains('topItems'))); + expect(text, isNot(contains('"v"'))); + expect(text, isNot(contains('secret-token-key'))); + expect(text, isNot(contains('987654'))); + }); } diff --git a/test/services/inspection_store_test.dart b/test/services/inspection_store_test.dart index af93fc8..43ab16c 100644 --- a/test/services/inspection_store_test.dart +++ b/test/services/inspection_store_test.dart @@ -5,6 +5,7 @@ import 'package:view_source_vibe/models/inspection/browser_snapshot.dart'; 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/permission_request.dart'; import 'package:view_source_vibe/models/inspection/server_snapshot.dart'; import 'package:view_source_vibe/services/inspection_store.dart'; import 'package:view_source_vibe/utils/text_decoding.dart'; @@ -49,7 +50,9 @@ BrowserSnapshot _browser(String url, {String? dom, List? resources, bool truncated = false, - int dropped = 0}) => + int dropped = 0, + Observed>? frames, + Observed? siteStorage}) => BrowserSnapshot( url: Uri.parse(url), platform: BrowserPlatform.macos, @@ -66,6 +69,8 @@ BrowserSnapshot _browser(String url, snapshotTimeMs: const Present(10.0, source: DataSource.browser), resourcesTruncated: truncated, resourcesDropped: dropped, + frames: frames ?? kFramesNotCollected, + siteStorage: siteStorage ?? kSiteStorageNotCollected, ); FileSnapshot _file() => FileSnapshot( @@ -147,6 +152,29 @@ void main() { containsAll(['https://a.test/a.js', 'https://a.test/b.js'])); }); + test('a later poll keeps and compares the capability flags', () { + final g = store.beginUrl(Uri.parse('https://a.test/')); + const no = Present(false, source: DataSource.browser); + store.applyBrowser( + g, + _browser('https://a.test/', dom: '

a

') + .copyWith(mediaDevices: no, geolocation: no)); + // A poll without the capabilities section must not wipe them. + store.applyBrowser(g, _browser('https://a.test/', dom: '

a

')); + var b = (store.current.browser as Present).value; + expect(b.mediaDevices, isA>()); + expect(b.geolocation, isA>()); + // A capability-only change is not a no-op. + notifies = 0; + store.applyBrowser( + g, + _browser('https://a.test/', dom: '

a

').copyWith( + mediaDevices: const Present(true, source: DataSource.browser))); + b = (store.current.browser as Present).value; + expect((b.mediaDevices as Present).value, isTrue); + expect(notifies, greaterThan(0)); + }); + test('stale robots and failure are ignored', () { final genA = store.beginUrl(Uri.parse('https://a.test/')); store.beginUrl(Uri.parse('https://b.test/')); @@ -511,4 +539,75 @@ void main() { expect(store.current.declaredMedia, isEmpty); }); }); + + group('frames and site storage', () { + const gone = Unavailable( + UnavailableReason.notReportedByPlatform, 'not collected'); + Observed storage(int usage) => Present( + SiteStorage( + usageBytes: Present(usage, source: DataSource.browser), + quotaBytes: const Unavailable(UnavailableReason.notReportedByPlatform), + indexedDbNames: const Present(['a'], source: DataSource.browser), + cacheNames: const Unavailable(UnavailableReason.notReportedByPlatform), + serviceWorkers: + const Unavailable(UnavailableReason.notReportedByPlatform), + ), + source: DataSource.browser); + Observed current() => + (store.current.browser as Present).value.siteStorage; + + test('a later poll without site storage keeps the earlier result', () { + final g = store.beginUrl(Uri.parse('https://a.test/')); + store.applyBrowser(g, _browser('https://a.test/', siteStorage: storage(1))); + store.applyBrowser( + g, + _browser('https://a.test/', + dom: '

', siteStorage: const Pending())); + expect((current() as Present).value.usageBytes, + isA>()); + store.applyBrowser(g, _browser('https://a.test/', dom: '

', siteStorage: gone)); + expect(current(), isA>()); + }); + + test('equal frames and storage keep identity, no notification', () { + final g = store.beginUrl(Uri.parse('https://a.test/')); + Observed> fr() => Present([ + const FrameInfo(src: 'https://f.test/', allow: 'camera'), + ], source: DataSource.browser); + store.applyBrowser( + g, _browser('https://a.test/', frames: fr(), siteStorage: storage(1))); + final before = store.current.browser; + final n = notifies; + store.applyBrowser( + g, _browser('https://a.test/', frames: fr(), siteStorage: storage(1))); + expect(identical(store.current.browser, before), isTrue); + expect(notifies, n); + }); + }); + + group('permission requests', () { + PermissionRequestEvent ev(String origin, List r) => + PermissionRequestEvent( + origin: origin, resources: r, sinceLoadStart: Duration.zero); + + test('a permission request is kept for its own page only', () { + final store = InspectionStore(); + final gen = store.beginUrl(Uri.parse('https://a.test/')); + store.addPermissionRequest(gen, ev('https://a.test', ['camera'])); + expect(store.current.permissionRequests.single.resources, ['camera']); + final next = store.beginUrl(Uri.parse('https://b.test/')); + store.addPermissionRequest(gen, ev('https://a.test', ['microphone'])); + expect(store.current.permissionRequests, isEmpty); + expect(next, isNot(gen)); + }); + + test('at most 50 requests are kept', () { + final store = InspectionStore(); + final gen = store.beginUrl(Uri.parse('https://a.test/')); + for (var i = 0; i < 60; i++) { + store.addPermissionRequest(gen, ev('https://a.test', ['camera'])); + } + expect(store.current.permissionRequests.length, 50); + }); + }); } diff --git a/test/widgets/permissions_data_test.dart b/test/widgets/permissions_data_test.dart new file mode 100644 index 0000000..43c401e --- /dev/null +++ b/test/widgets/permissions_data_test.dart @@ -0,0 +1,125 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:material_ui/material_ui.dart'; +import 'package:view_source_vibe/models/inspection/browser_snapshot.dart'; +import 'package:view_source_vibe/models/inspection/page_inspection.dart'; +import 'package:view_source_vibe/models/inspection/permission_request.dart'; +import 'package:view_source_vibe/services/inspection_store.dart'; +import 'package:view_source_vibe/widgets/permissions_view.dart'; + +import '../derive/fixtures.dart' show inspection, serverSnap, browserSnap, b; +import '../fixtures/inspection/local_file.dart'; +import '../fixtures/inspection/pump.dart'; + +PageInspection pageWith({ + Map> headers = const {}, + List frames = const [], + SiteStorage? storage, + List requests = const [], +}) { + final snap = browserSnap(url: 'https://a.test/'); + final full = BrowserSnapshot( + url: snap.url, + platform: snap.platform, + contentType: snap.contentType, + renderedDom: snap.renderedDom, + resources: snap.resources, + cookies: snap.cookies, + storage: b(BrowserStorage( + local: b(StorageArea( + count: 3, + bytes: b(120), + topItems: const [StorageItem('secret-token-key', 987654)])), + session: b(StorageArea(count: 0, bytes: b(0), topItems: const [])), + )), + navigation: snap.navigation, + snapshotTimeMs: snap.snapshotTimeMs, + frames: b(frames), + frameCount: frames.length, + siteStorage: storage == null ? kSiteStorageNotCollected : b(storage), + ); + return inspection( + server: serverSnap(url: 'https://a.test/', headers: headers), + browser: full) + .copyWith(permissionRequests: requests); +} + +final pageWithPermissions = pageWith( + headers: { + 'permissions-policy': ['camera=(), fullscreen=*'] + }, + frames: const [ + FrameInfo( + src: 'https://video.other.net/embed', + allow: 'camera; autoplay', + sandbox: ''), + ], + storage: SiteStorage( + usageBytes: b(2048), + quotaBytes: b(10 * 1024 * 1024), + indexedDbNames: b(['keyval-store']), + cacheNames: b(['workbox-precache']), + serviceWorkers: b([ + const ServiceWorkerInfo(scope: 'https://a.test/', script: '/sw.js'), + ]), + ), + requests: const [ + PermissionRequestEvent( + origin: 'https://maps.test', + resources: ['geolocation'], + sinceLoadStart: Duration(milliseconds: 1500)), + ], +); + +final pageWithoutPolicy = pageWith(); + +void main() { + Future pumpPermissions(WidgetTester tester, PageInspection p) async { + tester.view.physicalSize = const Size(900, 6000); + tester.view.devicePixelRatio = 1; + addTearDown(tester.view.reset); + final store = seedStore(InspectionStore(), p); + for (final r in p.permissionRequests) { + store.addPermissionRequest(store.current.generation, r); + } + final service = makeService(store); + addTearDown(service.dispose); + await pumpWithProviders(tester, + service: service, child: const PermissionsView()); + await tester.pump(); + } + + testWidgets('the tab shows the four sections with their data', + (tester) async { + await pumpPermissions(tester, pageWithPermissions); + expect(find.text('Allowed by the site'), findsOneWidget); + expect(find.text('Handed to embedded frames'), findsOneWidget); + expect(find.text('Asked while loading'), findsOneWidget); + expect(find.text('Stored on this device'), findsOneWidget); + expect(find.text('camera'), findsWidgets); + // the note, and the badge on the delegation row + expect(find.textContaining('third-party frame(s) may use: camera'), + findsOneWidget); + expect(find.text('third-party'), findsOneWidget); + expect(find.text('video.other.net'), findsOneWidget); + expect(find.text('Denied by this app'), findsOneWidget); + expect(find.textContaining('geolocation'), findsWidgets); + expect(find.textContaining('keyval-store'), findsOneWidget); + expect(find.textContaining('workbox-precache'), findsOneWidget); + expect(find.textContaining('https://a.test/'), findsWidgets); + expect(find.textContaining('2.0 KB of 10.0 MB'), findsOneWidget); + }); + + testWidgets('no header: the reason is shown, not an empty box', + (tester) async { + await pumpPermissions(tester, pageWithoutPolicy); + expect(find.textContaining('Not reported by the server'), findsOneWidget); + expect(find.text('No requests.'), findsOneWidget); + }); + + testWidgets('a local file says the tab is only for web pages', + (tester) async { + await pumpPermissions(tester, localFile()); + expect(find.textContaining('only for web pages'), findsOneWidget); + expect(find.text('Allowed by the site'), findsNothing); + }); +}