Enterprise agent platform for building, deploying, and operating AI teammates.
fix(mcp-oauth): revoke the tokens a refresh was issued for a grant that is gone (#3810)
Zhenghao Yin3dfc170c74c3
diff --git a/src/xagent/web/services/mcp_oauth.py b/src/xagent/web/services/mcp_oauth.pyindex ef257b037..d63aee7c1 100644--- a/src/xagent/web/services/mcp_oauth.py+++ b/src/xagent/web/services/mcp_oauth.py@@ -90,6 +90,14 @@ class _RetryRuntimeGrantSelection(RuntimeError):self.message = message+class _RefreshedGrantGone(_RetryRuntimeGrantSelection):+ """The grant was deleted or revoked while its refresh request was in flight.++ The token endpoint has already issued a replacement for it that no row can+ hold any more.+ """++class _InvalidGrantRefreshError(MCPOAuthRuntimeError):"""The token endpoint answered ``invalid_grant`` for the grant being refreshed."""@@ -822,6 +830,10 @@ async def _refresh_runtime_grant_in_dedicated_session( # noqa: PLR0913encrypted_refresh_token = str(grant.refresh_token)refresh_token = decrypt_value(encrypted_refresh_token)refresh_resource = str(grant.resource)+ # What a reconnect that replaces this grant keeps equal (see+ # ``_replaced_by_an_active_grant``), read while the row still exists.+ grant_client_id = int(grant.mcp_oauth_client_id)+ grant_issuer = str(grant.issuer)oauth_client_snapshot = SimpleNamespace(token_endpoint=str(grant.oauth_client.token_endpoint),client_id=str(grant.oauth_client.client_id),@@ -829,6 +841,10 @@ async def _refresh_runtime_grant_in_dedicated_session( # noqa: PLR0913token_endpoint_auth_method=str(grant.oauth_client.token_endpoint_auth_method or "none"),+ # Read now: the client row can be deleted with its server while+ # the request below is in flight, and the revocation after a+ # refresh that lost its grant needs the revocation endpoint.+ metadata_json=grant.oauth_client.metadata_json,)refresh_db.rollback()@@ -914,7 +930,29 @@ async def _refresh_runtime_grant_in_dedicated_session( # noqa: PLR0913.with_for_update().first())- if locked_grant is None or not _runtime_grant_matches(+ if locked_grant is None or locked_grant.status != "active":+ if _replaced_by_an_active_grant(+ refresh_db,+ grant_id=grant_id,+ server_id=server_id,+ user_id=user_id,+ resource_owner_key=resource_owner_key,+ client_id=grant_client_id,+ issuer=grant_issuer,+ resource=refresh_resource,+ ):+ # Reconnected meanwhile: the successor is the live+ # authorization, so the retry selects it and the response+ # is dropped unrevoked.+ raise _RetryRuntimeGrantSelection(+ "authorization_required",+ "MCP OAuth grant changed while preparing runtime authorization",+ )+ raise _RefreshedGrantGone(+ "authorization_required",+ "MCP OAuth grant changed while preparing runtime authorization",+ )+ if not _runtime_grant_matches(locked_grant,server_id=server_id,user_id=user_id,@@ -997,10 +1035,113 @@ async def _refresh_runtime_grant_in_dedicated_session( # noqa: PLR0913)return runtime_auth+ except _RefreshedGrantGone:+ # ``refresh_db.begin()`` rolled back on the way out, so no database+ # lock is held while the provider is called.+ await _revoke_tokens_issued_to_a_gone_grant(+ oauth_client_snapshot, grant_id=grant_id, token_data=token_data+ )+ raisefinally:refresh_db.close()+def _replaced_by_an_active_grant( # noqa: PLR0913+ db: Any,+ *,+ grant_id: int,+ server_id: int,+ user_id: int,+ resource_owner_key: str,+ client_id: int,+ issuer: str,+ resource: str,+) -> bool:+ """Whether the owner holds another active grant in place of ``grant_id``.++ A reconnect that grants a different scope deletes the earlier grant as+ superseded and stores its successor in one callback transaction+ (``_delete_superseded_mcp_oauth_grants``), deliberately without revoking at+ the provider: a provider whose revocation ends the whole authorization (a+ user, application and audience grant) would take the successor with it.+ The key is the one that supersession matches on.++ Call it after the locked re-read has found ``grant_id`` deleted or+ inactive. The deletion and the successor commit together, so a read that+ sees the grant gone starts after that commit and sees the successor too.++ One window stays, accepted because it is narrow: a grant that was really+ deleted (a disconnect) and then a brand-new reconnect that commits+ between this read and the provider call in+ ``_revoke_tokens_issued_to_a_gone_grant``. That call then revokes tokens+ the new authorization may share. Closing it would need coordination+ between reconnect and refresh that this deliberately does not add.+ """+ from ..models.mcp_oauth import MCPOAuthGrant++ return (+ db.query(MCPOAuthGrant.id)+ .filter(+ MCPOAuthGrant.id != grant_id,+ MCPOAuthGrant.mcp_server_id == server_id,+ MCPOAuthGrant.user_id == user_id,+ MCPOAuthGrant.resource_owner_key == resource_owner_key,+ MCPOAuthGrant.mcp_oauth_client_id == client_id,+ MCPOAuthGrant.issuer == issuer,+ MCPOAuthGrant.resource == resource,+ MCPOAuthGrant.status == "active",+ )+ .first()+ is not None+ )+++async def _revoke_tokens_issued_to_a_gone_grant(+ oauth_client: Any, *, grant_id: int, token_data: dict[str, Any]+) -> None:+ """Revoke the token pair a refresh issued after its grant was deleted or revoked.++ The grant was deleted (a server disconnect, a workspace removal) or revoked+ while the token endpoint request was in flight, so the replacement pair+ that came back is persisted nowhere and nobody can revoke it later. A+ provider that rotates refresh tokens has invalidated the old one but left+ the new pair usable, and revoking only the old token at disconnect time+ does not touch it. Best effort, like every other revocation here: a+ failure is logged and never raised into the refresh caller.++ Only for a grant that is gone with no successor+ (``_replaced_by_an_active_grant``): a reconnect that replaced it owns the+ live authorization, which revoking this response could end.++ Reuses the revocation of a token whose final persistence failed+ (``_revoke_mcp_oauth_issued_token_externally``); ``flow_id`` is only the+ label that helper logs, here the grant's id.+ """+ from ..api.mcp import (+ _mcp_oauth_issued_token_snapshot,+ _revoke_mcp_oauth_issued_token_externally,+ )++ logger.warning(+ "MCP OAuth grant %s was deleted or revoked while its token refresh was "+ "in flight; revoking the tokens the refresh issued",+ grant_id,+ )+ try:+ await _revoke_mcp_oauth_issued_token_externally(+ _mcp_oauth_issued_token_snapshot(+ client=oauth_client, flow_id=grant_id, token_data=token_data+ )+ )+ except Exception as exc:+ logger.warning(+ "Revoking the tokens a refresh issued for gone MCP OAuth grant %s "+ "failed (exception_type=%s)",+ grant_id,+ type(exc).__name__,+ )++def select_mcp_oauth_owner(db: Any,*,diff --git a/tests/web/api/test_mcp_oauth_reconnect_grants.py b/tests/web/api/test_mcp_oauth_reconnect_grants.pyindex ab2c349a3..99b322041 100644--- a/tests/web/api/test_mcp_oauth_reconnect_grants.py+++ b/tests/web/api/test_mcp_oauth_reconnect_grants.py@@ -7,7 +7,9 @@ deletes the owner's other active grants for the same server, client, issuerand resource, so one usable grant is left.Grants of other owners, users, clients, issuers, resources and servers arenot touched, a callback that fails keeps the earlier grant, and the runtime-refresh path keeps working with the grant that is left.+refresh path keeps working with the grant that is left. A runtime refresh+whose grant is deleted or revoked while the token endpoint request is in+flight revokes the token pair it was issued, which no row can hold."""from __future__ import annotations@@ -640,7 +642,14 @@ async def test_runtime_refreshing_a_replaced_grant_moves_to_the_new_grant(engine, db_session, monkeypatch, provider):"""A refresh of the earlier grant that is in flight while the callback- replaces it selects the new grant instead of failing."""+ replaces it selects the new grant instead of failing.++ The refresh is answered with a rotated pair that nothing stores, but it is+ not revoked: the callback replaced the grant on purpose, without revoking,+ and the provider here ends the whole authorization (user, application and+ audience) when any token is revoked, as Auth0 does with "Refresh Token+ Revocation Deletes Grant". Revoking the rotated pair would end the new+ grant's authorization too."""db, user = db_sessionserver = _add_server(db, user, AUTH)client = _add_client(db, server)@@ -651,6 +660,17 @@ async def test_runtime_refreshing_a_replaced_grant_moves_to_the_new_grant(_token_response(monkeypatch, {**ISSUED_TOKENS, "scope": NARROW})callback_db = sessionmaker(bind=engine, autoflush=False, autocommit=False)()refreshed_tokens: list[str] = []+ authorization = {"alive": True, "revoked": []}++ async def revoke_the_whole_authorization(snapshot):+ authorization["revoked"].append(snapshot.refresh_token)+ authorization["alive"] = False++ monkeypatch.setattr(+ mcp_api,+ "_revoke_mcp_oauth_issued_token_externally",+ revoke_the_whole_authorization,+ )async def reconnect_during_refresh(_client, *, refresh_token, resource, client=None, **_kwargs@@ -660,7 +680,11 @@ async def test_runtime_refreshing_a_replaced_grant_moves_to_the_new_grant(_callback_request("state-inflight"), callback_db)assert _redirect_query(response) == {"mcp_oauth_success": ["1"]}- return {"access_token": "refreshed-earlier-access-token", "scope": FULL}+ return {+ "access_token": "refreshed-earlier-access-token",+ "refresh_token": "rotated-earlier-refresh-token",+ "scope": FULL,+ }monkeypatch.setattr(mcp_oauth_service, "_refresh_mcp_oauth_grant", reconnect_during_refresh@@ -683,6 +707,134 @@ async def test_runtime_refreshing_a_replaced_grant_moves_to_the_new_grant(assert runtime_auth.grant_id == int(grant.id) != earlier_idassert runtime_auth.access_token == "issued-access-token"assert runtime_auth.refreshed is False+ assert authorization == {"alive": True, "revoked": []}+ assert grant.status == "active"+ assert decrypt_value(grant.refresh_token) == "issued-refresh-token"+++REFRESHED_TOKENS = {+ "access_token": "refreshed-access-token",+ "refresh_token": "refreshed-refresh-token",+ "token_type": "Bearer",+ "expires_in": 3600,+}+++async def _refresh_expired_grant(engine, db, user, monkeypatch, *, during_refresh):+ """Run a runtime refresh of an expired grant whose token endpoint request+ calls ``during_refresh(grant_id, session)`` on another connection before+ it returns ``REFRESHED_TOKENS``. Returns the server and the grant id."""+ server = _add_server(db, user, AUTH)+ client = _add_client(db, server)+ grant_id = _add_grant(db, server=server, user=user, client=client, scope=FULL)+ _expire(db, db.get(MCPOAuthGrant, grant_id))++ async def token_endpoint(+ _client, *, refresh_token, resource, client=None, **_kwargs+ ):+ assert refresh_token == "earlier-refresh-token"+ other = sessionmaker(bind=engine, autoflush=False, autocommit=False)()+ try:+ during_refresh(grant_id, other)+ other.commit()+ finally:+ other.close()+ return dict(REFRESHED_TOKENS)++ monkeypatch.setattr(mcp_oauth_service, "_refresh_mcp_oauth_grant", token_endpoint)+ with pytest.raises(mcp_oauth_service.MCPOAuthRuntimeError) as raised:+ await resolve_mcp_oauth_runtime_auth(+ db,+ server_id=server.id,+ user_id=user.id,+ auth_config=AUTH,+ resource_owner_key=OWNER,+ )+ assert raised.value.code == "authorization_required"+ return server, grant_id+++def _delete_grant(grant_id: int, session) -> None:+ session.query(MCPOAuthGrant).filter(MCPOAuthGrant.id == grant_id).delete()+++def _revoke_grant(grant_id: int, session) -> None:+ session.query(MCPOAuthGrant).filter(MCPOAuthGrant.id == grant_id).update(+ {MCPOAuthGrant.status: "revoked"}+ )+++async def test_a_refresh_whose_grant_is_deleted_meanwhile_revokes_what_it_was_issued(+ engine, db_session, monkeypatch, provider+):+ """The token endpoint answers after the grant was deleted, so the rotated+ pair has nowhere to be stored. Revoking only the grant's old tokens at+ deletion cannot reach it: it is revoked here, and nothing is persisted."""+ db, user = db_session++ server, _grant_id = await _refresh_expired_grant(+ engine, db, user, monkeypatch, during_refresh=_delete_grant+ )++ [snapshot] = provider["issued"]+ assert snapshot.access_token == "refreshed-access-token"+ assert snapshot.refresh_token == "refreshed-refresh-token"+ assert snapshot.revocation_endpoint == f"{ISSUER}/revoke"+ assert snapshot.client_id == "client-1"+ assert _owner_grants(db, server) == []+++async def test_a_refresh_whose_grant_is_revoked_meanwhile_revokes_what_it_was_issued(+ engine, db_session, monkeypatch, provider+):+ """A grant marked revoked locally (a suspension does not call the+ provider) is not a place to store a refresh either: the row keeps its old+ tokens and the issued pair is revoked."""+ db, user = db_session++ server, grant_id = await _refresh_expired_grant(+ engine, db, user, monkeypatch, during_refresh=_revoke_grant+ )++ [snapshot] = provider["issued"]+ assert snapshot.access_token == "refreshed-access-token"+ assert snapshot.refresh_token == "refreshed-refresh-token"+ [grant] = _owner_grants(db, server)+ assert int(grant.id) == grant_id+ assert grant.status == "revoked"+ assert decrypt_value(grant.access_token) == "earlier-access-token"+ assert decrypt_value(grant.refresh_token) == "earlier-refresh-token"+++async def test_a_refresh_that_is_stored_revokes_nothing(+ engine, db_session, monkeypatch, provider+):+ db, user = db_session+ server = _add_server(db, user, AUTH)+ client = _add_client(db, server)+ grant_id = _add_grant(db, server=server, user=user, client=client, scope=FULL)+ _expire(db, db.get(MCPOAuthGrant, grant_id))++ async def token_endpoint(_client, **_kwargs):+ return dict(REFRESHED_TOKENS)++ monkeypatch.setattr(mcp_oauth_service, "_refresh_mcp_oauth_grant", token_endpoint)++ runtime_auth = await resolve_mcp_oauth_runtime_auth(+ db,+ server_id=server.id,+ user_id=user.id,+ auth_config=AUTH,+ resource_owner_key=OWNER,+ )++ assert runtime_auth.refreshed is True+ assert provider["issued"] == []+ [grant] = _owner_grants(db, server)+ assert decrypt_value(grant.refresh_token) == "refreshed-refresh-token"@pytest.mark.parametrize(diff --git a/tests/web/api/test_mcp_oauth_reconnect_grants_postgresql.py b/tests/web/api/test_mcp_oauth_reconnect_grants_postgresql.pyindex 3068a0bbd..d36fff763 100644--- a/tests/web/api/test_mcp_oauth_reconnect_grants_postgresql.py+++ b/tests/web/api/test_mcp_oauth_reconnect_grants_postgresql.py@@ -3,7 +3,9 @@``uq_mcp_oauth_grants_lookup`` covers every grant row whatever its status, andthe lookup hash includes the granted scope. The callback deletes the grants itreplaces before it looks up the row for the new scope, so neither a later-refresh nor a refresh running at the same time collides with that constraint.+refresh nor a refresh running at the same time collides with that constraint. A refresh that+finds its grant revoked, once it holds the row lock, revokes the token pair it+was issued only after the lock is gone."""from __future__ import annotations@@ -315,3 +317,50 @@ def test_reconnect_waits_for_a_refresh_of_the_grant_it_replaces([(grant_id, scope, status, token)] = _owner_grants(factory)assert (scope, status, token) == (NARROW, "active", "issued-access-token")assert (grant_id == seed["earlier_id"]) is keeps_the_earlier_row+++def test_tokens_issued_to_a_revoked_grant_are_revoked_with_no_row_lock_held(+ factory, monkeypatch+):+ """The refresh finds its grant revoked under ``FOR UPDATE``. The provider+ call for the pair it was issued must not run inside that transaction: from+ another connection the row is lockable while the revocation is sent."""+ seed = _seed(factory, earlier_expired=True)++ async def fake_refresh(_client, *, refresh_token, resource, client=None, **_kwargs):+ with factory() as other:+ other.query(MCPOAuthGrant).filter(+ MCPOAuthGrant.id == seed["earlier_id"]+ ).update({MCPOAuthGrant.status: "revoked"})+ other.commit()+ return {"access_token": "refreshed-access-token", "refresh_token": "rotated"}++ revoked: list[tuple[str, str | None, bool]] = []++ async def record_revocation(snapshot):+ with factory() as other:+ lockable = (+ other.execute(+ text(+ "SELECT id FROM mcp_oauth_grants WHERE id = :id "+ "FOR UPDATE NOWAIT"+ ),+ {"id": seed["earlier_id"]},+ ).first()+ is not None+ )+ other.rollback()+ revoked.append((snapshot.access_token, snapshot.refresh_token, lockable))++ monkeypatch.setattr(mcp_oauth_service, "_refresh_mcp_oauth_grant", fake_refresh)+ monkeypatch.setattr(+ mcp_api, "_revoke_mcp_oauth_issued_token_externally", record_revocation+ )++ with pytest.raises(mcp_oauth_service.MCPOAuthRuntimeError):+ _resolve(factory, seed)++ assert revoked == [("refreshed-access-token", "rotated", True)]+ assert _owner_grants(factory) == [+ (seed["earlier_id"], FULL, "revoked", "earlier-access-token")+ ]