Skip to content

Commit 495bbd5

Browse files
committed
Override None timeout with a default one
Until all dependent libraries expose configurable timeouts, we do not want to accept an accidentally passed None timeout. Assumption: there are no good real-world use cases where a None timeout would be desirable, as that can make a request to hang indefinitely.
1 parent 849438c commit 495bbd5

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

google/auth/transport/requests.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,13 @@ def request(
289289
# thread-safety.
290290
_credential_refresh_attempt = kwargs.pop("_credential_refresh_attempt", 0)
291291

292+
# Until all libraries expose explicit timeouts, a rouge None timeout
293+
# might sneak in - for example, a default timeout value in one of the
294+
# internal classes. Since it's desired to have at least some timeout,
295+
# we override any `None` values for now.
296+
if timeout is None:
297+
timeout = _DEFAULT_TIMEOUT
298+
292299
# Make a copy of the headers. They will be modified by the credentials
293300
# and we want to pass the original headers if we recurse.
294301
request_headers = headers.copy() if headers is not None else {}

tests/transport/test_requests.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,21 @@ def test_request_default_timeout(self):
192192
expected_timeout = google.auth.transport.requests._DEFAULT_TIMEOUT
193193
assert patched_request.call_args.kwargs.get("timeout") == expected_timeout
194194

195+
def test_request_overriding_none_timeouts(self):
196+
credentials = mock.Mock(wraps=CredentialsStub())
197+
response = make_response()
198+
adapter = AdapterStub([response])
199+
200+
authed_session = google.auth.transport.requests.AuthorizedSession(credentials)
201+
authed_session.mount(self.TEST_URL, adapter)
202+
203+
patcher = mock.patch("google.auth.transport.requests.requests.Session.request")
204+
with patcher as patched_request:
205+
authed_session.request("GET", self.TEST_URL, timeout=None)
206+
207+
expected_timeout = google.auth.transport.requests._DEFAULT_TIMEOUT
208+
assert patched_request.call_args.kwargs.get("timeout") == expected_timeout
209+
195210
def test_request_no_refresh(self):
196211
credentials = mock.Mock(wraps=CredentialsStub())
197212
response = make_response()

0 commit comments

Comments
 (0)