From 4eff22a234979c927c09b48ddc04032a32618dc9 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Mon, 23 Nov 2026 15:46:06 -0600 Subject: [PATCH] fix(idp-provider): test-rp selects its JWKS key by token kid MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Slice 3 proof RP hardcoded jwks.keys[0] as the verification key. During the Slice 5 live rollover's overlap phase with the new key active, that check rejected a correctly signed token ("id_token header alg/kid does not match JWKS") even though the provider's JWKS, signing, and userinfo verification were all correct — keys[0] was the old key, the token carried the new kid. The RP now does what a real relying party does: parse the token header, find the JWKS entry whose kid matches, and verify against that key. Found by the live rollover recorded on #13658; crate tests still 19/19. --- pkgs/idp-provider/examples/test-rp.rs | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/pkgs/idp-provider/examples/test-rp.rs b/pkgs/idp-provider/examples/test-rp.rs index 74ab36b455..c1342c9284 100644 --- a/pkgs/idp-provider/examples/test-rp.rs +++ b/pkgs/idp-provider/examples/test-rp.rs @@ -185,14 +185,7 @@ fn main() { } let jwks: serde_json::Value = serde_json::from_str(&body).unwrap_or_else(|_| run("jwks is not JSON")); - let jwk = &jwks["keys"][0]; - let kid = jwk["kid"].as_str().unwrap_or("").to_string(); let decode = |s: &str| B64URL.decode(s).unwrap_or_default(); - let pubkey = RsaPublicKey::new( - BigUint::from_bytes_be(&decode(jwk["n"].as_str().unwrap_or(""))), - BigUint::from_bytes_be(&decode(jwk["e"].as_str().unwrap_or(""))), - ) - .unwrap_or_else(|_| run("JWKS key is not a valid RSA public key")); // 3. Authorize (local door) with PKCE. let verifier = random_b64url(32); @@ -265,9 +258,25 @@ fn main() { } let header: serde_json::Value = serde_json::from_slice(&decode(parts[0])).unwrap_or_else(|_| run("bad jwt header")); - if header["alg"] != "RS256" || header["kid"] != kid { - run("id_token header alg/kid does not match JWKS"); + if header["alg"] != "RS256" { + run("id_token header alg is not RS256"); } + // Select the JWKS key by the token's kid: during a rollover + // overlap the set holds several keys, and keys[0] is not + // necessarily the one that signed. + let kid = header["kid"].as_str().unwrap_or("").to_string(); + let jwk = jwks["keys"] + .as_array() + .and_then(|keys| { + keys.iter() + .find(|k| k["kid"].as_str() == Some(kid.as_str())) + }) + .unwrap_or_else(|| run("id_token kid is not in the JWKS")); + let pubkey = RsaPublicKey::new( + BigUint::from_bytes_be(&decode(jwk["n"].as_str().unwrap_or(""))), + BigUint::from_bytes_be(&decode(jwk["e"].as_str().unwrap_or(""))), + ) + .unwrap_or_else(|_| run("JWKS key is not a valid RSA public key")); let signature = rsa::pkcs1v15::Signature::try_from(decode(parts[2]).as_slice()) .unwrap_or_else(|_| run("bad signature encoding")); VerifyingKey::::new(pubkey)