Repository navigation
Conversation
A request body that is neither UTF-8 nor gzip (for example a binary upload) made json_params_matcher raise UnicodeDecodeError or BadGzipFile from the gzip fallback, aborting the request instead of returning a mismatch so another registered response could match.
A body with a valid gzip header but corrupt deflate data makes gzip.decompress raise zlib.error, which is not an OSError subclass.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What type of PR is this? (check all applicable)
Description
json_params_matcherdecodes a bytes body as UTF-8. If that fails, it triesgzip.decompress(), which was added in #665. When a body is neither UTF-8 nor gzip, such as a binary file upload, that fallback raisesBadGzipFile. A gzip body whose content isn't UTF-8 raisesUnicodeDecodeErrorinstead. Either way the exception comes out of the matcher and aborts the whole request, so other registered responses never get a chance to match:This PR has the matcher catch those decode errors and return a mismatch with a reason, the same way it already handles
JSONDecodeError. The request then falls through to the next response. If nothing matches, it gets the usual "Connection refused by Responses" message instead of a gzip traceback. Gzipped JSON bodies still match as before.Related Issues
PR checklist
Before submitting this pull request, I have done the following:
toxandpre-commitchecks locally. I ran pre-commit on the changed files and pytest on Python 3.12 (urllib3 2) and Python 3.9 (urllib3 1.26): 253 passed. mypy shows the same 3 errors asmasterand none new.Added/updated tests?
test_json_params_matcher_body_not_utf8_or_gzipfails onmasterand passes with this change. It covers a binary body falling through to a second response, and a gzip body that isn't UTF-8.Disclosure: an AI coding agent (Claude) on the breken-ai account found this bug, wrote the fix and test, and ran the checks listed above.