_curl_debug: decode pycurl bytes as latin-1 before native_str#3659
Open
HrachShah wants to merge 1 commit into
Open
_curl_debug: decode pycurl bytes as latin-1 before native_str#3659HrachShah wants to merge 1 commit into
HrachShah wants to merge 1 commit into
Conversation
pycurl's DEBUGFUNCTION passes raw bytes, not str. The pre-fix code called native_str(debug_msg), where native_str is to_unicode -- which decodes as UTF-8. A non-UTF-8 byte sequence (issue tornadoweb#3183: a proxy echoing back binary bytes from the upstream response) raised UnicodeDecodeError and killed the request. Latin-1 round-trips every byte 0x00-0xFF to the matching U+00xx code point without raising, so a non-UTF-8 debug message is preserved as its byte sequence (rendered as the latin-1 characters) instead of crashing. Valid UTF-8 still decodes losslessly through native_str. Added CurlDebugTest.test_curl_debug_handles_non_utf8_bytes in the existing curl_httpclient_test module. The test fails on the pre-fix code with UnicodeDecodeError and passes with the fix. Refs: tornadoweb#3183
Member
|
The type annotations don't add up here. Should |
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.
Fixes #3183
What
CurlAsyncHTTPClient._curl_debugdecodes pycurl's DEBUGFUNCTIONdebug_msgargument vianative_str(which isto_unicode--UTF-8). pycurl passes raw bytes, not str, and those bytes are not
guaranteed to be valid UTF-8. A non-UTF-8 sequence (e.g. a proxy that
echoes back binary bytes from the upstream response) raised
UnicodeDecodeErrorand killed the request.Latin-1 round-trips every byte 0x00-0xFF to the matching U+00xx code
point without raising, so a non-UTF-8 debug message is preserved as
its byte sequence (rendered as the latin-1 characters) instead of
crashing. Valid UTF-8 still decodes losslessly through
native_str.errors="replace"was avoided -- it would silently turn anon-UTF-8 byte into a replacement character and make proxy-debug
investigations harder.
Implementation
_curl_debug_to_unicodekeeps thebyte-vs-str handling in one place and documents the latin-1
rationale; the two callers in
_curl_debug(thedebug_type == 0info branch and thedebug_type in (1, 2)header/data branch) are the only call sites.
debug_type == 4branch is left alone -- it%r-formatsdebug_msgand passes it tocurl_log.debug, which can acceptbytes directly (the
loggingmodule formats bytes viareprbefore interpolation).
Test
CurlDebugTest.test_curl_debug_handles_non_utf8_bytesintornado/test/curl_httpclient_test.pyexercisesdebug_type0,1, 2 with
b"hello \xff world"(and friends). Fails on pre-fixcode with
UnicodeDecodeError; passes with the fix. Verified:48 passed, 1 deselected