Repository navigation
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".
| with self.l('try:'): | ||
| self.l('import base64') | ||
| self.l('{variable} = base64.b64decode({variable})') | ||
| self.l('{variable} = base64.b64decode({variable}, validate=True)') |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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.