Skip to content

Failed response.json() call in _helpers.response_log() causes response object's state to change #15150

@anarkiwi

Description

@anarkiwi

Hello,

In v2.40, new helpers were introduced that can log various requests and responses.

One such helper, response_log() tries to jsonify a response instance (https://github.com/googleapis/google-auth-library-python/blob/56b4d3b3bfd6f935411130c8828cdeea90aa69f1/google/auth/_helpers.py#L477) and use the result to log.

The issue with this is it causes the response instance state to change. Called via https://github.com/psf/requests/blob/c65c780849563c891f35ffc98d3198b71011c012/src/requests/models.py#L965, we can retrieve content which causes _content_consumed to change to True, even though the JSON decode failed.

This means it isn't safe to call json() on the response and keep the response unchanged. I can see the argument that this is a bug in requests, but for the moment there is now a subtle side effect introduced just by attempting logging - the implicit assumption is response won't change. I can imagine a less than ideal option would be to copy the response before trying to json()?

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions