Skip to content

Agent Zero AI framework

Harden OAuth auth path and proxy edge cases

Cooper Gamble96088de92362
diff --git a/plugins/_oauth/helpers/codex.py b/plugins/_oauth/helpers/codex.py
index 740b5d180..3bdd579d6 100644
--- a/plugins/_oauth/helpers/codex.py
+++ b/plugins/_oauth/helpers/codex.py
@@ -36,7 +36,7 @@ AUTH_FILENAME = "auth.json"
ACCESS_EXPIRY_MARGIN = timedelta(minutes=5)
REFRESH_INTERVAL = timedelta(minutes=55)
FALLBACK_CODEX_VERSION = "0.124.0"
-OAUTH_ERROR_KEYS = {"error", "error_description"}
+OAUTH_ERROR_KEYS = ("error_description", "error")
DEVICE_CODE_TIMEOUT_SECONDS = 15 * 60
WINDOWS_LOCK_RETRY_SECONDS = 0.05
USAGE_ENDPOINT_PATHS = (
@@ -1097,13 +1097,22 @@ def _validate_private_auth_path(path: Path) -> Path:
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."
- )
+ raise _private_auth_path_error()
+ try:
+ if resolved_path.stat().st_nlink > 1:
+ raise _private_auth_path_error()
+ except FileNotFoundError:
+ pass
return resolved_path
+def _private_auth_path_error() -> RuntimeError:
+ return 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."
+ )
+
+
def _known_codex_auth_paths() -> list[Path]:
candidates = [
Path.home() / ".codex" / AUTH_FILENAME,
diff --git a/plugins/_oauth/helpers/routes.py b/plugins/_oauth/helpers/routes.py
index 6815dfea4..d859059a0 100644
--- a/plugins/_oauth/helpers/routes.py
+++ b/plugins/_oauth/helpers/routes.py
@@ -295,7 +295,12 @@ def _supplied_proxy_token() -> str:
def _host_is_local(host: str) -> bool:
- hostname = (host or "").split(":", 1)[0].strip("[]").lower()
+ hostname = (host or "").strip().lower()
+ if hostname.startswith("["):
+ closing_bracket = hostname.find("]")
+ hostname = hostname[1:closing_bracket] if closing_bracket >= 0 else hostname.strip("[]")
+ elif hostname.count(":") == 1:
+ hostname = hostname.split(":", 1)[0]
if hostname in {"localhost", "127.0.0.1", "::1"}:
return True
try:
diff --git a/tests/test_oauth_codex.py b/tests/test_oauth_codex.py
index 4d0683daf..8ddf6ae8e 100644
--- a/tests/test_oauth_codex.py
+++ b/tests/test_oauth_codex.py
@@ -14,6 +14,7 @@ import yaml
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from plugins._oauth.helpers import codex
+from plugins._oauth.helpers import routes
from plugins._oauth.extensions.python._functions.models.get_api_key.end._20_codex_account_dummy_key import (
CodexAccountDummyKey,
)
@@ -242,6 +243,21 @@ def test_normalize_usage_payload_accepts_zero_percent_headers():
assert usage["primary"]["label"] == "5h"
+def test_token_error_message_prefers_description():
+ class FakeResponse:
+ status_code = 400
+ text = '{"error":"invalid_grant","error_description":"refresh token was already used"}'
+
+ @staticmethod
+ def json():
+ return {
+ "error": "invalid_grant",
+ "error_description": "refresh token was already used",
+ }
+
+ assert codex._token_error_message(FakeResponse()) == "refresh token was already used"
+
+
def test_default_auth_file_ignores_codex_cli_credentials(tmp_path, monkeypatch):
shared_auth = tmp_path / ".codex" / "auth.json"
private_auth = tmp_path / "usr" / "plugins" / "_oauth" / "codex" / "auth.json"
@@ -280,6 +296,17 @@ def test_explicit_codex_cli_auth_hard_link_is_rejected(tmp_path, monkeypatch):
codex.resolve_auth_write_path()
+def test_explicit_private_auth_hard_link_is_rejected(tmp_path, monkeypatch):
+ private_auth = tmp_path / "private-auth.json"
+ private_auth.write_text(json.dumps({"tokens": {"refresh_token": "private"}}), encoding="utf-8")
+ alias = tmp_path / "agent-zero-auth.json"
+ alias.hardlink_to(private_auth)
+ 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]] = []
@@ -392,6 +419,20 @@ def test_lock_file_retries_windows_contention(tmp_path, monkeypatch):
assert sleeps == [codex.WINDOWS_LOCK_RETRY_SECONDS, codex.WINDOWS_LOCK_RETRY_SECONDS]
+ ("host", "expected"),
+ [
+ ("localhost:5000", True),
+ ("127.0.0.1:5000", True),
+ ("[::1]:5000", True),
+ ("::1", True),
+ ("example.com:5000", False),
+ ],
+)
+def test_proxy_local_host_detection_supports_loopback_ipv6(host, expected):
+ assert routes._host_is_local(host) is expected
+
+
def test_load_auth_serializes_refresh_across_threads(tmp_path, monkeypatch):
auth_path = tmp_path / "auth.json"
_write_refreshable_auth(auth_path)