Skip to content

Reject characters outside the base64 alphabet in contentEncoding - #241

Open
kakkar87d wants to merge 2 commits into
horejsek:masterfrom
kakkar87d:content-encoding-strict-base64
Open

kakkar87d wants to merge 2 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
with self.l('try:'):
self.l('import base64')
self.l('{variable} = base64.b64decode({variable})')
self.l('{variable} = base64.b64decode({variable}, validate=True)')

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

validate=True also rejects line breaks, so MIME-style base64 (wrapped at 76 chars) now fails. Draft 07 points contentEncoding at RFC 2045 section 6.1, and that form wraps lines. It passes on master:

import email.base64mime, fastjsonschema
v = fastjsonschema.compile({"$schema": "http://json-schema.org/draft-07/schema", "contentEncoding": "base64"})
v(email.base64mime.body_encode(bytes(range(100))))  # master: ok, this PR: "data must be encoded by base64"

One option is to drop CR/LF before the strict decode, so the " / space / % cases in the test are still rejected:

self.l("{variable} = base64.b64decode({variable}.replace(\"\\r\", \"\").replace(\"\\n\", \"\"), validate=True)")

The aGVsbG8=\n test case would then become valid.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, I'd missed that draft 07 points at RFC 2045. Pushed your suggestion: CR and LF are stripped before the strict decode (on its own generated line to keep pylint happy), so your wrapped body_encode example validates again while the quote, space and % cases are still rejected. Moved the trailing-newline case over to a new test with CRLF and a 76-column wrapped value, and updated the PR description to match.

This branch has not been deployed

No deployments
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.

2 participants