JWT_JWKS_KID_MISMATCH security auth_error ai_generated partial

JWT验证失败:在JWKS中找不到与kid 'abc123'匹配的密钥

JWT verification failed: no matching key found in JWKS for kid 'abc123'

ID: security/oauth2-jwk-set-without-kid

其他格式: JSON · Markdown 中文 · English
85%修复率
85%置信度
1证据数
2023-02-15首次发现

版本兼容性

版本状态引入弃用备注
jsonwebtoken 8.5.1 active
jose 4.14.4 active
Keycloak 20.0.0 active
Auth0 Node.js SDK 3.0.0 active

根因分析

JWT的'kid'头与从身份提供者获取的JSON Web Key Set(JWKS)中的任何密钥ID都不匹配,通常是由于JWKS缓存过期或密钥轮换配置错误。

English

The JWT contains a 'kid' header that does not match any key ID in the JSON Web Key Set (JWKS) fetched from the identity provider, often due to caching stale JWKS or misconfigured key rotation.

generic

官方文档

https://auth0.com/docs/secure/tokens/json-web-tokens/validate-json-web-tokens

解决方案

  1. 在验证之前刷新JWKS缓存。在Node.js中使用jose库时,设置较短的缓存持续时间并在出错时强制刷新:`const keys = await client.getSigningKeys(true);`
  2. 确保身份提供者的JWKS端点可访问且'kid'值一致。使用`curl https://your-idp.com/.well-known/jwks.json`并将'kid'值与令牌头进行比较。
  3. 实现回退机制:如果JWT中缺少'kid',尝试使用JWKS中的所有密钥进行匹配。这可以处理旧令牌,但出于安全考虑应弃用。

无效尝试

常见但无效的做法:

  1. 40% 失败

    Restarting the server only clears the in-memory cache, but if the JWKS is cached in a shared store like Redis, the stale keys persist and the error remains.

  2. 60% 失败

    Hardcoding the public key in the code bypasses the JWKS entirely, but it breaks when the identity provider rotates keys, leading to future authentication failures.

  3. 90% 失败

    Disabling JWT signature validation entirely removes the security check, leaving the application vulnerable to forged tokens and is never an acceptable fix.