feat(#831): per-send channel region scoping (Epic #815 C3)

sendChannelMessage routes both the text and reaction sends through _sendChannelFrameScoped: when the channel has a region scope (C1 store), it sets the flood-scope key (CMD 54 sub-0, SHA256 of the region) before the send and clears to node default after. Fail-closed: a scoped channel on firmware without region support is blocked, never sent unscoped. Tested via a seam: unscoped passthrough, scoped set/send/clear with the right key, and the blocked case.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
fix/812-land-814-815-on-dev
diffwireauto 2 months ago
parent b3cb4c6a3f
commit bdf313c12b

@ -60,6 +60,8 @@ import '../utils/battery_utils.dart';
import '../utils/platform_info.dart';
import 'meshcore_uuids.dart';
import 'meshcore_protocol.dart';
import '../helpers/region_key.dart';
import '../storage/channel_region_scope_store.dart';
import 'offband_device_ui.dart';
import 'caplog_reassembler.dart';
@ -552,6 +554,8 @@ class MeshCoreConnector extends ChangeNotifier {
final MessageStore _messageStore = MessageStore();
final ChannelOrderStore _channelOrderStore = ChannelOrderStore();
final ChannelSettingsStore _channelSettingsStore = ChannelSettingsStore();
final ChannelRegionScopeStore _channelRegionScopeStore =
ChannelRegionScopeStore();
final ContactSettingsStore _contactSettingsStore = ContactSettingsStore();
final ContactStore _contactStore = ContactStore();
final ContactDiscoveryStore _discoveryContactStore = ContactDiscoveryStore();
@ -4687,7 +4691,8 @@ class MeshCoreConnector extends ChangeNotifier {
_pendingChannelSentQueue.add(reactionQueueId);
await _waitForRadioQuiet(lastInboundRxTime: _lastChannelMsgRxTime);
try {
await sendFrame(
await _sendChannelFrameScoped(
channel.index,
buildSendChannelTextMsgFrame(
channel.index,
text,
@ -4729,7 +4734,8 @@ class MeshCoreConnector extends ChangeNotifier {
final outboundText = prepareChannelOutboundText(channel.index, text);
await _waitForRadioQuiet(lastInboundRxTime: _lastChannelMsgRxTime);
try {
await sendFrame(
await _sendChannelFrameScoped(
channel.index,
buildSendChannelTextMsgFrame(
channel.index,
outboundText,
@ -4758,6 +4764,66 @@ class MeshCoreConnector extends ChangeNotifier {
}
}
/// Send a channel frame, applying the channel's region scope on the wire if
/// one is configured (Feature #812, Epic #815). Fail-closed: a scoped channel
/// on firmware that cannot scope is blocked, never sent unscoped; the scope is
/// set before the send and cleared to the node default after.
Future<void> _sendChannelFrameScoped(
int channelIndex,
Uint8List frame, {
String? channelSendQueueId,
bool expectsGenericAck = false,
}) async {
final regionName = await _channelRegionScopeStore.scopeFor(channelIndex);
if (regionName == null) {
await sendFrame(
frame,
channelSendQueueId: channelSendQueueId,
expectsGenericAck: expectsGenericAck,
);
return;
}
if (!supportsRegionScope) {
throw StateError(
'channel $channelIndex is region-scoped to "$regionName" but the '
'firmware does not support region scoping',
);
}
final key = transportKeyForName(regionName);
if (key == null) {
throw StateError(
'cannot derive a transport key for region "$regionName"',
);
}
await setChannelSendScope(key);
try {
await sendFrame(
frame,
channelSendQueueId: channelSendQueueId,
expectsGenericAck: expectsGenericAck,
);
} finally {
try {
await clearSendScope();
} catch (e) {
appLogger.warn(
'failed to clear region send-scope after a channel send: $e',
tag: 'Region',
);
}
}
}
@visibleForTesting
ChannelRegionScopeStore get channelRegionScopeStoreForTest =>
_channelRegionScopeStore;
@visibleForTesting
Future<void> sendChannelFrameScopedForTest(
int channelIndex,
Uint8List frame,
) => _sendChannelFrameScoped(channelIndex, frame);
Future<void> removeContact(Contact contact) async {
if (!isConnected) return;
@ -6590,6 +6656,7 @@ class MeshCoreConnector extends ChangeNotifier {
_messageStore.setPublicKeyHex = selfPublicKeyHex;
_channelOrderStore.setPublicKeyHex = selfPublicKeyHex;
_channelSettingsStore.setPublicKeyHex = selfPublicKeyHex;
_channelRegionScopeStore.setPublicKeyHex = selfPublicKeyHex;
_contactSettingsStore.setPublicKeyHex = selfPublicKeyHex;
_contactStore.setPublicKeyHex = selfPublicKeyHex;
_channelStore.setPublicKeyHex = selfPublicKeyHex;

@ -0,0 +1,72 @@
// Epic #815 T C3: per-send region scoping around a channel send.
// Drives the scoping helper via its test seam, capturing the frame sequence.
import 'dart:typed_data';
import 'package:flutter_test/flutter_test.dart';
import 'package:meshcore_open/connector/meshcore_connector.dart';
import 'package:meshcore_open/connector/meshcore_protocol.dart';
import 'package:meshcore_open/storage/prefs_manager.dart';
import 'package:shared_preferences/shared_preferences.dart';
// Known vector: first 16 bytes of SHA256("#oki").
final _okiKey = Uint8List.fromList([
0x59, 0x9f, 0xe2, 0x54, 0x60, 0x02, 0x70, 0x9f, //
0xa4, 0x93, 0xb1, 0x5d, 0xe5, 0x8b, 0xdd, 0x0d,
]);
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
late MeshCoreConnector connector;
final frame = Uint8List.fromList([cmdSendChannelTxtMsg, 0, 1, 2, 3]);
setUp(() async {
SharedPreferences.setMockInitialValues({});
PrefsManager.reset();
await PrefsManager.initialize();
connector = MeshCoreConnector();
connector.setConnectedForTest();
connector.setFirmwareVerCodeForTest(22);
connector.channelRegionScopeStoreForTest.setPublicKeyHex =
'aabbccddee0011223344';
});
test('an unscoped channel sends the frame alone', () async {
final sent = <Uint8List>[];
connector.sendFrameOverrideForTest = sent.add;
await connector.sendChannelFrameScopedForTest(0, frame);
expect(sent, hasLength(1));
expect(sent.single, frame);
});
test('a scoped channel sets the key, sends, then clears', () async {
await connector.channelRegionScopeStoreForTest.setScope(0, 'oki');
final sent = <Uint8List>[];
connector.sendFrameOverrideForTest = sent.add;
await connector.sendChannelFrameScopedForTest(0, frame);
expect(sent, hasLength(3));
// set scope: [54, 0, key16]
expect(sent[0][0], cmdSetFloodScopeKey);
expect(sent[0][1], floodScopeSubSetKey);
expect(sent[0].sublist(2), _okiKey);
// the message
expect(sent[1], frame);
// clear scope: [54, 0]
expect(sent[2], [cmdSetFloodScopeKey, floodScopeSubSetKey]);
});
test(
'a scoped channel on unsupported firmware is blocked, nothing sent',
() async {
await connector.channelRegionScopeStoreForTest.setScope(0, 'oki');
connector.setFirmwareVerCodeForTest(7);
final sent = <Uint8List>[];
connector.sendFrameOverrideForTest = sent.add;
await expectLater(
connector.sendChannelFrameScopedForTest(0, frame),
throwsStateError,
);
expect(sent, isEmpty);
},
);
}
Loading…
Cancel
Save

Powered by TurnKey Linux.