Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
11 changes: 11 additions & 0 deletions docs/data-formats.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ can be created with `git-zcrypt init-manifest --path <dir>`. Existing encrypted
blobs carry the key-id metadata needed for `git-zcrypt register <file>...` 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:
Expand Down
24 changes: 16 additions & 8 deletions src/key_manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf> {
let root = worktree_root()?;
let dir = resolve_dir(&root, path)?;
Expand All @@ -47,17 +54,18 @@ pub fn add_key_for_path(path: &Path, key_id: &str, key_name: &str) -> Result<Pat
Ok(manifest)
}

pub fn key_allowed_for_path(path: &Path, key_id: &str) -> Result<bool> {
pub fn authorize_key_for_path(path: &Path, key_id: &str) -> Result<Authorization> {
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<ResolvedWorktreeFile> {
Expand Down
22 changes: 16 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down
9 changes: 7 additions & 2 deletions tests/filter_roundtrip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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"));
}

Expand Down
Loading