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

Base64.getMimeDecoder().decode() throws IAE for a single non-base64 character

    XMLWordPrintable

Details

    • 8
    • b77
    • Verified

    Description

      The spec
      http://download.java.net/jdk8/docs/api/java/util/Base64.html
      specifies for Mime decoder:

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

      However this is not so if the string being processed contains a single non-Base64 character.

      Please see the following sample

      import java.util.Arrays;
      import java.util.Base64;

      public class SingleNonB64AlphCharNotIgnored {

          public static void main(String[] args) {

              for (String ignoredChar : Arrays.asList("#", "(", "!", "\\", "-", "_")) {
                  try {
                      final byte[] decodedBytes = Base64.getMimeDecoder().decode(ignoredChar);
                      System.err.println("decodedBytes.length = " + decodedBytes.length);
                  } catch (Exception e) {
                      System.err.println(e + " thrown for '" + ignoredChar + "'");
                  }
              }
          }

      }


      The output is:

      java.lang.IllegalArgumentException: Input byte[] should at least have 2 bytes for base64 bytes thrown for '#'
      java.lang.IllegalArgumentException: Input byte[] should at least have 2 bytes for base64 bytes thrown for '('
      java.lang.IllegalArgumentException: Input byte[] should at least have 2 bytes for base64 bytes thrown for '!'
      java.lang.IllegalArgumentException: Input byte[] should at least have 2 bytes for base64 bytes thrown for '\'
      java.lang.IllegalArgumentException: Input byte[] should at least have 2 bytes for base64 bytes thrown for '-'
      java.lang.IllegalArgumentException: Input byte[] should at least have 2 bytes for base64 bytes thrown for '_'


      Exception is thrown not by all decoding methods - for example these two methods decode passed single non-base64 char to zero-size byte array as expected:

      Decoder.decode(ByteBuffer src, ByteBuffer dst)
      Decoder.wrap(InputStream is)



      The following JCK testcase will fail due to this issue:

      api/java_util/Base64/Decoder/index.html#MimeDec_NonBase64CharsIgnored[downToZero_singleChar]

      Attachments

        Activity

          People

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

            Dates

              Created:
              Updated:
              Resolved: