fix(security): write config atomically with 0700 dir / 0600 file - #29
Merged
Conversation
write_config used create_dir_all + in-place fs::write, so umask left ~/.anyrouter at 0755 and config.yaml at 0644, and a crash mid-write truncated the file that holds api_key / management_key / relay_token. After creating the dir, set 0700 on unix. Serialize to config.yaml.tmp, chmod 0600, then rename over the destination (remove the tmp on rename failure). Signature of write_config is unchanged. Closes #19 Co-authored-by: Duyet Le <me@duyet.net> Co-authored-by: duyetbot <bot@duyet.net>
duyet
marked this pull request as ready for review
August 28, 2026 04:42
Contributor
anyr size and startupStartup is wall time for a cold Size budget:
Budget check: ok raw timings[
{
"asset": "anyr-darwin-arm64",
"kind": "native",
"path": "/Users/runner/work/cli/cli/anyr-darwin-arm64",
"bytes": 2803528,
"size": "2.7 MiB",
"version": "0.1.11 (built 2026-08-28 04:43:32)",
"target": "aarch64-apple-darwin",
"os": "macos-latest",
"startup_version": {
"n": 21,
"min_ms": 5.24,
"median_ms": 6.29,
"p95_ms": 10.06,
"mean_ms": 6.79
},
"startup_help": {
"n": 21,
"min_ms": 3.41,
"median_ms": 4.68,
"p95_ms": 5.7,
"mean_ms": 4.64
}
},
{
"asset": "anyr-darwin-x86_64",
"kind": "native",
"path": "/Users/runner/work/cli/cli/anyr-darwin-x86_64",
"bytes": 3014704,
"size": "2.9 MiB",
"version": "0.1.11 (built 2026-08-28 04:43:27)",
"target": "x86_64-apple-darwin",
"os": "macos-latest",
"startup_version": {
"n": 21,
"min_ms": 29.04,
"median_ms": 31.7,
"p95_ms": 36.05,
"mean_ms": 32.36
},
"startup_help": {
"n": 21,
"min_ms": 28.15,
"median_ms": 29.3,
"p95_ms": 30.22,
"mean_ms": 29.35
}
},
{
"asset": "anyr-linux-arm64",
"kind": "native",
"path": "/home/runner/work/cli/cli/anyr-linux-arm64",
"bytes": 2757672,
"size": "2.6 MiB",
"version": "0.1.11 (built 2026-08-28 04:42:51)",
"target": "aarch64-unknown-linux-gnu",
"os": "ubuntu-24.04-arm",
"startup_version": {
"n": 21,
"min_ms": 0.67,
"median_ms": 0.7,
"p95_ms": 0.81,
"mean_ms": 0.72
},
"startup_help": {
"n": 21,
"min_ms": 0.69,
"median_ms": 0.72,
"p95_ms": 0.79,
"mean_ms": 0.73
}
},
{
"asset": "anyr-linux-x86_64",
"kind": "native",
"path": "/home/runner/work/cli/cli/anyr-linux-x86_64",
"bytes": 3112584,
"size": "3.0 MiB",
"version": "0.1.11 (built 2026-08-28 04:42:51)",
"target": "x86_64-unknown-linux-gnu",
"os": "ubuntu-latest",
"startup_version": {
"n": 21,
"min_ms": 0.99,
"median_ms": 1.02,
"p95_ms": 1.07,
"mean_ms": 1.02
},
"startup_help": {
"n": 21,
"min_ms": 0.98,
"median_ms": 1.01,
"p95_ms": 1.11,
"mean_ms": 1.02
}
},
{
"asset": "anyr-windows-x86_64.exe",
"kind": "native",
"path": "D:\\a\\cli\\cli\\anyr-windows-x86_64.exe",
"bytes": 2577920,
"size": "2.5 MiB",
"version": "0.1.11 (built 2026-08-28T04:44:13Z)",
"target": "x86_64-pc-windows-msvc",
"os": "windows-latest",
"startup_version": {
"n": 21,
"min_ms": 8.98,
"median_ms": 9.19,
"p95_ms": 9.74,
"mean_ms": 9.27
},
"startup_help": {
"n": 21,
"min_ms": 8.96,
"median_ms": 9.24,
"p95_ms": 10.67,
"mean_ms": 9.44
}
},
{
"asset": "anyr.wasm",
"kind": "wasm",
"path": "/home/runner/work/cli/cli/target/wasm-pkg/anyr_cli_bg.wasm",
"bytes": 179312,
"size": "175.1 KiB",
"version": "wasm",
"target": "wasm32-unknown-unknown",
"os": ""
}
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #19 (Plan 004).
write_confignow creates~/.anyrouterwith mode 0700 (unix), writesconfig.yaml.tmp, chmods it 0600, then renames over the destination so a crash cannot truncate the live file that holdsapi_key/management_key/relay_token.Signature of
write_config(config: &Config, path: &Path) -> Result<(), String>is unchanged. No keyring. Windows ACL hardening is out of scope (#[cfg(unix)]only).Drift check
git diff --stat 61ee3c7..HEAD -- src/config.rs src/key.rsat plan time:src/config.rsgained the extra-field serialize round-trip only. Livewrite_configmatched the issue excerpt, so this proceeded.Other
fs::write(...config.yaml...)sites are test fixtures insrc/upgrade.rs(auto_update_config_false_disables,resolve_channel_reads_config_then_env). Production credential writes still funnel throughwrite_config. Those fixtures were not changed.Path::new("/x/config.yaml").with_extension("yaml.tmp")yields/x/config.yaml.tmp(notconfig.yaml.yaml.tmp).Dir chmod uses
let mut perms(the plan snippet omittedmut;PermissionsExt::set_modeneeds it, matching the file-chmod block andupgrade.rs).Reviewer entry
src/config.rswrite_config— dir 0700, temp + rename, file 0600Checklist
Verification
grep -n set_mode src/config.rs— exactly two sites (dir0o700, file0o600)pub fn write_config(config: &Config, path: &Path) -> Result<(), String>unchangedsrc/config.rsmodifiedcargo test --locked --lib config— pass (incl. the two new tests)cargo test --locked --all-targets— 156 lib + 52 cli + 7 release_lockcargo clippy --locked --all-targets— no new warnings insrc/config.rs