From 77cfe34da8b96f1996e48eb9b3879394b59f3d39 Mon Sep 17 00:00:00 2001 From: koba-e964 <3303362+koba-e964@users.noreply.github.com> Date: Sun, 27 Sep 2026 22:05:13 +0900 Subject: [PATCH] fix(filter): allow rebase without committed manifest --- .pre-commit-config.yaml | 10 ++++++++++ README.md | 7 +++++++ docs/data-formats.md | 11 +++++++++++ src/key_manifest.rs | 24 ++++++++++++++++-------- src/main.rs | 22 ++++++++++++++++------ tests/filter_roundtrip.rs | 9 +++++++-- 6 files changed, 67 insertions(+), 16 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 4d04aba..b02a96f 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -7,3 +7,13 @@ repos: language: system types: [rust] pass_filenames: false + - id: cargo-test + name: cargo test + entry: cargo test + language: system + pass_filenames: false + - id: cargo-test + name: cargo test + entry: cargo test + language: system + pass_filenames: false diff --git a/README.md b/README.md index 09c0d56..92c680e 100644 --- a/README.md +++ b/README.md @@ -192,6 +192,13 @@ importing or deriving the missing key, re-smudge a file with: git restore --source=HEAD --worktree -- secrets/secret.txt ``` +If no manifest exists yet—for example while `git rebase --autosquash` applies a +fixup that adds `.gitattributes` and an encrypted file—smudge also preserves the +encrypted bytes and succeeds with a warning. A present manifest that omits the +blob's key id is still an error; this keeps the manifest as the decryption +authorization boundary while allowing historical commits to be replayed in the +right order. + Back keys up and transfer them securely. Losing or deleting the only copy of a key makes encrypted blobs that use it unrecoverable. diff --git a/docs/data-formats.md b/docs/data-formats.md index 8953be0..3be4129 100644 --- a/docs/data-formats.md +++ b/docs/data-formats.md @@ -57,6 +57,17 @@ can be created with `git-zcrypt init-manifest --path `. Existing encrypted blobs carry the key-id metadata needed for `git-zcrypt register ...` to populate the selected manifest when the matching local key alias is available. +If no manifest exists while Git is reconstructing a historical tree, smudge +passes the validated encrypted blob through unchanged and exits successfully. +This is important for `git rebase --autosquash`: a fixup commit can introduce +`.gitattributes` and an encrypted file before the commit that creates the +manifest is applied. Passing ciphertext preserves the secret and lets rebase +finish; a later checkout after the manifest is present can decrypt it normally. +A manifest that exists but does not declare the blob key remains an error, so +the manifest continues to authorize which keys may decrypt a path. This +distinction keeps the rebase recovery narrow instead of making all filter +failures non-fatal. + ## Local Key File Format Each local key file is a versioned binary wrapper around raw key material: diff --git a/src/key_manifest.rs b/src/key_manifest.rs index dd220ca..5bf8847 100644 --- a/src/key_manifest.rs +++ b/src/key_manifest.rs @@ -24,6 +24,13 @@ pub struct ResolvedWorktreeFile { pub repo_path: PathBuf, } +#[derive(Debug, PartialEq, Eq)] +pub enum Authorization { + Allowed, + MissingManifest, + KeyNotDeclared, +} + pub fn init_manifest(path: &Path) -> Result { let root = worktree_root()?; let dir = resolve_dir(&root, path)?; @@ -47,17 +54,18 @@ pub fn add_key_for_path(path: &Path, key_id: &str, key_name: &str) -> Result Result { +pub fn authorize_key_for_path(path: &Path, key_id: &str) -> Result { key_store::validate_key_id(key_id)?; let root = worktree_root()?; - let manifest = find_manifest_path(&root, path)?.with_context(|| { - format!( - "no {MANIFEST_FILE} found for {}; run git-zcrypt init-manifest", - path.display() - ) - })?; + let Some(manifest) = find_manifest_path(&root, path)? else { + return Ok(Authorization::MissingManifest); + }; let keys = read_manifest(&manifest)?; - Ok(keys.contains_key(key_id)) + if keys.contains_key(key_id) { + Ok(Authorization::Allowed) + } else { + Ok(Authorization::KeyNotDeclared) + } } pub fn resolve_worktree_file(path: &Path) -> Result { diff --git a/src/main.rs b/src/main.rs index 0a4e6c1..8a7fd33 100644 --- a/src/main.rs +++ b/src/main.rs @@ -87,12 +87,22 @@ fn smudge(path: &Path) -> Result<()> { let store = key_store::KeyStore::discover()?; let input = read_stdin()?; let encrypted = blob::decode(&input)?; - if !key_manifest::key_allowed_for_path(path, &encrypted.key_id)? { - crate::bail!( - "key {} is not declared for {}", - encrypted.key_id, - path.display() - ); + match key_manifest::authorize_key_for_path(path, &encrypted.key_id)? { + key_manifest::Authorization::Allowed => {} + key_manifest::Authorization::KeyNotDeclared => { + crate::bail!( + "key {} is not declared for {}", + encrypted.key_id, + path.display() + ); + } + key_manifest::Authorization::MissingManifest => { + eprintln!( + "warning: no git-zcrypt-keys.json found for {}; leaving encrypted bytes unchanged", + path.display() + ); + return write_stdout(&input); + } } let Some(key) = store.try_read_key_by_id(&encrypted.key_id)? else { eprintln!( diff --git a/tests/filter_roundtrip.rs b/tests/filter_roundtrip.rs index 6b0986b..74ca9dd 100644 --- a/tests/filter_roundtrip.rs +++ b/tests/filter_roundtrip.rs @@ -600,7 +600,7 @@ fn clone_without_local_key_checks_out_encrypted_blob_and_restore_resmudges() { } #[test] -fn smudge_fails_when_manifest_is_missing() { +fn smudge_passes_through_when_manifest_is_missing() { let repo = init_repo(); let clean = filter( &repo, @@ -619,7 +619,12 @@ fn smudge_fails_when_manifest_is_missing() { &["smudge", "--path", "secrets/secret.txt"], &clean.stdout, ); - assert!(!smudge.status.success()); + assert!( + smudge.status.success(), + "{}", + String::from_utf8_lossy(&smudge.stderr) + ); + assert_eq!(smudge.stdout, clean.stdout); assert!(String::from_utf8_lossy(&smudge.stderr).contains("no git-zcrypt-keys.json found")); }