fix(#1256): key region scope by channel PSK, not slot index

A sync reshuffle moved a channel's region scope to the wrong slot occupant (Dot). Key the scope by the channel PSK like channel_message_store (#194), fall back to the index key only while the PSK is unknown (logged, never silent), and best-effort migrate existing index-keyed scopes on first read. Regression test: indices swap, PSKs stable, each scope stays on its own channel.
pull/1263/head
diffwireauto 4 days ago
parent a37d7f2b4b
commit a8e7bc34b6

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

@ -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<String?> 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<void> 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);
}
}

@ -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 = <int, String?>{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);
});
});
}

Loading…
Cancel
Save

Powered by TurnKey Linux.