Skip to content

Add enforce_hmac_key_length configuration option#716

Merged
anakinj merged 1 commit intojwt:mainfrom
304:fix/hmac-min-key-length
Feb 15, 2026
Merged

Add enforce_hmac_key_length configuration option#716
anakinj merged 1 commit intojwt:mainfrom
304:fix/hmac-min-key-length

Conversation

@304
Copy link
Contributor

@304 304 commented Feb 14, 2026

Description

Add enforce_hmac_key_length configuration option to enforce minimum key lengths for HMAC algorithms. This is a security hardening measure that helps prevent brute-force attacks on weak keys.

Recommended minimum key lengths per RFC 7518 Section 3.2:

  • HS256: 256 bits (32 bytes)
  • HS384: 384 bits (48 bytes)
  • HS512: 512 bits (64 bytes)

See: https://www.rfc-editor.org/rfc/rfc7518.html#section-3.2

Usage:

  JWT.configuration.decode.enforce_hmac_key_length = true

For backward compatibility, it's disabled by default (false). But it would be nice to enforce it in a future major version like it's done for RSA key validation (minimum 2048 bits). For now, users must explicitly opt-in.

Checklist

Before the PR can be merged be sure the following are checked:

  • There are tests for the fix or feature added/changed
  • A description of the changes and a reference to the PR has been added to CHANGELOG.md. More details in the CONTRIBUTING.md

@304 304 force-pushed the fix/hmac-min-key-length branch 2 times, most recently from 8a2e20e to c6b8232 Compare February 14, 2026 16:14
algorithms: algorithms,
required_claims: required_claims
required_claims: required_claims,
hmac_min_key_length: hmac_min_key_length
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we instead of letting users set the min length just let the gem validate against the recommended minimums, so just enable or disable the feature. Then in future versions we could enable it by default. Something like enforce_hmac_key_length

I think this would be a bit less complex and easier for users to toggle on.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or is there a use-case where you would like to enforce a higher minimum than the default...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! I initially considered making it configurable, but default minimums should be sufficient.

I’ve pushed new changes to the branch using the enforce_hmac_key_length boolean flag instead.

Thanks for the review, @anakinj! Could you please take another look?

class Hmac
include JWT::JWA::SigningAlgorithm

MIN_KEY_LENGTHS = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Future us could wonder where are these values coming from, a comment pointing to the RFC would be useful at this point.

Add boolean toggle to enforce minimum HMAC key lengths based on RFC 7518
Section 3.2 (HS256: 32, HS384: 48, HS512: 64 bytes). Default is false
(opt-in), allowing future versions to enable by default.
@304 304 force-pushed the fix/hmac-min-key-length branch from c6b8232 to 27a6325 Compare February 15, 2026 10:12
@304 304 changed the title Add configurable HMAC minimum key length Add enforce_hmac_key_length configuration option Feb 15, 2026
Copy link
Member

@anakinj anakinj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great stuff, thanks for the addition. Next major release should swap the default to true

@anakinj anakinj merged commit d3e52e9 into jwt:main Feb 15, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants