返回提示词目录第 1168 页

加固身份令牌验证与密钥处理

排查各入口的令牌校验缺口,统一签名与声明检查,完善密钥缓存并补充异常令牌测试。

完整提示词

<context>
Most JWT vulnerabilities are verification code that accepts tokens it should reject. The known mistakes: calling `decode` instead of `verify`, or verifying with signature checks turned off; letting the token's `alg` header choose the algorithm, which enables `alg: none` and the RS256-to-HS256 confusion where the public key is used as an HMAC secret; not checking `aud`, so a token issued for another API or client in the same identity provider works here; not checking `iss`, which matters with multi-tenant providers where any tenant can mint tokens; trusting `jku`, `x5u` or an embedded `jwk` header to tell you where the key is; using `kid` unsanitised in a file path or database query; no `exp` check or a huge clock skew allowance; accepting ID tokens as access tokens; and fetching JWKS on every request or never refreshing it, so key rotation either overloads the provider or breaks login. Weak HMAC secrets can be brute-forced offline from a single token. Verification belongs in one shared middleware, not repeated per route.
</context>

<task>
Harden JWT verification in {{target}}.
{{#stack}}Stack: {{stack}}{{/stack}}

1. Find every place tokens are decoded or verified, including websockets, background consumers, gateways and internal services, and the library and configuration each uses.
2. For each, check: signature verified, algorithms pinned to an explicit list, key source fixed in configuration (JWKS URL from the issuer's discovery document or a configured key, never from token headers), `iss` exact match, `aud` contains this service, `exp` and `nbf` enforced with small skew (seconds to a couple of minutes), token type correct (access, not ID token; check `typ` or a token-use claim where the issuer sets one), required claims present, and HMAC secret strength if symmetric.
3. Consolidate into one verification function or middleware configured from settings, and route every entry point through it.
4. Make JWKS handling robust: cache keys, refresh on unknown `kid` with a rate limit, and fail closed if keys cannot be loaded.
5. Add tests with tokens minted in the test: valid token accepted; `alg: none`, HS256 signed with the public key, wrong issuer, wrong audience, expired, not yet valid, unknown `kid`, an ID token, and a tampered payload all rejected.
6. Run the tests and the linter.
</task>

<constraints>
- Never use real production tokens or signing keys in tests; generate test keys.
- Do not log full tokens; log the `kid`, issuer and failure reason only.
- If an audience or issuer value is not known, ask rather than leaving the check off.
- Keep valid clients working; list any client that sends tokens the stricter checks will now reject.
{{> guardrails/investigate-before-answering}}
{{> guardrails/scope-discipline}}
{{> guardrails/verify-before-done}}
</constraints>

<output_format>
## Verification checks
Table: entry point, signature, algorithms pinned, key source, iss, aud, exp and nbf, token type, status before, status after.
## Changes
One line per file: `path`, what changed.
## Tests
One line per test with the token it uses.
## Verification
Commands run and their actual results.
## Residual risk
What verification alone does not solve, such as revocation before expiry, and the options.
</output_format>

可替换内容

  • {{target}}
  • {{#stack}}
  • {{stack}}
  • {{/stack}}
  • {{> guardrails/investigate-before-answering}}
  • {{> guardrails/scope-discipline}}
  • {{> guardrails/verify-before-done}}

分类与标签

编程开发、身份认证、令牌校验、密钥缓存、安全测试

前往 AI 图片创作