Repository navigation
Reject characters outside the base64 alphabet in contentEncoding - #241
Merged
horejsek merged 3 commits intoNov 24, 2026
Merged
Conversation
contentEncoding: base64 values were decoded with base64.b64decode(value), which by default drops every character outside the base64 alphabet before decoding, so only the leftover characters had to form valid base64. Strings such as 'aGVsbG8=" onerror="alert(1)', 'aGVs bG8=' and '%%%%' were accepted, although RFC 4648 section 3.3 requires them to be rejected and the JSON-Schema-Test-Suite marks '%' as invalid. Decode with validate=True so these characters make the value fail with "must be encoded by base64".
bardrop
reviewed
Nov 20, 2026
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.
contentEncoding: base64accepts characters outside the base64 alphabetThe value is decoded with
base64.b64decode(value), and with the defaultvalidate=Falsethat drops every character outsideA-Za-z0-9+/=before decoding, so only the leftover characters have to form valid base64:RFC 4648 section 3.3 says decoders must reject data containing characters outside the alphabet, and the JSON-Schema-Test-Suite marks
%as invalid (draft7optional/content.json; that case only passes today because removing the%happens to leave a padding error). Content checks run on the data being validated, so a field declared as base64 can carry quotes, angle brackets, spaces or line breaks into code that expects the validated value to be plain base64, for example when it is put into adata:URL. Theif data == ""check after the decode compares bytes with a str and never fires, so input that decodes to nothing is not caught either.The decode now passes
validate=True. Over about 8.7M generated strings per version on Python 3.10 to 3.14, the new check never accepts a string the old one rejected and never accepts a character outside the alphabet (on 3.12 and later it also rejects misplaced=padding). CR and LF are stripped before the decode so MIME-style wrapped base64 (RFC 2045, which draft 07 pointscontentEncodingat) still validates; other whitespace is rejected. The cases added totests/test_string.py(draft 07 and 2019-09) fail onmasterand pass here, the full suite passes on Python 3.10 to 3.14, and pylint output is unchanged.CHANGELOG.txthas an entry under UNRELEASED.