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

btoa error with special characters #39088

Closed
Tofandel opened this issue Jun 19, 2021 · 3 comments
Closed

btoa error with special characters #39088

Tofandel opened this issue Jun 19, 2021 · 3 comments
Labels
buffer Issues and PRs related to the buffer subsystem.

Comments

@Tofandel
Copy link
Contributor

Tofandel commented Jun 19, 2021

  • Version: 16.3.0
  • Platform: Linux
  • Subsystem: Ubuntu

What steps will reproduce the bug?

node -e "console.log(btoa(Buffer.from([0x72, 0xbe, 0x72, 0x91, 0xfb, 0xf6, 0x40, 0x0f, 0x87, 0xc4, 0x45, 0x5e, 0x23, 0xd0, 0x1c, 0xd5])))"

How often does it reproduce? Is there a required condition?

Throws an error

What is the expected behavior?

No error and cr5ykfv2QA+HxEVeI9Ac1Q== logged

What do you see instead?

InvalidCharacterError: Invalid character
      at btoa (node:buffer:1226:7)

The correct implementation of btoa which doesn't throw an error

const btoa = function (str) {
    return Buffer.from(str, 'binary').toString('base64');
};
@Tofandel Tofandel reopened this Jun 19, 2021
@Tofandel Tofandel changed the title btoa errors with special characters since v16 btoa errors with special characters Jun 19, 2021
@Tofandel Tofandel changed the title btoa errors with special characters btoa error with special characters Jun 19, 2021
@targos targos added the buffer Issues and PRs related to the buffer subsystem. label Jun 19, 2021
@targos
Copy link
Member

targos commented Jun 19, 2021

That's because btoa is supposed to be called with a string argument, not a Buffer.
When you pass something that is not a string, it is coerced automatically. With a Buffer, it is done by calling buffer.toString(), which converts the Buffer to a string using UTF-8 encoding. btoa expects Latin-1 encoding.

@Tofandel
Copy link
Contributor Author

Tofandel commented Jun 19, 2021

That's a weird spec.. I can see this mentioned in #3462.. I guess custom implementation will still be the preferred way

@kiwavi
Copy link

kiwavi commented May 16, 2024

That's because btoa is supposed to be called with a string argument, not a Buffer. When you pass something that is not a string, it is coerced automatically. With a Buffer, it is done by calling buffer.toString(), which converts the Buffer to a string using UTF-8 encoding. btoa expects Latin-1 encoding.

When I pass a string like 𝑯𝒆𝒍𝒍𝒐 I'm getting DOMException [InvalidCharacterError]: Invalid character. Does it only support certain fonts? Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
buffer Issues and PRs related to the buffer subsystem.
Projects
None yet
Development

No branches or pull requests

3 participants