From d59896c0ff7476843101f5025063515059271b14 Mon Sep 17 00:00:00 2001 From: oc-code-02 Date: Mon, 23 Nov 2026 07:17:58 -0600 Subject: [PATCH] feat(id): refuse hosted import on CSE-choice mismatch (slice 4c of #2852, #3810) verify_import step 1 gains ExportError::CseChoiceMismatch: a manifest whose client_side_encryption differs from the binding record it travels with does not describe one stored data set (spec section 7: the flag changes only by re-encrypting via export/import). The choice is manifest-signed, so an unsigned flip still fails as ManifestSignatureInvalid. ingest_import's verification-token gate now pins the CSE choice too, refusing before any write. Tests (failing-first: CseChoiceMismatch case failed on origin/main as Ok/verified): both mismatch directions, unsigned flag flip, token-with-different-choice refused pre-write. Evidence: cargo test -p id --lib -- hosted_export:: hosted_ingest:: hosted_binding:: => 60 passed, 0 failed, on the laptop. Refs #3810, #2852. --- pkgs/id/src/hosted_export.rs | 77 ++++++++++++++++++++++++++++++++++++ pkgs/id/src/hosted_ingest.rs | 26 +++++++++++- 2 files changed, 101 insertions(+), 2 deletions(-) diff --git a/pkgs/id/src/hosted_export.rs b/pkgs/id/src/hosted_export.rs index b3f718d6fb..5c8c300a93 100644 --- a/pkgs/id/src/hosted_export.rs +++ b/pkgs/id/src/hosted_export.rs @@ -280,6 +280,12 @@ pub enum ExportError { /// The manifest names a different binding or key than the /// binding record it travels with. ManifestBindingMismatch, + /// The manifest's §7 client-side-encryption choice differs from + /// the binding record's. The flag can only change by + /// re-encrypting via export/import, so an export whose halves + /// disagree does not describe one stored data set (§6.2 step 1, + /// §7). + CseChoiceMismatch, /// The manifest signature does not verify (against the user's /// root key, or the service key for an `unkeyed_export`). ManifestSignatureInvalid, @@ -330,6 +336,12 @@ impl std::fmt::Display for ExportError { "manifest does not name the binding record it travels with" ) } + Self::CseChoiceMismatch => { + write!( + f, + "manifest's client-side-encryption choice differs from the binding record's" + ) + } Self::ManifestSignatureInvalid => { write!(f, "manifest signature does not verify") } @@ -518,6 +530,17 @@ pub fn verify_import( ) { return Err(ExportError::ManifestSignatureInvalid); } + // Step 1, §7: the manifest's CSE choice is part of naming the + // binding's stored state. It is covered by the manifest + // signature (canonical_bytes), and the binding record's own + // flag is attested by the service; disagreement means the two + // halves do not describe one stored data set — an export + // stating the wrong choice misdirects every downstream reader + // of the payloads (§7: the flag changes only by re-encrypting + // via export/import). + if manifest.client_side_encryption != binding.client_side_encryption { + return Err(ExportError::CseChoiceMismatch); + } // Step 2: the binding chain. The transcript is reconstructed // from the exported record itself, never fetched. @@ -741,6 +764,60 @@ mod tests { assert!(!verified.client_side_encryption); } + #[test] + fn manifest_cse_choice_mismatching_the_binding_is_refused() { + // §7: the flag can only change by re-encrypting via + // export/import, so a manifest stating a different choice + // than the binding record it travels with does not name + // that binding (§6.2 step 1). Stating encryption under a + // binding that stored plaintext would decrypt wrong; + // stating plaintext under an encrypted binding downgrades + // opaque payloads — the import refuses, and ingests + // nothing. + let (_svc, mut export) = sample_export(Some(false)); + assert!(!export.binding.client_side_encryption); + export.manifest.client_side_encryption = true; + // Re-sign so only the choice rule can fail: this is the + // misassembled pair (or the re-signed forgery), not a + // tampered manifest. + export.manifest.signature = hex_encode( + &root_key(21) + .sign(&export.manifest.canonical_bytes()) + .to_bytes(), + ); + assert_eq!( + verify_import(&export, false), + Err(ExportError::CseChoiceMismatch) + ); + // The other direction — CSE binding claimed as plaintext — + // is refused the same way. + let (_svc, mut cse_export) = sample_export(None); + assert!(cse_export.binding.client_side_encryption); + cse_export.manifest.client_side_encryption = false; + cse_export.manifest.signature = hex_encode( + &root_key(21) + .sign(&cse_export.manifest.canonical_bytes()) + .to_bytes(), + ); + assert_eq!( + verify_import(&cse_export, false), + Err(ExportError::CseChoiceMismatch) + ); + } + + #[test] + fn flipping_the_manifest_cse_flag_without_resigning_is_refused() { + // The CSE choice is inside the manifest's canonical bytes, + // so a flag flip without the key fails the signature first + // — the choice is authenticated, not decorative. + let (_svc, mut export) = sample_export(None); + export.manifest.client_side_encryption = false; + assert_eq!( + verify_import(&export, false), + Err(ExportError::ManifestSignatureInvalid) + ); + } + #[test] fn tampered_manifest_is_refused() { let (_svc, mut export) = sample_export(None); diff --git a/pkgs/id/src/hosted_ingest.rs b/pkgs/id/src/hosted_ingest.rs index 3c04fd0c71..9d347ba114 100644 --- a/pkgs/id/src/hosted_ingest.rs +++ b/pkgs/id/src/hosted_ingest.rs @@ -249,8 +249,9 @@ async fn place_blob(blobs: &BlobStore, blob: &ExportBlob) -> EntryOutcome { /// /// `verified` is the token [`crate::hosted_export::verify_import`] /// minted for this export; it must name the export's binding, key, -/// and entry counts. `now_ms` is the importing node's clock, handed -/// to the event store like any other ingest's. +/// entry counts, and §7 client-side-encryption choice. `now_ms` is +/// the importing node's clock, handed to the event store like any +/// other ingest's. /// /// # Errors /// @@ -270,6 +271,7 @@ pub async fn ingest_import( || verified.iroh_id != export.manifest.iroh_id || verified.object_count != export.objects.len() as u64 || verified.blob_count != export.blobs.len() as u64 + || verified.client_side_encryption != export.manifest.client_side_encryption { return Err(ImportError::VerificationMismatch); } @@ -557,4 +559,24 @@ mod tests { let author = *p2p_key(SEED).verifying_key().as_bytes(); assert!(target.event(&author, 0).unwrap().is_none()); } + + #[tokio::test] + async fn a_token_with_a_different_cse_choice_is_refused_before_any_write() { + let (events, content) = source_chain(); + let export = make_export(&events, Some(&content)); + let mut wrong = verify_import(&export, false).unwrap(); + wrong.client_side_encryption = !wrong.client_side_encryption; + let (_dir, mut target) = open_store(); + let blobs = blob_store(); + let err = ingest_import(&mut target, &blobs, &export, &wrong, 0) + .await + .unwrap_err(); + assert!(matches!(err, ImportError::VerificationMismatch)); + // The token is the authority to proceed only with the exact + // verified interpretation; reject before any object or blob + // is written. + let author = *p2p_key(SEED).verifying_key().as_bytes(); + assert!(target.event(&author, 0).unwrap().is_none()); + assert!(blobs.get_bytes(BlobHash::new(&content)).await.is_err()); + } }