Skip to content

Reject characters outside the base64 alphabet in contentEncoding - #241

Merged
horejsek merged 3 commits into
horejsek:masterfrom
kakkar87d:content-encoding-strict-base64
Nov 24, 2026
Merged

horejsek merged 3 commits into
horejsek:masterfrom
kakkar87d:content-encoding-strict-base64

Conversation

@kakkar87d

@kakkar87d kakkar87d commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

contentEncoding: base64 accepts characters outside the base64 alphabet

The value is decoded with base64.b64decode(value), and with the default validate=False that drops every character outside A-Za-z0-9+/= before decoding, so only the leftover characters have to form valid base64:

>>> v = fastjsonschema.compile({'contentEncoding': 'base64'})
>>> v('aGVsbG8=" onerror="alert(1)')   # accepted
>>> v('aGVs bG8=')                     # accepted
>>> v('%%%%')                          # accepted, decodes to b''

RFC 4648 section 3.3 says decoders must reject data containing characters outside the alphabet, and the JSON-Schema-Test-Suite marks % as invalid (draft7 optional/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 a data: URL. The if 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 points contentEncoding at) still validates; other whitespace is rejected. The cases added to tests/test_string.py (draft 07 and 2019-09) fail on master and pass here, the full suite passes on Python 3.10 to 3.14, and pylint output is unchanged. CHANGELOG.txt has an entry under UNRELEASED.

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".
Comment thread fastjsonschema/draft07.py Resolved
@horejsek
horejsek merged commit 18d698f into horejsek:master Nov 24, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants