Skip to content

fix(auth)!: refuse credential files that others can reach or that are symlinks - #147

Open
jpage-godaddy wants to merge 3 commits into
credential-enumerationfrom
file-storage-hardening
Open

jpage-godaddy wants to merge 3 commits into
credential-enumerationfrom
file-storage-hardening

Conversation

@jpage-godaddy

@jpage-godaddy jpage-godaddy commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

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 to main as each merges.

  • Fix DEVEX-1436: FileStorage holds credentials in plaintext and is used when file is selected or when auto finds no reachable keychain, which is where nothing else protects them. It read whatever was at the path and trusted it.
  • Reads (Unix): the file is opened without following a symlink, and non-blocking so a FIFO planted at the path cannot hang the CLI. It is used only if it is a regular file owned by the effective user with no group or other permission bits. Anything else reads as no credential, with a warning in the log, so the user logs in again and the login replaces it.
  • Writes: FileStorage::save now uses fs::write_private_file_atomic (pub(crate)): 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. Only that directory is changed, never the config base or $HOME. The directory is opened with O_NOFOLLOW | O_DIRECTORY and restricted through that handle, so a symlink planted at <app>/credentials is an error instead of having its target's mode changed and the credential written there; ancestors may still be links, since a symlinked ~/.config is 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 with ENOTDIR first (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 no unsafe is needed. It is already in the dependency tree through tempfile and crossterm; Cargo.lock gains one line.
  • Windows is deliberately unchanged. The file lives under %APPDATA%, which inherits the user profile's access control. An explicit ACL would need roughly 240 lines of unsafe Win32 FFI (and relaxing the workspace's unsafe_code = deny) to cover only a XDG_CONFIG_HOME pointed at a shared location, and std::fs::rename already replaces an existing file. docs/auth.md says so. The ticket's Windows ACL and atomic-replace items are dropped, not deferred.
  • Path and JSON format are unchanged.

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 600 on the file restores a stored login.

Test plan

  • cargo fmt --all --check
  • cargo clippy --all-targets -- -D warnings, and again with --features pkce-auth and --features id-token-verify
  • cargo test --all-targets and cargo test --doc with each feature set
  • RUSTDOCFLAGS='-D warnings' cargo doc --no-deps, and again with each feature set
  • cli-engine/scripts/check-module-size.sh

All 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; a 0300 directory 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, no O_NONBLOCK, mode not checked, owner not checked, save using the general writer, an existing directory not restricted, is_file not 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.rs wrote a world-readable credential file and now writes 0600 like 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

  • auto mode is unaffected when a keychain is reachable; it only reads the file when none is.
  • The write still goes through the path after the directory check, so a link swapped in between the check and the write is not closed by this PR; that would need a directory-relative write (openat), which is left out.
  • A read that is refused returns None like an absent file, so callers need no new error path, and the warning names the path and reason.

… 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>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

Comment thread cli-engine/src/fs.rs Resolved
Comment thread cli-engine/src/auth/storage.rs Resolved
…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>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

Comment thread cli-engine/src/fs.rs Resolved
… 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>
@jpage-godaddy
jpage-godaddy requested a balanced review from Copilot November 25, 2026 23:35

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The review identifies no active issues with the Unix credential-storage hardening changes.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants