From 4391b25d01135b1c2f9f97f0754510c59f6e19c4 Mon Sep 17 00:00:00 2001 From: Conrad Ludgate Date: Tue, 1 Oct 2024 10:36:49 +0100 Subject: [PATCH] proxy: ignore typ and use jwt.alg rather than jwk.alg (#9215) Microsoft exposes JWKs without the alg header. It's only included on the tokens. Not a problem. Also noticed that wrt the `typ` header: > It will typically not be used by applications when it is already known that the object is a JWT. This parameter is ignored by JWT implementations; any processing of this parameter is performed by the JWT application. Since we know we are expecting JWTs only, I've followed the guidance and removed the validation. --- proxy/src/auth/backend/jwt.rs | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/proxy/src/auth/backend/jwt.rs b/proxy/src/auth/backend/jwt.rs index 38dd30ce92..b62a11ccb2 100644 --- a/proxy/src/auth/backend/jwt.rs +++ b/proxy/src/auth/backend/jwt.rs @@ -261,10 +261,6 @@ impl JwkCacheEntryLock { let sig = base64::decode_config(signature, base64::URL_SAFE_NO_PAD) .context("Provided authentication token is not a valid JWT encoding")?; - ensure!( - header.typ == "JWT", - "Provided authentication token is not a valid JWT encoding" - ); let kid = header.key_id.context("missing key id")?; let mut guard = self @@ -299,7 +295,7 @@ impl JwkCacheEntryLock { verify_ec_signature(header_payload.as_bytes(), &sig, key)?; } jose_jwk::Key::Rsa(key) => { - verify_rsa_signature(header_payload.as_bytes(), &sig, key, &jwk.prm.alg)?; + verify_rsa_signature(header_payload.as_bytes(), &sig, key, &header.algorithm)?; } key => bail!("unsupported key type {key:?}"), }; @@ -381,7 +377,7 @@ fn verify_rsa_signature( data: &[u8], sig: &[u8], key: &jose_jwk::Rsa, - alg: &Option, + alg: &jose_jwa::Algorithm, ) -> anyhow::Result<()> { use jose_jwa::{Algorithm, Signing}; use rsa::{ @@ -392,7 +388,7 @@ fn verify_rsa_signature( let key = RsaPublicKey::try_from(key).map_err(|_| anyhow::anyhow!("invalid RSA key"))?; match alg { - Some(Algorithm::Signing(Signing::Rs256)) => { + Algorithm::Signing(Signing::Rs256) => { let key = VerifyingKey::::new(key); let sig = Signature::try_from(sig)?; key.verify(data, &sig)?; @@ -406,9 +402,6 @@ fn verify_rsa_signature( /// #[derive(serde::Deserialize, serde::Serialize)] struct JwtHeader<'a> { - /// must be "JWT" - #[serde(rename = "typ")] - typ: &'a str, /// must be a supported alg #[serde(rename = "alg")] algorithm: jose_jwa::Algorithm, @@ -592,7 +585,6 @@ mod tests { key: jose_jwk::Key::Ec(pk), prm: jose_jwk::Parameters { kid: Some(kid), - alg: Some(jose_jwa::Algorithm::Signing(jose_jwa::Signing::Es256)), ..Default::default() }, }; @@ -606,7 +598,6 @@ mod tests { key: jose_jwk::Key::Rsa(pk), prm: jose_jwk::Parameters { kid: Some(kid), - alg: Some(jose_jwa::Algorithm::Signing(jose_jwa::Signing::Rs256)), ..Default::default() }, }; @@ -615,7 +606,6 @@ mod tests { fn build_jwt_payload(kid: String, sig: jose_jwa::Signing) -> String { let header = JwtHeader { - typ: "JWT", algorithm: jose_jwa::Algorithm::Signing(sig), key_id: Some(&kid), };