Skip to content

Commit be269eb

Browse files
committed
fix(mobile): use only the verified envelope for a repeated inventory ID
The verification cache was keyed by event ID but then read the current envelope, so a relay could resend a verified ID with a different d tag and skip verification. A repeated ID now uses the envelope verified for it, and a regression covers both orders of the forged copy. Signed-off-by: Tom Brow <[email protected]>
1 parent 2073e26 commit be269eb

2 files changed

Lines changed: 43 additions & 8 deletions

File tree

‎mobile/lib/shared/me/me_assistants.dart‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ Future<List<String>> _managedAgentPubkeys(
105105
String? agent,
106106
}) async {
107107
final heads = <String, NostrEvent>{};
108-
final verified = <String>{};
108+
final verified = <String, NostrEvent>{};
109109
NostrEvent? cursor;
110110
for (var page = 0; ; page++) {
111111
if (page == _maxInventoryPages) {
@@ -135,21 +135,23 @@ Future<List<String>> _managedAgentPubkeys(
135135
throw StateError('Agent inventory response is outside its owner.');
136136
}
137137
for (final event in events) {
138-
final d = event.getTagValue('d');
139-
if (d == null || (agent != null && d != agent)) continue;
140138
// Like desktop, skip an envelope that fails verification so a forged
141-
// record can neither add an agent nor supersede a genuine one. An ID
142-
// already verified needs no second check when pages overlap.
143-
if (!verified.contains(event.id)) {
139+
// record can neither add an agent nor supersede a genuine one. When
140+
// pages overlap, a repeated ID uses only the envelope already verified
141+
// for it, never the bytes the relay sent again.
142+
var record = verified[event.id];
143+
if (record == null) {
144144
try {
145145
nostr.Event.fromJson(jsonEncode(event.toJson()));
146146
} catch (_) {
147147
continue;
148148
}
149-
verified.add(event.id);
149+
record = verified[event.id] = event;
150150
}
151+
final d = record.getTagValue('d');
152+
if (d == null || (agent != null && d != agent)) continue;
151153
final previous = heads[d];
152-
if (previous == null || _newer(event, previous)) heads[d] = event;
154+
if (previous == null || _newer(record, previous)) heads[d] = record;
153155
}
154156
if (events.length < _pageSize) break;
155157
// Relay order is newest first, then ID ascending at ties.

‎mobile/test/shared/me/me_assistants_test.dart‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,39 @@ void main() {
452452
isFalse,
453453
);
454454
});
455+
for (final forgedFirst in [false, true]) {
456+
test(
457+
'a repeated ID cannot inject an agent (forged first: $forgedFirst)',
458+
() async {
459+
final genuine = managed(agent.public);
460+
final forged = NostrEvent.fromJson(
461+
genuine.toJson()
462+
..['tags'] = [
463+
['d', other.public],
464+
],
465+
);
466+
final relay = _Relay(
467+
inventory: [
468+
forgedFirst ? [forged, genuine] : [genuine, forged],
469+
],
470+
profiles: [
471+
profile(agent, ownedBy: owner),
472+
profile(other, ownedBy: owner),
473+
],
474+
);
475+
final container = setup(relay);
476+
expect(
477+
(await container.read(
478+
meAssistantsProvider.future,
479+
)).map((a) => a.pubkey),
480+
[agent.public],
481+
);
482+
final sync = container.read(meSyncProvider);
483+
expect(await isOwnedMeAssistant(sync, other.public), isFalse);
484+
expect(await isOwnedMeAssistant(sync, agent.public), isTrue);
485+
},
486+
);
487+
}
455488
test(
456489
'creation rejects revoked ownership and unrelated returned profiles',
457490
() async {

0 commit comments

Comments
 (0)