Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion googleapiclient/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def _should_retry_response(resp_status, content):
if "reason" in reason:
reason = reason["reason"]
else:
reason = data[0]["error"]["errors"]["reason"]
reason = data[0]["error"]["errors"][0]["reason"]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

While indexing data[0]["error"]["errors"][0]["reason"] fixes the immediate TypeError when errors is a list, this line is still highly vulnerable to other unhandled exceptions. Since the except block only catches (UnicodeDecodeError, ValueError, KeyError), any of the following scenarios will raise an unhandled TypeError, IndexError, or AttributeError and crash the request execution:

  1. data is an empty list [] (raises IndexError).
  2. data is not a list (e.g., None, int, bool, or a string) (raises TypeError).
  3. data[0] is not a dictionary (raises TypeError).
  4. data[0]["error"] is not a dictionary (raises TypeError or AttributeError if using .get()).
  5. data[0]["error"]["errors"] is an empty list or not a list (raises IndexError or TypeError).

To make this completely robust and prevent any unhandled exceptions from escaping _should_retry_response, we should perform defensive type and boundary checks before accessing these nested keys.

Suggested change
reason = data[0]["error"]["errors"][0]["reason"]
reason = None
if isinstance(data, list) and data and isinstance(data[0], dict):
error = data[0].get("error")
if isinstance(error, dict):
errors = error.get("errors")
if isinstance(errors, list) and errors and isinstance(errors[0], dict):
reason = errors[0].get("reason")

except (UnicodeDecodeError, ValueError, KeyError):
LOGGER.warning("Invalid JSON content from response: %s", content)
return False
Expand Down
17 changes: 15 additions & 2 deletions tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,7 @@ def test_media_io_base_download_unknown_media_size(self):
}
}"""

LIST_NOT_CONFIGURED_RESPONSE = """[
LIST_NOT_CONFIGURED_RESPONSE = """[{
"error": {
"errors": [
{
Expand All @@ -907,7 +907,7 @@ def test_media_io_base_download_unknown_media_size(self):
"code": 403,
"message": "Access Not Configured"
}
]"""
}]"""


class Callbacks(object):
Expand Down Expand Up @@ -1170,6 +1170,19 @@ def test_no_retry_403_list_fails(self):
request.execute()
request._sleep.assert_not_called()

def test_retry_403_list_rate_limit(self):
content = json.dumps([json.loads(RATE_LIMIT_EXCEEDED_RESPONSE)])
http = HttpMockSequence(
[({"status": "403"}, content), ({"status": "200"}, "{}")]
)
model = JsonModel()
uri = "https://www.googleapis.com/someapi/v1/collection/?foo=bar"
request = HttpRequest(http, model.response, uri)
request._sleep = mock.MagicMock()

self.assertEqual({}, request.execute(num_retries=1))
request._sleep.assert_called_once()

def test_null_postproc(self):
resp, content = HttpRequest.null_postproc("foo", "bar")
self.assertEqual(resp, "foo")
Expand Down
Loading