Agent Zero AI framework
Reject shared OAuth auth file aliases
Cooper Gambled36228af8272
diff --git a/plugins/_oauth/helpers/codex.py b/plugins/_oauth/helpers/codex.pyindex fe2447629..740b5d180 100644--- a/plugins/_oauth/helpers/codex.py+++ b/plugins/_oauth/helpers/codex.py@@ -1093,11 +1093,14 @@ def _write_auth_file_in_place(path: Path, data: dict[str, Any]) -> None:def _validate_private_auth_path(path: Path) -> Path:resolved_path = path.expanduser().resolve(strict=False)- if _path_key(resolved_path) in {_path_key(candidate) for candidate in _known_codex_auth_paths()}:- raise RuntimeError(- "Agent Zero OAuth credentials must use an Agent Zero-owned auth file. "- "Choose a private auth_file_path or leave it empty for the default private store."- )+ for candidate in _known_codex_auth_paths():+ if _path_key(resolved_path) == _path_key(candidate) or _same_existing_file(+ resolved_path, candidate+ ):+ raise RuntimeError(+ "Agent Zero OAuth credentials must use an Agent Zero-owned auth file. "+ "Choose a private auth_file_path or leave it empty for the default private store."+ )return resolved_path@@ -1117,6 +1120,13 @@ def _path_key(path: Path) -> str:return os.path.normcase(str(path.expanduser().resolve(strict=False)))+def _same_existing_file(path: Path, candidate: Path) -> bool:+ try:+ return path.samefile(candidate)+ except OSError:+ return False++def _auth_lock_path(path: Path) -> Path:digest = hashlib.sha256(_path_key(path).encode("utf-8")).hexdigest()return Path(files.get_abs_path("usr", "plugins", "_oauth", "codex", "locks", f"{digest}.lock"))diff --git a/tests/test_oauth_codex.py b/tests/test_oauth_codex.pyindex e8360e1b1..4d0683daf 100644--- a/tests/test_oauth_codex.py+++ b/tests/test_oauth_codex.py@@ -267,6 +267,19 @@ def test_explicit_codex_cli_auth_path_is_rejected(tmp_path, monkeypatch):codex.resolve_auth_write_path()+def test_explicit_codex_cli_auth_hard_link_is_rejected(tmp_path, monkeypatch):+ shared_auth = tmp_path / ".codex" / "auth.json"+ shared_auth.parent.mkdir()+ shared_auth.write_text(json.dumps({"tokens": {"refresh_token": "shared"}}), encoding="utf-8")+ alias = tmp_path / "agent-zero-auth.json"+ alias.hardlink_to(shared_auth)+ monkeypatch.setenv("HOME", str(tmp_path))+ monkeypatch.setattr(codex, "codex_config", lambda: {"auth_file_path": str(alias)})++ with pytest.raises(RuntimeError, match="Agent Zero-owned auth file"):+ codex.resolve_auth_write_path()++def test_write_auth_file_uses_atomic_replace_and_private_permissions(tmp_path, monkeypatch):auth_path = tmp_path / "auth.json"replacements: list[tuple[Path, Path]] = []