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') missing the last byte #2063

Closed
YUxiangLuo opened this issue Feb 13, 2023 · 2 comments · Fixed by #2079
Closed

Buffer.from('str', 'base64') missing the last byte #2063

YUxiangLuo opened this issue Feb 13, 2023 · 2 comments · Fixed by #2079
Labels
bug Something isn't working node.js Compatibility with Node.js APIs

Comments

@YUxiangLuo
Copy link
Contributor

What version of Bun is running?

0.5.6

What platform is your computer?

Linux ip-172-26-7-81 5.4.0-1018-aws #18-Ubuntu SMP Wed Jun 24 01:15:00 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

What steps can reproduce the bug?

// b.js
let x = Buffer.from("eyJlbWFpbCI6Ijg3MTg4NDYxN0BxcS5jb20iLCJpZCI6OCwicm9sZSI6Im5vcm1hbCIsImlhdCI6MTY3NjI4NDQyMSwiZXhwIjoxNjc2ODg5MjIxfQ", 'base64'); console.log(x.length, x[82], x[83], x[84]); console.log(x.toString('utf-8'));

// bun run b.js

What is the expected behavior?

85 50 49 125
{"email":"871884617@qq.com","id":8,"role":"normal","iat":1676284421,"exp":1676889221}

What do you see instead?

84 50 49 undefined
{"email":"871884617@qq.com","id":8,"role":"normal","iat":1676284421,"exp":1676889221

Additional information

No response

@YUxiangLuo YUxiangLuo added the bug Something isn't working label Feb 13, 2023
@Electroid Electroid added the node.js Compatibility with Node.js APIs label Feb 13, 2023
@jwhear jwhear mentioned this issue Feb 15, 2023
@jwhear
Copy link
Contributor

jwhear commented Feb 15, 2023

This is an interesting issue. The test string provided is not strictly valid base64 as it is missing padding and not a multiple of 4 characters. So in one sense the issue is that Bun is silently failing here and returning an incorrect length. A "true" fix would be to use base64url as your encoding type.
However... Node is more permissive and silently allows missing padding characters, see this issue thread. So in the interest of matching Node's behavior, I think we need to make our decoder permissive of missing padding and whitespace.

@jwhear
Copy link
Contributor

jwhear commented Feb 15, 2023

And it looks like the decoder is already permissive, so it's just a matter of ensuring that the length calculation is permissive of potentially missing padding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working node.js Compatibility with Node.js APIs
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants