fix(app): reserved bundled module ids, existing-data warning, clipboard cleared on hide, fields reset per screen

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dasemuandClaude Opus 5.5 committed 2026-10-26 16:34:25 +01:00
1 parent 0024438706
commit ada391a88e
11 files changed
+404 -95

No files matched your search

+3 -1
View File
@@ -22,8 +22,10 @@ class _SynapseAppState extends State<SynapseApp> {
..loadTypeIcons()
..modules.load();
/// On phones, modules lock what they hold (the secrets vault) when the app is paused.
/// Modules lock what they hold (the secrets vault) when the app is hidden (minimised,
/// another app in front) or paused.
late final _lifecycle = AppLifecycleListener(
onHide: () => _state.modules.suspend(),
onPause: () => _state.modules.suspend(),
);
+6
View File
@@ -155,6 +155,10 @@ abstract class GraphBackend {
/// Drops a module's running instance (its in-memory state, e.g. unlocked keys).
Future<void> stopModule(String id);
/// Whether the graph already holds data for module [id] (synced or left by an earlier
/// install); a module installed under that id can read it.
Future<bool> moduleHasData(String id);
}
class _FfiLayout implements LayoutEngine {
@@ -411,4 +415,6 @@ class FfiGraphBackend implements GraphBackend {
@override
Future<void> stopModule(String id) => _g.stopModule(id: id);
@override
Future<bool> moduleHasData(String id) => _g.moduleHasData(id: id);
}
+4
View File
@@ -132,6 +132,10 @@ abstract class SynapseGraph implements RustOpaqueInterface {
/// Sends a JSON request to a module and returns its JSON response.
Future<String> moduleCall({required String id, required String request});
/// Whether the graph already holds data for module `id` (a module installed under that id
/// can read it; the consent dialog warns about it).
Future<bool> moduleHasData({required String id});
/// Installed modules, by name.
Future<List<ModuleDto>> modules();
+87 -39
View File
@@ -69,7 +69,7 @@ class RustLib extends BaseEntrypoint<RustLibApi, RustLibApiImpl, RustLibWire> {
String get codegenVersion => '2.13.0';
@override
int get rustContentHash => -821319892;
int get rustContentHash => -984841087;
static const kDefaultExternalLibraryLoaderConfig =
ExternalLibraryLoaderConfig(
@@ -270,6 +270,11 @@ abstract class RustLibApi extends BaseApi {
required String request,
});
Future<bool> crateApiGraphSynapseGraphModuleHasData({
required SynapseGraph that,
required String id,
});
Future<List<ModuleDto>> crateApiGraphSynapseGraphModules({
required SynapseGraph that,
});
@@ -1845,6 +1850,44 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
argNames: ["that", "id", "request"],
);
@override
Future<bool> crateApiGraphSynapseGraphModuleHasData({
required SynapseGraph that,
required String id,
}) {
return handler.executeNormal(
NormalTask(
callFfi: (port_) {
final serializer = SseSerializer(generalizedFrbRustBinding);
sse_encode_Auto_Ref_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInnerSynapseGraph(
that,
serializer,
);
sse_encode_String(id, serializer);
pdeCallFfi(
generalizedFrbRustBinding,
serializer,
funcId: 36,
port: port_,
);
},
codec: SseCodec(
decodeSuccessData: sse_decode_bool,
decodeErrorData: sse_decode_AnyhowException,
),
constMeta: kCrateApiGraphSynapseGraphModuleHasDataConstMeta,
argValues: [that, id],
apiImpl: this,
),
);
}
TaskConstMeta get kCrateApiGraphSynapseGraphModuleHasDataConstMeta =>
const TaskConstMeta(
debugName: "SynapseGraph_module_has_data",
argNames: ["that", "id"],
);
@override
Future<List<ModuleDto>> crateApiGraphSynapseGraphModules({
required SynapseGraph that,
@@ -1860,7 +1903,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
pdeCallFfi(
generalizedFrbRustBinding,
serializer,
funcId: 36,
funcId: 37,
port: port_,
);
},
@@ -1902,7 +1945,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
pdeCallFfi(
generalizedFrbRustBinding,
serializer,
funcId: 37,
funcId: 38,
port: port_,
);
},
@@ -1933,7 +1976,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
pdeCallFfi(
generalizedFrbRustBinding,
serializer,
funcId: 38,
funcId: 39,
port: port_,
);
},
@@ -1969,7 +2012,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
pdeCallFfi(
generalizedFrbRustBinding,
serializer,
funcId: 39,
funcId: 40,
port: port_,
);
},
@@ -2007,7 +2050,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
pdeCallFfi(
generalizedFrbRustBinding,
serializer,
funcId: 40,
funcId: 41,
port: port_,
);
},
@@ -2045,7 +2088,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
pdeCallFfi(
generalizedFrbRustBinding,
serializer,
funcId: 41,
funcId: 42,
port: port_,
);
},
@@ -2083,7 +2126,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
pdeCallFfi(
generalizedFrbRustBinding,
serializer,
funcId: 42,
funcId: 43,
port: port_,
);
},
@@ -2121,7 +2164,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
pdeCallFfi(
generalizedFrbRustBinding,
serializer,
funcId: 43,
funcId: 44,
port: port_,
);
},
@@ -2159,7 +2202,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
pdeCallFfi(
generalizedFrbRustBinding,
serializer,
funcId: 44,
funcId: 45,
port: port_,
);
},
@@ -2197,7 +2240,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
pdeCallFfi(
generalizedFrbRustBinding,
serializer,
funcId: 45,
funcId: 46,
port: port_,
);
},
@@ -2235,7 +2278,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
pdeCallFfi(
generalizedFrbRustBinding,
serializer,
funcId: 46,
funcId: 47,
port: port_,
);
},
@@ -2277,7 +2320,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
pdeCallFfi(
generalizedFrbRustBinding,
serializer,
funcId: 47,
funcId: 48,
port: port_,
);
},
@@ -2317,7 +2360,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
pdeCallFfi(
generalizedFrbRustBinding,
serializer,
funcId: 48,
funcId: 49,
port: port_,
);
},
@@ -2357,7 +2400,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
pdeCallFfi(
generalizedFrbRustBinding,
serializer,
funcId: 49,
funcId: 50,
port: port_,
);
},
@@ -2397,7 +2440,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
pdeCallFfi(
generalizedFrbRustBinding,
serializer,
funcId: 50,
funcId: 51,
port: port_,
);
},
@@ -2435,7 +2478,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
pdeCallFfi(
generalizedFrbRustBinding,
serializer,
funcId: 51,
funcId: 52,
port: port_,
);
},
@@ -2475,7 +2518,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
pdeCallFfi(
generalizedFrbRustBinding,
serializer,
funcId: 52,
funcId: 53,
port: port_,
);
},
@@ -2515,7 +2558,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
pdeCallFfi(
generalizedFrbRustBinding,
serializer,
funcId: 53,
funcId: 54,
port: port_,
);
},
@@ -2553,7 +2596,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
pdeCallFfi(
generalizedFrbRustBinding,
serializer,
funcId: 54,
funcId: 55,
port: port_,
);
},
@@ -2591,7 +2634,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
pdeCallFfi(
generalizedFrbRustBinding,
serializer,
funcId: 55,
funcId: 56,
port: port_,
);
},
@@ -2631,7 +2674,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
pdeCallFfi(
generalizedFrbRustBinding,
serializer,
funcId: 56,
funcId: 57,
port: port_,
);
},
@@ -2671,7 +2714,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
pdeCallFfi(
generalizedFrbRustBinding,
serializer,
funcId: 57,
funcId: 58,
port: port_,
);
},
@@ -2711,7 +2754,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
pdeCallFfi(
generalizedFrbRustBinding,
serializer,
funcId: 58,
funcId: 59,
port: port_,
);
},
@@ -2751,7 +2794,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
pdeCallFfi(
generalizedFrbRustBinding,
serializer,
funcId: 59,
funcId: 60,
port: port_,
);
},
@@ -2791,7 +2834,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
pdeCallFfi(
generalizedFrbRustBinding,
serializer,
funcId: 60,
funcId: 61,
port: port_,
);
},
@@ -2831,7 +2874,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
pdeCallFfi(
generalizedFrbRustBinding,
serializer,
funcId: 61,
funcId: 62,
port: port_,
);
},
@@ -2871,7 +2914,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
pdeCallFfi(
generalizedFrbRustBinding,
serializer,
funcId: 62,
funcId: 63,
port: port_,
);
},
@@ -2911,7 +2954,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
pdeCallFfi(
generalizedFrbRustBinding,
serializer,
funcId: 63,
funcId: 64,
port: port_,
);
},
@@ -2951,7 +2994,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
pdeCallFfi(
generalizedFrbRustBinding,
serializer,
funcId: 64,
funcId: 65,
port: port_,
);
},
@@ -2991,7 +3034,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
pdeCallFfi(
generalizedFrbRustBinding,
serializer,
funcId: 65,
funcId: 66,
port: port_,
);
},
@@ -3031,7 +3074,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
pdeCallFfi(
generalizedFrbRustBinding,
serializer,
funcId: 66,
funcId: 67,
port: port_,
);
},
@@ -3069,7 +3112,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
pdeCallFfi(
generalizedFrbRustBinding,
serializer,
funcId: 67,
funcId: 68,
port: port_,
);
},
@@ -3105,7 +3148,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
pdeCallFfi(
generalizedFrbRustBinding,
serializer,
funcId: 68,
funcId: 69,
port: port_,
);
},
@@ -3145,7 +3188,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
pdeCallFfi(
generalizedFrbRustBinding,
serializer,
funcId: 69,
funcId: 70,
port: port_,
);
},
@@ -3176,7 +3219,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
that,
serializer,
);
return pdeCallFfi(generalizedFrbRustBinding, serializer, funcId: 70)!;
return pdeCallFfi(generalizedFrbRustBinding, serializer, funcId: 71)!;
},
codec: SseCodec(
decodeSuccessData: sse_decode_String,
@@ -3207,7 +3250,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
pdeCallFfi(
generalizedFrbRustBinding,
serializer,
funcId: 71,
funcId: 72,
port: port_,
);
},
@@ -3247,7 +3290,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
pdeCallFfi(
generalizedFrbRustBinding,
serializer,
funcId: 72,
funcId: 73,
port: port_,
);
},
@@ -3287,7 +3330,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
pdeCallFfi(
generalizedFrbRustBinding,
serializer,
funcId: 73,
funcId: 74,
port: port_,
);
},
@@ -6039,6 +6082,11 @@ class SynapseGraphImpl extends RustOpaque implements SynapseGraph {
request: request,
);
/// Whether the graph already holds data for module `id` (a module installed under that id
/// can read it; the consent dialog warns about it).
Future<bool> moduleHasData({required String id}) => RustLib.instance.api
.crateApiGraphSynapseGraphModuleHasData(that: this, id: id);
/// Installed modules, by name.
Future<List<ModuleDto>> modules() =>
RustLib.instance.api.crateApiGraphSynapseGraphModules(that: this);
+37 -9
View File
@@ -42,6 +42,9 @@ class ModulesController extends ChangeNotifier {
final _timers = <String, Timer>{};
Timer? _clipboardTimer;
/// Text a module copied with a clear delay, until it is cleared.
String? _clipboardSecret;
bool _disposed = false;
Iterable<ModuleDto> get enabled => installed.where((m) => m.enabled);
@@ -76,6 +79,14 @@ class ModulesController extends ChangeNotifier {
Future<ModuleDto> inspect(Uint8List wasm) => backend.inspectModule(wasm);
Future<bool> hasData(String id) async {
try {
return await backend.moduleHasData(id);
} catch (_) {
return false;
}
}
Future<ModuleDto> install(Uint8List wasm, List<String> granted) async {
final m = await backend.installModule(wasm, granted);
await load();
@@ -129,8 +140,10 @@ class ModulesController extends ChangeNotifier {
notifyListeners();
}
/// The app is going to the background: modules lock what they hold (e.g. the vault).
/// The app is going to the background: modules lock what they hold (e.g. the vault), and a
/// secret still on the clipboard is cleared (a backgrounded app cannot rely on its timer).
Future<void> suspend() async {
await clearClipboard();
var changed = false;
for (final m in enabled.toList()) {
final r = await call(m.id, {'op': 'suspend'});
@@ -156,14 +169,10 @@ class ModulesController extends ChangeNotifier {
await Clipboard.setData(ClipboardData(text: text));
final ms = copy['clear_after_ms'];
_clipboardTimer?.cancel();
_clipboardSecret = null;
if (ms is int && ms > 0) {
_clipboardTimer = Timer(Duration(milliseconds: ms), () async {
// Only clear what we put there.
final now = await Clipboard.getData(Clipboard.kTextPlain);
if (now?.text == text) {
await Clipboard.setData(const ClipboardData(text: ''));
}
});
_clipboardSecret = text;
_clipboardTimer = Timer(Duration(milliseconds: ms), clearClipboard);
}
}
if (e['toast'] is String) toasts.add(e['toast'] as String);
@@ -176,13 +185,32 @@ class ModulesController extends ChangeNotifier {
return (toasts: toasts, openView: openView);
}
/// Clears the clipboard if it still holds the secret a module copied (or if it cannot be
/// read, as on Android in the background: then clearing is the safe choice).
Future<void> clearClipboard() async {
final secret = _clipboardSecret;
_clipboardTimer?.cancel();
_clipboardSecret = null;
if (secret == null) return;
String? now;
var readable = true;
try {
now = (await Clipboard.getData(Clipboard.kTextPlain))?.text;
} catch (_) {
readable = false;
}
if (!readable || now == null || now == secret) {
await Clipboard.setData(const ClipboardData(text: ''));
}
}
@override
void dispose() {
_disposed = true;
for (final t in _timers.values) {
t.cancel();
}
_clipboardTimer?.cancel();
clearClipboard().ignore();
super.dispose();
}
}
+10
View File
@@ -87,8 +87,18 @@ class _ModuleViewState extends State<ModuleView> {
});
}
/// The `key` of the last tree: a different one is another screen.
Object? _screen;
/// Applies field values from the new tree and drops controllers of fields that are gone.
/// On a new screen every field starts from the tree, so text typed on one screen (a
/// password) never carries over to a field with the same id on another.
void _syncFields(Map<String, dynamic> tree) {
if (tree['key'] != _screen) {
_screen = tree['key'];
_sent.clear();
_hidden.clear();
}
final seen = <String>{};
void walk(Object? w) {
if (w is List) {
+46 -6
View File
@@ -98,8 +98,14 @@ class ModulesSettings extends StatelessWidget {
}
}
/// Reads the module, asks for consent to its permissions and installs it.
Future<void> installModuleBytes(BuildContext context, Uint8List bytes) async {
/// Reads the module, asks for consent to its permissions and installs it. Ids starting with
/// `synapse.` are reserved for the modules bundled with the app ([bundled]), so a file cannot
/// pose as one of them and inherit its data.
Future<void> installModuleBytes(
BuildContext context,
Uint8List bytes, {
bool bundled = false,
}) async {
final modules = AppScope.read(context).modules;
final messenger = ScaffoldMessenger.of(context);
final ModuleDto m;
@@ -111,11 +117,27 @@ Future<void> installModuleBytes(BuildContext context, Uint8List bytes) async {
);
return;
}
if (!context.mounted) return;
if (!bundled && m.id.startsWith('synapse.')) {
messenger.showSnackBar(
SnackBar(
content: Text(
'${m.id}: ids starting with "synapse." are reserved for modules included '
'with Synapse.',
),
),
);
return;
}
final previous = modules.byId(m.id);
final existingData = previous == null && await modules.hasData(m.id);
if (!context.mounted) return;
final ok = await showDialog<bool>(
context: context,
builder: (ctx) => _ConsentDialog(module: m, previous: previous),
builder: (ctx) => _ConsentDialog(
module: m,
previous: previous,
existingData: existingData,
),
);
if (ok != true) return;
try {
@@ -136,10 +158,17 @@ String _message(Object e) {
}
class _ConsentDialog extends StatelessWidget {
const _ConsentDialog({required this.module, this.previous});
const _ConsentDialog({
required this.module,
this.previous,
this.existingData = false,
});
final ModuleDto module;
final ModuleDto? previous;
/// The graph already holds data under this module id, which it will be able to read.
final bool existingData;
@override
Widget build(BuildContext context) {
final colors = context.colors;
@@ -164,6 +193,15 @@ class _ConsentDialog extends StatelessWidget {
const SizedBox(height: 8),
Text(m.description),
],
if (existingData) ...[
const SizedBox(height: 12),
Text(
'This graph already holds data of a module with the id ${m.id} '
'(from another device or an earlier install). This module will be '
'able to read it. Install it only if you trust where it comes from.',
style: TextStyle(color: Theme.of(context).colorScheme.error),
),
],
const SizedBox(height: 16),
Text(
m.permissions.isEmpty
@@ -402,7 +440,9 @@ class _BundledModule extends StatelessWidget {
key: ValueKey('module-install-$id'),
onPressed: () async {
final bytes = await loadBundledModule(asset);
if (context.mounted) await installModuleBytes(context, bytes);
if (context.mounted) {
await installModuleBytes(context, bytes, bundled: true);
}
},
child: const Text('Install'),
),
+100 -1
View File
@@ -1,4 +1,3 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
@@ -337,4 +336,104 @@ void main() {
expect(b.moduleRequests.last.$2['op'], 'suspend');
expect(app.modules.macroVersion, greaterThan(before));
});
testWidgets('a file cannot pose as a bundled module', (tester) async {
final b = MemoryBackend()..inspectResult = vault();
pickModuleFile = () async => Uint8List(4);
await pump(tester, b, route: const SettingsRoute(), height: 3000);
await tester.tap(find.byKey(const ValueKey('module-install-file')));
await settle(tester);
expect(
find.textContaining('reserved for modules included'),
findsOneWidget,
);
expect(find.text('Install Secrets 0.1.0?'), findsNothing);
expect(b.installedModules, isEmpty);
});
testWidgets('installing over existing data warns in the consent dialog', (
tester,
) async {
final m = vault();
final b = MemoryBackend()
..inspectResult = ModuleDto(
id: 'acme.notes',
name: 'Notes',
version: '1',
description: '',
author: 'Acme',
permissions: const ['storage'],
views: const [],
macros: const [],
enabled: true,
size: m.size,
)
..moduleData.add('acme.notes');
pickModuleFile = () async => Uint8List(4);
await pump(tester, b, route: const SettingsRoute(), height: 3000);
await tester.tap(find.byKey(const ValueKey('module-install-file')));
await settle(tester);
expect(find.text('Install Notes 1?'), findsOneWidget);
expect(find.textContaining('already holds data'), findsOneWidget);
});
testWidgets('suspending clears a copied secret from the clipboard', (
tester,
) async {
final writes = mockClipboard(tester);
final b = MemoryBackend();
b.installedModules['synapse.secrets'] = vault();
final app = await pump(tester, b);
await app.modules.applyEffects({
'effects': [
{
'copy': {'text': 'hunter2', 'clear_after_ms': 30000},
},
],
});
await app.modules.suspend();
expect(writes, ['hunter2', '']);
// The pending timer was cancelled: nothing more happens later.
await tester.pump(const Duration(seconds: 31));
expect(writes, ['hunter2', '']);
});
testWidgets('fields start empty on a new screen even with a reused id', (
tester,
) async {
final b = MemoryBackend();
b.installedModules['synapse.secrets'] = vault();
var screen = 'edit';
b.moduleHandlers['synapse.secrets'] = (req) {
if (req['op'] == 'event') screen = 'locked';
return {
'ui': {
'type': 'column',
'key': screen,
'children': [
{
'type': 'field',
'id': 'password',
'label': screen == 'edit' ? 'Password' : 'Master password',
'value': '',
'secret': true,
},
{'type': 'button', 'label': 'Go', 'action': 'go'},
],
},
};
};
await pump(
tester,
b,
route: const ModuleRoute('synapse.secrets', 'vault', title: 'Secrets'),
);
await tester.enterText(find.widgetWithText(TextField, 'Password'), 'typed');
await tester.tap(find.text('Go'));
await settle(tester);
final field = tester.widget<TextField>(
find.widgetWithText(TextField, 'Master password'),
);
expect(field.controller!.text, '');
});
}
+5
View File
@@ -73,6 +73,11 @@ class MemoryBackend implements GraphBackend {
@override
Future<void> stopModule(String id) async => stoppedModules.add(id);
/// Module ids with data in the graph.
final moduleData = <String>{};
@override
Future<bool> moduleHasData(String id) async => moduleData.contains(id);
static ModuleDto withEnabled(ModuleDto m, bool enabled) => ModuleDto(
id: m.id,
name: m.name,