Skip to content

fix(login): atomically replace file-backed credentials - #1

Open
metacircu1ar wants to merge 1 commit into
mainfrom
fix/atomic-auth-file-write
Open

metacircu1ar wants to merge 1 commit into
mainfrom
fix/atomic-auth-file-write

Conversation

@metacircu1ar

Copy link
Copy Markdown
Owner

When two Codex processes share file-backed credentials, a save currently truncates auth.json before writing its replacement. A process reading during that write can receive empty, partial, or mixed JSON and fail to load credentials. This race is independent of refresh-token reuse grace periods.

Reproduced failure

The regression test opens the old credentials file and reads its first half, saves a shorter replacement, then finishes the original read. On upstream base 8f21b7fffbefe77529325fc86c95289371841c6c, it fails deterministically with EOF while parsing a string. It uses temporary directories and synthetic API keys; no real login or token refresh is involved.

Fix

  • Serialize the complete credentials first and reuse codex_utils_path::write_atomically to write a temporary file in the destination directory and atomically replace the published file. Existing readers retain the old complete file; new readers see a complete replacement.
  • Update that helper to create files with standard OpenOptions inside tempfile::Builder::make_in and publish with std::fs::rename. Rust 1.95 can use POSIX rename semantics on Windows when classic replacement is denied, supporting existing open readers. tempfile::persist uses classic MoveFileExW without this fallback. Standard creation also avoids retaining the Windows temporary-file attribute. Cleanup stays enabled until publication succeeds.
  • Resolve stable auth.json symlink chains before replacement, including relative links and missing targets. Reject unresolved/cyclic chains instead of overwriting the symlink.
  • Use the helper's private temporary-file permissions on Unix (0600, including when replacing an existing permissive file).
  • Cover partial reads, Unix permissions, symlink preservation, cycle rejection, and temporary-file cleanup after a failed publication. Add direct shared-helper tests for open readers, failed publication, and Windows file attributes.

Validation

Tested on macOS arm64 with Rust 1.95.0, using release builds throughout. All post-fix validation applies to this patch atop 8f21b7fffbefe77529325fc86c95289371841c6c; a merge with later fork-main changes has not been tested.

  • Before the fix: cargo +1.95.0 test --release --locked -p codex-login --lib file_storage_save_preserves_an_in_progress_read -- --nocapture fails with the expected JSON parse error.
  • After the fix: cargo +1.95.0 test --release --locked -p codex-login -p codex-utils-path passes all 206 login unit tests, 42 login integration tests, and 8 path-utility tests, including the new regressions.
  • cargo +1.95.0 clippy --release --locked -p codex-login -p codex-utils-path --all-targets -- -D warnings passes.
  • cargo +1.95.0 fmt -p codex-login -p codex-utils-path --check and git diff --check pass.
  • cargo +1.95.0 check --release --locked -p codex-utils-path --all-targets --target x86_64-pc-windows-gnu passes. This checks Windows compilation of the helper and tests; it does not execute them on Windows.

Scope

This fixes credential-file publication, not cross-process token refresh serialization or stale-but-valid credentials overwriting newer credentials. It adds no power-loss durability guarantee. Atomic replacement creates a new file: hard-link aliases and custom inode metadata/ACLs are not preserved. On Windows, replacement may still fail when permissions, handle sharing, or filesystem support disallow it; there is no delete-and-recreate fallback. Windows execution has not been validated locally.

This pull request is intentionally opened against the author's fork. Upstream currently does not accept external pull requests.

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.

1 participant