fix(nous): never push or adopt the shared login across identities

Review findings (warnings): two non-JWT access tokens both read as account
identity None, so "same account" was True and a newer profile was pushed over a
DIFFERENT account's shared store. And (pre-existing) the adopt branch had no
identity gate at all: a profile signed in as account A adopted account B's newer
shared tokens and rewrote its auth.json to B.

Push, the renewal skip and adopt now all require a KNOWN identical identity
(guest anon credential or account JWT sub); a missing sub is unknown and never
matches another unknown. A second identity keeps using the existing `renewal`
slot of the shared store.
This commit is contained in:
Teknium
2026-11-01 23:19:27 -08:00
parent d0f69f7fcb
commit 8a930dc139
2 changed files with 31 additions and 3 deletions

View File

@@ -426,6 +426,13 @@ def _nous_state_order(a: dict[str, Any], b: dict[str, Any]) -> Optional[int]:
return oauth_pair_order(a, b)
def _same_known_nous_identity(a: dict[str, Any], b: dict[str, Any]) -> bool:
"""True when *a* and *b* are provably the same identity. An account whose access token carries no
JWT ``sub`` is UNKNOWN, never "the same" as another unknown one."""
identity = _nous_identity(a)
return identity[1] not in (None, "") and identity == _nous_identity(b)
def _merge_shared_nous_oauth_state(state: dict[str, Any]) -> bool:
"""Adopt the shared store's OAuth tokens into a profile-local Nous state when they are NEWER.
@@ -444,9 +451,11 @@ def _merge_shared_nous_oauth_state(state: dict[str, Any]) -> bool:
return False # a free-tier identity has no refresh token; nothing to merge into an OAuth state
refresh_changed = shared_refresh.strip() != str(state.get("refresh_token") or "").strip()
order = _nous_state_order(shared, state)
same = _same_known_nous_identity(shared, state)
# Unstamped on both sides: no evidence either way, keep the legacy "a different token wins".
if not (order == 1 or (order is None and refresh_changed)):
if order == -1 and refresh_changed and _nous_identity(shared) == _nous_identity(state):
# Never across identities: a profile signed in as one account keeps it (profiles are islands).
if not (same and (order == 1 or (order is None and refresh_changed))):
if order == -1 and refresh_changed and same:
_oauth_trace(
"nous_shared_store_older_than_profile",
shared_refresh_token_fp=_token_fingerprint(shared_refresh),
@@ -494,7 +503,7 @@ def _shared_copy_is_newer(held: Optional[dict[str, Any]], state: dict[str, Any],
if held is None or is_guest_state(state):
return False
copy = held if same else held.get(_SHARED_RENEWAL_KEY)
if not isinstance(copy, dict) or _nous_identity(copy) != _nous_identity(state):
if not isinstance(copy, dict) or not _same_known_nous_identity(copy, state):
return False
return _nous_state_order(copy, state) == 1

View File

@@ -140,3 +140,22 @@ def test_a_later_rotation_with_a_shorter_ttl_is_still_the_newer_pair(homes, monk
assert auth_nous._read_shared_nous_state()["refresh_token"] == "RT3" # never rolled back to RT2
auth_mod.resolve_nous_runtime_credentials(force_refresh=True)
assert posted[-1] == "RT3"
def test_unknown_identities_never_push_over_the_shared_store(homes):
hermes_home, _ = homes
# No JWT ``sub`` on either side: unknown identities, so a newer profile is not pushed over shared.
newer, older = (_state("A", "RTA", issued=-10, token="opaqueA"), _state("B", "RTB", issued=-600, token="opaqueB"))
_seed(hermes_home, newer, older)
with auth_nous._nous_shared_store_lock():
assert auth_nous._merge_shared_nous_oauth_state(dict(newer)) is False
assert auth_nous._read_shared_nous_state()["refresh_token"] == "RTB"
def test_a_profile_never_adopts_another_accounts_newer_shared_login(homes):
"""Profiles are islands: account B's shared login is not account A's next refresh token."""
hermes_home, _ = homes
_seed(hermes_home, _state("KA", "RTA", issued=-600, sub="uA"), _state("KB", "RTB", issued=-10, sub="uB"))
auth_mod.resolve_nous_access_token()
assert _profile_refresh_token(hermes_home) == "RTA"
assert auth_nous._read_shared_nous_state()["refresh_token"] == "RTB"