Details
-
Bug
-
Resolution: Fixed
-
P2
-
None
-
b77
-
Verified
Description
For two-character and three-character Base64 strings IAE is not thrown by "eager" decoding methods.
RFC 4648 says that an implementation must by default provide padding when encoding (if not specified otherwise) so this makes me think that by default a string with a dropped padding is not a valid Base64 scheme and IAE should be thrown by decoding methods.
Or this aspect needs to be specified.
However if an encoded stream containing not padded data is wrapped and then decoded
then an exception is thrown. Seems that there is a behavioral inconsistency here.
Please see the following code sample:
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Base64;
import static java.nio.charset.StandardCharsets.*;
public class WrapDecodeNoPaddingStream {
public static void main(String[] args) throws IOException {
final Base64.Decoder decoder = Base64.getDecoder();
// decodes to "A"
System.err.println(
new String(decoder.decode("QQ"), US_ASCII)
);
// throws java.io.IOException: Base64 stream has un-decoded dangling byte(s).
decoder.wrap(
new ByteArrayInputStream(
"QQ".getBytes(US_ASCII)
)).read();
}
}
The output will be:
A
Exception in thread "main" java.io.IOException: Base64 stream has un-decoded dangling byte(s).
at java.util.Base64$DecInputStream.read(Base64.java:1251)
at java.util.Base64$DecInputStream.read(Base64.java:1224)
at WrapDecodeNoPaddingStream.main(WrapDecodeNoPaddingStream.java:22)
RFC 4648 says that an implementation must by default provide padding when encoding (if not specified otherwise) so this makes me think that by default a string with a dropped padding is not a valid Base64 scheme and IAE should be thrown by decoding methods.
Or this aspect needs to be specified.
However if an encoded stream containing not padded data is wrapped and then decoded
then an exception is thrown. Seems that there is a behavioral inconsistency here.
Please see the following code sample:
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Base64;
import static java.nio.charset.StandardCharsets.*;
public class WrapDecodeNoPaddingStream {
public static void main(String[] args) throws IOException {
final Base64.Decoder decoder = Base64.getDecoder();
// decodes to "A"
System.err.println(
new String(decoder.decode("QQ"), US_ASCII)
);
// throws java.io.IOException: Base64 stream has un-decoded dangling byte(s).
decoder.wrap(
new ByteArrayInputStream(
"QQ".getBytes(US_ASCII)
)).read();
}
}
The output will be:
A
Exception in thread "main" java.io.IOException: Base64 stream has un-decoded dangling byte(s).
at java.util.Base64$DecInputStream.read(Base64.java:1251)
at java.util.Base64$DecInputStream.read(Base64.java:1224)
at WrapDecodeNoPaddingStream.main(WrapDecodeNoPaddingStream.java:22)