fix(cli): enforce 0600 on an existing private validator key file - #288
fix(cli): enforce 0600 on an existing private validator key file#2880xrlawrence wants to merge 2 commits into
Conversation
`OpenOptions::mode(0o600)` applies only when the file is created. If the key path already exists with looser permissions, the mode is silently ignored and the private validator key is written into a world-readable file. This is reachable: `arc init --overwrite` writes a fresh key over an existing path (`cmd/init.rs:53`), so a key file restored from a backup or left behind by an older version keeps its original mode. Call `set_permissions(0o600)` after opening so an existing file is tightened before the key is written to it. Adds two tests: one covering the existing create path, and one that pre-creates a 0644 file and asserts it is tightened to 0600. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
|
Verified the claims independently against
One residual gap worth noting (fine as a follow-up rather than blocking this): Two minor observations, take or leave:
The fix is minimal, correctly ordered, well-tested, and matches the crate's existing |
|
nice catch 👍 thanks |
CI clippy failed with `used unwrap() on a Result value` in the new tests. The workspace denies `clippy::unwrap_used`, and `clippy.toml` relaxes it with `allow-unwrap-in-tests = true`. That relaxation only applies to items clippy recognises as test code, which requires a plain `#[cfg(test)]` module. The new tests were gated `#[cfg(all(test, unix))]`, so clippy did not treat them as tests and the deny applied. Switch to `#[cfg(test)] mod tests` with `#[cfg(unix)]` on the individual helper and tests, matching the convention already used in cmd/init.rs and cmd/start.rs. No change to what is tested. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Problem
OpenOptions::mode(0o600)incrates/malachite-cli/src/file.rsapplies only when the file is created. If the key path already exists with looser permissions, the mode is silently ignored and the private validator key is written into a world-readable file.This is reachable rather than theoretical.
cmd/init.rs:53guards with:so
init --overwriteagainst an existing key file (restored from a backup, or left by an older version) writes a fresh private key while keeping the file's original mode.Fix
Call
set_permissions(0o600)after opening, so an existing file is tightened before the key is written to it. Creation behaviour is unchanged.Tests
Adds two tests to
file.rs:save_priv_validator_key_creates_file_with_0600covers the existing create pathsave_priv_validator_key_tightens_existing_loose_permissionspre-creates a0644file and asserts it becomes0600The second fails without the fix and passes with it.
Existing
0600assertions incmd/init.rsandcmd/start.rsonly cover newly created files, which is why this gap was not caught.Notes
Unix-only, matching the existing
#[cfg(unix)]structure. The parent directory is still created with the defaultcreate_dir_allmode; tightening that to0700felt out of scope here since the directory holds non-secret config too, but happy to add it if you would prefer.🤖 Generated with Claude Code