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
// Copyright (c) 2026 The NORA Authors
// SPDX-License-Identifier: MIT
//! Compile-time integrity witnesses for stored artifacts (typestate).
//!
//! NORA already enforces its integrity guarantees at runtime: the buffered
//! [`Storage::get`](crate::storage::Storage::get) gate hash-pin-verifies bytes
//! before returning them (`#582`/`#604`), the streaming docker path
//! tamper-detects at EOF, and `put` couples the body write with a hash-pin
//! record on every backend. This module pushes those guarantees one step
//! **left** — into the type system — and, just as importantly, makes the
//! *known holes* impossible to hide.
//!
//! # The honest tiers (grounded in live code)
//!
//! A served-bytes value carries a zero-sized [`Integrity`] witness `S`:
//!
//! - [`Verified`] — the bytes were SHA-256-checked against an expected digest
//! and **matched**. This is *sound by construction*: the only constructor,
//! [`Blob::<Verified>::verify`], performs the hash itself, so possessing a
//! `Blob<Verified>` is a proof that `sha256(bytes) == expected`. No `unsafe`,
//! no privileged constructor, no cross-crate seal to trust. What `expected`
//! *means* is the caller's to state: for a hash-pinned cache read it is the
//! digest NORA recorded at store time (tamper-evidence against on-disk
//! corruption — `src/storage/mod.rs`), and for a content-addressed
//! artifact (a docker blob, whose key *is* its digest) it is the canonical
//! upstream digest.
//!
//! - [`TamperEvident`] — streamed bytes whose digest is verified only at EOF
//! (`src/registry/docker.rs`, `VerifyingReader`). A mismatch aborts the
//! response, but bytes already streamed cannot be un-sent. Strictly weaker
//! than [`Verified`]; named so it can never be silently treated as the strong
//! tier.
//!
//! - [`Unverified`] — raw bytes with no NORA-side integrity guarantee: a
//! streaming `get_reader`/`get_range` read, an in-memory buffer, or
//! proxy-upstream bytes that were never NORA-stored.
//!
//! # Why this is push-left, not theatre
//!
//! A serve sink that demands `Blob<Verified>` (see [`verified_body`]) *cannot*
//! be handed raw or merely-tamper-evident bytes — that is a compile error, not
//! a runtime check that a future refactor might skip. And the open-world hole
//! (an object stored with no pin) is forced into the open by [`GateOutcome`]:
//! a caller of
//! [`Storage::get_verified`](crate::storage::Storage::get_verified) must
//! `match` and decide what to do with [`GateOutcome::Unpinned`] — it can never
//! be mistaken for a cryptographically verified read.
//!
//! Compile-fail proofs live in `tests/typestate_compile_fail.rs` (trybuild);
//! the positive direction is unit-tested below.
use PhantomData;
/// Sealed so no code outside this module can add an integrity tier or impl the
/// [`Integrity`] trait for a forged marker.
/// The integrity tier of a [`Blob`]. Sealed: the variants are exactly
/// [`Unverified`], [`TamperEvident`], and [`Verified`].
/// Raw bytes with no NORA-side integrity guarantee (streaming read, in-memory
/// buffer, or never-NORA-stored upstream bytes). Uninhabited — used only as a
/// type tag.
/// Streamed bytes verified at EOF only: a mismatch aborts the response but
/// cannot un-send bytes already sent. Weaker than [`Verified`]. Uninhabited.
/// Bytes whose SHA-256 matched an expected digest. The strong tier; mintable
/// only by [`Blob::<Verified>::verify`], which performs the check. Uninhabited.
/// A payload `T` tagged with its compile-time [`Integrity`] tier `S`.
///
/// The payload is private: the only way to obtain a `T` back is [`into_inner`],
/// and the only way to obtain a [`Verified`]-tagged blob is the hash-checking
/// [`verify`] constructor — so the tier on a `Blob` always reflects a real
/// integrity check (or its honest absence).
///
/// [`into_inner`]: Blob::into_inner
/// [`verify`]: Blob::verify
/// Verification failed while trying to mint a [`Verified`] blob.
/// The honest outcome of a buffered, integrity-gated read
/// ([`Storage::get_verified`](crate::storage::Storage::get_verified)).
///
/// A caller cannot get bytes out without `match`ing, so the open-world hole is
/// impossible to ignore: either the bytes matched a recorded pin
/// ([`Verified`](GateOutcome::Verified)), or no pin existed for the key and they
/// were served without a cryptographic check
/// ([`Unpinned`](GateOutcome::Unpinned)).
/// A serve sink that accepts **only** cryptographically [`Verified`] bytes.
///
/// Routing a response body through this function makes "serve unverified bytes
/// on the verified path" a compile error: a [`Blob<Unverified>`](Blob) or
/// [`Blob<TamperEvident>`](Blob) simply does not fit the parameter. It is the
/// type-level counterpart of the runtime fail-closed gate.
///
/// # The verified path type-checks
///
/// ```
/// use nora_registry::verified::{verified_body, Blob, Verified};
/// // sha256("abc")
/// let digest = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad";
/// let blob = Blob::<Verified, _>::verify(b"abc".to_vec(), digest)
/// .expect("abc hashes to the digest above");
/// assert_eq!(verified_body(blob), b"abc".to_vec());
/// ```
///
/// # Raw bytes do NOT compile on the verified path
///
/// ```compile_fail
/// use nora_registry::verified::{verified_body, Blob};
/// let raw = Blob::raw(b"abc".to_vec());
/// let _ = verified_body(raw); // expected Blob<Verified>, found Blob<Unverified>
/// ```
///
/// # Tamper-evident (streamed) bytes do NOT compile either
///
/// ```compile_fail
/// use nora_registry::verified::{verified_body, Blob};
/// let te = Blob::from_eof_verifier(b"abc".to_vec());
/// let _ = verified_body(te); // expected Blob<Verified>, found Blob<TamperEvident>
/// ```
///
/// # A `Verified` witness cannot be forged
///
/// There is no non-checking constructor, and the payload field is private, so
/// the hash-checking [`Blob::<Verified>::verify`] is the only way in:
///
/// ```compile_fail
/// use nora_registry::verified::{Blob, Verified};
/// let _: Blob<Verified, Vec<u8>> = Blob::raw(b"abc".to_vec()); // raw is Unverified
/// ```
// ---------------------------------------------------------------------------
// Streaming serve sink: the type-level witness on the STREAMING path (#849).
//
// The buffered path above discharges a compile-time witness (`verified_body`
// takes only `Blob<Verified>`). Streaming can't carry a whole-`Blob` value, so
// the guarantee is restored one level up: only a stream that was wired through an
// EOF-verifying wrapper implements `VerifiedByteStream`, and `stream_body` — the
// single verified `Body::from_stream` site — accepts only that. Handing a raw
// reader stream to it is a compile error, exactly like `verified_body(raw)`.
// ---------------------------------------------------------------------------
/// Seal for [`VerifiedByteStream`]. NORA's own EOF-verifying stream types attest it where
/// they are defined (raw `VerifyingStream`, docker `ReaderStream<VerifyingReader<_>>`).
/// `pub` (but doc-hidden) only because those types live in the `nora` binary crate while
/// this trait lives in the `nora_registry` library — the handlers must be able to impl it.
/// The guarantee that matters holds regardless: a *new* NORA stream does not fit
/// [`stream_body`] without a deliberate, visible `impl` (a raw reader stream is a compile
/// error), and the single `Body::from_stream` serve site is [`stream_body`].
/// A byte stream whose bytes flow through an EOF-verifying wrapper — it hashes while
/// streaming and aborts the body on a digest mismatch (tamper-evident). The streaming
/// analogue of holding a [`Blob<Verified>`], and, like it, unforgeable outside NORA:
/// [`stream_body`] accepts only this type, so serving a raw reader stream on an
/// integrity path is a **compile error**.
/// The sole streaming serve sink for integrity-checked bytes — the compile-time
/// counterpart of [`verified_body`]. A raw reader stream does not implement
/// [`VerifiedByteStream`], so it cannot be turned into a response body here.
///
/// # A raw stream does NOT compile on the verified streaming path
///
/// ```compile_fail
/// use nora_registry::verified::stream_body;
/// // a plain ReaderStream (no EOF digest check) is not a VerifiedByteStream
/// let raw = tokio_util::io::ReaderStream::new(tokio::io::empty());
/// let _ = stream_body(raw); // the trait bound `VerifiedByteStream` is not satisfied
/// ```
/// An `AsyncRead` that verifies its content digest at EOF — docker's `VerifyingReader`,
/// which recomputes SHA-256 as bytes flow and errors the read on mismatch. The reader
/// analogue of [`VerifiedByteStream`]; accepted only by [`reader_stream_body`].
/// The docker-blob streaming sink: frames a [`VerifiedByteReader`] into a response body.
/// Like [`stream_body`], a raw reader (no EOF digest check) does not implement the trait,
/// so serving it here is a **compile error**. This is where the single `Body::from_stream`
/// for the reader path lives.
///
/// # A raw reader does NOT compile on the verified streaming path
///
/// ```compile_fail
/// use nora_registry::verified::reader_stream_body;
/// // a plain AsyncRead (no EOF digest check) is not a VerifiedByteReader
/// let _ = reader_stream_body(tokio::io::empty()); // trait bound `VerifiedByteReader` unmet
/// ```
/// The explicit open-world streaming arm: partial-content / byte-range serves, where a
/// partial range has no whole-file digest to check against. Named — never silent — the
/// streaming counterpart of [`GateOutcome::Unpinned`]: the call site states that these
/// bytes are served without a NORA-side cryptographic check.
// ---------------------------------------------------------------------------
// Write-side witness: "store unpinned" — the S3 hole, named in the type.
// ---------------------------------------------------------------------------
/// Durability tier of a completed store. Sealed, like [`Integrity`].
/// Body **and** hash-pin both landed durably. Uninhabited.
/// Stored without a digest, so no pin was recorded and the artifact is served
/// open-world. Names that hole in the type system. Uninhabited.
/// Receipt for a completed [`Storage::put`](crate::storage::Storage::put),
/// carrying the [`Durability`] tier it achieved.
/// An operation that requires a durable integrity pin (e.g. an immutable
/// publish that must be re-verifiable). Accepts **only** a
/// [`StoreReceipt<Pinned>`] — passing an [`StoreReceipt<Unpinnable>`] is a
/// compile error, so the open-world hole cannot be silently relied upon.
///
/// # A pinned store type-checks
///
/// ```
/// use nora_registry::verified::{require_pinned, StoreReceipt};
/// let local = StoreReceipt::pinned("raw/x");
/// assert_eq!(require_pinned(local), "raw/x");
/// ```
///
/// # An unpinnable store does NOT compile here
///
/// ```compile_fail
/// use nora_registry::verified::{require_pinned, StoreReceipt};
/// let unpinned = StoreReceipt::unpinnable("raw/x");
/// let _ = require_pinned(unpinned); // expected StoreReceipt<Pinned>, found <Unpinnable>
/// ```