fix: reject interior padding in rfc4648 decode - #344
Open
maximilliangrand wants to merge 1 commit into
Open
Conversation
The padded codecs (base64pad, base64urlpad, base32pad, base32hexpad and the upper variants) accepted a '=' in the middle of a string and decoded it to bogus bytes instead of rejecting it. createAlphabetIdx indexed the trailing '=' of the padded alphabets, giving it a symbol value, and decode strips only trailing '=', so an interior '=' was folded into the output as data. Skip '=' when building the alphabet index. Trailing padding is handled by the strip loop in decode and the pad flag in encode, so any non-trailing '=' is now rejected as a non-base character. Valid input round-trips unchanged.
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.
The rfc4648 padded codecs (
base64pad,base64urlpad,base32pad,base32hexpadand the upper variants) accept a=in the middle of a string and decode it to bogus bytes instead of rejecting it.Repro on 14.0.5:
Per RFC 4648 §3.2 the
=padding may only appear as a trailing run, so both inputs are invalid. Python agrees:base64.b64decode('QQ=Q', validate=True)raises. base32 is affected too (base32pad.decode('ca=aa')returns[0x08, 0x00]).Root cause:
createAlphabetIdxinsrc/bases/base.tsalso indexes the trailing=of the padded alphabets, giving it a symbol value;decodestrips only trailing=, so an interior=passes the unknown-character check and is folded in as data.Fix: skip
=when building the alphabet index. Trailing padding is already handled by the strip loop indecodeand the pad flag inencode, so any non-trailing=is now rejected as a non-base character. Added a test that round-trips valid input and rejects a spliced interior=; all valid inputs still round-trip.