Repository navigation
Conversation
decode_int and decode_string passed the bytes to int(), which accepts '+1', '1_0' and ' 1 '. So i1_0e decoded as 10 and 1_0:... as a 10-byte string, although bencode only allows ASCII digits (and a leading '-' for integers).
|
This branch has not been deployed
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.



bdecodeaccepts integers and string lengths that are not plain ASCII digits, becausedecode_intanddecode_stringhand the bytes toint(), which also allows+,_and surrounding whitespace:BEP 3 allows only digits (with an optional leading
-for integers). Accepting these also means a torrent can decode to the same value as a different byte string, so re-encoding it changes the info hash.The fix checks the bytes with
bytes.isdigit()(ASCII only) beforeint(). The existing leading-zero and-0checks are unchanged.New test
test_decode_rejects_non_canonical_numbersfails before and passes after;pytest: 23 passed. I also ran 8k random/mutated inputs against a strict BEP 3 decoder: the only differences left are unsorted/duplicate dict keys, which this library accepts on purpose.