feat(oidc): cache OIDC provider instances to prevent redundant calls#2308
feat(oidc): cache OIDC provider instances to prevent redundant calls#2308cemalkilic wants to merge 2 commits intomasterfrom
Conversation
|
|
||
| // Create provider with background context to ensure it's not tied to request lifecycle | ||
| // Use a background context with a deadline if the original context has one | ||
| bgCtx := context.Background() |
There was a problem hiding this comment.
🟡 Severity: MEDIUM
Critical context loss vulnerability: The cache replaces the original context with context.Background(), discarding security-critical context values like InsecureIssuerURLContext used for Apple OIDC. When Apple requests use oidc.InsecureIssuerURLContext(ctx, issuer) (token_oidc.go:139), this context setting is lost during cache miss, creating a provider with different security properties than intended. This could cause Apple authentication to fail or behave unexpectedly.
Helpful? Add 👍 / 👎
💡 Fix Suggestion
Suggestion: Replace context.Background() with context.WithoutCancel(ctx) to preserve context values (like InsecureIssuerURLContext for Apple OIDC) while still detaching from the parent request's cancellation. This maintains the original design intent of decoupling the cached provider from request lifecycle while fixing the security context loss issue.
⚠️ Experimental Feature: This code suggestion is automatically generated. Please review carefully.
| bgCtx := context.Background() | |
| bgCtx := context.WithoutCancel(ctx) |
Pull Request Test Coverage Report for Build 20601521323Details
💛 - Coveralls |
Problem
I noticed we were making redundant HTTP calls to OIDC discovery endpoints (
.well-known/openid-configuration) on every single OAuth/OIDC request:/authorizerequest -> HTTP call to provider's discovery endpoint/callbackrequest -> Another HTTP call to the same endpoint/token?grant_type=idtoken-> Yet another HTTP callImpact: 10-500ms added latency per OAuth flow, 2+ unnecessary external HTTP requests per login. At scale this is huge amount of unnecessary traffic.
Solution
Implemented thread-safe caching of
oidc.Providerinstances with smart defaults:Also added a new optional environment variable:
Default: 1 hour (reasonable since OIDC discovery configs rarely change)
Notes
Safe for Cross-Account Use
IMPORTANT: The cache stores ONLY discovery metadata (endpoints, supported algorithms, etc), NOT user tokens or session data.
JWKS Key Rotation Still Works
go-oidclibrary internally handles JWKS key fetching and rotationAzure Multi-Tenant Support