Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

Per-request Authorization and X-Server-Timeout leak into the client's default headers (stale token in Interactions and Live)

オープン
#3,158 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@Priyanshujindal がすでに取り組んでいます。

2026年10月17日 から。

  • #3159 @Priyanshujindal による — オープン

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
45/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python

調査の方向性

Start by reading BaseApiClient._build_request in _api_client.py and the header handling in _gaos/google_genai.py and live.py. Run the mentioned _build_request unit test and add or run regression coverage for sync and async models-to-Interactions and models-to-Live flows. Done means request-specific headers no longer mutate client defaults, refreshed tokens and per-request timeouts are used, and Live sets the quota project itself.

索引モデルが issue の本文から書いたものです。

説明

Description

When a request is sent without per-request http_options, BaseApiClient._build_request reuses the client's own self._http_options.headers dict for the request. The request path then writes per-request values into it:

  • X-Server-Timeout, via populate_server_timeout_header (_api_client.py ~1545);
  • Authorization and x-goog-user-project (~1564–1566 and ~1641–1645).

Those values become client defaults, and other code reads them as such:

  • Interactions bridge: it snapshots the client's headers (_gaos/google_genai.py ~99–103), and _apply_default_headers copies the stale Authorization onto every request. _apply_auth then skips the fresh token because a header is already set.
  • Live: live.connect only sets Authorization if it's absent (live.py ~1141).
  • Server timeout: populate_server_timeout_header only sets X-Server-Timeout if it's absent.
Impact
  • Vertex AI with ADC or credentials: if any REST call (e.g. models.generate_content) runs before client.interactions (or the other Interactions-layer resources) is first used, those requests send the first bearer token for the life of the process. Once that token expires, after about an hour, they start failing with 401. Long-running servers hit this.
  • Live: it connects with the token from the last REST call, even after a refresh.
  • Timeouts: with a client-level timeout, a later per-request timeout still sends the earlier X-Server-Timeout, and the server enforces it. So the same call behaves differently depending on what ran before.
Repro (offline, mocked transport, fake credentials)
import httpx
import google.oauth2.credentials
from google import genai
from google.genai import types

GEN = {'candidates': [{'content': {'role': 'model', 'parts': [{'text': 'ok'}]}, 'finishReason': 'STOP'}]}
INTERACTION = {'id': 'abc', 'status': 'completed', 'outputs': [], 'object': 'interaction',
               'created': '2026-01-01T00:00:00Z', 'updated': '2026-01-01T00:00:00Z'}
sent = []


def handler(request: httpx.Request) -> httpx.Response:
  kind = 'interactions' if '/interactions/' in request.url.path else 'models'
  sent.append((kind, request.headers.get('authorization')))
  return httpx.Response(200, json=INTERACTION if kind == 'interactions' else GEN)


creds = google.oauth2.credentials.Credentials(token='token-1')
client = genai.Client(
    vertexai=True, project='p', location='us-central1', credentials=creds,
    http_options=types.HttpOptions(httpx_client=httpx.Client(transport=httpx.MockTransport(handler))),
)

client.models.generate_content(model='gemini-2.5-flash', contents='hi')
creds.token = 'token-2'  # what google-auth does when it refreshes the token
client.interactions.get(id='abc')
client.models.generate_content(model='gemini-2.5-flash', contents='hi')
creds.token = 'token-3'
client.interactions.get(id='abc')

for kind, auth in sent:
  print(f'{kind:13s} {auth}')
print('client default headers:', {k: v for k, v in client._api_client._http_options.headers.items() if k.lower() in ('authorization', 'x-server-timeout')})

Output on main (364889aa, google-genai 2.31.0):

models        Bearer token-1
interactions  Bearer token-1
models        Bearer token-2
interactions  Bearer token-1      <- credentials hold token-3
client default headers: {'Authorization': 'Bearer token-2'}

If interactions is used before any models call, it gets the fresh token each time, so this depends on call order.

Proposed fix

Copy the headers at the one place that builds per-request headers, so per-request values stay on the HttpRequest:

@@ class BaseApiClient: def _build_request(
     if patched_http_options.headers is None:
       raise ValueError('Request headers must be set.')
-    populate_server_timeout_header(
-        patched_http_options.headers, timeout_in_seconds
-    )
+    # Without per-request options, patched_http_options is the client's own
+    # options. Copy the headers so that this request's X-Server-Timeout, and
+    # the Authorization header set when it is sent, do not become client
+    # defaults that later requests, Interactions and Live reuse.
+    headers = dict(patched_http_options.headers)
+    populate_server_timeout_header(headers, timeout_in_seconds)
     return HttpRequest(
         method=http_method,
         url=url,
-        headers=patched_http_options.headers,
+        headers=headers,
         data=request_dict,
         timeout=timeout_in_seconds,
     )

With this, Interactions and Live always send the current token, X-Server-Timeout matches each request's timeout, and the client's default headers stay as configured. It covers sync and async and every transport.

I'd rather not patch the readers (filter Authorization in the bridge, always overwrite X-Server-Timeout). That would be three separate patches, and it would break the existing rule that a user-set X-Server-Timeout wins (test_server_timeout_not_overwritten).

One behaviour change worth noting: on Vertex, Live currently only gets x-goog-user-project incidentally, after some REST call has written it into the shared headers. With this fix it won't, so Live should set it itself from credentials.quota_project_id.

I have regression tests that fail on main:

  • a unit test on _build_request;
  • sync and async models → Interactions;
  • models → Live.

Happy to open a PR.

主要言語
Python
スター
4k
フォーク
1k
平均マージ
1日 8時間
マージ済み PR(30日)
93

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

googleapis/python-genai のほかの issue

googleapis/python-genai の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。