1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use anyhow::{Context, Result};
use bytes::Bytes;
use iroh::Endpoint;
use iroh_blobs::provider::events::{
AbortReason, ConnectMode, EventMask, EventSender, ObserveMode, ProviderMessage, RequestMode,
ThrottleMode,
};
use iroh_blobs::store::fs::FsStore;
use iroh_blobs::ticket::BlobTicket;
use iroh_blobs::{BlobFormat, BlobsProtocol, Hash};
use mcpmesh_net::TrustGate;
use crate::audit::{AuditRecord, AuditSink, now_ts};
use crate::blobs::APP_BLOB_ALPN;
use crate::blobs::scope::ScopeStore;
use crate::daemon::RELAY_READY_TIMEOUT;
/// The request-time scope-gate `EventMask` for the serving app-blob provider.
///
/// SECURITY — deny-by-default on every non-GET request type, made EXPLICIT (not left to a vestigial
/// routing quirk). In the pinned iroh-blobs 0.103.0 the generic `EventSender::request()` reads ONLY
/// `mask.get` for EVERY request type (get/get_many/push/observe), so `get: Intercept` currently
/// routes all four to the drain loop, which denies the non-GET kinds explicitly.
/// To keep the deny-by-default INDEPENDENT of that single-field
/// routing, each non-GET request type is ALSO pinned to its most-refusing mask mode, so a FUTURE
/// iroh-blobs that honors the per-type fields still refuses them WITHOUT serving bytes:
/// - `get_many` / `push` = `RequestMode::Disabled`: the crate refuses this request type at the
/// protocol level with `Permission` and fires NO event — registry
/// `iroh-blobs-0.103.0/src/provider/events.rs:504-506` (`RequestMode::Disabled => return
/// Err(e!(ProgressError::Permission))`), doc at `events.rs:62-66`. Our legitimate clients only
/// ever do a single-blob `get`, so disabling these breaks nothing. (`push` is `Disabled` in
/// `EventMask::DEFAULT` already; pinning it makes the intent explicit.)
/// - `observe` = `ObserveMode::Intercept`: `ObserveMode` has NO `Disabled` variant
/// (`events.rs:34-44` — only `None`/`Notify`/`Intercept`), so the strongest available refusal is
/// `Intercept`, which fires an `ObserveRequestReceived` the drain loop denies with `Permission`.
/// `ObserveMode::None` (the default) would mean "no event, request served normally" → a silent
/// bypass, so it is explicitly the WRONG choice here.
///
/// `connected: Intercept` records the authenticated endpoint id; `get: Intercept` scope-checks every
/// single-blob GET (the AC fetch path — unchanged). `throttle` stays at its default
/// (`ThrottleMode::None`) — it is a transfer-throttling knob, not a request-serving gate.
const APP_BLOB_EVENT_MASK: EventMask = EventMask {
connected: ConnectMode::Intercept,
get: RequestMode::Intercept,
get_many: RequestMode::Disabled,
push: RequestMode::Disabled,
observe: ObserveMode::Intercept,
throttle: ThrottleMode::None,
};
/// The gated app-blob provider. `events` is `Some` for a serving daemon (the request-time
/// scope Intercept gate is armed) and `None` for a caller-only fetcher. `scopes` is the persisted
/// scope table; a fetcher gets an empty one it never mutates.
///
/// The drain loop's `Receiver<ProviderMessage>` is moved into a task
/// spawned once in `load`. The loop lives as long as ANY `EventSender` clone lives; `AppBlobs` holds
/// one in `self.events` for the provider's lifetime (the daemon holds `AppBlobs` for its lifetime),
/// and every `protocol()` clones another into the `BlobsProtocol`. So the gate loop runs until the
/// daemon drops the provider — never terminating mid-serve.
pub struct AppBlobs {
store: FsStore,
endpoint: Endpoint,
events: Option<EventSender>,
scopes: Arc<ScopeStore>,
/// The request-time gate loop's handle, so shutdown can END it deterministically (#61).
///
/// That task owns an `Arc<dyn TrustGate>`, which on a pairing daemon holds the `PeerStore` and
/// therefore the redb data-dir lock. It used to be a fire-and-forget `tokio::spawn` whose handle
/// was discarded: the loop exits when the last `EventSender` drops, but only once the task is
/// next polled, so nothing guaranteed the lock was released by the time `shutdown` returned.
/// Unreachable while the provider was roster-only — an embedded `NodeBuilder` node never built
/// one — and it broke `shutdown_frees_the_root_*` the moment app blobs reached pairing mode.
gate_loop: tokio::sync::Mutex<Option<tokio::task::JoinHandle<()>>>,
/// Wait (bounded) for the relay handshake before minting a ticket (#83 ask 3).
///
/// OFF by default, switched ON by boot alone. The wait exists so a ticket carries the
/// home-relay URL a fetcher needs across NAT; on a relay-disabled endpoint `online()` never
/// completes, so it is a guaranteed [`RELAY_READY_TIMEOUT`] of dead time per mint. Defaulting
/// off keeps that cost out of every test fixture (relay-disabled by construction) while
/// production — the only place the relay URL matters — opts in explicitly.
relay_wait: std::sync::atomic::AtomicBool,
/// Serializes the HASH-MEMBERSHIP mutations (#104).
///
/// `ScopeStore` makes each individual mutation atomic, but `republish` is a read-check-write:
/// it verifies the blob is complete (an `.await` on the store) and only then inserts. A
/// concurrent `blob_unpublish` landing in that gap is silently undone — both verbs return
/// success and the operator's revocation disappears. An async lock is required because the
/// completeness check awaits, so `ScopeStore`'s `std::sync::Mutex` cannot be held across it.
///
/// Held by every verb that adds or removes a hash from a scope; grant/revoke of PRINCIPALS do
/// not contend, since they cannot race a membership decision.
hash_membership: tokio::sync::Mutex<()>,
/// TEST-ONLY: pause between `republish`'s completeness check and its scope insert, so the
/// interleaving #104 describes is deterministic rather than timing-dependent.
#[cfg(test)]
republish_delay: std::sync::Mutex<Option<std::time::Duration>>,
/// TEST-ONLY: pause between `publish_scope`'s import and its scope insert (#104).
#[cfg(test)]
publish_delay: std::sync::Mutex<Option<std::time::Duration>>,
}
impl AppBlobs {
/// End the request-time gate loop, releasing the `TrustGate` (and with it the redb handle).
/// Idempotent; a fetcher has no loop and is a no-op.
///
/// The `await` after `abort` is deliberate but NOT load-bearing for the current test: dropping
/// the provider already closes the event channel, and abort-without-await passes today. It is
/// here so the release is deterministic rather than dependent on when the runtime reaps the
/// task — the racy version is the kind that fails under load, not in CI.
pub async fn shutdown(&self) {
let handle = self.gate_loop.lock().await.take();
if let Some(h) = handle {
h.abort();
let _ = h.await;
}
}
}
impl AppBlobs {
/// A caller-only fetcher: an `FsStore` + endpoint, NO scope gate (`events: None`), an empty
/// scopes table it never persists. Used caller-side (the fetch path) and by the ungated tests.
pub async fn open_fetcher(blobs_dir: PathBuf, endpoint: Endpoint) -> Result<Arc<Self>> {
tokio::fs::create_dir_all(&blobs_dir)
.await
.with_context(|| format!("create blobs dir {}", blobs_dir.display()))?;
let store = FsStore::load(&blobs_dir)
.await
.with_context(|| format!("load blob store {}", blobs_dir.display()))?;
Ok(Arc::new(Self {
store,
endpoint,
events: None,
relay_wait: std::sync::atomic::AtomicBool::new(false),
hash_membership: tokio::sync::Mutex::new(()),
#[cfg(test)]
republish_delay: std::sync::Mutex::new(None),
#[cfg(test)]
publish_delay: std::sync::Mutex::new(None),
scopes: Arc::new(ScopeStore::new(blobs_dir.join("scopes.json"))),
gate_loop: tokio::sync::Mutex::new(None),
}))
}
/// The GATED provider: an `FsStore` + the request-time scope Intercept `EventSender`.
/// Spawns the drain loop ONCE, wired to the trust `gate` (resolve endpoint → identity) and
/// `scopes` (the authz table). `FsStore::load` is async/fallible;
/// the dir is created first.
pub async fn load(
blobs_dir: PathBuf,
scopes: Arc<ScopeStore>,
gate: Arc<dyn TrustGate>,
endpoint: Endpoint,
audit: AuditSink,
) -> Result<Arc<Self>> {
tokio::fs::create_dir_all(&blobs_dir)
.await
.with_context(|| format!("create blobs dir {}", blobs_dir.display()))?;
let store = FsStore::load(&blobs_dir)
.await
.with_context(|| format!("load blob store {}", blobs_dir.display()))?;
// The request-time scope gate: `APP_BLOB_EVENT_MASK` intercepts connect + single-blob GET,
// and pins every non-GET request type to deny-by-default (Disabled/Intercept — see the
// const's SECURITY note). Since `get: Intercept` also routes
// get_many/observe/push to the drain loop today; the pinned fields keep them refused even if
// a future iroh-blobs honors the per-type fields directly.
let (events, rx) = EventSender::channel(64, APP_BLOB_EVENT_MASK);
let gate_loop = spawn_gate_loop(rx, gate, scopes.clone(), audit);
Ok(Arc::new(Self {
store,
endpoint,
events: Some(events),
scopes,
gate_loop: tokio::sync::Mutex::new(Some(gate_loop)),
relay_wait: std::sync::atomic::AtomicBool::new(false),
hash_membership: tokio::sync::Mutex::new(()),
#[cfg(test)]
republish_delay: std::sync::Mutex::new(None),
#[cfg(test)]
publish_delay: std::sync::Mutex::new(None),
}))
}
/// The `BlobsProtocol` handler the accept loop dispatches `APP_BLOB_ALPN` to. Carries the scope
/// gate when `events` is `Some` (a serving daemon); ungated for a fetcher. `&self.store`
/// (a `&FsStore`) deref-coerces to `&Store`; `self.events.clone()` shares the ONE drain loop.
pub fn protocol(&self) -> BlobsProtocol {
BlobsProtocol::new(&self.store, self.events.clone())
}
/// TEST-ONLY: register an app-blob ALPN accept handler directly on `endpoint`, BYPASSING the
/// accept-time trust gate (the request-time scope gate still runs via `protocol()`'s events).
/// Production accept ALWAYS goes through the gated daemon loop (`spawn_accept_loop`'s
/// `APP_BLOB_ALPN` arm: resolve → 401 + rate-limit + check-register); this exists only so
/// same-file unit tests can serve blobs without assembling a daemon. `#[cfg(test)]` so it can
/// never leak into a production accept path.
#[cfg(test)]
pub(crate) fn spawn_accept(&self, endpoint: &Endpoint) {
let proto = self.protocol();
let ep = endpoint.clone();
tokio::spawn(async move {
while let Some(incoming) = ep.accept().await {
if let Ok(conn) = incoming.await
&& conn.alpn() == APP_BLOB_ALPN
{
let _ = iroh::protocol::ProtocolHandler::accept(&proto, conn).await;
}
}
});
}
/// Add a LOCAL file to the store (the large-blob idiom — `add_path`) and return
/// `(ticket_string, blake3_hex)` WITHOUT touching any scope (used for the ungated round-trip).
pub async fn publish_path(&self, path: &Path) -> Result<(String, String)> {
let tag = self
.store
.blobs()
.add_path(path)
.await
.with_context(|| format!("add blob from {}", path.display()))?;
let ticket = self.ticket_for(tag.hash).await;
Ok((ticket.to_string(), tag.hash.to_hex().to_string()))
}
/// Publish a LOCAL file INTO a scope: add it to the store AND record its hash in the
/// named scope (single-writer via `ScopeStore`). Returns `(ticket_string, blake3_hex)`.
pub async fn publish_scope(&self, scope: &str, path: &Path) -> Result<(String, String)> {
let (ticket, hash_hex) = self.publish_path(path).await?;
// #104: membership mutations are serialized as a family, so an import that finishes while
// an unpublish is in flight cannot interleave with it either.
let _membership = self.hash_membership.lock().await;
#[cfg(test)]
{
let d = *self
.publish_delay
.lock()
.expect("publish delay lock not poisoned");
if let Some(d) = d {
tokio::time::sleep(d).await;
}
}
self.scopes.publish_hash(scope, &hash_hex)?;
Ok((ticket, hash_hex))
}
/// Add a hash ALREADY COMPLETE in the local store to a scope (#83) — the "every recipient is a
/// source" primitive. Returns a ticket addressed to THIS node.
///
/// No filesystem round-trip: `blob_publish { scope, path }` was the only way back in, and it
/// re-imported bytes the store already held, producing a third copy with nothing to reclaim it
/// (#80).
///
/// **Completeness is checked first, and it is load-bearing.** Recording a hash in a scope
/// ADVERTISES it: the gate authorizes GETs for it and the returned ticket names us as the
/// source. `Blobs::has` is true only for `BlobStatus::Complete`, so an interrupted fetch's
/// partial bytes are refused exactly like absent ones — advertising what we cannot serve would
/// convert the publisher going offline into a hang at every fetcher.
///
/// Idempotent (the scope's hash set is a set).
///
/// **Do NOT call this unconditionally after every fetch.** Republishing into a scope
/// re-exposes the hash to every principal that scope ALREADY grants — including a hash an
/// operator deliberately withdrew with `blob_unpublish`, which removes reachability but not
/// the bytes, so `has()` stays true forever and a later republish silently restores access with
/// no grant call and no warning. Republish when the user asks to share, not as fetch hygiene.
///
/// **Grants nobody.** The republisher chooses a scope they already control; inheriting the
/// original publisher's grant list would be a silent authorization transfer. Sharing is
/// `blob_grant`'s job.
pub async fn republish(&self, scope: &str, hash_hex: &str) -> Result<(String, String)> {
// #104: hold the membership lock across the completeness CHECK and the scope INSERT. They
// are a read-check-write with an `.await` between them, so a concurrent `blob_unpublish`
// landing in the gap was silently undone — both verbs returned success and the operator's
// revocation vanished.
//
// What this does NOT do: make a revocation unloseable. The mutex gives mutual exclusion in
// LOCK-ACQUISITION order, not request-arrival order, so an unpublish that acquires FIRST
// still has its effect erased by a republish acquiring second — both returning success.
// That residue is the same semantic hazard the doc comment above describes (republish
// re-adds to a scope whose grants unpublish never touched); the lock removes the
// atomicity bug, where a decision made BEFORE the unpublish landed AFTER it. Eliminating
// the class needs state (a per-(scope, hash) revocation generation re-validated before the
// insert), not exclusion — tracked separately.
let _membership = self.hash_membership.lock().await;
// Scope first: a typo'd scope must not report as a missing blob.
if !self.scopes.has_scope(scope) {
anyhow::bail!(crate::daemon::NoSuchBlobScope(scope.to_string()));
}
// Parse (panic-safe) AND NORMALIZE before touching the scope. The gate compares against
// the canonical lowercase hex (`msg.request.hash.to_hex()`), so inserting the caller's raw
// string would record an entry that authorizes nothing: `blob_list` would show the file as
// shared, every fetcher would be denied, and `blob_unpublish` — which normalizes — could
// never remove it. That is #62's silent-no-op defect re-entered from the other side.
// `blob_publish` is safe only because it stores `tag.hash.to_hex()`.
let hash = crate::blobs::parse_blob_hash(hash_hex)?;
let canonical = hash.to_hex().to_string();
if !self.store.blobs().has(hash).await.unwrap_or(false) {
anyhow::bail!(crate::daemon::NoSuchBlob(canonical));
}
// #107: a deliberate withdrawal outranks "we still hold the bytes". Checked INSIDE the
// membership lock, so an unpublish that lands first cannot be overtaken — which is the
// half a lock alone could never fix, since exclusion is in acquisition order, not
// request-arrival order.
if self.scopes.is_withdrawn(scope, &canonical) {
anyhow::bail!(crate::daemon::BlobWithdrawn {
scope: scope.to_string(),
hash: canonical,
});
}
#[cfg(test)]
{
let d = *self
.republish_delay
.lock()
.expect("republish delay lock not poisoned");
if let Some(d) = d {
tokio::time::sleep(d).await;
}
}
self.scopes.publish_hash(scope, &canonical)?;
// Release BEFORE minting: `ticket_for` waits up to RELAY_READY_TIMEOUT (3s) for the relay
// handshake, and production turns that wait on. Holding the membership lock across it
// would block every concurrent `blob_unpublish` for the full 3s on a node whose handshake
// has not completed — making the REVOCATION path pay for the publisher's latency, which is
// backwards on a security surface. The insert above is the last thing the lock must cover.
drop(_membership);
Ok((self.ticket_for(hash).await.to_string(), canonical))
}
/// Mint a ticket for a hash this node holds, addressed to this node.
///
/// Waits (bounded by [`RELAY_READY_TIMEOUT`]) for the endpoint to come online first, so the
/// address carries the home-relay URL a fetcher needs across NAT (#83 ask 3). `mint_invite` has
/// done this since #4; the blob path minted immediately, so a file published shortly after boot
/// or after a network change could yield a direct-addresses-only ticket: LAN-dialable and
/// NAT-dead. A CAP, not a fixed wait — production returns the instant the relay handshake
/// completes, and the relay-disabled test preset simply falls through to direct addresses.
async fn ticket_for(&self, hash: Hash) -> BlobTicket {
if self.relay_wait.load(std::sync::atomic::Ordering::Relaxed) {
let _ = tokio::time::timeout(RELAY_READY_TIMEOUT, self.endpoint.online()).await;
}
BlobTicket::new(self.endpoint.addr(), hash, BlobFormat::Raw)
}
/// Turn the relay-ready wait ON. Boot calls this; nothing else should.
/// Is the relay-ready wait on? Test-only — production sets it and never asks (#105).
#[cfg(test)]
pub(crate) fn relay_wait_enabled(&self) -> bool {
self.relay_wait.load(std::sync::atomic::Ordering::Relaxed)
}
/// Turn the relay-ready wait ON. Boot calls this; nothing else should.
pub(crate) fn enable_relay_wait(&self) {
self.relay_wait
.store(true, std::sync::atomic::Ordering::Relaxed);
}
/// Grant a scope to a STABLE principal — a group name, a user_id, or an `eid:` device
/// principal (never a display nickname, #38) — persisted single-writer.
pub fn grant(&self, scope: &str, principal: &str) -> Result<()> {
self.scopes.grant(scope, principal)
}
/// Revoke `principals` from every scope (unpair hygiene, #38) — persisted single-writer.
/// Returns whether anything changed.
pub fn revoke_principals(&self, principals: &[String]) -> Result<bool> {
self.scopes.revoke_principals(principals)
}
/// Revoke `principals` from ONE scope (#62, `blob_revoke`) — the per-file un-share, the blob
/// analogue of #44. Distinct from [`revoke_principals`](Self::revoke_principals), which is
/// unpair hygiene across every scope.
pub fn revoke_from_scope(&self, scope: &str, principals: &[String]) -> Result<bool> {
self.scopes.revoke_from_scope(scope, principals)
}
/// Does this scope exist? The handlers use it to reject an unknown scope rather than acking it.
pub fn has_scope(&self, scope: &str) -> bool {
self.scopes.has_scope(scope)
}
/// Remove a hash from ONE scope (#62, `blob_unpublish`).
///
/// This is the AUTHORIZATION half and takes effect at once for NEW requests: the scope gate
/// requires the hash to be listed in some scope, so a subsequent GET is refused at the request
/// hook. The BYTES remain in the store — there is no reclaim (#80) — so do not describe this to
/// a user as deletion. A transfer already streaming is not interrupted.
pub async fn unpublish(&self, scope: &str, hash_hex: &str) -> Result<bool> {
// NORMALIZE FIRST (#107 review). Since #107 this call WRITES a persistent key into the
// withdrawn set, so a non-canonical rendering no longer merely fails to match — it records
// a junk entry that no `republish` will ever compare equal to, in a set nothing prunes.
// The control socket normalizes before calling, but `AppBlobs` is public API of a
// published crate, so a library consumer passing uppercase hex must not poison the
// sidecar. `republish` already normalizes one function away.
let canonical = crate::blobs::parse_blob_hash(hash_hex)?
.to_hex()
.to_string();
// #104: same lock as `republish`, so a revocation cannot land inside a republish's
// check-then-insert window and be overwritten by it.
let _membership = self.hash_membership.lock().await;
self.scopes.unpublish_hash(scope, &canonical)
}
/// TEST-ONLY: pause between the import and the scope insert (#104).
#[cfg(test)]
pub(crate) fn set_publish_delay(&self, d: std::time::Duration) {
*self
.publish_delay
.lock()
.expect("publish delay lock not poisoned") = Some(d);
}
/// TEST-ONLY: pause between the completeness check and the scope insert (#104).
#[cfg(test)]
pub(crate) fn set_republish_delay(&self, d: std::time::Duration) {
*self
.republish_delay
.lock()
.expect("republish delay lock not poisoned") = Some(d);
}
/// The current scope table (name, hashes, grants) for `list`.
pub fn list(&self) -> Vec<crate::blobs::scope::ScopeRow> {
self.scopes.list()
}
/// Fetch a ticket THROUGH this endpoint over `APP_BLOB_ALPN`, streaming BLAKE3-verified bytes
/// into `self.store` (the Downloader cannot dial a custom ALPN — see [`APP_BLOB_ALPN`]).
/// Returns the verified hash. A provider that refuses this
/// caller (accept-time 401 or request-time Permission) surfaces here as an `Err`.
pub async fn fetch(&self, ticket_str: &str) -> Result<Hash> {
let ticket: BlobTicket = ticket_str.parse().context("parse blob ticket")?;
let conn = self
.endpoint
.connect(ticket.addr().clone(), APP_BLOB_ALPN)
.await
.context("dial app-blob provider")?;
self.store
.remote()
.fetch(conn, ticket.hash())
.await
.context("fetch app blob")?;
Ok(ticket.hash())
}
/// Read a fully-present blob's bytes out of the store (callers/tests consume the fetched content).
pub async fn read_bytes(&self, hash: Hash) -> Result<Bytes> {
self.store
.get_bytes(hash)
.await
.context("read fetched app blob")
}
/// STREAM a blob from the store to `dest`, returning the bytes written (#82).
///
/// Peak memory is independent of blob size. The `read_bytes` + `fs::write` path this replaces
/// materialized the whole blob as one `Bytes` first — and `get_bytes`' own iroh doc warns it
/// *"will run out of memory when called for very large blobs"*. On a small headless node a
/// multi-GB fetch was an OOM kill rather than a slow transfer.
///
/// `ExportMode::Copy` (via `export`) writes an independent file, so the destination survives a
/// later store reclaim. `ExportMode::TryReference` would avoid the second copy but ties the
/// exported file's lifetime to the store — a separate decision, see #82's item 3.
pub async fn export_to(&self, hash: Hash, dest: &Path) -> Result<u64> {
self.store
.blobs()
.export(hash, dest)
.await
.with_context(|| format!("export app blob to {}", dest.display()))
}
}
/// The request-time scope Intercept drain loop (the security core). Single-consumer: this
/// task owns `rx`, so the `connection_id → endpoint_id` map is loop-local with NO lock
/// — FIFO delivery guarantees `ClientConnected(conn)` precedes any
/// `GetRequestReceived(conn)` on that connection. SECURITY-CRITICAL:
/// - `ClientConnected`: record the AUTHENTICATED `endpoint_id` (QUIC/TLS) → reply `Ok(())` to admit
/// (the accept-time gate already vetted the endpoint; the GET hook is the per-hash boundary). A
/// missing endpoint id (never on an authenticated conn) is denied defensively.
/// - `GetRequestReceived`: resolve the endpoint via the trust gate to its identity and ALLOW iff a
/// scope contains the hash AND grants one of the caller's principals — `groups ∪ {eid} ∪
/// {user_id}`, the shared `principal_set` (nicknames excluded, #38) — else `Permission`,
/// BEFORE any bytes (the Intercept path blocks the transfer on the provider's `rx.await??`).
/// - get_many/observe/push (all routed through `mask.get`): DENY
/// explicitly — deny-by-default, the store is not a general filesystem surface. Belt-and-suspenders
/// with `APP_BLOB_EVENT_MASK`, which ALSO pins these types (get_many/push = `Disabled`, observe =
/// `Intercept`): if a future iroh-blobs delivers them as events instead of refusing at the mask,
/// they are still denied here.
fn spawn_gate_loop(
mut rx: tokio::sync::mpsc::Receiver<ProviderMessage>,
gate: Arc<dyn TrustGate>,
scopes: Arc<ScopeStore>,
audit: AuditSink,
) -> tokio::task::JoinHandle<()> {
tokio::spawn(async move {
let mut conns: HashMap<u64, mcpmesh_net::EndpointId> = HashMap::new();
while let Some(msg) = rx.recv().await {
match msg {
ProviderMessage::ClientConnected(msg) => {
let res = match msg.endpoint_id {
Some(eid) => {
conns.insert(msg.connection_id, (*eid.as_bytes()).into());
Ok(())
}
None => Err(AbortReason::Permission),
};
msg.tx.send(res).await.ok();
}
ProviderMessage::GetRequestReceived(msg) => {
// Resolve the authenticated caller for BOTH the authz decision and the audit
// attribution (peer is the gate-resolved identity, not self-asserted).
let identity = conns
.get(&msg.connection_id)
.and_then(|eid| gate.resolve(eid));
let hash_hex = msg.request.hash.to_hex().to_string();
let allow = msg.request.ranges.is_blob()
&& identity.as_ref().is_some_and(|identity| {
// The grant namespace is THE flat principal set —
// groups ∪ {eid} ∪ {user_id} — via the ONE shared
// `principal_set` (same expansion as the mesh allow check and
// the plugin seam). Nicknames are deliberately EXCLUDED (#38):
// scope grants are written as stable principals at grant time,
// so a pairing-mode peer is granted (and fetches) by its
// `eid:` device principal; legacy nickname-audience grants
// stop matching BY DESIGN (the doctor lint + release notes
// cover the migration). Default-deny is untouched: an unlisted
// principal still gets `Permission` before any bytes.
let eid = identity.endpoint.principal();
let principals: HashSet<&str> = mcpmesh_local_api::principal_set(
Some(&eid),
identity.user_id.as_deref(),
&identity.groups,
)
.into_iter()
.collect();
scopes.snapshot().allows(&hash_hex, &principals)
});
// Audit the fetch: peer + hash + status (ok/denied). A COUNT/ref only —
// never blob content. Attributes to the resolved user_id/nickname, or "unknown".
let peer = identity
.as_ref()
.map(|i| i.user_id.clone().unwrap_or_else(|| i.name.clone()));
audit.record(AuditRecord::blob_fetch(
now_ts(),
peer,
hash_hex,
if allow { "ok".into() } else { "denied".into() },
));
let res = if allow {
Ok(())
} else {
Err(AbortReason::Permission)
};
msg.tx.send(res).await.ok();
}
// Deny-by-default for every non-GET request type.
ProviderMessage::GetManyRequestReceived(msg) => {
msg.tx.send(Err(AbortReason::Permission)).await.ok();
}
ProviderMessage::PushRequestReceived(msg) => {
msg.tx.send(Err(AbortReason::Permission)).await.ok();
}
ProviderMessage::ObserveRequestReceived(msg) => {
msg.tx.send(Err(AbortReason::Permission)).await.ok();
}
ProviderMessage::ConnectionClosed(msg) => {
conns.remove(&msg.connection_id);
}
_ => {}
}
}
})
}
#[cfg(test)]
mod tests {
use super::*;
use crate::blobs::APP_BLOB_ALPN;
use crate::blobs::scope::ScopeStore;
use mcpmesh_net::{EndpointId, PeerIdentity, StaticGate};
use std::sync::Arc;
/// #83: republishing a hash the store does NOT hold COMPLETE must fail, and must leave the
/// scope untouched.
///
/// Putting a hash in a scope ADVERTISES it — the gate will authorize GETs for it and the
/// returned ticket names us as the source. Advertising bytes we cannot serve converts the
/// original sender going offline into a hang at every fetcher, which is strictly worse than the
/// failure #83 reports. Partial bytes (an interrupted fetch leaves them) must fail the same way
/// as absent ones, which is why the predicate is `Blobs::has` (true only for
/// `BlobStatus::Complete`) rather than "do we know this hash".
#[tokio::test]
async fn republishing_a_blob_we_do_not_hold_fails_and_leaves_the_scope_untouched() {
let dir = tempfile::tempdir().unwrap();
let provider = AppBlobs::open_fetcher(dir.path().join("blobs"), ep().await)
.await
.unwrap();
provider.grant("room", "b64u:alice").unwrap();
// A well-formed hash the store has never seen.
let absent = blake3::hash(b"never fetched").to_hex().to_string();
let err = provider
.republish("room", &absent)
.await
.expect_err("republishing a blob we do not hold must fail");
assert!(
err.downcast_ref::<crate::daemon::NoSuchBlob>().is_some(),
"must be NoSuchBlob so the client can tell it apart from a bad scope, got: {err}"
);
let hashes: Vec<String> = provider
.list()
.into_iter()
.flat_map(|(_, hashes, _, _)| hashes)
.collect();
assert!(
!hashes.contains(&absent),
"a FAILED republish must not half-advertise the hash, got {hashes:?}"
);
}
/// The check ORDER: an unknown scope reports `NoSuchBlobScope`, even when the hash is also
/// absent. A typo'd scope must not be reported as a missing blob — the client's remedy differs.
#[tokio::test]
async fn an_unknown_scope_outranks_a_missing_blob() {
let dir = tempfile::tempdir().unwrap();
let provider = AppBlobs::open_fetcher(dir.path().join("blobs"), ep().await)
.await
.unwrap();
let absent = blake3::hash(b"nope").to_hex().to_string();
let err = provider
.republish("no-such-scope", &absent)
.await
.expect_err("unknown scope must fail");
assert!(
err.downcast_ref::<crate::daemon::NoSuchBlobScope>()
.is_some(),
"an unknown scope outranks a missing blob, got: {err}"
);
}
/// #83's exact scenario, end to end: a fetched blob becomes servable FROM THE FETCHER, and a
/// third peer gets it while the ORIGINAL PUBLISHER IS OFFLINE.
///
/// "Someone posts a file to a room of eight and closes their laptop." Before republish, the
/// only address anyone held pointed at the sleeping publisher, so the remaining peers failed
/// even though complete, byte-identical bytes sat on three machines.
///
/// B is a GATED provider (`AppBlobs::load`), which is what makes this test mean anything. An
/// ungated fetcher serves every hash it holds, so the scope insert republish performs is never
/// exercised and the test passes with republish recording nothing — verified by mutation.
#[tokio::test]
async fn a_fetched_blob_is_servable_from_the_fetcher_after_the_publisher_goes_away() {
tokio::time::timeout(std::time::Duration::from_secs(60), async {
let c_ep = ep().await;
let c_eid = EndpointId::from_bytes(*c_ep.id().as_bytes());
let mut entries = HashMap::new();
entries.insert(
c_eid,
PeerIdentity {
endpoint: c_eid,
name: "carol".into(),
user_id: Some("carol".into()),
groups: vec![],
},
);
let b_gate: Arc<dyn mcpmesh_net::TrustGate> = Arc::new(StaticGate::new(entries));
// A publishes (ungated — A's gate is not what is under test).
let adir = tempfile::tempdir().unwrap();
let a_ep = ep().await;
let a = AppBlobs::open_fetcher(adir.path().join("blobs"), a_ep.clone())
.await
.unwrap();
a.spawn_accept(&a_ep);
let src = adir.path().join("shared.bin");
std::fs::write(&src, b"the file everyone wants").unwrap();
let (a_ticket, hash_hex) = a.publish_path(&src).await.unwrap();
// B fetches it, and is GATED when it serves.
let bdir = tempfile::tempdir().unwrap();
let b_ep = ep().await;
let b = AppBlobs::load(
bdir.path().join("blobs"),
Arc::new(ScopeStore::new(bdir.path().join("scopes.json"))),
b_gate,
b_ep.clone(),
crate::audit::AuditSink::disabled(),
)
.await
.unwrap();
b.spawn_accept(&b_ep);
b.fetch(&a_ticket).await.unwrap();
// B republishes into a scope IT controls and grants C.
b.grant("b-room", "carol").unwrap();
let (b_ticket, _canon) = b.republish("b-room", &hash_hex).await.unwrap();
assert_ne!(b_ticket, a_ticket, "the ticket must name B, not A");
// A goes away — the laptop closes.
a_ep.close().await;
// C fetches from B regardless.
let cdir = tempfile::tempdir().unwrap();
let c = AppBlobs::open_fetcher(cdir.path().join("blobs"), c_ep)
.await
.unwrap();
let got = c
.fetch(&b_ticket)
.await
.expect("C must fetch from B with A offline — the whole point of #83");
assert_eq!(
&c.read_bytes(got).await.unwrap()[..],
b"the file everyone wants"
);
})
.await
.expect("republish round-trip timed out");
}
/// Republish must NOT inherit the original publisher's grants. A principal A shared with, but
/// B did not, is refused by B — otherwise republishing would silently widen access to everyone
/// the previous holder had shared with, which no one asked for and no one would see.
#[tokio::test]
async fn republish_does_not_inherit_the_publishers_grants() {
tokio::time::timeout(std::time::Duration::from_secs(90), async {
let m_ep = ep().await;
let m_eid = EndpointId::from_bytes(*m_ep.id().as_bytes());
let mut entries = HashMap::new();
entries.insert(
m_eid,
PeerIdentity {
endpoint: m_eid,
name: "mallory".into(),
user_id: Some("mallory".into()),
groups: vec![],
},
);
let b_gate: Arc<dyn mcpmesh_net::TrustGate> = Arc::new(StaticGate::new(entries));
// A publishes and grants mallory.
let adir = tempfile::tempdir().unwrap();
let a_ep = ep().await;
let a = AppBlobs::open_fetcher(adir.path().join("blobs"), a_ep.clone())
.await
.unwrap();
a.spawn_accept(&a_ep);
let src = adir.path().join("f.bin");
std::fs::write(&src, b"a's file").unwrap();
let (a_ticket, hash_hex) = a.publish_path(&src).await.unwrap();
a.grant("a-room", "mallory").unwrap();
// B fetches and republishes into ITS scope, granting nobody.
let bdir = tempfile::tempdir().unwrap();
let b_ep = ep().await;
let b = AppBlobs::load(
bdir.path().join("blobs"),
Arc::new(ScopeStore::new(bdir.path().join("scopes.json"))),
b_gate,
b_ep.clone(),
crate::audit::AuditSink::disabled(),
)
.await
.unwrap();
b.spawn_accept(&b_ep);
b.fetch(&a_ticket).await.unwrap();
b.grant("b-room", "someone-else").unwrap();
let (b_ticket, _canon) = b.republish("b-room", &hash_hex).await.unwrap();
// mallory — granted by A, never by B — is refused by B.
let mdir = tempfile::tempdir().unwrap();
let mallory = AppBlobs::open_fetcher(mdir.path().join("blobs"), m_ep)
.await
.unwrap();
// A DENIED fetch does not fail fast (the gate refuses at accept and the fetcher
// retries), so bound it: both "errored" and "never completed" are denials — only
// SUCCESS is a failure of this property.
let res =
tokio::time::timeout(std::time::Duration::from_secs(10), mallory.fetch(&b_ticket))
.await;
assert!(
!matches!(res, Ok(Ok(_))),
"republishing must not transfer A's grants to B's copy — that would silently widen \
access to everyone the previous holder shared with (got {res:?})"
);
})
.await
.expect("grant-isolation test timed out");
}
/// #83 review: a NON-CANONICAL rendering of a hash must not create an entry that authorizes
/// nothing. The gate compares against canonical lowercase hex, so recording the caller's raw
/// string (a valid 52-char base32 form, or uppercase hex) would put a row in `blob_list` that
/// looks shared, denies every fetcher, and cannot be removed — `blob_unpublish` normalizes and
/// would find nothing to delete, acking a no-op. That is #62's defect from the other side.
#[tokio::test]
async fn a_non_canonical_hash_is_normalized_before_it_is_recorded() {
let dir = tempfile::tempdir().unwrap();
let provider = AppBlobs::open_fetcher(dir.path().join("blobs"), ep().await)
.await
.unwrap();
provider.grant("room", "b64u:alice").unwrap();
let src = dir.path().join("f.bin");
std::fs::write(&src, b"canonical me").unwrap();
let (_t, canonical) = provider.publish_path(&src).await.unwrap();
// The SAME hash in its base32 rendering — what `Hash`'s Display produces, and a form a
// client can legitimately hold. (Uppercase HEX is not an alternative spelling: iroh's
// parser rejects it outright, which the review's own probe confirmed.)
let parsed = crate::blobs::parse_blob_hash(&canonical).unwrap();
let base32 = data_encoding::BASE32_NOPAD
.encode(parsed.as_bytes())
.to_ascii_lowercase();
assert_ne!(base32, canonical, "the fixture must actually differ");
let (_ticket, returned) = provider
.republish("room", &base32)
.await
.expect("an alternative rendering of a held hash must republish");
assert_eq!(
returned, canonical,
"the RESULT must carry canonical hex — blob_publish does, and the docs promise the two \
are interchangeable"
);
let recorded: Vec<String> = provider
.list()
.into_iter()
.filter(|(name, _, _, _)| name == "room")
.flat_map(|(_, hashes, _, _)| hashes)
.collect();
assert_eq!(
recorded,
vec![canonical],
"the SCOPE must record canonical hex — the gate compares against it, so a raw-string \
entry would authorize nobody and be unremovable"
);
}
/// #104: a `blob_unpublish` concurrent with a `blob_republish` must not be silently undone.
///
/// `republish` is a read-check-write — it verifies completeness (an `.await`) and only then
/// inserts. Without a lock spanning both, an unpublish landing in that gap removes the hash,
/// republish then re-inserts it, and BOTH verbs report success: the operator was told the file
/// was withdrawn while it is being served.
///
/// Driven deterministically via the test-only delay seam rather than hoping for the
/// interleaving. With the lock, unpublish blocks until republish finishes and therefore
/// serializes AFTER it — the revocation is the last word, which is the outcome an operator
/// expects. Without it, unpublish slips into the gap and is overwritten.
#[tokio::test]
async fn a_concurrent_unpublish_is_not_lost_to_a_republish() {
// 120s: these fixtures bind real endpoints, which costs ~20s on a loaded machine, and the
// guard exists to catch a HANG (a deadlock on the new membership lock), not slowness.
tokio::time::timeout(std::time::Duration::from_secs(120), async {
let dir = tempfile::tempdir().unwrap();
let provider = AppBlobs::open_fetcher(dir.path().join("blobs"), ep().await)
.await
.unwrap();
provider.grant("room", "b64u:alice").unwrap();
let src = dir.path().join("f.bin");
std::fs::write(&src, b"contested").unwrap();
// Already published into the scope, so the unpublish below has something to remove.
let (_t, hash_hex) = provider.publish_scope("room", &src).await.unwrap();
provider.set_republish_delay(std::time::Duration::from_millis(600));
let p2 = provider.clone();
let h2 = hash_hex.clone();
let republish =
tokio::spawn(async move { p2.republish("room", &h2).await.map(|_| ()) });
// Let republish get past its completeness check and into the gap.
tokio::time::sleep(std::time::Duration::from_millis(150)).await;
let removed = provider.unpublish("room", &hash_hex).await.unwrap();
republish.await.unwrap().unwrap();
assert!(removed, "the unpublish must actually have removed the hash");
let hashes: Vec<String> = provider
.list()
.into_iter()
.flat_map(|(_, hashes, _, _)| hashes)
.collect();
assert!(
!hashes.contains(&hash_hex),
"the revocation must survive — a republish that overwrites a concurrent unpublish \
tells the operator the file was withdrawn while it is still being served (scope \
now holds {hashes:?})"
);
})
.await
.expect("republish/unpublish race test timed out");
}
/// #104: `publish_scope` takes the same membership lock, and nothing tested it — removing that
/// lock alone passed the whole suite, so a refactor could drop it silently.
///
/// Same mechanism as the republish race: `add_path` is a slow async import, and the scope
/// insert that follows is unconditional. A `blob_unpublish` of a hash the import is about to
/// re-add loses its effect. Reachable whenever two clients hold the same bytes — which is
/// ordinary, since the hash is the content.
#[tokio::test]
async fn a_concurrent_unpublish_is_not_lost_to_a_publish() {
tokio::time::timeout(std::time::Duration::from_secs(120), async {
let dir = tempfile::tempdir().unwrap();
let provider = AppBlobs::open_fetcher(dir.path().join("blobs"), ep().await)
.await
.unwrap();
provider.grant("room", "b64u:alice").unwrap();
let src = dir.path().join("f.bin");
std::fs::write(&src, b"contested by publish").unwrap();
let (_t, hash_hex) = provider.publish_scope("room", &src).await.unwrap();
// Re-publishing the SAME bytes races an unpublish of the same hash.
provider.set_publish_delay(std::time::Duration::from_millis(600));
let p2 = provider.clone();
let src2 = src.clone();
let publish =
tokio::spawn(async move { p2.publish_scope("room", &src2).await.map(|_| ()) });
tokio::time::sleep(std::time::Duration::from_millis(150)).await;
let removed = provider.unpublish("room", &hash_hex).await.unwrap();
publish.await.unwrap().unwrap();
assert!(removed, "the unpublish must actually have removed the hash");
let hashes: Vec<String> = provider
.list()
.into_iter()
.flat_map(|(_, hashes, _, _)| hashes)
.collect();
assert!(
!hashes.contains(&hash_hex),
"a re-publish of identical bytes must not overwrite a concurrent revocation \
(scope now holds {hashes:?})"
);
})
.await
.expect("publish/unpublish race test timed out");
}
/// #105: the relay-ready wait is a CAP, and it actually RUNS.
///
/// The first version of this test asserted neither. On a relay-disabled endpoint the minted
/// ticket is byte-identical with and without the wait — no relay URL appears either way — so
/// the ONLY observable difference is elapsed time. Deleting the wait from `ticket_for`
/// entirely left both #105 tests passing (in 0.65s instead of 9.3s). Guarding the flag is not
/// guarding the behaviour the flag exists to produce.
///
/// Because `online()` never completes with relays disabled, an enabled wait MUST consume the
/// full cap. So the elapsed time is a two-sided assertion: the lower bound fails if the wait
/// is removed or skipped, the upper bound fails if it becomes unbounded or is lengthened.
#[tokio::test]
async fn the_relay_wait_actually_runs_and_is_capped() {
let dir = tempfile::tempdir().unwrap();
let provider_ep = ep().await;
let provider = AppBlobs::open_fetcher(dir.path().join("blobs"), provider_ep.clone())
.await
.unwrap();
// F3: pin the DEFAULT too. Without this, flipping `relay_wait`'s initial value to `true`
// would make the boot guard in `boot.rs` stop failing when its one call is deleted — the
// whole point of #105 would evaporate silently.
assert!(
!provider.relay_wait_enabled(),
"the wait must default OFF — every hand-built fixture would otherwise pay the full cap \
per mint, and the boot guard would stop guarding anything"
);
provider.enable_relay_wait();
provider.spawn_accept(&provider_ep);
let src = dir.path().join("capped.bin");
std::fs::write(&src, b"capped").unwrap();
let started = std::time::Instant::now();
let published = provider.publish_path(&src).await.unwrap();
let elapsed = started.elapsed();
assert!(
elapsed >= crate::daemon::RELAY_READY_TIMEOUT,
"the wait must actually RUN — `online()` never completes on a relay-disabled endpoint, \
so an enabled wait consumes the full cap. Minting in {elapsed:?} means the wait was \
skipped or removed"
);
assert!(
elapsed < crate::daemon::RELAY_READY_TIMEOUT + std::time::Duration::from_secs(2),
"and it must be CAPPED — minting took {elapsed:?}, so the bound is longer than \
RELAY_READY_TIMEOUT or the wait is unbounded"
);
// F5: the fetch is bounded too — an unbounded one hangs the whole test binary with no
// failing test name, since libtest has no per-test timeout.
let cdir = tempfile::tempdir().unwrap();
let caller = AppBlobs::open_fetcher(cdir.path().join("blobs"), ep().await)
.await
.unwrap();
let hash = tokio::time::timeout(
std::time::Duration::from_secs(30),
caller.fetch(&published.0),
)
.await
.expect("fetch timed out")
.expect("the fallback direct-address ticket must still round-trip");
assert_eq!(&caller.read_bytes(hash).await.unwrap()[..], b"capped");
}
/// #107: the race #104's lock could NOT close. A mutex orders by ACQUISITION, not by request
/// arrival, so an unpublish that acquires first is still erased by a republish acquiring
/// second — both returning success, operator told the file was withdrawn while it is served.
///
/// Closed with state rather than exclusion: unpublish records a withdrawal, and republish
/// refuses it. Asserted in the ORDER THAT USED TO LOSE — unpublish completes first, then
/// republish runs — which is exactly the interleaving a lock cannot help with.
#[tokio::test]
async fn a_completed_unpublish_is_not_undone_by_a_later_republish() {
tokio::time::timeout(std::time::Duration::from_secs(90), async {
let dir = tempfile::tempdir().unwrap();
let provider = AppBlobs::open_fetcher(dir.path().join("blobs"), ep().await)
.await
.unwrap();
provider.grant("room", "b64u:alice").unwrap();
let src = dir.path().join("f.bin");
std::fs::write(&src, b"withdrawn content").unwrap();
let (_t, hash_hex) = provider.publish_scope("room", &src).await.unwrap();
assert!(provider.unpublish("room", &hash_hex).await.unwrap());
// The bytes are still in the store (#80: no reclaim), so `has()` is true and the ONLY
// thing standing between the operator's revocation and its silent undoing is #107.
let err = provider
.republish("room", &hash_hex)
.await
.expect_err("a withdrawn hash must not republish");
assert!(
err.downcast_ref::<crate::daemon::BlobWithdrawn>().is_some(),
"must be BlobWithdrawn so a client can tell it from 'fetch it first', got: {err}"
);
let hashes: Vec<String> = provider
.list()
.into_iter()
.flat_map(|(_, hashes, _, _)| hashes)
.collect();
assert!(
!hashes.contains(&hash_hex),
"and the scope must still not list it (got {hashes:?})"
);
})
.await
.expect("durable revocation test timed out");
}
/// The deliberate re-share still works: `blob_publish` from a FILE clears the withdrawal, and
/// a republish afterwards is allowed again. Without this, a withdrawal would be permanent and
/// an operator could never re-share the same content into that scope.
#[tokio::test]
async fn publishing_from_the_file_again_lifts_the_withdrawal() {
tokio::time::timeout(std::time::Duration::from_secs(90), async {
let dir = tempfile::tempdir().unwrap();
let provider = AppBlobs::open_fetcher(dir.path().join("blobs"), ep().await)
.await
.unwrap();
provider.grant("room", "b64u:alice").unwrap();
let src = dir.path().join("f.bin");
std::fs::write(&src, b"re-shared on purpose").unwrap();
let (_t, hash_hex) = provider.publish_scope("room", &src).await.unwrap();
provider.unpublish("room", &hash_hex).await.unwrap();
provider.republish("room", &hash_hex).await.unwrap_err();
// The deliberate act: name the FILE again.
provider.publish_scope("room", &src).await.unwrap();
provider
.republish("room", &hash_hex)
.await
.expect("after a deliberate re-publish, republish is allowed again");
})
.await
.expect("un-withdraw test timed out");
}
/// Republish is idempotent (the scope hash set is a set), so a client may call it
/// unconditionally after every fetch without special-casing the second time.
#[tokio::test]
async fn republishing_twice_is_not_an_error_and_records_one_entry() {
let dir = tempfile::tempdir().unwrap();
let provider = AppBlobs::open_fetcher(dir.path().join("blobs"), ep().await)
.await
.unwrap();
provider.grant("room", "b64u:alice").unwrap();
let src = dir.path().join("f.bin");
std::fs::write(&src, b"dupe").unwrap();
let (_t, hash_hex) = provider.publish_path(&src).await.unwrap();
provider.republish("room", &hash_hex).await.unwrap();
provider.republish("room", &hash_hex).await.unwrap();
// Constrain the SCOPE NAME too: without it, a mutation inserting into a hardcoded scope,
// or into every scope, passes.
let rooms: Vec<(String, Vec<String>)> = provider
.list()
.into_iter()
.map(|(name, hashes, _, _)| (name, hashes))
.collect();
assert_eq!(
rooms,
vec![("room".to_string(), vec![hash_hex.clone()])],
"exactly one entry, in the NAMED scope, not two and not elsewhere"
);
}
/// Lock the exact serving mask: single-blob GET is scope-checked (`Intercept`); every other
/// request type is pinned to deny-by-default so the refusal does NOT rely on 0.103.0's
/// `mask.get`-routes-all quirk. A regression that loosens any of these fails here.
#[test]
fn app_blob_event_mask_pins_non_get_request_types_to_deny_by_default() {
assert_eq!(APP_BLOB_EVENT_MASK.connected, ConnectMode::Intercept);
assert_eq!(APP_BLOB_EVENT_MASK.get, RequestMode::Intercept);
// get_many/push refuse at the protocol level with Permission (events.rs:504-506), no event.
assert_eq!(APP_BLOB_EVENT_MASK.get_many, RequestMode::Disabled);
assert_eq!(APP_BLOB_EVENT_MASK.push, RequestMode::Disabled);
// observe has no `Disabled` variant; `Intercept` routes it to the drain loop's deny arm.
assert_eq!(APP_BLOB_EVENT_MASK.observe, ObserveMode::Intercept);
// throttle is a transfer knob, not a request gate — left at its default.
assert_eq!(APP_BLOB_EVENT_MASK.throttle, ThrottleMode::None);
}
async fn ep() -> iroh::Endpoint {
iroh::Endpoint::builder(iroh::endpoint::presets::Minimal)
.relay_mode(iroh::RelayMode::Disabled)
.alpns(vec![APP_BLOB_ALPN.to_vec()])
.bind()
.await
.expect("bind endpoint")
}
#[tokio::test]
async fn ungated_fetcher_still_round_trips() {
tokio::time::timeout(std::time::Duration::from_secs(30), async {
let pdir = tempfile::tempdir().unwrap();
let provider_ep = ep().await;
let provider = AppBlobs::open_fetcher(pdir.path().join("blobs"), provider_ep.clone())
.await
.unwrap();
provider.spawn_accept(&provider_ep);
let src = pdir.path().join("p.bin");
std::fs::write(&src, b"hello scopes").unwrap();
let (ticket, _hash) = provider.publish_path(&src).await.unwrap();
let cdir = tempfile::tempdir().unwrap();
let caller_ep = ep().await;
let caller = AppBlobs::open_fetcher(cdir.path().join("blobs"), caller_ep.clone())
.await
.unwrap();
let hash = caller.fetch(&ticket).await.unwrap();
assert_eq!(&caller.read_bytes(hash).await.unwrap()[..], b"hello scopes");
})
.await
.expect("timed out");
}
#[tokio::test]
async fn granted_caller_fetches_but_ungranted_and_uncontained_are_denied() {
tokio::time::timeout(std::time::Duration::from_secs(30), async {
// Two callers: alice (granted) and bob (rostered but ungranted for this scope).
let alice_ep = ep().await;
let bob_ep = ep().await;
let alice_id: EndpointId = alice_ep.id().into();
let bob_id: EndpointId = bob_ep.id().into();
// Provider gate resolves BOTH (both pass the accept-time gate); scope grants only alice.
let mut entries = std::collections::HashMap::new();
entries.insert(
alice_id,
PeerIdentity {
endpoint: [0u8; 32].into(),
name: "alice".into(),
user_id: Some("alice".into()),
groups: vec!["team-eng".into()],
},
);
entries.insert(
bob_id,
PeerIdentity {
endpoint: [0u8; 32].into(),
name: "bob".into(),
user_id: Some("bob".into()),
groups: vec!["team-eng".into()],
},
);
let gate: Arc<dyn mcpmesh_net::TrustGate> = Arc::new(StaticGate::new(entries));
let pdir = tempfile::tempdir().unwrap();
let scopes = Arc::new(ScopeStore::new(pdir.path().join("scopes.json")));
let provider_ep = ep().await;
let provider = AppBlobs::load(
pdir.path().join("blobs"),
scopes,
gate,
provider_ep.clone(),
crate::audit::AuditSink::disabled(),
)
.await
.unwrap();
provider.spawn_accept(&provider_ep);
// Publish into scope "docs" and grant it to the user_id "alice" ONLY (not team-eng).
let src = pdir.path().join("secret.bin");
std::fs::write(&src, b"top secret bytes").unwrap();
let (ticket, _hash) = provider.publish_scope("docs", &src).await.unwrap();
provider.grant("docs", "alice").unwrap();
// GRANTED (alice) → fetch succeeds + verifies.
let cdir = tempfile::tempdir().unwrap();
let alice = AppBlobs::open_fetcher(cdir.path().join("a"), alice_ep.clone())
.await
.unwrap();
let hash = alice.fetch(&ticket).await.expect("granted alice fetches");
assert_eq!(
&alice.read_bytes(hash).await.unwrap()[..],
b"top secret bytes"
);
// UNGRANTED (bob — rostered, team-eng, but "docs" grants only alice) → the request hook
// denies with Permission BEFORE any bytes; the fetch errors.
let bob = AppBlobs::open_fetcher(cdir.path().join("b"), bob_ep.clone())
.await
.unwrap();
let bob_res =
tokio::time::timeout(std::time::Duration::from_secs(10), bob.fetch(&ticket)).await;
assert!(
matches!(bob_res, Ok(Err(_))),
"ungranted bob is refused: {bob_res:?}"
);
})
.await
.expect("timed out");
}
/// The #38 inversion for the blob-scope gate — grants hold STABLE principals only:
/// a PAIRING-MODE peer (unbound: `user_id: None`, no groups) granted by its `eid:`
/// device principal CAN fetch; a peer whose only "grant" names its display NICKNAME
/// is DENIED (nicknames are self-asserted/rewritable and never admit). Identities
/// carry their REAL authenticated endpoint bytes so the eid arm is honest.
#[tokio::test]
async fn pairing_mode_eid_grant_admits_and_nickname_grant_stays_denied() {
tokio::time::timeout(std::time::Duration::from_secs(30), async {
let carol_ep = ep().await; // pairing-mode: granted by her eid: device principal
let mallory_ep = ep().await; // "granted" only by nickname — must stay denied
let carol_id: EndpointId = carol_ep.id().into();
let mallory_id: EndpointId = mallory_ep.id().into();
let mut entries = std::collections::HashMap::new();
entries.insert(
carol_id,
PeerIdentity {
endpoint: carol_id, // the REAL authenticated bytes — the eid arm is honest
name: "carol".into(),
user_id: None, // no device→user binding — eid: is the ONLY principal
groups: vec![],
},
);
entries.insert(
mallory_id,
PeerIdentity {
endpoint: mallory_id,
name: "mallory".into(),
user_id: None,
groups: vec![],
},
);
let gate: Arc<dyn mcpmesh_net::TrustGate> = Arc::new(StaticGate::new(entries));
let pdir = tempfile::tempdir().unwrap();
let scopes = Arc::new(ScopeStore::new(pdir.path().join("scopes.json")));
let provider_ep = ep().await;
let provider = AppBlobs::load(
pdir.path().join("blobs"),
scopes,
gate,
provider_ep.clone(),
crate::audit::AuditSink::disabled(),
)
.await
.unwrap();
provider.spawn_accept(&provider_ep);
let src = pdir.path().join("attach.bin");
std::fs::write(&src, b"eid-scoped bytes").unwrap();
let (ticket, _hash) = provider
.publish_scope("kb-attach-carol", &src)
.await
.unwrap();
// Grant by the STABLE eid: device principal (iroh EndpointId Display is the same
// hex-lower encoding as `EndpointId::principal()`).
provider
.grant("kb-attach-carol", &format!("eid:{}", carol_ep.id()))
.unwrap();
// A NICKNAME entry on the same scope — display names must NEVER admit (#38), so
// this grants mallory nothing even though her resolved identity is named "mallory".
provider.grant("kb-attach-carol", "mallory").unwrap();
let cdir = tempfile::tempdir().unwrap();
let carol = AppBlobs::open_fetcher(cdir.path().join("c"), carol_ep.clone())
.await
.unwrap();
let hash = carol
.fetch(&ticket)
.await
.expect("a pairing-mode peer granted by its eid: principal fetches");
assert_eq!(
&carol.read_bytes(hash).await.unwrap()[..],
b"eid-scoped bytes"
);
// NICKNAME NEVER ADMITS: mallory resolves at accept time and the scope lists the
// bare string "mallory", but her nickname is not a principal → Permission.
let mallory = AppBlobs::open_fetcher(cdir.path().join("m"), mallory_ep.clone())
.await
.unwrap();
let res =
tokio::time::timeout(std::time::Duration::from_secs(10), mallory.fetch(&ticket))
.await;
assert!(
matches!(res, Ok(Err(_))),
"a nickname-only grant is refused: {res:?}"
);
})
.await
.expect("eid-grant test timed out");
}
/// A served GET records a `blob_fetch` audit line attributed to the authenticated peer, with the
/// hash and status=ok ("each blob fetch — peer + hash + …"). Uses a real temp AuditLog.
#[tokio::test]
async fn served_get_records_blob_fetch_audit() {
use crate::audit::{AuditLog, AuditSink};
tokio::time::timeout(std::time::Duration::from_secs(30), async {
let alice_ep = ep().await;
let alice_id: EndpointId = alice_ep.id().into();
let mut entries = std::collections::HashMap::new();
entries.insert(
alice_id,
PeerIdentity {
endpoint: [0u8; 32].into(),
name: "alice".into(),
user_id: Some("alice".into()),
groups: vec![],
},
);
let gate: Arc<dyn mcpmesh_net::TrustGate> = Arc::new(StaticGate::new(entries));
let pdir = tempfile::tempdir().unwrap();
let audit_dir = pdir.path().join("audit");
let sink = AuditSink::new(AuditLog::spawn(audit_dir.clone()));
let scopes = Arc::new(ScopeStore::new(pdir.path().join("scopes.json")));
let provider_ep = ep().await;
let provider = AppBlobs::load(
pdir.path().join("blobs"),
scopes,
gate,
provider_ep.clone(),
sink,
)
.await
.unwrap();
provider.spawn_accept(&provider_ep);
let src = pdir.path().join("doc.bin");
std::fs::write(&src, b"auditable bytes").unwrap();
let (ticket, hash_hex) = provider.publish_scope("docs", &src).await.unwrap();
provider.grant("docs", "alice").unwrap();
let cdir = tempfile::tempdir().unwrap();
let alice = AppBlobs::open_fetcher(cdir.path().join("a"), alice_ep.clone())
.await
.unwrap();
let _ = alice.fetch(&ticket).await.expect("granted alice fetches");
let month = &crate::audit::now_ts()[..7];
let file = audit_dir.join(format!("{month}.jsonl"));
let mut ok = false;
for _ in 0..50 {
if let Ok(b) = std::fs::read_to_string(&file)
&& b.contains("\"kind\":\"blob_fetch\"")
&& b.contains("\"peer\":\"alice\"")
&& b.contains(&hash_hex)
{
ok = true;
break;
}
tokio::time::sleep(std::time::Duration::from_millis(20)).await;
}
assert!(
ok,
"a served GET records blob_fetch(peer=alice, hash, status)"
);
})
.await
.expect("blob_fetch audit test timed out");
}
}