Skip to content

Consider using a different Base64 decoder #143

@omsmith

Description

@omsmith

As per auth0/node-jsonwebtoken#208, some users of your library are getting stuck on passing in a String as a secret. My initial response was "that's not base64, it should be throwing" however given the decoder used, that doesn't appear to be the case. I expect the experience would be much improved if a proper base64 decoder was used, which complained about invalid base64.

As an example, here's the difference between the current parseBase64Binary and Java 8's Base64:

private static void printWithBadDecoder( String in ) {
  System.out.println(Arrays.toString(
    javax.xml.bind.DatatypeConverter.parseBase64Binary( in )
  ));
}

private static void printWithGoodDecoder( String in ) {
  System.out.println(Arrays.toString(
    Base64.getDecoder().decode( in )
  ));
}

public static void main( String[] args ) {
  printWithBadDecoder( "my-secret-token-to-change-in-production" );
  printWithBadDecoder( "mysecrettokentochangeinproduction" );
  printWithBadDecoder( "mysecrettokentochangeinproductio" );

  printWithGoodDecoder( "my-secret-token-to-change-in-production" );
  printWithGoodDecoder( "mysecrettokentochangeinproduction" );
  printWithGoodDecoder( "mysecrettokentochangeinproductio" );
}
[-101, 43, 30, 114, -73, -83, -74, -119, 30, -98, -38, 28, -123, -87, -32, 122, 41, -23, -82, -121, 110, 114, -40, -88]
[-101, 43, 30, 114, -73, -83, -74, -119, 30, -98, -38, 28, -123, -87, -32, 122, 41, -23, -82, -121, 110, 114, -40, -88]
[-101, 43, 30, 114, -73, -83, -74, -119, 30, -98, -38, 28, -123, -87, -32, 122, 41, -23, -82, -121, 110, 114, -40, -88]
Exception in thread "main" java.lang.IllegalArgumentException: Illegal base64 character 2d
Exception in thread "main" java.lang.IllegalArgumentException: Last unit does not have enough valid bits
[-101, 43, 30, 114, -73, -83, -74, -119, 30, -98, -38, 28, -123, -87, -32, 122, 41, -23, -82, -121, 110, 114, -40, -88]

As you can see, the current decoder skips over illegal characters, and then afterward doesn't require it be correctly sized (ignoring any remaining characters).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions