Skip to content

Overloaded methods of setSigningKey() behave differently  #783

@mipo256

Description

@mipo256

There is an overloaded mehtod - JwtBuilder#setSigningKey. It may accept base64 encoded String, or it may accept byte[]

Here is the implementation in your source:

    @Override
    public JwtParser setSigningKey(byte[] key) {
        Assert.notEmpty(key, "signing key cannot be null or empty.");
        this.keyBytes = key;
        return this;
    }

    @Override
    public JwtParser setSigningKey(String base64EncodedKeyBytes) {
        Assert.hasText(base64EncodedKeyBytes, "signing key cannot be null or empty.");
        this.keyBytes = TextCodec.BASE64.decode(base64EncodedKeyBytes);
        return this;
    }

It seems to me, that in 2nd method, if base64 encoded key is decoded before being set to keyBytes --> keyBytes field is a plain bytes key, i.e. not byte of base64 encoded key. However, it seems not be the case with setSigningKey(byte[] key) method.

Problem:

  1. When I pass just bytes in the UTF-8 unicode encoding to method setSigningKey(byte[] key), then the library complains about signature.

  2. However, if I pass bytes of base64 encoded key into setSigningKey(byte[] key), then the key parsed fine. The is clear contriversion in the API, because setSigningKey(byte[] key) works with base64 encoded bytes, while setSigningKey(String base64EncodedKeyBytes) before setting the key decodes the base64 string. That's really wired.

I have created the repository to demostrate this behavior: https://github.com/Mikhail2048/jjwt-base64-bug-reporduction
There are 2 test cases, you can clone and run them. Jjwt version is 0.9.1 - latest I have found publically.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions