facebook / hermes

A JavaScript engine optimized for running React Native.
https://hermesengine.dev/
MIT License
9.41k stars 596 forks source link

`atob` is less forgiving with padding than JSC, Node and browser based implementations #1379

Closed levibuzolic closed 2 weeks ago

levibuzolic commented 3 weeks ago

Bug Description

Hermes git revision (if applicable): 6285c8be11d22c30997e84b0cb8087bce127f1d6 React Native version: 0.74.0 OS: macOS

Steps To Reproduce

Before I dive into the issue, I wanted to thanks to the team for implementing btoa and atob, it's a great addition to the engine!

It's a common convention for Base64 strings to have their trailing padding = trimmed to make them compatible with URIs. For example the string "hello" would be encoded as aGVsbG8=, but can usually be trimmed down to aGVsbG8 and still be decoded correctly.

In JSC, Node, Chrome, Safari, Firefox:

atob("aGVsbG8=")
// "hello"

atob("aGVsbG8")
// "hello"

hermes appears to be quite strict with its validation of base64 strings and will fail on the second case:

atob("aGVsbG8=")
// "hello"

atob("aGVsbG8")
// Error: Not a valid base64 encoded string length

I also wanted to note that I'm not 100% sure if this is necessarily a bug, reading the spec for forgiving-base64 decode, I think it is. But if not, it's probably worth aligning the implementation to ensure compatibility.

dannysu commented 3 weeks ago

@levibuzolic, thank you for the report. This does look like a bug. I'll work on a fix.

dannysu commented 2 weeks ago

Fixed in Hermes with 0888914515ba3436d0e64dab05dc18166998bdb0

levibuzolic commented 2 weeks ago

@dannysu thanks so much for jumping on this so quickly. 🙇‍♂️