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

base64::decode is too strict #5239

Closed
danpayne17 opened this issue May 24, 2024 · 2 comments · Fixed by #5252
Closed

base64::decode is too strict #5239

danpayne17 opened this issue May 24, 2024 · 2 comments · Fixed by #5252
Assignees

Comments

@danpayne17
Copy link

danpayne17 commented May 24, 2024

Describe the bug
The base64::decode Rhai function is too strict and does not support decoding base64 strings that exclude padding characters and it does not support decoding base64 strings that were encoded using the URL-safe alphabet. Instead, we have to manually add the padding and convert all characters exclusive to URL-safe alphabet to the standard alphabet:

  let remainder = base64_encoded_str.len % 4;
  switch remainder {
    2 => base64_encoded_str += "==",
    3 => base64_encoded_str += "=",
  }

  base64_encoded_str.replace("-", "+");
  base64_encoded_str.replace("_", "/");

  base64::decode(base64_encoded_str);

We use the base64::decode function to decode JWT tokens (we can't use the router's built-in JWT decoding feature for...reasons) and these tokens always exclude padding and always use the URL-safe alphabet but they are not compatible with this decode method.

More information about base64 alphabets and padding can be found here on this related Rust page: https://docs.rs/base64/latest/base64/
And Wikipedia's section for base64 on URL applications: https://en.wikipedia.org/wiki/Base64#URL_applications

To Reproduce
Steps to reproduce the behavior:

  1. Decode the following base64 string using base64::decode in a Rhai script: LSTCoXlGeBXCuMORSkHCgTzCpMKAw6jDkcOTw7t-IEDCrG7Dl8OyHn_DmTAdyZ0lw6zDksOHfwfCjA
  2. Observe it throws an error

Expected behavior
It decodes the base64 encoded string without error

Router:

  • Version: 1.45.1

Additional context
Add any other context about the problem here.

@Geal
Copy link
Contributor

Geal commented May 27, 2024

thanks for the report. The rhai base64 decoding supports multiple alphabets already, but it looks like it was not documented: #3885

@Geal
Copy link
Contributor

Geal commented May 27, 2024

I just looked into it, the base64 alphabet documentation was moved to the right place

@Geal Geal self-assigned this May 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants