JDK-8013902 : Base64.getMimeDecoder().decode() does not ignore padding chars
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util
  • Affected Version: 8
  • Priority: P2
  • Status: Closed
  • Resolution: Not an Issue
  • Submitted: 2013-05-04
  • Updated: 2014-08-26
  • Resolved: 2013-05-07
Related Reports
Relates :  
Relates :  
Description
import java.util.Base64;

public class StartsWithPadding {
    public static void main(String[] args) {
        Base64.getMimeDecoder().decode("$=#");
    }
} 

A following case results to:
Exception in thread "main" java.lang.IllegalArgumentException: Input byte array has wrong 4-byte ending unit
at java.util.Base64$Decoder.decode0(Base64.java:1076)
at java.util.Base64$Decoder.decode(Base64.java:695)
at java.util.Base64$Decoder.decode(Base64.java:718)
at StartsWithPadding.main(StartsWithPadding.java:8) 


This issue is reproducible in b87, looks like problem wasn't completely fixed.
Comments
The "fix" I put in with 8008925 is to clarify the spec "if there is padding character present in the final unit, the correct number of padding character(s) must be present, otherwise IAE is thrown during decoding", So, yes even with the "fix", the test case is still failing, because it has the padding character in the "final" unit, but the final unit is a wrong base64 combination . Current implementation has the following code to deal with a "padding" character, // xx= shiftto==6&&sp==sl missing last = // xx=y shiftto==6 last is not = // = shiftto==18 unnecessary padding // x= shiftto==12 be taken care later // together with single x, invalid anyway if (shiftto == 6 && (sp == sl || src[sp++] != '=') || shiftto == 18) { throw new IllegalArgumentException( "Input byte array has wrong 4-byte ending unit"); } break; The test case "$=#" is being handled as "=" (as unnecessary padding), the $ and # is being "ignored", but the "=" is not, as the padding character is part of the base64 alphabet table, which should not be ignored, even in MIME mode. The MIME base64 spec says "...All line separators or other characters not found in the base64 alphabet table are ignored in decoding operation." On 05/07/2013 05:30 AM, Dmitry Bessonov wrote: > The problem of the tests was that they treated padding char not as part of Base64 alphabet. > I have made the updates to the tests and the failures mentioned in JDK-8008925 have disappeared.
07-05-2013

a JCK8 b21 was used.
06-05-2013

A reproducible case: api/java_util/Base64/Decoder/index.html#MimeDec_NonBase64CharsIgnored
06-05-2013

Closing as incomplete until we know which version of the JCK is being used.
05-05-2013

As part of JDK-8008925, the spec was fixed to make it clear that "If there is padding character present in the final unit, the correct number of padding character(s) must be present, otherwise IllegalArgumentException is thrown during decoding". So which JCK build is used here? I just checked a build of jdk8/tl (which also has the fix for JDK-8007799) with JCK8 b21 and all of the Base64 tests are passing: 05-May-2013 09:33:51 Harness starting test run with configuration "jck_runtime_linux"... Passed: api/java_util/Base64/Decoder/index.html#DecodeSrcDstBuffersIAEs Passed: api/java_util/Base64/Decoder/index.html#DecodeFromString Passed: api/java_util/Base64/Decoder/index.html#DecodingBytes Passed: api/java_util/Base64/Decoder/index.html#DecoderNPEs Passed: api/java_util/Base64/Decoder/index.html#DecoderBasicRFCSamples Passed: api/java_util/Base64/Decoder/index.html#DecodeSrcDstBuffers Passed: api/java_util/Base64/Decoder/index.html#DecodingToArray Passed: api/java_util/Base64/Decoder/index.html#DecodingGeneralBasic Passed: api/java_util/Base64/Decoder/index.html#DecodingToBuffer Passed: api/java_util/Base64/Decoder/index.html#DecodingGeneralURLSafe Passed: api/java_util/Base64/Decoder/index.html#DecodingNonValidBase64Scheme_MIME Passed: api/java_util/Base64/Decoder/index.html#DecodingNoSpaceInArray Passed: api/java_util/Base64/Encoder/index.html#EncodeByteBufferMulti Passed: api/java_util/Base64/Decoder/index.html#MimeDec_NonBase64CharsIgnored Passed: api/java_util/Base64/Encoder/index.html#EncoderBasicRFCSamples Passed: api/java_util/Base64/Encoder/index.html#EncoderNPEs Passed: api/java_util/Base64/Encoder/index.html#EncodingMime Passed: api/java_util/Base64/Encoder/index.html#EncodingNoSpaceInArray Passed: api/java_util/Base64/Encoder/index.html#EncodingToArray Passed: api/java_util/Base64/Encoder/index.html#EncodingToBuffer Passed: api/java_util/Base64/Encoder/index.html#EncodingToString Passed: api/java_util/Base64/Encoder/index.html#EncodingURLSafe Passed: api/java_util/Base64/index.html#EncodingDecodingBasic Passed: api/java_util/Base64/index.html#Samples Passed: api/java_util/Base64/index.html#EncodingDecodingMime Passed: api/java_util/Base64/index.html#EncodingDecodingURL Passed: api/java_util/Base64/Decoder/index.html#DecodingNonValidBase64Scheme_URL Passed: api/java_util/Base64/Decoder/index.html#DecodingNonValidBase64Scheme_Basic Passed: api/java_util/Base64/index.html#EncodingDecodingMimeCustom Passed: api/java_util/Base64/index.html#GetEncoderMimeCustom 05-May-2013 09:34:34 Finished executing all tests, wait for cleanup... 05-May-2013 09:34:34 Harness done with cleanup from test run. Total time = 42s Setup time = 0s Cleanup time = 0s Test results: passed: 30 Results written to /u/alanb/testing/jck/JCK-runtime-8_b21-work
05-05-2013

Affected tests: api/java_util/Base64/Decoder/index.html#MimeDec_NonBase64CharsIgnored[downToZero_severalChars] api/java_util/Base64/Decoder/index.html#MimeDec_NonBase64CharsIgnored[downToZero_twoChars] api/java_util/Base64/Decoder/index.html#MimeDec_NonBase64CharsIgnored[validInput_ignoredSeveralCharsAtTheBeginning] api/java_util/Base64/Decoder/index.html#MimeDec_NonBase64CharsIgnored[validInput_ignoredSeveralCharsAtTheEnd] api/java_util/Base64/Decoder/index.html#DecodingNonValidBase64Scheme_MIME[wrapDecode_threeBase64Chars_ignoredInTheMiddle] api/java_util/Base64/Decoder/index.html#DecodingNonValidBase64Scheme_MIME[wrapDecode_threeBase64Chars_ignoredInTheBeginning] api/java_util/Base64/Decoder/index.html#DecodingNonValidBase64Scheme_MIME[decode_twoBase64Chars_ignoredInTheMiddle] api/java_util/Base64/Decoder/index.html#DecodingNonValidBase64Scheme_MIME[decode_twoBase64Chars_ignoredInTheEnd] api/java_util/Base64/Decoder/index.html#DecodingNonValidBase64Scheme_MIME[decode_twoBase64Chars_ignoredAtTheBeginning] api/java_util/Base64/Decoder/index.html#DecodingNonValidBase64Scheme_MIME[decode_threeBase64Chars_ignoredInTheMiddle] api/java_util/Base64/Decoder/index.html#DecodingNonValidBase64Scheme_MIME[ decode_threeBase64Chars_ignoredInTheBeginning]
04-05-2013