Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

feat: Warn about keys and tokens the next major version will reject - #823

Open
anakinj wants to merge 2 commits into
jwt:mainfrom
anakinj:feat/deprecation-warnings
Open

anakinj wants to merge 2 commits into
jwt:mainfrom
anakinj:feat/deprecation-warnings

Conversation

@anakinj

@anakinj anakinj commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

Description

Adds deprecation warnings for keys and tokens that still work but that the next major version will reject, as planned in #819, #820 and #821.

The helper

JWT::Deprecations.warning(message) prints [DEPRECATION WARNING] <message> with Kernel#warn. It follows the existing JWT.configuration.deprecation_warnings setting:

  • :once, the default, prints each message once per process;
  • :warn prints every time;
  • :silent prints nothing.

It is much smaller than the JWT::Deprecations module from 2.x. There is no thread-local store and no "only if the token turns out valid" context. Each warning is printed at a point where it is already known to be relevant:

Warning When it is printed
HMAC secret shorter than the hash output (32, 48 or 64 bytes for HS256, HS384 or HS512) when signing with it, and when a signature verifies with it, unless enforce_hmac_key_length already rejects it
RSA key shorter than 2048 bits when an RS* or PS* signature verifies with it (signing already raises)
PSS salt not as long as the hash when a PS* signature verifies only with another salt length
alg header that only matches when the case is ignored, such as hs256 after the token's signature has verified

When verifying, a warning is printed only after the signature has verified, so a forged token can't trigger one. The exception is a header like NONE when the caller allows the none algorithm. The messages contain no keys and no token content except the alg value, which is a case variant of the algorithm's name.

Behaviour changes

  • PSS: PS* signatures are verified with the hash length first, and with any salt length (:auto) only if that fails. The accepted signatures are the same as before: :digest accepts a subset of what :auto accepts. An invalid signature costs a second public-key verification.
  • Custom HMAC algorithms: a custom algorithm such as JWT::JWA::Hmac.new('HS1', OpenSSL::Digest::SHA1) has no listed minimum length. With enforce_hmac_key_length on, it used to raise a plain ArgumentError. Now its minimum is the length of its hash output, 20 bytes for SHA1, as Enforce minimum HMAC key lengths by default #811 suggested. enforce_hmac_key_length applies to it, and it gets the warning.
  • Specs: spec_helper.rb sets deprecation_warnings to :silent instead of :warn. Most specs use short HMAC secrets such as 'secret', which would print a warning in nearly every example. The specs that check warnings turn them on.

UPGRADING.md has a new section that lists the warnings and what to do about each. I titled it 3.4.0, assuming release-please makes this a minor release. The README's HMAC examples now use a 34-byte secret, the HMAC section states the minimum lengths, and a new section describes deprecation_warnings.

Specs

  • deprecations_spec.rb: :warn, :once and :silent.
  • hmac_spec.rb:
    • a short secret prints the warning when signing, and when a signature verifies;
    • it prints nothing when the signature doesn't match, or when enforce_hmac_key_length rejects the key;
    • a custom HMAC algorithm gets its hash length as the minimum, both for the check and for the warning.
  • rsa_spec.rb and ps_spec.rb: a 1024-bit key prints the warning when a signature verifies, and nothing when it doesn't. A 2048-bit key prints nothing.
  • ps_spec.rb: salt lengths 0 and the maximum verify and print the warning. The hash length and invalid signatures print nothing.
  • jwt_spec.rb:
    • an hs256 header prints the warning, through JWT.decode and with a JWK as the key;
    • nothing is printed for a wrong secret, a lowercase algorithm: option, a header that names another allowed algorithm, or a custom algorithm that accepts another name;
    • nothing is printed for a forged HS512 token when the key is too short for HS512.

The new specs fail without the change, and mutations of each condition break at least one spec. The full suite passes and RuboCop is clean.

Closes #821
Refs #819, #820, #811, #812

BEGIN_COMMIT_OVERRIDE
feat: Warn about HMAC secrets shorter than RFC 7518 requires and RSA keys under 2048 bits
feat: Warn about PSS signatures whose salt is not as long as the hash
feat: Warn about alg headers that only match when the case is ignored
fix: Use the hash length as the minimum key length for custom HMAC algorithms
END_COMMIT_OVERRIDE

Checklist

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

  • The pull request title follows Conventional Commits, for example fix: Reject tokens with too many segments
  • There are tests for the fix or feature added/changed

Posted by Claude on behalf of @anakinj.

A small JWT::Deprecations helper prints a warning according to
JWT.configuration.deprecation_warnings: once per message by default,
every time with :warn, and never with :silent.

Warnings are printed when:
- signing or verifying with an HMAC secret shorter than RFC 7518
  requires, unless enforce_hmac_key_length already rejects it;
- verifying with an RSA key shorter than 2048 bits;
- a PS256, PS384 or PS512 signature only verifies with a salt length
  other than the hash length;
- the alg header only matches the verifying algorithm when the case is
  ignored.

PSS signatures are now verified with the hash length first, and with
any salt length only if that fails. The PSS and alg warnings are only
printed after the signature has verified.

The specs run with warnings silenced, and the warning specs turn them
on.

@anakinj anakinj left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Notes from an independent review. Merge after small changes.

The core holds up:

  • PSS: the :digest-first check accepts exactly what main accepts. Salt lengths 0, 20, :max and the hash length on PS256, PS384 and PS512, with 1024- and 2048-bit keys, give the same results as main. The second verification is a public-key operation, so it leaks nothing secret.
  • HMAC: the rework of validate_key! keeps every behaviour from main except the crash for custom HMAC algorithms.
  • :once: it is thread-safe. 50 threads making 200 calls each printed each message once.
  • Ruby 2.5: CI ran the new PSS specs with OpenSSL 1.1.1w, and they passed.

The line comments cover the alg check giving a false warning for custom algorithms, key warnings that a forged token can trigger, custom HMAC algorithms, two spec gaps, and the none case in UPGRADING.md.

Outside the diff:

  • PR description: "a forged token can't trigger the key warnings" isn't accurate, as the comment on hmac.rb:59 shows.
  • README: the HS256 examples at README.md:76 and README.md:224 use 'my$ecretK3y', which is 11 bytes, so copying them now prints the HMAC warning. 32-byte example secrets would avoid that. The README also never mentions deprecation_warnings.
  • Changelog: BEGIN_COMMIT_OVERRIDE has no entry for the custom HMAC enforce_hmac_key_length change, so it won't reach the CHANGELOG.

Checked without finding problems:

  • UPGRADING.md: the RFC wording, "signing already raises", and the algorithm option staying case-insensitive. "ruby-jwt has always signed that way" is true for every release with PS support.
  • Logs: only alg can appear in a warning, and for built-in algorithms only as a case variant of a registered name.
  • EdDSA: headers like eddsa and ED25519 from jwt-eddsa 2.0.0 warn as expected.
  • Spec helper: the switch from :warn to :silent hides nothing, because nothing on main printed warnings and no spec checked stderr.
  • Mutations: the specs catch most of the ones tried. The three that survive are covered in the spec comments.

The full suite passes (920 examples), RuboCop is clean and all 16 CI checks pass.

Posted by Claude on behalf of @anakinj.

Comment thread lib/jwt/encoded_token.rb Outdated

def warn_about_alg_case(jwa)
alg = header['alg']
return if !alg.is_a?(String) || jwa.alg == alg || !jwa.valid_alg?(alg)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

jwa.valid_alg?(alg) stands in for "matches when the case is ignored", but custom algorithms can override valid_alg?. One that accepts aliases gets a wrong warning. For example, take alg 'HS256' with valid_alg? accepting %w[HS256 HMAC-SHA256]. A valid token with the header HMAC-SHA256 prints:

[DEPRECATION WARNING] The alg header HMAC-SHA256 only matches HS256 when the case is ignored. ...

That also means the alg written to the log isn't limited to a case variant of a known name. Comparing the names directly fixes both, and the suite stays green:

return unless alg.is_a?(String) && jwa.alg.is_a?(String) && jwa.alg != alg && jwa.alg.casecmp?(alg)

Posted by Claude on behalf of @anakinj.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in 6fe646e. The check now compares the names directly: jwa.alg != alg && jwa.alg.casecmp?(alg). A new spec uses a custom algorithm whose valid_alg? also accepts HMAC-SHA256, and it prints no warning. Going back to valid_alg? fails that spec.

Posted by Claude on behalf of @anakinj.

Comment thread lib/jwt/jwa/hmac.rb Outdated

yield "HMAC key must be at least #{min_length} bytes for #{alg} algorithm" if JWT.configuration.decode.enforce_hmac_key_length

Deprecations.warning("HMAC keys shorter than #{min_length} bytes for #{alg} are deprecated and will be rejected in the next " \

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

When verifying, this warning comes before the signature is checked, as do the RSA warnings in rsa.rb and ps.rb. So a forged token can trigger it when several algorithms or keys are allowed:

  • HMAC: a 40-byte secret with algorithms: %w[HS256 HS512] and a forged token whose header is HS512 raises VerificationError, but prints HMAC keys shorter than 64 bytes for HS512 are deprecated .... Real HS256 tokens never print it.
  • RSA: with the keys [rsa2048.public_key, rsa1024.public_key] and RS256, valid tokens print nothing, but a forged one prints the short RSA key warning.

The text is fixed and :once limits it. Under :warn, though, it is one line per forged request, and it can point at a key or algorithm no accepted token used.

Should verifying warn only when the signature verified, like the PSS and alg warnings? For example, validate_key! could return whether the key is short. Then sign warns every time, and verify warns only when secure_compare is true. The same works for Rsa#verify and Ps#verify.

Posted by Claude on behalf of @anakinj.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes. In 6fe646e, verifying warns only when the signature verified, for HMAC, RS* and PS*. Signing still warns every time. New specs:

  • a short HMAC secret, and a 1024-bit key with RS256 or PS256, print nothing when the signature does not match;
  • a forged HS512 token with algorithms: %w[HS256 HS512] and a 32-byte secret prints nothing through JWT.decode.

The PR description is updated to match.

Posted by Claude on behalf of @anakinj.

Comment thread lib/jwt/jwa/hmac.rb Outdated

min_length = MIN_KEY_LENGTHS[alg]
yield "HMAC key must be at least #{min_length} bytes for #{alg} algorithm" if key.bytesize < min_length
return unless min_length && key.bytesize < min_length

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

With enforce_hmac_key_length = true, a custom JWT::JWA::Hmac.new('HS1', OpenSSL::Digest::SHA1) crashed on main with ArgumentError: comparison of Integer with nil failed. Now it signs with 'secret' with no error and no warning, so the option silently does nothing for custom HMAC algorithms.

#811 suggested taking the minimum from the hash size instead. That keeps the option meaningful, and these algorithms would get the warning too:

min_length = MIN_KEY_LENGTHS.fetch(alg) { digest.new.digest_length }

That is 20 bytes for SHA1, and it would change the new HS1 spec. If the check stays skipped, it is worth a line in UPGRADING.md and in the commit override.

Posted by Claude on behalf of @anakinj.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Went with the hash length: MIN_KEY_LENGTHS.fetch(alg) { digest.new.digest_length }. With enforce_hmac_key_length on, HS1 with 'short' now raises HMAC key must be at least 20 bytes for HS1 algorithm. With it off, it prints the warning. UPGRADING.md mentions it, and the commit override has a fix: entry for it.

Posted by Claude on behalf of @anakinj.

Comment thread spec/jwt/jwa/hmac_spec.rb
end
end

context 'when deprecation warnings are printed' do

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

No spec checks that a short key prints no warning when enforce_hmac_key_length rejects it. Moving the Deprecations.warning call above the yield in validate_key! leaves the suite green. Something like:

context 'when enforce_hmac_key_length rejects the key' do
  before { JWT.configuration.decode.enforce_hmac_key_length = true }

  it 'prints no warning' do
    expect { expect { subject }.to raise_error(JWT::EncodeError) }.not_to output.to_stderr
  end
end

Posted by Claude on behalf of @anakinj.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Added in 6fe646e. "When enforce_hmac_key_length rejects the key" expects JWT::EncodeError and no output. Printing the warning before the check now fails 3 specs.

Posted by Claude on behalf of @anakinj.

Comment thread spec/jwt/jwt_spec.rb
end.not_to output.to_stderr
end

it 'prints no warning when only the given algorithm is in another case' do

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

No spec covers two parts of warn_about_alg_case:

  • Mismatched algorithms: removing || !jwa.valid_alg?(alg) passes the suite. Without that check, EncodedToken.new(token).valid_signature?(algorithm: %w[HS256 HS512], key: secret) on a token with an HS512 header and an HS256 signature prints The alg header HS512 only matches HS256 when the case is ignored.
  • JWKs: limiting the warning to JWA::VerifierContext also passes the suite.

One spec for the mismatched case (no warning) and one with key: JWT::JWK.new(hmac_secret, alg: 'HS256') and the hs256 token (warning) would cover them. They might fit better in encoded_token_spec.rb.

Posted by Claude on behalf of @anakinj.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Added both next to the other alg specs:

  • a token with an HS512 header and an HS256 signature, verified with algorithm: %w[HS256 HS512], prints nothing;
  • the hs256 token verified with JWT::JWK.new(hmac_secret, alg: 'HS256') prints the warning.

Dropping the casecmp? check now fails 2 specs.

Posted by Claude on behalf of @anakinj.

Comment thread UPGRADING.md Outdated
- **PS256, PS384 and PS512 signatures whose salt is not as long as the hash.** RFC 7518 requires the hash length. ruby-jwt has always signed that way, so only tokens from other libraries can be affected.
- **An `alg` header that only matches when the case is ignored,** such as `hs256` for HS256. Algorithm names are case-sensitive (RFC 7515). The `algorithm` option of `JWT.decode` and `JWT.encode` can stay in any case, because only the token's header is checked.

The PSS and `alg` warnings are printed only for tokens whose signature verified, so tokens from an attacker don't print them.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This doesn't hold when none is allowed. With algorithm: 'none' and a key given, an unsigned token with the header NONE decodes, as it does on main. It now also prints The alg header NONE only matches none when the case is ignored ..., so anyone can trigger that warning in this setup. It is limited to the 16 case variants of none. Maybe "tokens whose signature verified, or that use an allowed none algorithm".

Posted by Claude on behalf of @anakinj.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Reworded to: "When verifying, the warnings are printed only after the signature has verified, so a forged token doesn't print them. The exception is a token whose alg header is none in another case, such as NONE, when you allow the none algorithm."

Posted by Claude on behalf of @anakinj.

… a minimum

When verifying, the short HMAC secret and RSA key warnings were printed
before the signature was checked, so a forged token could print them
when several algorithms or keys were allowed. They are now printed
only when the signature verifies, like the PSS and alg warnings.
Signing still warns every time.

The alg warning compared the header using valid_alg?, which custom
algorithms can override to accept other names. It now compares the
names directly, ignoring the case.

A custom HMAC algorithm has no listed minimum length. The minimum is
now the length of its hash output, so enforce_hmac_key_length applies
to it, and it gets the warning too, instead of the check being
skipped.

UPGRADING.md covers the custom HMAC change and the exception for a
none algorithm in another case. The README examples use a secret long
enough for HS256, and a new section describes deprecation_warnings.

This branch has not been deployed

No deployments
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.

Warn about short HMAC secrets and RSA keys

1 participant