diff --git a/lib/connector/meshcore_connector.dart b/lib/connector/meshcore_connector.dart index 332b40a9..c18c20b7 100644 --- a/lib/connector/meshcore_connector.dart +++ b/lib/connector/meshcore_connector.dart @@ -9727,6 +9727,10 @@ class MeshCoreConnector extends ChangeNotifier { _channelOrderStore.setPublicKeyHex = selfPublicKeyHex; _channelSettingsStore.setPublicKeyHex = selfPublicKeyHex; _channelRegionScopeStore.setPublicKeyHex = selfPublicKeyHex; + // Key region scope by the channel's PSK identity (not its reusable slot + // index) so a sync reshuffle can't move a scope to the wrong channel. (#1256) + _channelRegionScopeStore.channelPskResolver = (index) => + _findChannelByIndex(index)?.pskHex; _contactSettingsStore.setPublicKeyHex = selfPublicKeyHex; _contactStore.setPublicKeyHex = selfPublicKeyHex; _channelStore.setPublicKeyHex = selfPublicKeyHex; diff --git a/lib/storage/channel_region_scope_store.dart b/lib/storage/channel_region_scope_store.dart index 89995266..de9aa6da 100644 --- a/lib/storage/channel_region_scope_store.dart +++ b/lib/storage/channel_region_scope_store.dart @@ -3,23 +3,83 @@ import 'prefs_manager.dart'; /// Per-channel region scope, persisted per device (Feature #812, Epic #815). /// -/// Stores the region name a channel's outgoing messages should ride, keyed by -/// the first 10 hex chars of the connected device's public key (the same -/// scoping the other channel stores use). The name is the `#`-stripped region -/// name from discovery; null/absent means the channel is unscoped. +/// Keyed by the channel **PSK** (the channel's identity), scoped per device by +/// the first 10 hex chars of the connected device's public key. A slot +/// reshuffle on sync cannot move a scope to the wrong channel, because the key +/// is the PSK, not the slot index (#1256). Falls back to the slot-index key +/// only while the PSK is unknown (channel list not yet loaded), mirroring +/// `channel_message_store` (#194). The name is the `#`-stripped region name +/// from discovery; null/absent means the channel is unscoped. class ChannelRegionScopeStore { static const String _keyPrefix = 'channel_region_scope_'; + static const String _pskMarker = 'psk_'; String publicKeyHex = ''; set setPublicKeyHex(String value) => publicKeyHex = value.length > 10 ? value.substring(0, 10) : ''; - String _key(int channelIndex) => '$_keyPrefix${publicKeyHex}_$channelIndex'; + /// Resolves a slot index to the channel's PSK hex, or null while unknown. + /// Mirrors `channel_message_store.channelPskResolver` (#194/#1062). + String? Function(int index)? channelPskResolver; + + String _indexKey(int channelIndex) => + '$_keyPrefix${publicKeyHex}_$channelIndex'; + String _pskKey(String pskHex) => + '$_keyPrefix${publicKeyHex}_$_pskMarker$pskHex'; + + /// Active key: the PSK identity when known, else the slot index. A fallback + /// to the index key while a resolver exists means the channel list was not + /// loaded first; it reads a DIFFERENT key, so it is logged, never silent + /// (SAFELANE 6). + String _activeKey(int channelIndex, {bool forWrite = false}) { + final pskHex = channelPskResolver?.call(channelIndex); + if (pskHex != null && pskHex.isNotEmpty) { + return _pskKey(pskHex); + } + if (channelPskResolver != null) { + if (forWrite) { + appLogger.error( + 'Region-scope WRITE for channel $channelIndex fell back to the ' + 'slot-index key: the slot did not resolve to a PSK. (#1256)', + tag: 'Storage', + ); + } else { + appLogger.warn( + 'Region scope for channel $channelIndex has no PSK yet; falling ' + 'back to the slot-index key. The channel list was not loaded first. ' + '(#1256)', + tag: 'Storage', + ); + } + } + return _indexKey(channelIndex); + } /// The region name scoping [channelIndex], or null when unscoped. Future scopeFor(int channelIndex) async { if (publicKeyHex.isEmpty) return null; - return PrefsManager.instance.getString(_key(channelIndex)); + final key = _activeKey(channelIndex); + final value = PrefsManager.instance.getString(key); + if (value != null && value.isNotEmpty) return value; + + // One-time best-effort migration into the PSK-identity key: when the PSK is + // known and the PSK key is empty, adopt a pre-#1256 slot-index value and + // drop the source. Maps the current index to the PSK (owner: best-effort; + // an already-reshuffled scope binds to the current occupant once). + final indexKey = _indexKey(channelIndex); + if (key != indexKey) { + final legacy = PrefsManager.instance.getString(indexKey); + if (legacy != null && legacy.isNotEmpty) { + appLogger.info( + 'Migrating region scope $indexKey -> $key (PSK-keyed, #1256)', + tag: 'Storage', + ); + await PrefsManager.instance.setString(key, legacy); + await PrefsManager.instance.remove(indexKey); + return legacy; + } + } + return null; } /// Scope [channelIndex] to [regionName]. @@ -28,12 +88,20 @@ class ChannelRegionScopeStore { appLogger.warn('No device key set; cannot save channel region scope.'); return; } - await PrefsManager.instance.setString(_key(channelIndex), regionName); + final key = _activeKey(channelIndex, forWrite: true); + await PrefsManager.instance.setString(key, regionName); + // Kill any stale pre-#1256 index-keyed value so it can never be re-adopted + // after this PSK scope is later cleared. + final indexKey = _indexKey(channelIndex); + if (key != indexKey) await PrefsManager.instance.remove(indexKey); } /// Remove the region scope on [channelIndex] (back to unscoped). Future clearScope(int channelIndex) async { if (publicKeyHex.isEmpty) return; - await PrefsManager.instance.remove(_key(channelIndex)); + final key = _activeKey(channelIndex, forWrite: true); + await PrefsManager.instance.remove(key); + final indexKey = _indexKey(channelIndex); + if (key != indexKey) await PrefsManager.instance.remove(indexKey); } } diff --git a/test/storage/channel_region_scope_store_test.dart b/test/storage/channel_region_scope_store_test.dart index 9ca28c8a..939fb405 100644 --- a/test/storage/channel_region_scope_store_test.dart +++ b/test/storage/channel_region_scope_store_test.dart @@ -57,4 +57,43 @@ void main() { await store.setScope(0, 'oki'); expect(await store.scopeFor(0), isNull); }); + + group('PSK identity keying (#1256)', () { + test('scope follows the channel PSK across a slot reshuffle', () async { + final store = storeFor(_devA); + final slots = {0: 'pskaaaa', 1: 'pskbbbb'}; + store.channelPskResolver = (i) => slots[i]; + await store.setScope(0, 'oki'); // channel pskaaaa at slot 0 + + // A sync reshuffles the slots: pskaaaa moves to slot 1, pskbbbb to slot 0. + slots[0] = 'pskbbbb'; + slots[1] = 'pskaaaa'; + + expect(await store.scopeFor(1), 'oki'); // scope followed pskaaaa + expect(await store.scopeFor(0), isNull); // pskbbbb has no scope + }); + + test('keys by PSK, not index, when the resolver is set', () async { + final store = storeFor(_devA); + store.channelPskResolver = (i) => 'pskzzzz'; + await store.setScope(0, 'day'); + // A different slot index resolving to the same PSK sees the same scope. + expect(await store.scopeFor(9), 'day'); + }); + + test('adopts a legacy index-keyed scope once the PSK is known', () async { + // Pre-fix value: written with no resolver, so it lives on the index key. + await storeFor(_devA).setScope(2, 'cvg'); + + final store = storeFor(_devA); + store.channelPskResolver = (i) => i == 2 ? 'pskcccc' : null; + expect(await store.scopeFor(2), 'cvg'); // migrated off the index key + + // Migration is sticky: now reachable by PSK even if the slot moves, and + // the old index key is gone. + store.channelPskResolver = (i) => i == 5 ? 'pskcccc' : null; + expect(await store.scopeFor(5), 'cvg'); + expect(await store.scopeFor(2), isNull); + }); + }); }