Throw an error on invalid base64 strings #28
Merged
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.
RFC 4648 states that "implementations MUST reject the encoded data if it contains characters outside the base alphabet when interpreting base-encoded data, unless the specification referring to this document explicitly states otherwise."
RFC 7515 specifies base64url encoding as "base64 encoding using the URL- and filename-safe character set defined in Section 5 of RFC 4648, with all trailing '=' characters omitted and without the inclusion of any line breaks, whitespace, or other additional characters."
Python's
base64.urlsafe_b64decode()
does not validate its argument, ignoring unknown characters instead. It also accepts "+" and "/" in addition to "-" and "_".base64.b64decode()
's validate argument makes it validate its input, but it still accepts non-urlsafe base64 encoding as well.This commit therefore changes
util.urlsafe_b64decode()
to explicitly check for "+" and "/" in input before passing it tobase64.b64decode()
with its validate argument set toTrue
.