fix(google_oauth): close TOCTOU window when saving credentials#16718
Closed
CharlieKerfoot wants to merge 1 commit into
Closed
fix(google_oauth): close TOCTOU window when saving credentials#16718CharlieKerfoot wants to merge 1 commit into
CharlieKerfoot wants to merge 1 commit into
Conversation
Collaborator
Contributor
|
Salvaged via #19673 onto current main with conflict resolution (kept newer |
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.
Problem
save_credentials created the temp file via
open(tmp, "w")and only chmodedd it to 0o600 afterward. Between the create and the chmod, the file existed on disk at the process umask (commonly 0o644 aka publicly readable). On a multi-user host or a container with a permissive umask, another local user could read the Google OAuth refresh and access tokens during the short write window. The lock file at line 181 already opens withos.open(..., 0o600), so it makes sense for the credentials file to too.Fix
open(tmp, "w")+ lateros.chmodwithos.open(tmp, O_WRONLY|O_CREAT|O_EXCL, 0o600)wrapped inos.fdopen. The file never exists at any other mode.os.chmod(path.parent, 0o700)after mkdir, so sibling users can't traverse into the creds dir. Wrapped in try/except for Windows where POSIX modes aren't enforced (no-op).Tests
uv run pytest tests/agent/test_gemini_cloudcode.py85/85 tests passedhermes auth login google, verify ~/.hermes/google_oauth_credentials.json is mode 0600 and the parent dir is 0700