Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw an error on invalid base64 strings #28

Merged
merged 1 commit into from
Dec 3, 2024

Conversation

viccie30
Copy link
Contributor

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 to base64.b64decode() with its validate argument set to True.

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 "_" [1].

base64.b64decode()'s validate argument makes it validate its input, but
it still accepts non-urlsafe base64 encoding as well [1].

This commit therefore changes util.urlsafe_b64decode() to explicitly
check for "+" and "/" in input before passing it to base64.b64decode()
with its validate argument set to True.

[1]: python/cpython#125346
@viccie30 viccie30 changed the title Throw an error on non-valid base64 strings Throw an error on invalid base64 strings Nov 20, 2024
Copy link

codecov bot commented Nov 28, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (141ddf2) to head (ef339e8).
Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main       #28   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           44        44           
  Lines         2564      2567    +3     
  Branches       364       303   -61     
=========================================
+ Hits          2564      2567    +3     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@lepture lepture merged commit 2b95926 into authlib:main Dec 3, 2024
3 of 7 checks passed
@viccie30 viccie30 deleted the validate-urlsafe-b64decode branch December 3, 2024 07:03
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