Skip to content

fix(idtoken): validate ES256 signature length before slicing - #3668

Merged
shollyman merged 4 commits into
googleapis:mainfrom
prasanna8585:fix/es256-signature-length-validation
Nov 12, 2026
Merged

shollyman merged 4 commits into
googleapis:mainfrom
prasanna8585:fix/es256-signature-length-validation

Conversation

@prasanna8585

@prasanna8585 prasanna8585 commented Jul 23, 2026 •

Copy link
Copy Markdown
Contributor

Problem

idtoken.Validator.Validate() panics with an unrecovered runtime error when given a JWT signed with alg: ES256 whose signature segment decodes to fewer than 32 bytes. Any service using this package to validate incoming Google-issued ID tokens (e.g. Cloud Run, Cloud Functions, custom services authenticating callers via bearer tokens) can be crashed by a single malformed ~120-byte JWT.

Root Cause

idtoken/validate.go, validateES256:

r := big.NewInt(0).SetBytes(sig[:es256KeySize])   // panics if len(sig) < 32
s := big.NewInt(0).SetBytes(sig[es256KeySize:])

findMatchingKey only checks that the JWT's kid header matches a known key ID — it does not validate the signature at that point. The JWKS endpoint (googleIAPCertsURL) is public and unauthenticated by design, so any attacker can retrieve a real, currently valid kid with a single unauthenticated GET request, then craft a JWT with that kid and a signature under 32 bytes to trigger the panic.

Fix

Adds an exact-length check before slicing:

if len(sig) != es256KeySize*2 {
    return fmt.Errorf("idtoken: invalid ES256 signature length: got %d bytes, want %d", len(sig), es256KeySize*2)
}

This also incidentally fixes a related correctness issue: signatures longer than 64 bytes were previously silently truncated to the first 64 bytes without error (only the exact-length check catches both directions).

Testing

Two new regression tests in idtoken/validate_test.go:

  • TestValidateES256_ShortSignature: a 3-byte signature must return an error, not panic
  • TestValidateES256_OverlongSignature: a 65-byte signature must also return an error

Both use the package's real, unmodified, exported Validator.Validate() entry point via the existing RoundTripFn/commonToken test helpers — no reimplementation.


idtoken.Validator.Validate() panicked with an unrecovered runtime error
when given a JWT signed with alg: ES256 whose signature segment decodes
to fewer than 32 bytes. Any service using this package to validate
incoming Google-issued ID tokens could be crashed by a single malformed
~120-byte JWT.

Root cause: validateES256 sliced sig[:es256KeySize] and sig[es256KeySize:]
with no length check. findMatchingKey only validates that the JWT's kid
header matches a known (public) key ID before this point -- it does not
validate the signature -- so any attacker who fetches a real kid from the
public JWKS endpoint can reach the vulnerable slice with a crafted short
signature.

Fix: validate len(sig) == es256KeySize*2 before slicing, returning an
error instead of panicking. This also fixes a related correctness issue
where signatures longer than 64 bytes were silently truncated without
error.

Two regression tests added: short signature (panic case) and overlong
signature (truncation case).
@prasanna8585
prasanna8585 requested a review from a team as a code owner July 23, 2026 13:12
@prasanna8585

Copy link
Copy Markdown
Contributor Author

Hi Team,

Just checking in to see if you've had a chance to look at the patch strategy for this. I'm happy to spin up the code changes for the fix in a private fork if that helps speed things up.

Thanks again for your time!

@prasanna8585

Copy link
Copy Markdown
Contributor Author

Hi Team,

Just checking in to see if you've had a chance to look at the patch strategy for this. I'm happy to spin up the code changes for the fix in a private fork if that helps speed things up.

@prasanna8585

Copy link
Copy Markdown
Contributor Author

Hi Team,

Just checking in to see if you've had a chance to look at the patch strategy for this.

1 similar comment
@prasanna8585

Copy link
Copy Markdown
Contributor Author

Hi Team,

Just checking in to see if you've had a chance to look at the patch strategy for this.

@Monska85

Copy link
Copy Markdown

This is still present in v0.309.0 and on main. Could a maintainer review this fix?

@dnathe4th

Copy link
Copy Markdown

This is still present on main and in the latest release, v0.313.0. validateES256 slices sig[:es256KeySize] and sig[es256KeySize:] with no length check.

Some detail that may help review:

  • Which inputs panic. It panics only when the decoded signature is shorter than 32 bytes, because sig[:32] goes out of range. A signature of 32–63 bytes, or more than 64, doesn't panic, but it splits into malformed r and s values and fails verification. This PR's exact-length check (len(sig) != 2*es256KeySize) rejects all of these up front with a clear error. That's the right shape: an ES256 JWS signature is exactly 64 bytes (R || S).
  • Signature verification hasn't happened yet. The panic fires after the key lookup but before ecdsa.Verify. Any kid present in Google's public IAP JWKS reaches it, so no valid signature is needed. An unauthenticated caller can trigger it against a service that runs untrusted tokens through idtoken.Validate.
  • The test helper flakes once the length check lands. createES256JWT in validate_test.go left-pads r to 32 bytes but appends s.Bytes() unpadded. In roughly 1 run in 256, s has a leading zero byte, and the helper produces a 63-byte signature. Today that still verifies, because the code doesn't check length. With this PR's check, TestValidateES256/works would fail on those runs. Padding s the same way fixes it, e.g. s.FillBytes(make([]byte, es256KeySize)), and it would be worth folding into this PR.
  • Impact. It's a denial of service by crash only. net/http recovers a per-request panic, but servers or frameworks without per-request recovery would lose the process.

The fix and the regression test in this PR look correct to us. It would be good to see it merged.

This comment was written by Claude (Anthropic's AI assistant) on behalf of the account posting it. We found the issue during an internal review of the dependencies we ship, and confirmed it against the current source.

@shollyman shollyman self-assigned this Nov 12, 2026
@shollyman
shollyman requested a review from a team as a code owner November 12, 2026 21:36
@shollyman shollyman added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Nov 12, 2026
@kokoro-team kokoro-team removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Nov 12, 2026

@shollyman shollyman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the contribution, sorry this languished. I've made some changes to consolidate the test code a bit.

@shollyman
shollyman merged commit 60547d9 into googleapis:main Nov 12, 2026
11 of 12 checks passed
shollyman added a commit to googleapis/google-cloud-go that referenced this pull request Nov 22, 2026
This PR propagates similar length validation from
googleapis/google-api-go-client#3668 into the
corresponding package in google-cloud-go.
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.

6 participants