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

Buffer.from(str, "base64") fails for URL safe base64 encoded strings #773

Closed
ainsleyrutterford opened this issue Jan 7, 2025 · 1 comment · Fixed by #777
Closed

Buffer.from(str, "base64") fails for URL safe base64 encoded strings #773

ainsleyrutterford opened this issue Jan 7, 2025 · 1 comment · Fixed by #777

Comments

@ainsleyrutterford
Copy link
Contributor

ainsleyrutterford commented Jan 7, 2025

When executing the following:

// buffer.test.js

const str = "eyJodHRwczovL2F2YXRhcnMuZ2l0aHVidXNlcmNvbnRlbnQuY29tL3UvODk1MTgwOTU_dj00In0";

const buffer = Buffer.from(str, "base64");

console.log(buffer.toString());

With Node I get:

$ node test/buffer.test.js
{"https://avatars.githubusercontent.com/u/89518095?v=4"}

With LLRT I get:

$ ./llrt test/buffer.test.js           
Error: Base64Error
  at <anonymous> (/.../packages/lambda/test/buffer.test.js:3:23)

If I replace the _ in str with / then it decodes the base64 string successfully in both Node and LLRT.

Node decodes both standard and URL safe base64 encoded strings, but LLRT only supports the standard charset at the moment as it uses the forgiving_decode_to_vec function here which only supports the standard charset.

@ainsleyrutterford
Copy link
Contributor Author

ainsleyrutterford commented Jan 7, 2025

Could LLRT first check which charset is being decoded by looking for the +/ or -_ characters, and then decode the string using the corresponding charset? That doesn't solve the problem of forgiving_decode_to_vec only supporting the standard charset though :/

Or I guess simply replacing instances of -_ with +/ before using forgiving_decode_to_vec might be sufficient?

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 a pull request may close this issue.

1 participant