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 1 commit 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 |
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