Repository navigation
Conversation
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
left a comment
There was a problem hiding this comment.
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,:maxand 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:59shows. - README: the HS256 examples at
README.md:76andREADME.md:224use'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 mentionsdeprecation_warnings. - Changelog:
BEGIN_COMMIT_OVERRIDEhas no entry for the custom HMACenforce_hmac_key_lengthchange, so it won't reach the CHANGELOG.
Checked without finding problems:
- UPGRADING.md: the RFC wording, "signing already raises", and the
algorithmoption staying case-insensitive. "ruby-jwt has always signed that way" is true for every release with PS support. - Logs: only
algcan appear in a warning, and for built-in algorithms only as a case variant of a registered name. - EdDSA: headers like
eddsaandED25519from jwt-eddsa 2.0.0 warn as expected. - Spec helper: the switch from
:warnto:silenthides 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.
|
|
||
| def warn_about_alg_case(jwa) | ||
| alg = header['alg'] | ||
| return if !alg.is_a?(String) || jwa.alg == alg || !jwa.valid_alg?(alg) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
|
|
||
| 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 " \ |
There was a problem hiding this comment.
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 isHS512raisesVerificationError, but printsHMAC 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.
There was a problem hiding this comment.
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 throughJWT.decode.
The PR description is updated to match.
Posted by Claude on behalf of @anakinj.
|
|
||
| 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| end | ||
| end | ||
|
|
||
| context 'when deprecation warnings are printed' do |
There was a problem hiding this comment.
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
endPosted by Claude on behalf of @anakinj.
| end.not_to output.to_stderr | ||
| end | ||
|
|
||
| it 'prints no warning when only the given algorithm is in another case' do |
There was a problem hiding this comment.
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 anHS512header and an HS256 signature printsThe alg header HS512 only matches HS256 when the case is ignored. - JWKs: limiting the warning to
JWA::VerifierContextalso 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.
There was a problem hiding this comment.
Added both next to the other alg specs:
- a token with an
HS512header and an HS256 signature, verified withalgorithm: %w[HS256 HS512], prints nothing; - the
hs256token verified withJWT::JWK.new(hmac_secret, alg: 'HS256')prints the warning.
Dropping the casecmp? check now fails 2 specs.
Posted by Claude on behalf of @anakinj.
| - **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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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>withKernel#warn. It follows the existingJWT.configuration.deprecation_warningssetting::once, the default, prints each message once per process;:warnprints every time;:silentprints nothing.It is much smaller than the
JWT::Deprecationsmodule 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:enforce_hmac_key_lengthalready rejects italgheader that only matches when the case is ignored, such ashs256When 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
NONEwhen the caller allows thenonealgorithm. The messages contain no keys and no token content except thealgvalue, which is a case variant of the algorithm's name.Behaviour changes
:auto) only if that fails. The accepted signatures are the same as before::digestaccepts a subset of what:autoaccepts. An invalid signature costs a second public-key verification.JWT::JWA::Hmac.new('HS1', OpenSSL::Digest::SHA1)has no listed minimum length. Withenforce_hmac_key_lengthon, it used to raise a plainArgumentError. 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_lengthapplies to it, and it gets the warning.spec_helper.rbsetsdeprecation_warningsto:silentinstead 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,:onceand:silent.hmac_spec.rb:enforce_hmac_key_lengthrejects the key;rsa_spec.rbandps_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:hs256header prints the warning, throughJWT.decodeand with a JWK as the key;algorithm:option, a header that names another allowed algorithm, or a custom algorithm that accepts another name;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:
fix: Reject tokens with too many segmentsPosted by Claude on behalf of @anakinj.