Skip to content

Missing HMAC_SECRET When HMAC Encoding Not Raising Proper Error #526

Closed
@jonmchan

Description

According to the documentation, when utilizing the HMAC algorithm, a JWT::DecodeError should be raised if the hmac_secret is not provided.

https://github.com/jwt/ruby-jwt#hmac

# The secret must be a string. A JWT::DecodeError will be raised if it isn't provided.
hmac_secret = 'my$ecretK3y'

As it stands, there is inconsistent behavior between what version of Openssl is installed on the system. For example, for the following systems:

irb(main):001:0> puts OpenSSL::OPENSSL_VERSION
OpenSSL 1.1.1f  31 Mar 2020
=> nil                                                 
irb(main):002:0> JWT.encode( {user: 'test'}, nil, 'HS256')
=> "eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjoidGVzdCJ9.J6fk_CtqWW8QMxkcNc1olC3YI_C-B2FGpIcHegIaZ08"
irb(main):003:0> JWT.encode( {user: 'test'}, '', 'HS256')
=> "eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjoidGVzdCJ9.J6fk_CtqWW8QMxkcNc1olC3YI_C-B2FGpIcHegIaZ08"

This should not be succeeding. Not sure what is being used to generate the HMAC.

However, on newer versions of OpenSSL, we observe the following:

irb(main):001:0> puts OpenSSL::OPENSSL_VERSION
OpenSSL 3.0.5 5 Jul 2022
=> nil 
irb(main):003:0> JWT.encode( {user: 'test'}, '', 'HS256')
/Users/jmc/.rbenv/versions/3.1.2/lib/ruby/3.1.0/openssl/hmac.rb:36:in `initialize': EVP_PKEY_new_mac_key: malloc failure (OpenSSL::HMACError)
irb(main):003:0> JWT.encode( {user: 'test'}, nil, 'HS256')
/Users/jmc/.rbenv/versions/3.1.2/lib/ruby/3.1.0/openssl/hmac.rb:36:in `initialize': EVP_PKEY_new_mac_key: malloc failure (OpenSSL::HMACError)

Neither of these is the proper JWT::DecodeError as defined by the documentation. Catching this properly would be good.

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