Skip to content
New issue

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

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

Already on GitHub? # to your account

Base64URL valid characters are not properly validated #69

Closed
pacu opened this issue Nov 20, 2024 · 1 comment · Fixed by #70
Closed

Base64URL valid characters are not properly validated #69

pacu opened this issue Nov 20, 2024 · 1 comment · Fixed by #70
Labels
bug Something isn't working

Comments

@pacu
Copy link
Collaborator

pacu commented Nov 20, 2024

reported Electric-Coin-Company/zashi-ios#1420 here

@pacu pacu added the bug Something isn't working label Nov 20, 2024
@daira
Copy link
Contributor

daira commented Nov 21, 2024

I have little time to work on this, but I see the problem. It's here:

var base64 = base64URL.replacingOccurrences(of: "_", with: "/")
.replacingOccurrences(of: "-", with: "+")

This converts the base64url string into a base64 string just by substituting / in place of _ and + in place of -. This doesn't detect cases where the string already contained / or +, or contains = (which ZIP 321 also prohibits).

Before doing this substitution, it should be explicitly checking that the string contains only the allowed base64url characters. That is, the input should be checked to match the regex [-_A-Za-z0-9]* . (I don't know whether that's the simplest way to implement it using the Swift APIs.)

Note that we can't assume that Data(base64Encoded:) doesn't have undocumented Postel's Rule acceptance of other strings that are not strictly valid base64. So, we have to restrict our use of it to strings that we know are valid base64, because they were transformed from valid base64url. As it happens, the character set restriction is the only check needed, because a string of any number of base64url-allowed characters is a valid base64url.

@pacu pacu changed the title Create test to reproduce invalid characters not accepted and fix bugs Base64URL valid characters are not properly validated Nov 21, 2024
@pacu pacu closed this as completed in #70 Nov 26, 2024
pacu added a commit that referenced this issue Nov 26, 2024
[#69] Fix: Base64URL valid characters are not properly validated
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants