isBase64: reject strings whose length is 1 mod 4 when padding is off#2816
Open
HrachShah wants to merge 2 commits into
Open
isBase64: reject strings whose length is 1 mod 4 when padding is off#2816HrachShah wants to merge 2 commits into
HrachShah wants to merge 2 commits into
Conversation
added 2 commits
July 9, 2026 08:02
…(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)
tux-tn
requested changes
Jul 15, 2026
tux-tn
left a comment
Member
There was a problem hiding this comment.
Thank you for your PR, I added some suggestions to your changes.
Also, could you either move the isHash change into a separate PR or update this PR’s title and description to document why it is included? It is currently outside the stated Base64 scope.
| // unpadded base64 lengths: 0, 2, 3, 4, 6, 7, 8, 10, 11, 12, 14, 15, 16, ... | ||
| // i.e. (length % 4) is 0, 2, or 3 — never 1. Lengths of the form | ||
| // (4k+1) cannot be produced by any base64 encoding of any byte string. | ||
| const validUnpaddedLength = (n) => n % 4 !== 1; |
Member
There was a problem hiding this comment.
This line is failing eslint validation also this helper is used only one time.
Suggested change
| const validUnpaddedLength = (n) => n % 4 !== 1; |
Comment on lines
+22
to
+26
| if (options.padding) { | ||
| if (str.length % 4 !== 0) return false; | ||
| } else if (!validUnpaddedLength(str.length)) { | ||
| return false; | ||
| } |
Member
There was a problem hiding this comment.
The single-use validUnpaddedLength helper and nested branching are unnecessary. Also the remainder of str.length % 4 is computed two times.
It can be easily replaced by a single check:
Suggested change
| if (options.padding) { | |
| if (str.length % 4 !== 0) return false; | |
| } else if (!validUnpaddedLength(str.length)) { | |
| return false; | |
| } | |
| const remainder = str.length % 4; | |
| const isValidLength = options.padding ? remainder === 0 : remainder !== 1; | |
| if (!isValidLength) return false; |
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).