-
Notifications
You must be signed in to change notification settings - Fork 52.4k
security(photon): create auth.json temp file with 0o600 atomically #60427
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
solyanviktor-star
wants to merge
2
commits into
NousResearch:main
from
solyanviktor-star:fix/photon-auth-token-perms
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,9 +3,11 @@ | |
|
|
||
| import json | ||
| import os | ||
| import stat | ||
| from base64 import b64encode | ||
| from pathlib import Path | ||
| from typing import Any, Dict | ||
| from unittest import mock | ||
|
|
||
| import pytest | ||
|
|
||
|
|
@@ -71,6 +73,92 @@ def test_store_and_load_photon_token(tmp_hermes_home: Path) -> None: | |
| assert auth_json["credential_pool"]["photon"][0]["access_token"] == "abc123def456" | ||
|
|
||
|
|
||
| @pytest.mark.skipif(os.name != "posix", reason="POSIX mode bits only") | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This asserts only the final target mode, which the removed open→write→chmod implementation also produced. Please additionally spy on |
||
| def test_save_auth_never_world_readable(tmp_hermes_home: Path) -> None: | ||
| """auth.json must be created 0o600 — no window at process umask.""" | ||
| photon_auth.store_photon_token("secret-token") | ||
| mode = (tmp_hermes_home / "auth.json").stat().st_mode & 0o777 | ||
| assert mode == 0o600 | ||
|
|
||
|
|
||
| def test_save_auth_leaves_no_temp_files(tmp_hermes_home: Path) -> None: | ||
| photon_auth.store_photon_token("secret-token") | ||
| leftovers = [p.name for p in tmp_hermes_home.iterdir() if p.name != "auth.json"] | ||
| assert leftovers == [] | ||
|
|
||
|
|
||
| def test_save_auth_uses_os_open_with_0o600_mode(tmp_hermes_home: Path) -> None: | ||
| """Regression: the writer must call ``os.open`` with O_CREAT | O_EXCL and | ||
| an explicit 0o600 mode so the temp file is created restricted atomically. | ||
| The final-mode check alone would also pass under the old open() → write → | ||
| chmod() writer, so this spy protects the atomic-create guarantee itself.""" | ||
| observed_opens: list[tuple[str, int, int]] = [] | ||
| real_os_open = os.open | ||
|
|
||
| def spying_os_open(path, flags, mode=0o777, *args, **kwargs): | ||
| observed_opens.append((str(path), flags, mode)) | ||
| return real_os_open(path, flags, mode, *args, **kwargs) | ||
|
|
||
| with mock.patch.object(os, "open", spying_os_open): | ||
| photon_auth.store_photon_token("secret-token") | ||
|
|
||
| tmp_opens = [ | ||
| (p, fl, m) for (p, fl, m) in observed_opens if "auth.json.tmp" in p | ||
| ] | ||
| assert tmp_opens, ( | ||
| f"os.open was never called for the auth.json temp file; " | ||
| f"observed={observed_opens!r}" | ||
| ) | ||
| for path, flags, mode in tmp_opens: | ||
| assert flags & os.O_CREAT, f"temp open missing O_CREAT: path={path}" | ||
| assert flags & os.O_EXCL, ( | ||
| f"temp open missing O_EXCL — TOCTOU-safe pattern regressed: " | ||
| f"path={path}, flags={flags}" | ||
| ) | ||
| expected = stat.S_IRUSR | stat.S_IWUSR | ||
| assert mode == expected, ( | ||
| f"temp open mode 0o{mode:o} != 0o{expected:o} — " | ||
| f"umask would apply and potentially expose tokens" | ||
| ) | ||
|
|
||
|
|
||
| def test_save_auth_closes_raw_fd_when_fdopen_fails(tmp_hermes_home: Path) -> None: | ||
| """If ``os.fdopen`` raises before taking ownership of the raw descriptor, | ||
| the writer must close the fd itself (and still remove the temp file).""" | ||
| opened_fds: list[int] = [] | ||
| closed_fds: list[int] = [] | ||
| real_os_open = os.open | ||
| real_os_close = os.close | ||
|
|
||
| def spying_os_open(path, flags, mode=0o777, *args, **kwargs): | ||
| fd = real_os_open(path, flags, mode, *args, **kwargs) | ||
| if "auth.json.tmp" in str(path): | ||
| opened_fds.append(fd) | ||
| return fd | ||
|
|
||
| def spying_os_close(fd): | ||
| closed_fds.append(fd) | ||
| return real_os_close(fd) | ||
|
|
||
| def failing_fdopen(*args, **kwargs): | ||
| raise MemoryError("forced fdopen failure") | ||
|
|
||
| with mock.patch.object(os, "open", spying_os_open), \ | ||
| mock.patch.object(os, "close", spying_os_close), \ | ||
| mock.patch.object(os, "fdopen", failing_fdopen): | ||
| with pytest.raises(MemoryError): | ||
| photon_auth.store_photon_token("secret-token") | ||
|
|
||
| assert opened_fds, "os.open was never called for the auth.json temp file" | ||
| for fd in opened_fds: | ||
| assert fd in closed_fds, ( | ||
| f"raw fd {fd} leaked after forced os.fdopen failure; " | ||
| f"closed={closed_fds!r}" | ||
| ) | ||
| leftovers = [p.name for p in tmp_hermes_home.iterdir() if p.name != "auth.json"] | ||
| assert leftovers == [], f"temp file leaked after fdopen failure: {leftovers}" | ||
|
|
||
|
|
||
| def test_store_project_credentials_round_trip( | ||
| tmp_hermes_home: Path, monkeypatch: pytest.MonkeyPatch, | ||
| ) -> None: | ||
|
|
||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
os.fdopen()raises before it assumes ownership offd, this handler only unlinkstmp; the raw descriptor remains open. Wrapfdopenso that failure closesfdbefore re-raising, and add the corresponding regression test.