Closed richarddd closed 6 months ago
See https://infra.spec.whatwg.org/#forgiving-base64-decode
The spec does not allow your cases. You can test the behavior via any js runtime. A workaround is to strip all '=' characters from the end of string.
Welcome to Node.js v20.9.0.
Type ".help" for more information.
> atob("SGVsbG8sIHdvcmxkIQ")
'Hello, world!'
> atob("SGVsbG8sIHdvcmxkIQ=")
Uncaught:
DOMException [InvalidCharacterError]: Invalid character
at new DOMException (node:internal/per_context/domexception:53:5)
at __node_internal_ (node:internal/util:695:10)
at atob (node:buffer:1343:11)
> atob("aGVsbG8")
'hello'
> atob("aGVsbG8==")
Uncaught:
DOMException [InvalidCharacterError]: Invalid character
at new DOMException (node:internal/per_context/domexception:53:5)
at __node_internal_ (node:internal/util:695:10)
at atob (node:buffer:1343:11)
Currently forgiving mode handles no padding properly:
decoding
SGVsbG8sIHdvcmxkIQ
=Hello, world!
but decoding to little padding
SGVsbG8sIHdvcmxkIQ=
errors.It would be great if forgiving mode appends the correct padding. Similarly it could also ignore extra padding:
aGVsbG8==
=hello