Skip to content

Commit 207a1dd

Browse files
speedstorm1copybara-github
authored andcommitted
fix: Stop per-request Authorization and X-Server-Timeout from leaking into client default headers
fixes #3158 PiperOrigin-RevId: 1003716874
1 parent 1a64450 commit 207a1dd

5 files changed

Lines changed: 243 additions & 4 deletions

File tree

‎google/genai/_api_client.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,13 +1547,13 @@ def _build_request(
15471547

15481548
if patched_http_options.headers is None:
15491549
raise ValueError('Request headers must be set.')
1550-
populate_server_timeout_header(
1551-
patched_http_options.headers, timeout_in_seconds
1552-
)
1550+
# Copy so per-request headers do not become client defaults.
1551+
headers = dict(patched_http_options.headers)
1552+
populate_server_timeout_header(headers, timeout_in_seconds)
15531553
return HttpRequest(
15541554
method=http_method,
15551555
url=url,
1556-
headers=patched_http_options.headers,
1556+
headers=headers,
15571557
data=request_dict,
15581558
timeout=timeout_in_seconds,
15591559
)

‎google/genai/live.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,8 @@ async def connect(
11421142
)
11431143
if not headers.get('Authorization'):
11441144
headers['Authorization'] = f'Bearer {bearer_token}'
1145+
if creds.quota_project_id and not headers.get('x-goog-user-project'):
1146+
headers['x-goog-user-project'] = creds.quota_project_id
11451147

11461148
location = self._api_client.location
11471149
project = self._api_client.project

‎google/genai/tests/client/test_http_options.py‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
"""Tests for client behavior when issuing requests."""
1818

19+
from unittest import mock
20+
1921
import httpx
2022
import pytest
2123

@@ -211,3 +213,73 @@ def test_setting_30m_timeout_populates_header_and_request_timeout():
211213
)
212214
assert request.headers['X-Server-Timeout'] == '1800'
213215
assert request.timeout == 1800.0
216+
217+
218+
def test_build_request_does_not_mutate_client_headers():
219+
# Per-request headers must not leak into the client's default headers.
220+
api_client = _api_client.BaseApiClient(
221+
vertexai=False,
222+
api_key='test_api_key',
223+
http_options=types.HttpOptions(timeout=10000),
224+
)
225+
headers_before = dict(api_client._http_options.headers)
226+
request = api_client._build_request(
227+
http_method='POST',
228+
path='sample/path',
229+
request_dict={},
230+
)
231+
assert request.headers['X-Server-Timeout'] == '10'
232+
assert request.headers is not api_client._http_options.headers
233+
assert api_client._http_options.headers == headers_before
234+
235+
236+
def test_per_request_timeout_not_overridden_by_earlier_request():
237+
# A client-timeout request must not pin X-Server-Timeout for later ones.
238+
api_client = _api_client.BaseApiClient(
239+
vertexai=False,
240+
api_key='test_api_key',
241+
http_options=types.HttpOptions(timeout=10000),
242+
)
243+
api_client._build_request(
244+
http_method='POST',
245+
path='sample/path',
246+
request_dict={},
247+
)
248+
request = api_client._build_request(
249+
http_method='POST',
250+
path='sample/path',
251+
request_dict={},
252+
http_options=types.HttpOptions(timeout=3000),
253+
)
254+
assert request.headers['X-Server-Timeout'] == '3'
255+
256+
257+
def test_request_does_not_leak_authorization_into_client_headers():
258+
# The bearer token and quota project are set per request only.
259+
creds = mock.Mock()
260+
creds.quota_project_id = 'test-quota-project'
261+
api_client = _api_client.BaseApiClient(
262+
vertexai=True,
263+
project='test-project',
264+
location='us-central1',
265+
credentials=creds,
266+
)
267+
with (
268+
mock.patch.object(
269+
_api_client.BaseApiClient, '_access_token', return_value='token-1'
270+
),
271+
mock.patch.object(
272+
httpx.Client,
273+
'send',
274+
return_value=httpx.Response(
275+
200, request=httpx.Request('POST', ''), json={}
276+
),
277+
) as mock_send,
278+
):
279+
api_client.request('post', 'sample/path', {})
280+
281+
sent_headers = mock_send.call_args[0][0].headers
282+
assert sent_headers['authorization'] == 'Bearer token-1'
283+
assert sent_headers['x-goog-user-project'] == 'test-quota-project'
284+
assert 'Authorization' not in api_client._http_options.headers
285+
assert 'x-goog-user-project' not in api_client._http_options.headers

‎google/genai/tests/interactions/test_auth.py‎

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,3 +484,95 @@ async def test_async_interactions_vertex_extra_headers_override():
484484
headers = mock_send.call_args[0][0].headers
485485
assert headers['x-goog-api-key'] == 'manual-key'
486486
assert 'authorization' not in headers
487+
488+
489+
_GENERATE_CONTENT_BODY = {
490+
'candidates': [{
491+
'content': {'role': 'model', 'parts': [{'text': 'ok'}]},
492+
'finishReason': 'STOP',
493+
}]
494+
}
495+
496+
497+
def _send_recording_auth(recorded_auth):
498+
"""Returns a fake send that records auth and answers REST or Interactions."""
499+
500+
def send(req, *args, **kwargs):
501+
recorded_auth.append((req.url.path, req.headers.get('authorization')))
502+
if '/interactions' in req.url.path:
503+
return Response(
504+
200,
505+
request=Request('POST', ''),
506+
headers={'content-type': 'application/json'},
507+
content='{"status": "completed"}',
508+
)
509+
return Response(
510+
200, request=Request('POST', ''), json=_GENERATE_CONTENT_BODY
511+
)
512+
513+
return send
514+
515+
516+
def test_interactions_vertex_fresh_token_after_models_call():
517+
# A REST call before the first Interactions access must not pin its token.
518+
creds = mock.Mock()
519+
creds.quota_project_id = 'test-quota-project'
520+
client = Client(
521+
vertexai=True,
522+
project='test-project',
523+
location='us-central1',
524+
credentials=creds,
525+
)
526+
recorded_auth = []
527+
528+
with (
529+
mock.patch.object(
530+
BaseApiClient, '_access_token', side_effect=['token-1', 'token-2']
531+
),
532+
mock.patch.object(
533+
HTTPClient, 'send', side_effect=_send_recording_auth(recorded_auth)
534+
),
535+
):
536+
client.models.generate_content(model='gemini-2.5-flash', contents='hi')
537+
client.interactions.create(model='gemini-2.5-flash', input='Hello')
538+
539+
assert len(recorded_auth) == 2
540+
assert '/interactions' not in recorded_auth[0][0]
541+
assert recorded_auth[0][1] == 'Bearer token-1'
542+
assert recorded_auth[1][0].endswith('/interactions')
543+
assert recorded_auth[1][1] == 'Bearer token-2'
544+
545+
546+
@pytest.mark.asyncio
547+
async def test_async_interactions_vertex_fresh_token_after_models_call():
548+
# Async twin; client.aio shares the sync client's BaseApiClient.
549+
creds = mock.Mock()
550+
creds.quota_project_id = 'test-quota-project'
551+
client = Client(
552+
vertexai=True,
553+
project='test-project',
554+
location='us-central1',
555+
credentials=creds,
556+
)
557+
recorded_auth = []
558+
sync_send = _send_recording_auth(recorded_auth)
559+
560+
async def async_send(req, *args, **kwargs):
561+
return sync_send(req, *args, **kwargs)
562+
563+
with (
564+
mock.patch.object(
565+
BaseApiClient, '_access_token', side_effect=['token-1', 'token-2']
566+
),
567+
mock.patch.object(HTTPClient, 'send', side_effect=sync_send),
568+
mock.patch.object(AsyncHttpxClient, 'send', side_effect=async_send),
569+
):
570+
client.models.generate_content(model='gemini-2.5-flash', contents='hi')
571+
await client.aio.interactions.create(
572+
model='gemini-2.5-flash', input='Hello'
573+
)
574+
575+
assert len(recorded_auth) == 2
576+
assert recorded_auth[0][1] == 'Bearer token-1'
577+
assert recorded_auth[1][0].endswith('/interactions')
578+
assert recorded_auth[1][1] == 'Bearer token-2'

‎google/genai/tests/live/test_live.py‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
import certifi
3333
from google.oauth2.credentials import Credentials
34+
import httpx
3435
import pytest
3536
from websockets import client
3637

@@ -2214,6 +2215,78 @@ async def _test_connect():
22142215
await _test_connect()
22152216

22162217

2218+
@pytest.mark.asyncio
2219+
async def test_connect_uses_current_token_after_rest_call(mock_websocket):
2220+
# A REST call must not pin its bearer token for later Live connections.
2221+
credentials = Credentials(token='token-1')
2222+
client = gl_client.BaseApiClient(
2223+
vertexai=True,
2224+
project='test_project',
2225+
location='us-central1',
2226+
credentials=credentials,
2227+
)
2228+
capture = {}
2229+
2230+
@contextlib.asynccontextmanager
2231+
async def mock_connect(uri, additional_headers=None, **kwargs):
2232+
capture['headers'] = additional_headers
2233+
yield mock_websocket
2234+
2235+
with patch.object(
2236+
httpx.Client,
2237+
'send',
2238+
return_value=httpx.Response(
2239+
200, request=httpx.Request('POST', ''), json={}
2240+
),
2241+
):
2242+
client.request('post', 'sample/path', {})
2243+
credentials.token = 'token-2'
2244+
2245+
with patch.object(live, 'ws_connect', new=mock_connect):
2246+
async with live.AsyncLive(client).connect(model='test-model'):
2247+
pass
2248+
2249+
assert capture['headers']['Authorization'] == 'Bearer token-2'
2250+
2251+
2252+
@pytest.mark.asyncio
2253+
async def test_connect_sets_user_project_from_credentials(mock_websocket):
2254+
# Live sends the credentials' quota project, as REST requests do.
2255+
credentials = Credentials(token='t', quota_project_id='test-quota-project')
2256+
client = mock_api_client(vertexai=True, credentials=credentials)
2257+
capture = {}
2258+
2259+
@contextlib.asynccontextmanager
2260+
async def mock_connect(uri, additional_headers=None, **kwargs):
2261+
capture['headers'] = additional_headers
2262+
yield mock_websocket
2263+
2264+
with patch.object(live, 'ws_connect', new=mock_connect):
2265+
async with live.AsyncLive(client).connect(model='test-model'):
2266+
pass
2267+
2268+
assert capture['headers']['x-goog-user-project'] == 'test-quota-project'
2269+
2270+
2271+
@pytest.mark.asyncio
2272+
async def test_connect_no_user_project_without_quota_project(mock_websocket):
2273+
# Without a quota project on the credentials, Live omits the header.
2274+
credentials = Credentials(token='t')
2275+
client = mock_api_client(vertexai=True, credentials=credentials)
2276+
capture = {}
2277+
2278+
@contextlib.asynccontextmanager
2279+
async def mock_connect(uri, additional_headers=None, **kwargs):
2280+
capture['headers'] = additional_headers
2281+
yield mock_websocket
2282+
2283+
with patch.object(live, 'ws_connect', new=mock_connect):
2284+
async with live.AsyncLive(client).connect(model='test-model'):
2285+
pass
2286+
2287+
assert 'x-goog-user-project' not in capture['headers']
2288+
2289+
22172290
@pytest.mark.asyncio
22182291
async def test_connect_with_custom_base_url(mock_websocket):
22192292
# mock api client

0 commit comments

Comments
 (0)