isBase64: reject strings whose length is 1 mod 4 when padding is off#2816
Open
HrachShah wants to merge 1 commit into
Open
isBase64: reject strings whose length is 1 mod 4 when padding is off#2816HrachShah wants to merge 1 commit into
HrachShah wants to merge 1 commit into
Conversation
…(4k+1 lengths cannot be produced by any base64 encoding of any byte string, so isBase64 used to spuriously accept 'A', 'AAAAA', etc. as valid when padding was disabled)
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.
Bug: With padding: false, isBase64() accepted any string of valid base64 characters regardless of length, including impossible ones like 'A' (1 char), 'AAAAA' (5 chars), or 'AAAAAAAAA' (9 chars).
Why: A base64 string's length is always 0, 2, 3, 4, 6, 7, 8, 10, 11, 12, 14, ..., i.e. (length % 4) in {0, 2, 3}. A length of the form 4k+1 cannot be produced by any base64 encoding of any byte string. The padding-enabled path already enforced length % 4 == 0; the padding-disabled path was missing the analogous check.
Fix: After the character-class regex matches, also require length % 4 != 1 when padding is off.
Tests: New it() with both standard and urlSafe variants asserts that 1-mod-4 strings are rejected and 0/2/3-mod-4 strings (taken from real base64 encodings) are still accepted. Full suite: 319 passing (was 318).