gh-47482: Reject non-bytes-like input in the idna codec decoder#154699
Open
Cnashn wants to merge 2 commits into
Open
gh-47482: Reject non-bytes-like input in the idna codec decoder#154699Cnashn wants to merge 2 commits into
Cnashn wants to merge 2 commits into
Conversation
The decoder called bytes(input) on any non-bytes input, a bogus Python 3 forward-port. Objects that bytes() accepts were silently coerced instead of rejected: codecs.decode(5, "idna") returned five NUL characters and codecs.decode([65, 66], "idna") returned "AB". Raise TypeError with the same message other codecs use, while continuing to accept bytes-like objects such as bytearray and memoryview.
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.
Codec.decodeinLib/encodings/idna.pycalledbytes(input)on any input thatwas not already
bytes:bytes()accepts more than buffers, so objects it happens to understand weresilently coerced instead of rejected:
The first three return nonsense rather than raising, and the last leaks an
error message from
bytes()that never mentions what was actually expected.Every other bytes-to-text decoder raises
TypeError: a bytes-like object is required, not 'str'.This PR raises
TypeErrorwith that same message, and keeps acceptingbytes-like objects, so
bytearrayandmemoryviewcontinue to work:The type check now runs before the empty-input shortcut, so
codecs.decode("", "idna")raises instead of returning
'', matchingcodecs.decode("", "utf-8").This is the narrow half of the issue that already has agreement in the
discussion: rejecting non-bytes input "deliberately and explicitly, rather than
as a result of a bogus forward-port". I have deliberately not touched the
separate proposal in that thread to add a new
decode_idnafunction forun-IDNA-ing
strinput, since that is a feature and still undecided.The issue is marked "test needed" and noted that the behaviour was not covered
by the test suite, so
IDNACodecTestgains two tests: one that bytes-likeinput still decodes, and one that non-bytes-like input raises
TypeErrorwiththe expected message.
test_codecs,test_socket,test_ssl,test_httplib,test_urllib,test_urllib2,test_emailandtest_strall pass.One thing I noticed but left alone:
IncrementalDecoder._buffer_decodehas anisinstance(input, str)branch that is unreachable, becausecodecs.BufferedIncrementalDecoderconcatenates its bytes buffer to the inputbefore
_buffer_decodeis called. Happy to address that separately if wanted.