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
4 changes: 2 additions & 2 deletions googleapiclient/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ def _should_retry_response(resp_status, content):
if "reason" in reason:
reason = reason["reason"]
else:
reason = data[0]["error"]["errors"]["reason"]
except (UnicodeDecodeError, ValueError, KeyError):
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")

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks, good catch. These shapes already raised on main (for example, an empty errors list raised TypeError there and IndexError after my first commit), but they still escape as something other than HttpError, which is what this PR is meant to fix. Rather than nesting type checks, I added IndexError and TypeError to the existing except tuple. Those shapes now go down the same path as other invalid content: log a warning, no retry, and the caller gets an HttpError. I also added test_no_retry_403_malformed_body, which covers [], an empty errors list, ["x"] and null.

except (UnicodeDecodeError, ValueError, KeyError, IndexError, TypeError):
LOGGER.warning("Invalid JSON content from response: %s", content)
return False

Expand Down
28 changes: 26 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,30 @@ 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_no_retry_403_malformed_body(self):
for content in ["[]", '[{"error": {"errors": []}}]', '["x"]', "null"]:
http = HttpMockSequence([({"status": "403"}, content)])
uri = "https://www.googleapis.com/someapi/v1/collection/?foo=bar"
request = HttpRequest(http, JsonModel().response, uri)
request._sleep = mock.MagicMock()

with self.assertRaises(HttpError):
request.execute(num_retries=1)
request._sleep.assert_not_called()

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