Skip to content

fix: return null from asInt/asLong for out-of-range numeric claims - #817

Open
abdul-khaliq-khalid wants to merge 1 commit into
auth0:masterfrom
abdul-khaliq-khalid:claim-int-long-range
Open

abdul-khaliq-khalid wants to merge 1 commit into
auth0:masterfrom
abdul-khaliq-khalid:claim-int-long-range

Conversation

@abdul-khaliq-khalid

Copy link
Copy Markdown

Changes

JsonNodeClaim.asInt() and asLong() only checked data.isNumber() before calling Jackson's asInt()/asLong(), and those calls keep the low 32/64 bits of a larger number. So a claim like "org_id": 4294967419 (2^32 + 123) is returned as 123 instead of null, which is what the Claim javadoc promises when a value "can't be converted to an Integer".

This matters for verification because withClaim(String, Integer) and withClaim(String, Long) compare against claim.asInt()/claim.asLong(). Today a verifier built with .withClaim("org_id", 123) accepts a token carrying 4294967419, and .withClaim("org_id", 123L) accepts 18446744073709551739 (2^64 + 123), so the claim check passes for a value that is not the one the application asked for.

The guard now uses canConvertToInt() / canConvertToLong(), the same check asDate() and asInstant() already use. In-range values (including in-range floating point values) behave exactly as before; out-of-range values now return null, so the verifier rejects them with IncorrectClaimException. No public API changes.

References

Same family as the recent NumericDate range fix (GHSA-cg7w-5cc2-f2xm), applied to the integer accessors.

Testing

Added JsonNodeClaimTest.shouldGetNullIntIfValueOutOfIntRange, JsonNodeClaimTest.shouldGetNullLongIfValueOutOfLongRange, and two JWTVerifierTest cases that verify HS256 tokens with 2^32 + 123 / 2^64 + 123 against withClaim("name", 123) / withClaim("name", 123L). All four fail on master and pass with this change. I ran ./gradlew assemble apiDiff check jacocoTestReport locally: tests on Java 11, 17 and 21, checkstyle and apiDiff are green. The Java 8 test tasks couldn't run on my machine because there is no JDK 8 toolchain for Apple Silicon, so CI will cover that one.

  • This change adds test coverage
  • This change has been tested on the latest version of Java or why not

Checklist

asInt() and asLong() only checked isNumber() before narrowing, so a claim
such as 4294967419 (2^32 + 123) came back as 123 and satisfied
withClaim("name", 123). Use canConvertToInt()/canConvertToLong() so
out-of-range values return null, as the Claim javadoc describes.
@abdul-khaliq-khalid
abdul-khaliq-khalid requested a review from a team as a code owner October 21, 2026 10:36

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.

1 participant