dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.22k stars 1.57k forks source link

Base64.decode throws FormatException Invalid length, must be multiple of four #39510

Open adiospace opened 4 years ago

adiospace commented 4 years ago

Issue with dart:convert

base64.decode or base64Url.decode throws a FormatException('Invalid length, must be multiple of four") when decoding the following base64 encoded string "eyJlbWFpbCI6ImFnb2xhcnVAZ21haWwuY29tIiwibmFtZSI6IkFkcmlhbiBPbGFydSIsInRva2VuIjoiS3FBd2J2M3A0cVZVM3V1Mzg4Z3dqQUVMIiwidXNlcm5hbWUiOiJhZHJpYW5fb2xhcnUiLCJpZCI6NDAyNjIsInJvbGUiOiJ1c2VyIn0".

It works just fine in JavaScript env using atob.

To make it work I had to normalize it first using base64.normalize.

We can close this if it's ok to normalize it explicitly I was just expecting it to work directly using base64.decode method.

Dart VM version: 2.6.0 (Thu Oct 24 17:52:22 2019 +0200) on "macos_x64"

lrhn commented 4 years ago

Dart's base64 decoder is deliberately pedantic. That increases the chance that an accidentally truncated input string is detected (it has a 3/4 chance of being invalid).

Maybe we should add an option to accept non-padded data, instead of requiring it to be normalized before decoding.

libill commented 4 years ago

when it has non-padded data,we can add padded data(=) before decoder it.

trailerzone commented 3 years ago

is there a way to handle the error when it is throw?