Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

fix(copilot): read timezone-naive token expiry as UTC, not host local time (#3216)

Rajat Nagda2b2dc1b2c93c
diff --git a/headroom/copilot_auth.py b/headroom/copilot_auth.py
index 0211f8144..d8bcaf6d3 100644
--- a/headroom/copilot_auth.py
+++ b/headroom/copilot_auth.py
@@ -15,7 +15,7 @@ from collections.abc import Mapping
from contextvars import ContextVar
from ctypes import wintypes
from dataclasses import dataclass
-from datetime import datetime
+from datetime import datetime, timezone
from pathlib import Path
from typing import Any
from urllib import error as urllib_error
@@ -591,9 +591,19 @@ def _parse_expiry(value: Any) -> float | None:
pass
try:
normalized = raw.replace("Z", "+00:00")
- return datetime.fromisoformat(normalized).timestamp()
+ parsed = datetime.fromisoformat(normalized)
except ValueError:
return None
+ # A timezone-naive ISO string (no `Z`, no offset) must be read as UTC,
+ # not the host's local zone. `datetime.timestamp()` assumes local time
+ # for naive datetimes, so on a non-UTC box the same expiry resolves to a
+ # different epoch — hours early or late — silently expiring a live token
+ # or trusting a dead one. Every other ISO parser in the codebase treats
+ # naive as UTC (see telemetry/traffic_learner, proxy/memory_rank_policy);
+ # match that here.
+ if parsed.tzinfo is None:
+ parsed = parsed.replace(tzinfo=timezone.utc)
+ return parsed.timestamp()
return None
diff --git a/tests/test_copilot_auth.py b/tests/test_copilot_auth.py
index 2d8820e49..23cec644b 100644
--- a/tests/test_copilot_auth.py
+++ b/tests/test_copilot_auth.py
@@ -1832,6 +1832,31 @@ def test_exchange_token_sync_returns_payload_on_success(monkeypatch: pytest.Monk
assert result == payload
+def test_parse_expiry_reads_naive_iso_as_utc() -> None:
+ assert copilot_auth._parse_expiry("2026-01-01T00:00:00") == 1767225600.0
+
+
+def test_parse_expiry_reads_zulu_suffix_as_utc() -> None:
+ assert copilot_auth._parse_expiry("2026-01-01T00:00:00Z") == 1767225600.0
+
+
[email protected](not hasattr(time, "tzset"), reason="time.tzset is POSIX-only")
+def test_parse_expiry_naive_iso_is_independent_of_host_timezone(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ previous_tz = os.environ.get("TZ")
+ try:
+ monkeypatch.setenv("TZ", "Asia/Kolkata")
+ time.tzset()
+ assert copilot_auth._parse_expiry("2026-01-01T00:00:00") == 1767225600.0
+ finally:
+ if previous_tz is None:
+ monkeypatch.delenv("TZ", raising=False)
+ else:
+ monkeypatch.setenv("TZ", previous_tz)
+ time.tzset()
+
+
def test_exchange_token_sync_uses_configured_corporate_tls_context(
monkeypatch: pytest.MonkeyPatch,
) -> None: