Decode base64url strictly in Base64.urlSafeDecode (JWS signature malleability) - #82
Open
mohass1927 wants to merge 2 commits into
Open
mohass1927 wants to merge 2 commits into
mohass1927 wants to merge 2 commits into
Conversation
The decoder discarded the unused bits of the final quantum without checking them, so a non-canonical string decoded to the same bytes as its canonical equivalent. For JWS/JWT inputs that is signature malleability: two distinct compact serialisations verify against one signature. This mirrors the fix landed in tink-go (jwt/jwt_encoding.go and internal/jwk/jwk.go now decode strictly).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Base64.urlSafeDecodeaccepts non-canonical encodings: the final quantum's unused bits are discarded without being checked, so a string whose last character carries non-zero unused bits decodes to the same bytes as its canonical equivalent. RFC 4648 section 3.5 says a decoder must reject such a string.For JWS/JWT material this is signature malleability.
JwtFormatdecodes the signature segment withBase64.urlSafeDecode, so two distinct compact serialisations verify against a single signature. Token identity is therefore not unique, which breaks any control keyed on the token string (revocation/denylist entries, one-time-use enforced by string comparison, dedup or cache keys keyed on the compact form).Reproducer
With
com.google.crypto.tink:tink:1.23.0on the classpath, this pair differs only in the unused trailing bits of the last character:Before this change, both
urlSafeDecodecalls return the same 32 signature bytes. End to end, a token built from the canonical string verifies, and the same token with only that final character changed (...Xk->...Xl) also verifies throughJwtMac.verifyMacAndDecode— two distinct strings, one signature.Why this shape
This mirrors what landed in tink-go on 2026-09-11: commit
fe4b23e7("Use strict base64url decoding injwtto prevent signature malleability") switchedjwt/jwt_encoding.goandinternal/jwk/jwk.goto the strict decoder, with tests for a non-canonical JWS signature and a non-canonical Ed25519 JWKx. Java still accepts the non-canonical form on both paths, sinceJwtFormatandJwkSetConvertershare this decoder, and a check here covers both.The change
Two guards in the decoder's finish switch, where the trailing bits are currently thrown away:
case 2— twelve bits carried, eight used, so the low four must be zero.case 3— eighteen bits carried, sixteen used, so the low two must be zero.Canonical input is unaffected, including the short forms (
QQ,QUI,QUJD), which the new test asserts alongside the rejected non-canonical signature.Validation
I could not run this repository's Maven test suite from my environment. I validated the behaviour directly by compiling the unmodified and patched
Base64.javastandalone and exercising the three cases above: canonical decodes to 32 bytes, the non-canonical twin used to decode to the identical bytes and now throws, and the short forms still decode to 1/2/3 bytes. The JUnit test follows the existingBase64Testconventions (JUnit4, Truth).If you would rather limit the blast radius, the alternative is a strict variant used only by
JwtFormat/JwkSetConverterrather than tightening the shared decoder; I am happy to restructure either way. Flagging rather than assuming because I do not know whether any other caller depends on the lenient behaviour.