From bffe6e7ea1b338bebfbbeca40128b6e61a5bae98 Mon Sep 17 00:00:00 2001 From: Luis Pater Date: Mon, 23 Nov 2026 21:46:09 +0800 Subject: [PATCH] fix(antigravity): isolate connection pools after credential replacement - Scope pooled transports by stable auth identity and a digest of the refresh grant. - Add a regression test proving replacement credentials use a distinct TCP connection. Closes: #7961 --- .../runtime/executor/antigravity_executor.go | 56 ++++++++++------ .../antigravity_executor_transport_test.go | 64 ++++++++++++++++++- 2 files changed, 97 insertions(+), 23 deletions(-) diff --git a/internal/runtime/executor/antigravity_executor.go b/internal/runtime/executor/antigravity_executor.go index c1b53d9135..bf8e3fa61e 100644 --- a/internal/runtime/executor/antigravity_executor.go +++ b/internal/runtime/executor/antigravity_executor.go @@ -445,41 +445,57 @@ func antigravityProxiedHTTP11Transport(auth *cliproxyauth.Auth, proxyURL string, } // antigravityTransportScope returns the connection-pool scope for one credential. -// Runtime auths always carry an ID. Incomplete auth objects, such as those built by -// tests, plugins or SDK embedders, fall back to another stable credential marker so -// they neither share a pool with an unrelated OAuth identity nor allocate a fresh -// pool, and with it a fresh set of pool goroutines, on every single request. +// Runtime auths always carry an ID. The credential marker is included when available +// so replacing the account behind a stable ID cannot reuse the previous account's +// keep-alive TCP/TLS connections. Incomplete auth objects, such as those built by +// tests, plugins or SDK embedders, fall back to another stable marker so they neither +// share a pool with an unrelated OAuth identity nor allocate a fresh pool on every +// request. func antigravityTransportScope(auth *cliproxyauth.Auth) string { if auth == nil { return antigravityAnonymousTransportScope } + + credentialScope := antigravityTransportCredentialScope(auth) + identity := "" if id := strings.TrimSpace(auth.ID); id != "" { - return "id:" + id - } - if auth.Attributes != nil { + identity = "id:" + id + } else if auth.Attributes != nil { if path := strings.TrimSpace(auth.Attributes[cliproxyauth.AttributePath]); path != "" { - return "path:" + path - } - if source := strings.TrimSpace(auth.Attributes[cliproxyauth.AttributeSource]); source != "" { - return "source:" + source + identity = "path:" + path + } else if source := strings.TrimSpace(auth.Attributes[cliproxyauth.AttributeSource]); source != "" { + identity = "source:" + source } } - // Fall back to the credential material itself. Auth.Label is deliberately not used: - // it is documented as an optional human readable label for logging and carries no - // uniqueness guarantee, so two different OAuth identities sharing one label would - // wrongly share a TCP/TLS pool. - // + if identity != "" { + if credentialScope != "" { + return identity + "|" + credentialScope + } + if auth.RegistrationEpoch > 0 { + return fmt.Sprintf("%s|epoch:%d", identity, auth.RegistrationEpoch) + } + return identity + } + if credentialScope != "" { + return credentialScope + } + return antigravityAnonymousTransportScope +} + +func antigravityTransportCredentialScope(auth *cliproxyauth.Auth) string { + if auth == nil { + return "" + } // The refresh token is preferred over the access token because it stays stable - // across token rotation. Keying on the access token would move a credential to a new - // pool on every refresh, and would also strand refresh requests themselves, which - // run before any access token exists. + // across access-token rotation. Refresh requests also run before any access token + // exists, so this keeps their connection in the same credential pool. if refresh := strings.TrimSpace(metaStringValue(auth.Metadata, "refresh_token")); refresh != "" { return antigravityCredentialScope("refresh:", refresh) } if access := strings.TrimSpace(metaStringValue(auth.Metadata, "access_token")); access != "" { return antigravityCredentialScope("token:", access) } - return antigravityAnonymousTransportScope + return "" } // antigravityCredentialScope derives a pool scope from secret credential material. diff --git a/internal/runtime/executor/antigravity_executor_transport_test.go b/internal/runtime/executor/antigravity_executor_transport_test.go index b6d517c3cc..e2e78ad2d1 100644 --- a/internal/runtime/executor/antigravity_executor_transport_test.go +++ b/internal/runtime/executor/antigravity_executor_transport_test.go @@ -370,6 +370,64 @@ func TestNewAntigravityHTTPClientDistinctProxiesUseDistinctPools(t *testing.T) { } } +func TestNewAntigravityHTTPClientIsolatesReplacedCredentialPool(t *testing.T) { + enabledPool := true + cfg := &config.Config{ + Antigravity: config.AntigravityConfig{ + ConnectionPool: config.AntigravityConnectionPoolConfig{ + Enabled: &enabledPool, + }, + }, + } + + oldAuth := &cliproxyauth.Auth{ + ID: "shared-credential-file", + RegistrationEpoch: 1, + Metadata: map[string]any{ + "refresh_token": "refresh-account-a", + "project_id": "project-a", + }, + } + newAuth := oldAuth.Clone() + newAuth.RegistrationEpoch = 2 + newAuth.Metadata["refresh_token"] = "refresh-account-b" + newAuth.Metadata["project_id"] = "project-b" + + var mu sync.Mutex + var remoteAddrs []string + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + mu.Lock() + remoteAddrs = append(remoteAddrs, r.RemoteAddr) + mu.Unlock() + w.WriteHeader(http.StatusNoContent) + })) + defer server.Close() + + request := func(auth *cliproxyauth.Auth) { + client := newAntigravityHTTPClient(context.Background(), cfg, auth, 0) + resp, errGet := client.Get(server.URL) + if errGet != nil { + t.Fatalf("GET with epoch %d failed: %v", auth.RegistrationEpoch, errGet) + } + if errClose := resp.Body.Close(); errClose != nil { + t.Fatalf("close response body with epoch %d: %v", auth.RegistrationEpoch, errClose) + } + } + + request(oldAuth) + request(newAuth) + + mu.Lock() + got := append([]string(nil), remoteAddrs...) + mu.Unlock() + if len(got) != 2 { + t.Fatalf("observed %d upstream requests, want 2", len(got)) + } + if got[0] == got[1] { + t.Fatalf("replacement credential reused the previous account TCP connection %q", got[0]) + } +} + func TestNewAntigravityHTTPClientScopesPoolsByAuthIdentity(t *testing.T) { cfg := &config.Config{} const proxyURL = "http://127.0.0.1:18092" @@ -508,9 +566,9 @@ func TestAntigravityTransportScopeIgnoresNonUniqueLabel(t *testing.T) { // refreshing an access token must not move a credential onto a new pool, and a refresh // request that runs before any access token exists must resolve to the same scope. func TestAntigravityTransportScopeSurvivesAccessTokenRotation(t *testing.T) { - refreshOnly := &cliproxyauth.Auth{Metadata: map[string]any{"refresh_token": "stable-refresh"}} - beforeRotation := &cliproxyauth.Auth{Metadata: map[string]any{"refresh_token": "stable-refresh", "access_token": "access-1"}} - afterRotation := &cliproxyauth.Auth{Metadata: map[string]any{"refresh_token": "stable-refresh", "access_token": "access-2"}} + refreshOnly := &cliproxyauth.Auth{ID: "stable-id", Metadata: map[string]any{"refresh_token": "stable-refresh"}} + beforeRotation := &cliproxyauth.Auth{ID: "stable-id", Metadata: map[string]any{"refresh_token": "stable-refresh", "access_token": "access-1"}} + afterRotation := &cliproxyauth.Auth{ID: "stable-id", Metadata: map[string]any{"refresh_token": "stable-refresh", "access_token": "access-2"}} want := antigravityTransportScope(refreshOnly) if got := antigravityTransportScope(beforeRotation); got != want {