feat(#1309): open offband.app share links in Offband on Android and web

A share link can now be https://offband.app/add/channel#... or
/add/contact#..., on offband.app or dev.offband.app. The fields sit after
the #, which browsers never send, so a channel secret stays out of the
server's logs. The router turns the link into its meshcore:// twin, so
one parser still reads every form.

- Android: an autoVerify App Links filter for /add/ on both hosts, and
  web/.well-known/assetlinks.json with the release key's SHA-256, served
  as JSON. A verified link opens Offband with no chooser, so the stock
  app, which also answers meshcore://, is out of the way.
- Web: the page's own address arrives as the initial link. Once read it
  is cleared from the address bar, so a reload asks nothing.
- A tap on such a link in a chat message opens the add confirmation, as
  meshcore:// links already do.
- An /add/ address with broken fields shows the invalid-link message;
  the site's other pages are ignored.

iOS Universal Links follow once the Apple team ID and a profile with
Associated Domains are in place.

Agent: StormyElk (session 088ce922)
pull/1328/head
diffwireauto 2 days ago
parent be6b699405
commit edfb57c7fd

@ -61,6 +61,18 @@
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="meshcore"/>
</intent-filter>
<!-- https://offband.app/add/... share links (#1309). autoVerify
checks web/.well-known/assetlinks.json on each host, so a
verified link opens Offband with no app chooser. -->
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="https"/>
<data android:host="offband.app"/>
<data android:host="dev.offband.app"/>
<data android:pathPrefix="/add/"/>
</intent-filter>
</activity>
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->

@ -120,7 +120,8 @@ class LinkHandler {
static Future<void> 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<LinkRouter>().handle(url);
return;
}

@ -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.

@ -0,0 +1,2 @@
export 'address_bar_stub.dart'
if (dart.library.js_interop) 'address_bar_web.dart';

@ -0,0 +1,2 @@
/// Only the web client has an address bar.
void clearShareLinkFromAddressBar() {}

@ -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, '', '/');
}

@ -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'<intent-filter android:autoVerify="true">([\s\S]*?)</intent-filter>',
).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<String, dynamic>;
expect(entry['relation'], ['delegate_permission/common.handle_all_urls']);
final target = entry['target'] as Map<String, dynamic>;
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);

@ -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<ChannelShareLink>(),
);
});
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<ChannelShareLink>());
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');

@ -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"
]
}
}
]

@ -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

Loading…
Cancel
Save

Powered by TurnKey Linux.