Repository navigation
fix(auth)!: refuse credential files that others can reach or that are symlinks - #147
Open
jpage-godaddy wants to merge 3 commits into
Open
jpage-godaddy wants to merge 3 commits into
jpage-godaddy wants to merge 3 commits into
Conversation
… symlinks `FileStorage` held credentials in plaintext and trusted whatever it found at the path. On Unix it now opens the file without following a symlink (and without blocking on a FIFO) and reads it only when it is a regular file owned by the user running the CLI with no group or other permission bits. Anything else is treated as no credential, with a warning, so the user logs in again. Writes go through `fs::write_private_file_atomic`: a private temporary file renamed over the target, so the file is always `0600`, and the credentials directory is set to `0700` on every write, including one that already existed. A failed write no longer leaves a partial secret in a temporary file. `write_string_atomic` is unchanged for its other callers. `rustix` supplies the effective user id and the open flags, so no `unsafe` is needed. Windows is unchanged: the file lives under the user profile, whose access control already applies. BREAKING CHANGE: a credential file that is group- or world-accessible, owned by another user, or a symlink was read before and is now ignored until the next login rewrites it. `chmod 600` on the file restores a stored login. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
jpage-godaddy
requested review from
axburgess-godaddy,
mguerrero3-godaddy,
qcai-godaddy,
rts1-godaddy and
runruh-godaddy
as code owners
November 25, 2026 23:18
There was a problem hiding this comment.
🟡 Changes recommended
Credential-directory symlinks can cause login to change unrelated directories’ permissions, while the test guard releases its mutex before restoring XDG_CONFIG_HOME, creating a race.
2 open issues
What changed in this PR
Hardens plaintext credential storage on Unix, building on the credential-enumeration work. Credential paths and formats remain unchanged; Windows behavior is unchanged.
Changes:
- Rejects symlinks, non-regular files, foreign-owned files, and files with group or other permissions.
- Adds private atomic writes, credential-directory permission tightening, and cleanup after failed temporary writes.
- Adds Unix regression tests and documents the breaking read-policy change.
| File | Description |
|---|---|
cli-engine/tests/credential_store_config.rs |
Makes the credential fixture private on Unix. |
cli-engine/src/fs.rs |
Adds the private atomic writer and failed-write cleanup. |
cli-engine/src/auth/storage.rs |
Validates credential files, uses private writes, and adds hardening tests. |
cli-engine/src/auth/pkce/mod.rs |
Documents Unix credential-file rejection behavior. |
cli-engine/docs/auth.md |
Explains storage protections and Windows limitations. |
cli-engine/Cargo.toml |
Adds Unix-only rustix support for safe system calls. |
Cargo.lock |
Records the direct rustix dependency. |
Review effort: Balanced
…est env under its lock Restricting an existing credentials directory used `is_dir` and `set_permissions`, both of which follow symlinks, so a link planted at `<app>/credentials` made a save change the mode of whatever it pointed at to `0700` and write the credential there. The directory is now opened with `O_NOFOLLOW | O_DIRECTORY` and restricted through that handle; a link is an error and its target is left alone. Ancestors may still be links, since a symlinked `~/.config` is a common arrangement. The hardening tests' fixture dropped its mutex guard before restoring `XDG_CONFIG_HOME`, because fields drop in declaration order, so another test could set its own value just before the restore overwrote it. The environment guard is now declared first. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
🟡 Changes recommended
Opening the credentials directory with RDONLY fails for caller-owned directories with mode 0300, regressing previously successful writes and preventing permission repair to 0700.
1 open issue
2 resolved since last review
Review effort: Balanced
… read Opening the credentials directory with `O_RDONLY | O_NOFOLLOW | O_DIRECTORY` needs read permission, so a directory at mode `0300`, which the earlier writer could save into, made a save fail and was never repaired. `EACCES` on that open now falls back to restricting the directory by path. A symlink still fails the open with `ENOTDIR` before permissions are consulted, so the fallback does not follow one. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This branch has not been deployed
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
Stacked on #146 (
credential-enumeration); this PR targets that branch so the diff shows only the file-store hardening. The stack is #141, #143, #145, #146, then this. Retarget tomainas each merges.FileStorageholds credentials in plaintext and is used whenfileis selected or whenautofinds no reachable keychain, which is where nothing else protects them. It read whatever was at the path and trusted it.FileStorage::savenow usesfs::write_private_file_atomic(pub(crate)): a private temporary file renamed over the target, so the file is always0600, and the credentials directory is set to0700on every write, including one that already existed. Only that directory is changed, never the config base or$HOME. The directory is opened withO_NOFOLLOW | O_DIRECTORYand restricted through that handle, so a symlink planted at<app>/credentialsis an error instead of having its target's mode changed and the credential written there; ancestors may still be links, since a symlinked~/.configis a common arrangement. A directory the owner can write but not read (0300) cannot be opened that way, so it is restricted by path instead; a symlink never reaches that branch, because opening one with those flags fails withENOTDIRfirst (refined during review).write_string_atomic, used by the config file and shell completion, is unchanged. A failed temporary write no longer leaves a partial secret behind.rustix(fs,process) supplies the effective user id and the open flags, so nounsafeis needed. It is already in the dependency tree throughtempfileandcrossterm;Cargo.lockgains one line.%APPDATA%, which inherits the user profile's access control. An explicit ACL would need roughly 240 lines ofunsafeWin32 FFI (and relaxing the workspace'sunsafe_code = deny) to cover only aXDG_CONFIG_HOMEpointed at a shared location, andstd::fs::renamealready replaces an existing file.docs/auth.mdsays so. The ticket's Windows ACL and atomic-replace items are dropped, not deferred.Breaking change
A credential file that is group- or world-accessible, owned by another user, or a symlink used to be read and is now ignored until the next login rewrites it. The commit carries a
BREAKING CHANGE:footer for the changelog.chmod 600on the file restores a stored login.Test plan
cargo fmt --all --checkcargo clippy --all-targets -- -D warnings, and again with--features pkce-authand--features id-token-verifycargo test --all-targetsandcargo test --docwith each feature setRUSTDOCFLAGS='-D warnings' cargo doc --no-deps, and again with each feature setcli-engine/scripts/check-module-size.shAll pass locally. 11 new Unix tests cover: a private file is read (including
0400); group- and other-accessible modes are refused (seven modes); a symlink is not followed even when its target is private; a FIFO is refused without blocking (the test fails after 3 seconds instead of hanging); a file owned by another user is refused; the pure ownership and mode rules; saving repairs a loose file and a loose directory; a symlinked credentials directory is refused and its target untouched; a0300directory is still saved into and repaired; saving leaves no temporary file; and the general writer leaves an existing directory alone while the private one tightens it.To check the tests can fail, I removed each protection in turn: no
O_NOFOLLOW, noO_NONBLOCK, mode not checked, owner not checked,saveusing the general writer, an existing directory not restricted,is_filenot checked, the owner rule fed the file's own uid, the directory opened following symlinks, and no fallback for an unreadable directory. Each makes at least one test fail. Two first survived or hung, which showed gaps: the owner check had no real test because a test cannot create a file owned by another user (the effective user is now injectable so one can simulate it), and a FIFO test with a blocking open hung the whole run, which is why it now has a deadline.The existing fixture in
tests/credential_store_config.rswrote a world-readable credential file and now writes0600like a real one.Manual verification
Not run against a real keychain, and not run on Windows or macOS (the Unix paths are shared, but only Linux ran). The foreign-owner case is simulated by injecting the effective user rather than by a second account.
Notes for reviewers
automode is unaffected when a keychain is reachable; it only reads the file when none is.openat), which is left out.Nonelike an absent file, so callers need no new error path, and the warning names the path and reason.