From 510d5eecd14e69e625ea272ec6651292270cbd4a Mon Sep 17 00:00:00 2001 From: mnovak Date: Wed, 4 Nov 2026 00:27:37 +0100 Subject: [PATCH] [repo] Undo cargo fmt churn in files this branch does not change; CLAUDE.md: the trap --- CLAUDE.md | 4 +++ crates/custos-keys/src/lib.rs | 27 ++++----------- crates/custos-keys/tests/mandatory.rs | 3 +- crates/custos-seal/src/lib.rs | 4 +-- crates/custos-vault/tests/factors.rs | 38 ++++------------------ crates/custos-wasm/src/lib.rs | 24 +++----------- crates/custos-wasm/tests/factors.rs | 47 ++++++--------------------- 7 files changed, 34 insertions(+), 113 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index 3eda70b..f34553a 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -51,3 +51,7 @@ On the dev machine set `CARGO_HOME=F:\C-Drive-Migration\future-caches\cargo` (C: first, then a test, then the code. - Branch → PR on Gitea (token in `$GITEA_TOKEN`, never echoed) → self-review in the PR body → merge `--no-ff` → push → `git rev-parse HEAD origin/main` → tag `vX.Y.Z` (the private app pins tags). + +## Known traps +- `cargo fmt --all` reformats files that are not rustfmt-clean on `main` (custos-keys, custos-seal, custos-wasm and + some tests): run `rustfmt --edition 2024 ` on the files you changed, so a PR carries no unrelated churn. diff --git a/crates/custos-keys/src/lib.rs b/crates/custos-keys/src/lib.rs index 96eeab4..3dc5a7a 100644 --- a/crates/custos-keys/src/lib.rs +++ b/crates/custos-keys/src/lib.rs @@ -84,11 +84,9 @@ pub fn generate(alg: Algorithm, comment: &str) -> Result { /// An OpenSSH key keeps its own comment unless it has none. pub fn import(text: &str, comment: &str) -> Result { // Pasted or read from a Windows file: CRLF line ends. - let text = text.replace( - " + let text = text.replace(" ", " -", - ); +"); let text = text.trim(); let has = |label: &str| text.contains(&format!("-----BEGIN {label}-----")); if has("ENCRYPTED PRIVATE KEY") || text.contains("Proc-Type: 4,ENCRYPTED") { @@ -101,9 +99,7 @@ pub fn import(text: &str, comment: &str) -> Result { } let comment = if key.comment().is_empty() { comment.to_owned() } else { key.comment().to_owned() }; return match key.key_data() { - KeypairData::Ed25519(k) => { - from_ed25519(ed25519_dalek::SigningKey::from_bytes(&k.private.to_bytes()), &comment) - } + KeypairData::Ed25519(k) => from_ed25519(ed25519_dalek::SigningKey::from_bytes(&k.private.to_bytes()), &comment), KeypairData::Ecdsa(EcdsaKeypair::NistP256 { private, .. }) => { from_p256(p256::SecretKey::from_slice(private.as_slice()).map_err(|_| KeyError::Invalid)?, &comment) } @@ -139,13 +135,8 @@ pub fn import(text: &str, comment: &str) -> Result { /// rebuilt from its components here. fn rsa_from_openssh(k: &RsaKeypair) -> Result { let n = |m: &ssh_key::Mpint| rsa::BigUint::try_from(m).map_err(|_| KeyError::Invalid); - let sk = rsa::RsaPrivateKey::from_components( - n(&k.public.n)?, - n(&k.public.e)?, - n(&k.private.d)?, - vec![n(&k.private.p)?, n(&k.private.q)?], - ) - .map_err(|_| KeyError::Invalid)?; + let sk = rsa::RsaPrivateKey::from_components(n(&k.public.n)?, n(&k.public.e)?, n(&k.private.d)?, vec![n(&k.private.p)?, n(&k.private.q)?]) + .map_err(|_| KeyError::Invalid)?; sk.validate().map_err(|_| KeyError::Invalid)?; Ok(sk) } @@ -178,13 +169,7 @@ fn from_rsa(sk: rsa::RsaPrivateKey, comment: &str) -> Result assemble(&format!("rsa-{bits}"), KeypairData::Rsa(enc(RsaKeypair::try_from(sk))?), private_pem, public_pem, comment) } -fn assemble( - algorithm: &str, - data: KeypairData, - private_pem: String, - public_pem: String, - comment: &str, -) -> Result { +fn assemble(algorithm: &str, data: KeypairData, private_pem: String, public_pem: String, comment: &str) -> Result { let private = enc(PrivateKey::new(data, comment))?; let public = private.public_key(); Ok(KeyPair { diff --git a/crates/custos-keys/tests/mandatory.rs b/crates/custos-keys/tests/mandatory.rs index 415c3e9..caeed88 100644 --- a/crates/custos-keys/tests/mandatory.rs +++ b/crates/custos-keys/tests/mandatory.rs @@ -87,8 +87,7 @@ fn every_private_key_format_imports_to_the_same_key() { for text in inputs { // Surrounding whitespace (a paste, a file with CRLF line ends) does not matter. let pasted = format!("\r\n {}\r\n", text.replace('\n', "\r\n")); - let i = custos_keys::import(&pasted, "deploy") - .unwrap_or_else(|e| panic!("{alg:?} {}: {e:?}", text.lines().next().unwrap())); + let i = custos_keys::import(&pasted, "deploy").unwrap_or_else(|e| panic!("{alg:?} {}: {e:?}", text.lines().next().unwrap())); assert_eq!((i.algorithm.as_str(), i.fingerprint.as_str()), (alg.name(), k.fingerprint.as_str()), "{alg:?}"); assert_eq!(i.public_pem, k.public_pem); } diff --git a/crates/custos-seal/src/lib.rs b/crates/custos-seal/src/lib.rs index b4a6a36..1bff122 100644 --- a/crates/custos-seal/src/lib.rs +++ b/crates/custos-seal/src/lib.rs @@ -52,9 +52,7 @@ fn random() -> Zeroizing<[u8; N]> { fn derive(ikm: &[u8], salt: &[u8], info: &[&[u8]]) -> Zeroizing<[u8; 32]> { let mut k = Zeroizing::new([0u8; 32]); - hkdf::Hkdf::::new(Some(salt), ikm) - .expand(&info.concat(), k.as_mut()) - .expect("32 bytes is a valid HKDF length"); + hkdf::Hkdf::::new(Some(salt), ikm).expand(&info.concat(), k.as_mut()).expect("32 bytes is a valid HKDF length"); k } diff --git a/crates/custos-vault/tests/factors.rs b/crates/custos-vault/tests/factors.rs index ab2530e..fcc7816 100644 --- a/crates/custos-vault/tests/factors.rs +++ b/crates/custos-vault/tests/factors.rs @@ -86,9 +86,7 @@ fn a_factor_is_added_and_removed_without_the_others() { // Only the new factor's material is needed: its pairs come from the factor keys in the body. let phone = v.add_factor(Unlock::Material(FactorKind::Approval, &PHONE), "Phone").unwrap(); let bytes = v.to_bytes().unwrap(); - let mut v = - open(&bytes, Unlock::Material(FactorKind::Approval, &PHONE), Unlock::Material(FactorKind::Device, &DEVICE)) - .unwrap(); + let mut v = open(&bytes, Unlock::Material(FactorKind::Approval, &PHONE), Unlock::Material(FactorKind::Device, &DEVICE)).unwrap(); let kinds: Vec<_> = v.factors().iter().map(|f| f.kind).collect(); assert_eq!(kinds, [FactorKind::Password, FactorKind::Device, FactorKind::Approval]); // A lost device: drop its factor; the other two still open, the device no longer helps. @@ -110,8 +108,7 @@ fn a_forgotten_password_is_replaced_with_two_other_factors() { let mut v = two_factor_vault(); v.add_factor(Unlock::Material(FactorKind::Totp, &TOTP), "TOTP backup").unwrap(); let bytes = v.to_bytes().unwrap(); - let mut v = - open(&bytes, Unlock::Material(FactorKind::Device, &DEVICE), Unlock::Material(FactorKind::Totp, &TOTP)).unwrap(); + let mut v = open(&bytes, Unlock::Material(FactorKind::Device, &DEVICE), Unlock::Material(FactorKind::Totp, &TOTP)).unwrap(); v.change_password(&pw("New9-Password4"), FAST).unwrap(); let bytes2 = v.to_bytes().unwrap(); let dev = || Unlock::Material(FactorKind::Device, &DEVICE); @@ -171,14 +168,7 @@ fn a_machine_store_opens_with_its_device_key_alone() { let user = two_factor_vault().to_bytes().unwrap(); assert!(!Vault::is_machine_store(&user).unwrap()); assert!(matches!(Vault::open_machine_store(&user, &DEVICE), Err(Error::NeedTwoFactors))); - assert!( - Vault::open_with( - &bytes, - Unlock::Material(FactorKind::Device, &STORE), - Unlock::Material(FactorKind::Device, &STORE) - ) - .is_err() - ); + assert!(Vault::open_with(&bytes, Unlock::Material(FactorKind::Device, &STORE), Unlock::Material(FactorKind::Device, &STORE)).is_err()); } #[test] @@ -188,20 +178,10 @@ fn limits_are_enforced() { v.add_factor(Unlock::Material(FactorKind::Device, &[100 + i; 32]), "extra").unwrap(); } assert_eq!(v.factors().len(), 8); - assert!(matches!( - v.add_factor(Unlock::Material(FactorKind::Device, &[1; 32]), "ninth"), - Err(Error::TooManyFactors) - )); + assert!(matches!(v.add_factor(Unlock::Material(FactorKind::Device, &[1; 32]), "ninth"), Err(Error::TooManyFactors))); // Every pair still opens with 8 factors (8 factor slots + 28 pair slots). let bytes = v.to_bytes().unwrap(); - assert!( - open( - &bytes, - Unlock::Material(FactorKind::Device, &[100; 32]), - Unlock::Material(FactorKind::Device, &[105; 32]) - ) - .is_ok() - ); + assert!(open(&bytes, Unlock::Material(FactorKind::Device, &[100; 32]), Unlock::Material(FactorKind::Device, &[105; 32])).is_ok()); } #[test] @@ -210,10 +190,7 @@ fn no_factor_material_in_debug_output() { let u = Unlock::Material(FactorKind::Device, &secret); let p = pw("Orbit7-Fable3"); let printed = format!("{u:?} {:?}", Unlock::Password(&p)); - assert!( - !printed.contains("171") && !printed.to_lowercase().contains("ab, ab") && !printed.contains("Orbit7"), - "{printed}" - ); + assert!(!printed.contains("171") && !printed.to_lowercase().contains("ab, ab") && !printed.contains("Orbit7"), "{printed}"); let v = two_factor_vault(); let printed = format!("{v:?} {:?}", v.factors()); for f in v.factors() { @@ -230,8 +207,7 @@ fn the_key_pair_item_type_round_trips() { k.set("private-key", "-----BEGIN PRIVATE KEY-----\n…", 5); v.add(k); let bytes = v.to_bytes().unwrap(); - let back = - open(&bytes, Unlock::Password(&pw("Orbit7-Fable3")), Unlock::Material(FactorKind::Device, &DEVICE)).unwrap(); + let back = open(&bytes, Unlock::Password(&pw("Orbit7-Fable3")), Unlock::Material(FactorKind::Device, &DEVICE)).unwrap(); let item = back.items().iter().find(|i| i.kind == ItemType::KeyPair).unwrap(); assert!(item.fields.iter().any(|f| f.name == "private-key" && f.secret)); } diff --git a/crates/custos-wasm/src/lib.rs b/crates/custos-wasm/src/lib.rs index db1e966..1480b7e 100644 --- a/crates/custos-wasm/src/lib.rs +++ b/crates/custos-wasm/src/lib.rs @@ -48,21 +48,11 @@ const PHONE_APPROVAL: &[u8] = b"custos phone approval v1"; #[derive(Deserialize)] #[serde(tag = "kind", rename_all = "snake_case", rename_all_fields = "camelCase")] enum FactorIn { - Password { - password: SecretString, - }, + Password { password: SecretString }, /// A factor service's or a device's answer, sealed to a key from `recipient` (`pub`) for `context`. - Sealed { - factor: FactorKind, - r#pub: String, - sealed: String, - context: String, - }, + Sealed { factor: FactorKind, r#pub: String, sealed: String, context: String }, /// Material the page holds itself: a passkey's PRF output (base64). - Material { - factor: FactorKind, - material: SecretString, - }, + Material { factor: FactorKind, material: SecretString }, } #[derive(Deserialize)] @@ -436,12 +426,8 @@ pub fn handle(request: &[u8]) -> Value { } Request::DomiPair { r#pub, id, label, device } => { let device_ok = data_encoding::BASE64URL_NOPAD.decode(device.as_bytes()).is_ok_and(|d| d.len() == 32); - let id_ok = !id.is_empty() - && id.len() <= 64 - && id.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'-' || b == b'_'); - let (Some(public), true, true) = (b64(&r#pub), device_ok, id_ok) else { - return err("A malformed request."); - }; + let id_ok = !id.is_empty() && id.len() <= 64 && id.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'-' || b == b'_'); + let (Some(public), true, true) = (b64(&r#pub), device_ok, id_ok) else { return err("A malformed request.") }; let label: String = label.trim().chars().filter(|c| !c.is_control()).take(40).collect(); let plain = json!({ "label": label, "device": device }).to_string(); match custos_seal::seal(&public, format!("domi-pair:{id}").as_bytes(), plain.as_bytes()) { diff --git a/crates/custos-wasm/tests/factors.rs b/crates/custos-wasm/tests/factors.rs index ba0fd3e..cf583e6 100644 --- a/crates/custos-wasm/tests/factors.rs +++ b/crates/custos-wasm/tests/factors.rs @@ -17,14 +17,8 @@ fn call(v: Value) -> Value { /// A version-2 vault: the master password and a TOTP factor with `totp` as its material. fn vault(totp: &[u8]) -> Vault { let pw = SecretString::from(PASSWORD); - let mut v = Vault::create_with( - Unlock::Password(&pw), - "Master password", - Unlock::Material(FactorKind::Totp, totp), - "TOTP", - FAST, - ) - .unwrap(); + let mut v = Vault::create_with(Unlock::Password(&pw), "Master password", Unlock::Material(FactorKind::Totp, totp), "TOTP", FAST) + .unwrap(); let mut login = Item::new(ItemType::WebLogin, "GitHub", 0); login.set("password", SECRET, 0); v.add(login); @@ -59,8 +53,7 @@ fn open_with_password_and_a_sealed_totp_release() { assert_eq!(me["match"], custos_seal::match_code(&public)); // What the factor service sends back after a valid code. let sealed = BASE64.encode(&custos_seal::seal(&public, context.as_bytes(), &material).unwrap()); - let sealed_in = - |ctx: &str| json!({ "kind": "sealed", "factor": "totp", "pub": me["pub"], "sealed": sealed, "context": ctx }); + let sealed_in = |ctx: &str| json!({ "kind": "sealed", "factor": "totp", "pub": me["pub"], "sealed": sealed, "context": ctx }); let password = json!({ "kind": "password", "password": PASSWORD }); // Only the right context opens it. @@ -98,9 +91,7 @@ fn a_phone_pairs_then_opens_and_approves() { // The PC waits with a one-time key; the phone seals its pairing to it. let pc = Recipient::generate(); let context = "pair:abc"; - let r = call( - json!({ "op": "phone_enrol", "pub": BASE64.encode(&pc.public()), "context": context, "label": "Phone", "prf": prf64 }), - ); + let r = call(json!({ "op": "phone_enrol", "pub": BASE64.encode(&pc.public()), "context": context, "label": "Phone", "prf": prf64 })); assert_eq!(r["ok"], true, "{r}"); let sealed = BASE64.decode(r["sealed"].as_str().unwrap().as_bytes()).unwrap(); assert_eq!(r["code"], custos_seal::sender_code(&sealed).unwrap(), "the PC checks the code the phone shows"); @@ -114,8 +105,7 @@ fn a_phone_pairs_then_opens_and_approves() { // The PC adds both factors. let passkey_id = v.add_factor(Unlock::Material(FactorKind::Passkey, &prf), "Phone passkey").unwrap(); - let approval_id = - v.add_factor_apart(Unlock::Material(FactorKind::Approval, &approval), "Phone", &[&passkey_id]).unwrap(); + let approval_id = v.add_factor_apart(Unlock::Material(FactorKind::Approval, &approval), "Phone", &[&passkey_id]).unwrap(); let file = b64(&v); // The phone opens it with its passkey and the master password. @@ -140,18 +130,13 @@ fn a_phone_pairs_then_opens_and_approves() { let req_pub = BASE64.encode(&requester.public()); assert_eq!(call(json!({ "op": "match_code", "pub": req_pub }))["match"], requester.match_code()); let ctx = format!("approval:{approval_id}:r1"); - let wrong_key = call( - json!({ "op": "phone_approve", "wrapped": wrapped, "prf": BASE64.encode(&[8u8; 32]), "pub": req_pub, "context": ctx }), - ); + let wrong_key = call(json!({ "op": "phone_approve", "wrapped": wrapped, "prf": BASE64.encode(&[8u8; 32]), "pub": req_pub, "context": ctx })); assert_eq!(wrong_key["ok"], false); let r = call(json!({ "op": "phone_approve", "wrapped": wrapped, "prf": prf64, "pub": req_pub, "context": ctx })); let sealed = BASE64.decode(r["sealed"].as_str().unwrap().as_bytes()).unwrap(); let m = requester.open(&sealed, ctx.as_bytes()).unwrap(); let pw = SecretString::from(PASSWORD); - assert!( - Vault::open_with(&v.to_bytes().unwrap(), Unlock::Password(&pw), Unlock::Material(FactorKind::Approval, &m)) - .is_ok() - ); + assert!(Vault::open_with(&v.to_bytes().unwrap(), Unlock::Password(&pw), Unlock::Material(FactorKind::Approval, &m)).is_ok()); } #[test] @@ -159,9 +144,7 @@ fn a_phone_pairs_as_a_domi_device() { // The PC waits with a one-time key; the phone seals its device public key and label to it. let pc = Recipient::generate(); let device = data_encoding::BASE64URL_NOPAD.encode(&[5u8; 32]); - let r = call( - json!({ "op": "domi_pair", "pub": BASE64.encode(&pc.public()), "id": "abc_-1", "label": " Pixel\n", "device": device }), - ); + let r = call(json!({ "op": "domi_pair", "pub": BASE64.encode(&pc.public()), "id": "abc_-1", "label": " Pixel\n", "device": device })); assert_eq!(r["ok"], true, "{r}"); let sealed = BASE64.decode(r["sealed"].as_str().unwrap().as_bytes()).unwrap(); assert_eq!(r["code"], custos_seal::sender_code(&sealed).unwrap()); @@ -170,16 +153,6 @@ fn a_phone_pairs_as_a_domi_device() { assert_eq!(plain, json!({ "label": "Pixel", "device": device })); // Malformed: a device key of the wrong length, an id that would change the context. let short = data_encoding::BASE64URL_NOPAD.encode(&[5u8; 31]); - assert_eq!( - call( - json!({ "op": "domi_pair", "pub": BASE64.encode(&pc.public()), "id": "abc", "label": "x", "device": short }) - )["ok"], - false - ); - assert_eq!( - call( - json!({ "op": "domi_pair", "pub": BASE64.encode(&pc.public()), "id": "a:b", "label": "x", "device": device }) - )["ok"], - false - ); + assert_eq!(call(json!({ "op": "domi_pair", "pub": BASE64.encode(&pc.public()), "id": "abc", "label": "x", "device": short }))["ok"], false); + assert_eq!(call(json!({ "op": "domi_pair", "pub": BASE64.encode(&pc.public()), "id": "a:b", "label": "x", "device": device }))["ok"], false); }