From e614fb32e27d1b7feab0e9173359ceefdf409f81 Mon Sep 17 00:00:00 2001 From: zjs81 Date: Sun, 11 Oct 2026 22:09:08 -0700 Subject: [PATCH] Address review: consent race, unsigned advert bytes, antimeridian - Uploads carry a consent generation: nothing is sent if sharing is turned off while a batch is being prepared, and a late response never touches a queue that was cleared and refilled - Parse only the signed 32 advert app-data bytes, as the firmware does, so appended unsigned bytes can't add the hidden marker or change the name - Split online map requests that cross the antimeridian into two valid boxes and merge the results - README: link the network map next to the website --- README.md | 2 +- .../network_map/network_map_api_client.dart | 26 ++++++ .../network_map/network_map_records.dart | 9 +- .../network_map_upload_service.dart | 26 +++++- lib/widgets/network_map_online_layer.dart | 27 +++--- .../network_map_api_client_test.dart | 45 ++++++++++ .../network_map/network_map_records_test.dart | 29 +++++- .../network_map_upload_service_test.dart | 89 +++++++++++++++++++ 8 files changed, 239 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 68b015a6..92d60fe4 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Open-source Flutter client for MeshCore LoRa mesh networking devices. MeshCore Open is a cross-platform application for communicating with MeshCore LoRa mesh radios over Bluetooth Low Energy (BLE), USB serial, or TCP. The app enables long-range, off-grid communication through peer-to-peer messaging, public channels, and mesh networking capabilities. -**Website:** [meshcoreopen.org](https://meshcoreopen.org/) +**Website:** [meshcoreopen.org](https://meshcoreopen.org/) · **Network map:** [map.meshcoreopen.org](https://map.meshcoreopen.org/) Get it on Obtainium diff --git a/lib/services/network_map/network_map_api_client.dart b/lib/services/network_map/network_map_api_client.dart index 7ba3cebb..ec549959 100644 --- a/lib/services/network_map/network_map_api_client.dart +++ b/lib/services/network_map/network_map_api_client.dart @@ -25,6 +25,32 @@ class NetworkMapBbox { maxLon - minLon <= networkMapMaxBboxSpan && maxLat - minLat <= networkMapMaxBboxSpan; + /// Valid request boxes for a visible area, or null when it is larger than + /// [networkMapMaxBboxSpan]. [west] and [east] may be unwrapped (beyond + /// ±180) or wrapped across the antimeridian (west > east); an area that + /// crosses ±180 is split into two boxes, one on each side. + static List? forVisibleArea({ + required double west, + required double south, + required double east, + required double north, + }) { + final minLat = south.clamp(-90.0, 90.0); + final maxLat = north.clamp(-90.0, 90.0); + var span = east - west; + if (span < 0) span += 360; + if (span > networkMapMaxBboxSpan || + maxLat - minLat > networkMapMaxBboxSpan) { + return null; + } + final w = (west + 180) % 360 - 180; // Dart % is non-negative here. + final e = w + span; + NetworkMapBbox box(double a, double b) => + NetworkMapBbox(minLon: a, minLat: minLat, maxLon: b, maxLat: maxLat); + if (e <= 180) return [box(w, e)]; + return [box(w, 180), box(-180, e - 360)]; + } + String get query => [ minLon, minLat, diff --git a/lib/services/network_map/network_map_records.dart b/lib/services/network_map/network_map_records.dart index aeefb432..dc1a8d93 100644 --- a/lib/services/network_map/network_map_records.dart +++ b/lib/services/network_map/network_map_records.dart @@ -1,3 +1,4 @@ +import 'dart:math'; import 'dart:convert'; import 'dart:typed_data'; @@ -288,7 +289,13 @@ Future buildAdvertRecord( const appDataStart = _advertPubKeySize + 4 + _advertSignatureSize; if (payload.length <= appDataStart) return null; - final appData = payload.sublist(appDataStart); + // Only the first 32 app-data bytes are signed (firmware Mesh.cpp truncates + // to MAX_ADVERT_DATA_SIZE before verifying and parsing), so bytes appended + // after them must never reach the name, location or 🚫 check. + final appData = payload.sublist( + appDataStart, + min(payload.length, appDataStart + _maxAdvertDataSize), + ); final flags = appData[0]; final type = flags & 0x0F; if (type != advTypeRepeater && type != advTypeRoom) return null; diff --git a/lib/services/network_map/network_map_upload_service.dart b/lib/services/network_map/network_map_upload_service.dart index 54dae384..2c6fa895 100644 --- a/lib/services/network_map/network_map_upload_service.dart +++ b/lib/services/network_map/network_map_upload_service.dart @@ -140,6 +140,12 @@ class NetworkMapUploadService extends ChangeNotifier { StreamSubscription? _frameSub; Timer? _timer; bool _uploading = false; + + /// Bumped whenever sharing is turned off. An upload started under an older + /// generation is never sent and its result is ignored, so nothing leaves the + /// phone after consent is withdrawn and a late response can't touch a queue + /// that was cleared and refilled in the meantime. + int _consentGeneration = 0; int _failures = 0; Duration _retryAfter = Duration.zero; Duration _nextInterval = uploadInterval; @@ -189,7 +195,10 @@ class NetworkMapUploadService extends ChangeNotifier { } void _sync() { - if (!enabled) _queue.clear(); + if (!enabled && (isActive || !_queue.isEmpty || _uploading)) { + _queue.clear(); + _consentGeneration++; + } // The store-review demo radio is simulated; its traffic must not reach // the real map. final shouldListen = @@ -263,6 +272,8 @@ class NetworkMapUploadService extends ChangeNotifier { Future _upload() async { _uploading = true; + final generation = _consentGeneration; + bool stale() => generation != _consentGeneration || !enabled; _lastAttemptAt = DateTime.now(); try { _accrueListenTime(); @@ -289,6 +300,7 @@ class NetworkMapUploadService extends ChangeNotifier { packets: batch.packets, adverts: batch.adverts, ); + if (stale()) return; final jsonBytes = utf8.encode(jsonEncode(body)); final gzipped = gzipEncode(jsonBytes); final response = await _client @@ -301,6 +313,7 @@ class NetworkMapUploadService extends ChangeNotifier { body: gzipped ?? jsonBytes, ) .timeout(const Duration(seconds: 30)); + if (stale()) return; final status = response.statusCode; if (status == 202 || status == 200) { _queue.remove(batch.packets.length, batch.adverts.length); @@ -318,6 +331,7 @@ class NetworkMapUploadService extends ChangeNotifier { _failures = 0; } } catch (e) { + if (stale()) return; appLogger.warn('Network map upload failed: $e', tag: 'NetworkMap'); _onFailed(null, null); } finally { @@ -363,6 +377,16 @@ class NetworkMapUploadService extends ChangeNotifier { : Duration(seconds: min(retryAfter, maxBackoff.inSeconds)); } + @visibleForTesting + int get queuedPacketCount => _queue.packetCount; + + @visibleForTesting + void queuePacketForTest(NetworkMapPacketRecord record) => + _queue.addPacket(record, DateTime.now()); + + @visibleForTesting + Future uploadNowForTest() => _upload(); + Future _appString() async { if (_appId != null) return _appId!; var version = ''; diff --git a/lib/widgets/network_map_online_layer.dart b/lib/widgets/network_map_online_layer.dart index 750bbc58..f3a0d5e9 100644 --- a/lib/widgets/network_map_online_layer.dart +++ b/lib/widgets/network_map_online_layer.dart @@ -52,26 +52,33 @@ class NetworkMapOnlineController extends ChangeNotifier { } Future _load(LatLngBounds bounds) async { - final bbox = NetworkMapBbox( - minLon: bounds.west.clamp(-180.0, 180.0), - minLat: bounds.south.clamp(-90.0, 90.0), - maxLon: bounds.east.clamp(-180.0, 180.0), - maxLat: bounds.north.clamp(-90.0, 90.0), + final boxes = NetworkMapBbox.forVisibleArea( + west: bounds.west, + south: bounds.south, + east: bounds.east, + north: bounds.north, ); final generation = ++_generation; - if (!bbox.isWithinLimit) { + if (boxes == null) { _setState(NetworkMapLoadState.zoomIn); return; } _setState(NetworkMapLoadState.loading); try { final results = await Future.wait([ - _client.fetchRepeaters(bbox), - _client.fetchLinks(bbox), + Future.wait(boxes.map(_client.fetchRepeaters)), + Future.wait(boxes.map((b) => _client.fetchLinks(b))), ]); if (_disposed || generation != _generation) return; - _repeaters = results[0] as List; - _links = results[1] as List; + // Two boxes (antimeridian) can both return items on the seam. + _repeaters = { + for (final r in results[0].expand((l) => l as List)) + r.key: r, + }.values.toList(); + _links = { + for (final l in results[1].expand((l) => l as List)) + '${l.a}:${l.b}': l, + }.values.toList(); _setState(NetworkMapLoadState.ready); } catch (_) { if (_disposed || generation != _generation) return; diff --git a/test/services/network_map/network_map_api_client_test.dart b/test/services/network_map/network_map_api_client_test.dart index 8492ef41..a35d861f 100644 --- a/test/services/network_map/network_map_api_client_test.dart +++ b/test/services/network_map/network_map_api_client_test.dart @@ -41,6 +41,51 @@ const _bbox = NetworkMapBbox( ); void main() { + group('visible area to request boxes', () { + List>? boxes(double w, double s, double e, double n) => + NetworkMapBbox.forVisibleArea( + west: w, + south: s, + east: e, + north: n, + )?.map((b) => [b.minLon, b.minLat, b.maxLon, b.maxLat]).toList(); + + test('normal area is one box', () { + expect(boxes(-112.5, 33, -111.5, 34), [ + [-112.5, 33, -111.5, 34], + ]); + }); + + test('wrapped across the antimeridian is split in two', () { + expect(boxes(179, -18, -179, -16), [ + [179, -18, 180, -16], + [-180, -18, -179, -16], + ]); + }); + + test('unwrapped longitudes beyond 180 are normalised and split', () { + expect(boxes(175, 0, 185, 5), [ + [175, 0, 180, 5], + [-180, 0, -175, 5], + ]); + expect(boxes(190, 0, 200, 5), [ + [-170, 0, -160, 5], + ]); + }); + + test('too large an area asks to zoom in', () { + expect(boxes(-130, 20, -60, 50), isNull); + expect(boxes(170, -40, -170, 40), isNull); + }); + + test('every box is valid for the server', () { + for (final b in boxes(179.5, 10, -170.25, 12)!) { + expect(b[0] < b[2], isTrue); + expect(b[0] >= -180 && b[2] <= 180, isTrue); + } + }); + }); + test('parses repeaters and sends bbox', () async { late Uri requested; final client = NetworkMapApiClient( diff --git a/test/services/network_map/network_map_records_test.dart b/test/services/network_map/network_map_records_test.dart index 441f09f4..320311a5 100644 --- a/test/services/network_map/network_map_records_test.dart +++ b/test/services/network_map/network_map_records_test.dart @@ -75,6 +75,7 @@ Future> _signedAdvertPayload({ double? lon, String? name, bool corruptSignature = false, + List unsignedSuffix = const [], }) async { final publicKey = (await keyPair.extractPublicKey()).bytes; final timestamp = [0x10, 0x20, 0x30, 0x40]; @@ -99,7 +100,13 @@ Future> _signedAdvertPayload({ ], keyPair: keyPair); final sigBytes = List.from(signature.bytes); if (corruptSignature) sigBytes[10] ^= 0x01; - return [...publicKey, ...timestamp, ...sigBytes, ...appData]; + return [ + ...publicKey, + ...timestamp, + ...sigBytes, + ...appData, + ...unsignedSuffix, + ]; } void main() { @@ -279,6 +286,7 @@ void main() { double? lon, String? name, bool corrupt = false, + List unsignedSuffix = const [], }) async { return _rxFrame( payloadType: payloadTypeADVERT, @@ -291,6 +299,7 @@ void main() { lon: lon, name: name, corruptSignature: corrupt, + unsignedSuffix: unsignedSuffix, ), ); } @@ -361,6 +370,24 @@ void main() { expect(json['key'], keyHex); }); + test('bytes after the signed 32 app-data bytes are ignored', () async { + // flags (1) + location (8) + 23-byte name fill the signed 32 bytes. + const name = 'Hilltop Repeater North1'; + expect(utf8.encode(name).length, 23); + final frame = await advertFrame( + type: advTypeRepeater, + lat: 47.6, + lon: -122.3, + name: name, + unsignedSuffix: utf8.encode(' \u{1F6AB} evil'), + ); + final advert = (await buildAdvertRecord(frame, _receivedAt))!; + final json = advert.toJson(); + expect(json['hidden'], false); + expect(json['name'], name); + expect(json['loc'], isNotNull); + }); + test('adverts with a bad signature are dropped', () async { final frame = await advertFrame( type: advTypeRepeater, diff --git a/test/services/network_map/network_map_upload_service_test.dart b/test/services/network_map/network_map_upload_service_test.dart index db7c0efe..39442f7c 100644 --- a/test/services/network_map/network_map_upload_service_test.dart +++ b/test/services/network_map/network_map_upload_service_test.dart @@ -3,7 +3,9 @@ import 'package:http/http.dart' as http; import 'package:http/testing.dart'; import 'package:meshcore_open/connector/meshcore_connector.dart'; import 'package:meshcore_open/services/app_settings_service.dart'; +import 'package:meshcore_open/services/network_map/network_map_records.dart'; import 'package:meshcore_open/services/network_map/network_map_upload_service.dart'; +import 'dart:async'; import 'package:meshcore_open/storage/prefs_manager.dart'; import 'package:shared_preferences/shared_preferences.dart'; @@ -45,4 +47,91 @@ void main() { expect(service.packetsSentToday, 0); expect(requests, 0); }); + + group('withdrawing consent mid-upload', () { + NetworkMapPacketRecord packet() => NetworkMapPacketRecord( + t: DateTime.now().millisecondsSinceEpoch ~/ 60000 * 60, + pt: 5, + rt: 1, + hw: 1, + path: 'aa', + snr: 6.5, + rssi: -90, + size: 48, + h: '0011223344556677', + scoped: false, + ); + + Future<(NetworkMapUploadService, AppSettingsService)> setUpService( + MockClient client, + ) async { + final connector = _ConnectedConnector(); + final settings = AppSettingsService(); + await settings.loadSettings(); + await settings.setNetworkMapUploadEnabled(true); + final service = NetworkMapUploadService( + connector: connector, + settings: settings, + client: client, + ); + addTearDown(() { + service.dispose(); + connector.dispose(); + }); + return (service, settings); + } + + test( + 'nothing is sent when sharing is turned off before the POST', + () async { + var requests = 0; + final (service, settings) = await setUpService( + MockClient((_) async { + requests++; + return http.Response('{}', 202); + }), + ); + service.queuePacketForTest(packet()); + final upload = service.uploadNowForTest(); + await settings.setNetworkMapUploadEnabled(false); + await upload; + + expect(requests, 0); + expect(service.queuedPacketCount, 0); + expect(service.lastUploadAt, isNull); + }, + ); + + test('a late response never touches the refilled queue', () async { + final response = Completer(); + final posted = Completer(); + final (service, settings) = await setUpService( + MockClient((_) { + posted.complete(); + return response.future; + }), + ); + service.queuePacketForTest(packet()); + final upload = service.uploadNowForTest(); + await posted.future; + + // Off and on again while the first POST is in flight, then new records. + await settings.setNetworkMapUploadEnabled(false); + await settings.setNetworkMapUploadEnabled(true); + service.queuePacketForTest(packet()); + service.queuePacketForTest(packet()); + + response.complete(http.Response('{}', 202)); + await upload; + + expect(service.queuedPacketCount, 2); + expect(service.lastUploadAt, isNull); + expect(service.packetsSentToday, 0); + }); + }); +} + +class _ConnectedConnector extends MeshCoreConnector { + @override + bool get isConnected => true; }