diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index efbf7ed6..3fd8f5b6 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -61,6 +61,18 @@ + + + + + + + + + + diff --git a/lib/helpers/link_handler.dart b/lib/helpers/link_handler.dart index 6a5a5556..675113bf 100644 --- a/lib/helpers/link_handler.dart +++ b/lib/helpers/link_handler.dart @@ -120,7 +120,8 @@ class LinkHandler { static Future handleLinkTap(BuildContext context, String url) async { // A share link opens Offband's own add confirmation, not the browser. - if (url.toLowerCase().startsWith('meshcore://')) { + if (url.toLowerCase().startsWith('meshcore://') || + meshcoreFormOf(url) != null) { context.read().handle(url); return; } diff --git a/lib/services/link_router.dart b/lib/services/link_router.dart index 3a392e23..903ca775 100644 --- a/lib/services/link_router.dart +++ b/lib/services/link_router.dart @@ -3,6 +3,7 @@ import 'package:flutter/foundation.dart'; import '../connector/meshcore_protocol.dart'; import '../models/channel.dart'; import '../models/contact.dart'; +import '../utils/address_bar.dart'; /// A share link Offband can act on. sealed class ShareLink { @@ -49,9 +50,36 @@ class AdvertShareLink extends ShareLink { // in one radio packet, so anything past 255 bytes is not one. final _advertLink = RegExp(r'^meshcore://((?:[0-9a-fA-F]{2}){98,255})$'); +/// The hosts whose `/add/...` links are Offband share links. The app is +/// verified for both (#1309), so these open it instead of the browser. +const shareLinkHosts = {'offband.app', 'dev.offband.app'}; + +/// The `meshcore://` twin of an `https://offband.app/add/channel#...` or +/// `/add/contact#...` link, or null if [raw] is not one (#1309). +/// +/// The fields sit after `#`, which browsers never send to the server, so a +/// channel secret stays out of the server's logs. They are the same fields +/// as the `meshcore://` link's query, so one parser reads both. +String? meshcoreFormOf(String raw) { + final uri = Uri.tryParse(raw.trim()); + if (uri == null || !_isShareAddress(uri)) return null; + final path = uri.pathSegments.where((s) => s.isNotEmpty).toList(); + if (path.length != 2) return null; + if (path[1] != 'channel' && path[1] != 'contact') return null; + if (uri.query.isNotEmpty || uri.fragment.isEmpty) return null; + return 'meshcore://${path[1]}/add?${uri.fragment}'; +} + +/// True for any `https://offband.app/add/...` address, well formed or not. +bool _isShareAddress(Uri uri) => + uri.scheme.toLowerCase() == 'https' && + !uri.hasPort && + shareLinkHosts.contains(uri.host.toLowerCase()) && + uri.pathSegments.where((s) => s.isNotEmpty).firstOrNull == 'add'; + /// Parses [raw] into a link Offband can add, or null if it is not one. ShareLink? parseShareLink(String raw) { - final trimmed = raw.trim(); + final trimmed = meshcoreFormOf(raw) ?? raw.trim(); final channel = Channel.fromShareUri(trimmed); if (channel != null) return ChannelShareLink(channel); final contact = Contact.fromShareUri(trimmed); @@ -68,6 +96,10 @@ ShareLink? parseShareLink(String raw) { /// confirmation, and `LinkRouterHost` shows it. Only the latest link is held, /// and only in memory: an old link must not pop up days later. class LinkRouter extends ChangeNotifier { + LinkRouter({void Function()? clearAddressLink}) + : _clearAddressLink = clearAddressLink ?? clearShareLinkFromAddressBar; + + final void Function() _clearAddressLink; ShareLink? _pending; bool _pendingAnnounced = false; String? _rejected; @@ -96,9 +128,18 @@ class LinkRouter extends ChangeNotifier { /// acted on: the OS also reports ordinary launches, and on web the page's /// own address arrives as the "initial link", neither of which is the /// user asking to add anything, so they are ignored rather than reported. + /// + /// An offband.app `/add/...` address is a share link even when its fields + /// are missing or broken, so that is reported. On web it is the page's own + /// address: once read, it is cleared from the address bar so a reload + /// does not ask again (#1309). void handleFromOs(Uri uri) { - if (uri.scheme.toLowerCase() != 'meshcore') return; - handle(uri.toString()); + if (uri.scheme.toLowerCase() == 'meshcore') { + handle(uri.toString()); + } else if (_isShareAddress(uri)) { + handle(uri.toString()); + _clearAddressLink(); + } } /// The pending link if its confirmation can be shown now, removing it. diff --git a/lib/utils/address_bar.dart b/lib/utils/address_bar.dart new file mode 100644 index 00000000..812190e6 --- /dev/null +++ b/lib/utils/address_bar.dart @@ -0,0 +1,2 @@ +export 'address_bar_stub.dart' + if (dart.library.js_interop) 'address_bar_web.dart'; diff --git a/lib/utils/address_bar_stub.dart b/lib/utils/address_bar_stub.dart new file mode 100644 index 00000000..b95e6838 --- /dev/null +++ b/lib/utils/address_bar_stub.dart @@ -0,0 +1,2 @@ +/// Only the web client has an address bar. +void clearShareLinkFromAddressBar() {} diff --git a/lib/utils/address_bar_web.dart b/lib/utils/address_bar_web.dart new file mode 100644 index 00000000..23716a9a --- /dev/null +++ b/lib/utils/address_bar_web.dart @@ -0,0 +1,9 @@ +import 'package:web/web.dart' as web; + +/// Puts the address back to the site root once a share link has been read, +/// so a reload, a bookmark or a copied address does not carry the link, or +/// its channel secret, any further (#1309). The history entry's state is +/// kept: Flutter keeps its own navigation state there. +void clearShareLinkFromAddressBar() { + web.window.history.replaceState(web.window.history.state, '', '/'); +} diff --git a/test/platform/share_link_registration_test.dart b/test/platform/share_link_registration_test.dart index 85a73836..eb2c7e3b 100644 --- a/test/platform/share_link_registration_test.dart +++ b/test/platform/share_link_registration_test.dart @@ -1,3 +1,4 @@ +import 'dart:convert'; import 'dart:io'; import 'package:flutter_test/flutter_test.dart'; @@ -41,6 +42,56 @@ void main() { ); }); + test('Android verifies offband.app/add/ links for Offband (#1309)', () { + final manifest = _read('android/app/src/main/AndroidManifest.xml'); + final filters = RegExp( + r'([\s\S]*?)', + ).allMatches(manifest).map((m) => m[1]!).toList(); + expect(filters, hasLength(1), reason: 'one App Links filter'); + final filter = filters.single; + for (final part in [ + 'android.intent.action.VIEW', + 'android.intent.category.DEFAULT', + 'android.intent.category.BROWSABLE', + 'android:scheme="https"', + 'android:host="offband.app"', + 'android:host="dev.offband.app"', + 'android:pathPrefix="/add/"', + ]) { + expect(filter, contains(part)); + } + // A plain http filter would make Android skip verification entirely. + expect(filter, isNot(contains('android:scheme="http"'))); + }); + + test('assetlinks.json names Offband and its signing key (#1309)', () { + final links = jsonDecode(_read('web/.well-known/assetlinks.json')) as List; + expect(links, hasLength(1)); + final entry = links.single as Map; + expect(entry['relation'], ['delegate_permission/common.handle_all_urls']); + final target = entry['target'] as Map; + expect(target['namespace'], 'android_app'); + expect(target['package_name'], 'app.offband.meshcore'); + // The release key's SHA-256, read from a release APK with apksigner. + // Play App Signing uses the same key, uploaded with PEPK. + expect(target['sha256_cert_fingerprints'], [ + 'E7:DA:8C:D5:BF:22:AC:3E:DA:7C:B9:54:B8:B1:20:A1:' + '8B:6C:43:82:D1:B6:E6:FD:D2:04:DD:ED:DD:AF:54:88', + ]); + }); + + test('assetlinks.json is served as JSON (#1309)', () { + expect( + _read('web/_headers'), + matches( + RegExp( + r'^/\.well-known/assetlinks\.json\r?\n\s+Content-Type: application/json', + multiLine: true, + ), + ), + ); + }); + test('iOS opens meshcore:// links in Offband', () { final plist = _read('ios/Runner/Info.plist'); expect(_plistDeclaresScheme(plist, 'meshcore'), isTrue); diff --git a/test/services/link_router_test.dart b/test/services/link_router_test.dart index 6b9f655a..1a057a00 100644 --- a/test/services/link_router_test.dart +++ b/test/services/link_router_test.dart @@ -53,6 +53,87 @@ void main() { }); }); + group('offband.app links (#1309)', () { + String https(String host, String kind, String fields) => + 'https://$host/add/$kind#$fields'; + + test('read the same as their meshcore:// twins, on both hosts', () { + for (final host in shareLinkHosts) { + final channel = + parseShareLink( + https( + host, + 'channel', + 'name=%23echo&secret=5d25cf40b1f5b4a7eb0cf9703634a948', + ), + ) + as ChannelShareLink; + final twin = parseShareLink(_channelLink) as ChannelShareLink; + expect(channel.channel.name, twin.channel.name); + expect(channel.channel.pskHex, twin.channel.pskHex); + + final hashtag = + parseShareLink(https(host, 'channel', 'name=%23example')) + as ChannelShareLink; + expect(hashtag.channel.name, '#example'); + + final contact = + parseShareLink( + https(host, 'contact', 'name=Bob&public_key=$_key&type=1'), + ) + as ContactShareLink; + expect(contact.contact.name, 'Bob'); + // The add-contact dialog reads the link again, in the form it knows. + expect(contact.uri, _contactLink); + } + }); + + test('keep a name with reserved characters intact', () { + const secret = '5d25cf40b1f5b4a7eb0cf9703634a948'; + for (final name in ['a&b', 'x=y', '50% off', 'one+two', 'a b', '#c#d']) { + final link = + Channel( + index: 0, + name: name, + psk: Channel.parsePskHex(secret), + ).toShareUri().replaceFirst( + 'meshcore://channel/add?', + 'https://offband.app/add/channel#', + ); + final parsed = parseShareLink(link) as ChannelShareLink; + expect(parsed.channel.name, name, reason: link); + expect(parsed.channel.pskHex, secret, reason: link); + } + }); + + test('accept the host in any case and a trailing slash', () { + expect( + parseShareLink('https://Offband.App/add/channel/#name=%23example'), + isA(), + ); + }); + + test('anything else is not one', () { + for (final bad in [ + 'http://offband.app/add/channel#name=%23example', + 'https://example.com/add/channel#name=%23example', + 'https://offband.app.example.com/add/channel#name=%23example', + 'https://offband.app:8443/add/channel#name=%23example', + 'https://offband.app/add/channel', + 'https://offband.app/add/channel#', + // Fields in the query would reach the server's logs. + 'https://offband.app/add/channel?name=%23example', + 'https://offband.app/add/channel?x=1#name=%23example', + 'https://offband.app/add/group#name=%23example', + 'https://offband.app/add/channel/more#name=%23example', + 'https://offband.app/channel#name=%23example', + ]) { + expect(meshcoreFormOf(bad), isNull, reason: bad); + expect(parseShareLink(bad), isNull, reason: bad); + } + }); + }); + group('LinkRouter', () { test('a malformed link is reported, not dropped', () { final router = LinkRouter(); @@ -118,6 +199,37 @@ void main() { expect(router.takeRejected(), isNotNull, reason: 'a broken share link'); }); + test('an offband.app link from the OS is taken and cleared (#1309)', () { + var cleared = 0; + final router = LinkRouter(clearAddressLink: () => cleared++); + router.handleFromOs( + Uri.parse('https://offband.app/add/channel#name=%23example'), + ); + expect(router.pending, isA()); + expect(cleared, 1); + + // An /add/ address with broken fields is still a share link: it is + // reported, and still cleared so a reload does not report it again. + router.handleFromOs(Uri.parse('https://offband.app/add/channel')); + expect(router.takeRejected(), isNotNull); + expect(cleared, 2); + + // The site's other pages are not links. + router.handleFromOs(Uri.parse('https://offband.app/')); + router.handleFromOs(Uri.parse('https://offband.app/privacy#top')); + router.handleFromOs(Uri.parse('https://example.com/add/channel#x=1')); + expect(router.takeRejected(), isNull); + expect(cleared, 2); + }); + + test('a meshcore:// link from the OS clears nothing', () { + var cleared = 0; + LinkRouter( + clearAddressLink: () => cleared++, + ).handleFromOs(Uri.parse(_hashtagLink)); + expect(cleared, 0); + }); + test('a malformed link does not replace a held one', () { final router = LinkRouter()..handle(_channelLink); router.handle('meshcore://nope'); diff --git a/web/.well-known/assetlinks.json b/web/.well-known/assetlinks.json new file mode 100644 index 00000000..194c17f6 --- /dev/null +++ b/web/.well-known/assetlinks.json @@ -0,0 +1,12 @@ +[ + { + "relation": ["delegate_permission/common.handle_all_urls"], + "target": { + "namespace": "android_app", + "package_name": "app.offband.meshcore", + "sha256_cert_fingerprints": [ + "E7:DA:8C:D5:BF:22:AC:3E:DA:7C:B9:54:B8:B1:20:A1:8B:6C:43:82:D1:B6:E6:FD:D2:04:DD:ED:DD:AF:54:88" + ] + } + } +] diff --git a/web/_headers b/web/_headers index e4f84b9e..ce8fe760 100644 --- a/web/_headers +++ b/web/_headers @@ -23,6 +23,11 @@ /sqlite3.wasm Content-Type: application/wasm +# App Links verification (#1309). Android reads this file to confirm the app +# may open offband.app/add/... links, and accepts it only as JSON. +/.well-known/assetlinks.json + Content-Type: application/json + /* Cross-Origin-Opener-Policy: same-origin Cross-Origin-Embedder-Policy: require-corp