Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8008925

Base64.getMimeDecoder().decode() does not ignore padding chars

XMLWordPrintable

    • b86

      The spec http://download.java.net/jdk8/docs/api/java/util/Base64.html says:

      "MIME
      ... All line separators or other characters not found in the base64 alphabet table are ignored in decoding operation."

      This is not so if the decoded string has to-be-ignored padding character. The following code produces an IAE:

      ----------------------------------------------------------------------
      import java.util.Base64;

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

      This problem exists for all decoding methods for a MIME decoder.

      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)


      The following testcases will fail due to this issue:

      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]

            sherman Xueming Shen
            dbessono Dmitry Bessonov
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: