Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

fix: verify OIDC id_token signature via JWKS (closes #2799)

Vladimir Babinf0e14f6f0023
diff --git a/internal/application/service/user.go b/internal/application/service/user.go
index 001730aa1..827c6fb3d 100644
--- a/internal/application/service/user.go
+++ b/internal/application/service/user.go
@@ -3,11 +3,13 @@ package service
import (
"context"
"crypto/rand"
+ "crypto/rsa"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
+ "math/big"
"net/http"
"net/url"
"os"
@@ -1383,6 +1385,8 @@ type oidcDiscoveryDocument struct {
AuthorizationEndpoint string `json:"authorization_endpoint"`
TokenEndpoint string `json:"token_endpoint"`
UserInfoEndpoint string `json:"userinfo_endpoint"`
+ JwksURI string `json:"jwks_uri"`
+ Issuer string `json:"issuer"`
}
type oidcTokenResponse struct {
@@ -1477,6 +1481,9 @@ func (s *userService) populateOIDCEndpoints(ctx context.Context, cfg *config.OID
if cfg.UserInfoEndpoint == "" {
cfg.UserInfoEndpoint = doc.UserInfoEndpoint
}
+ if cfg.JwksURI == "" {
+ cfg.JwksURI = doc.JwksURI
+ }
if cfg.AuthorizationEndpoint == "" || cfg.TokenEndpoint == "" {
return errors.New("OIDC discovery document missing required endpoints")
}
@@ -1526,13 +1533,34 @@ func (s *userService) resolveOIDCUserInfo(ctx context.Context, cfg *config.OIDCA
claims := map[string]interface{}{}
if strings.TrimSpace(tokenResp.IDToken) != "" {
- idTokenClaims, err := decodeJWTClaims(tokenResp.IDToken)
- if err != nil {
- logger.Warnf(ctx, "Failed to decode OIDC id_token claims: %v", err)
- } else {
+ // Verify the id_token signature (JWKS) and standard claims before
+ // trusting anything it carries. If a jwks_uri is available we require a
+ // valid signature; a token that fails verification is discarded rather
+ // than silently trusted. When no jwks_uri is configured we fall back to
+ // the previous unverified decode only if there is another claim source
+ // (userinfo endpoint) to cross-check against.
+ if strings.TrimSpace(cfg.JwksURI) != "" {
+ idTokenClaims, err := s.verifyOIDCIDToken(ctx, cfg, tokenResp.IDToken)
+ if err != nil {
+ return nil, fmt.Errorf("OIDC id_token verification failed: %w", err)
+ }
for k, v := range idTokenClaims {
claims[k] = v
}
+ } else if strings.TrimSpace(cfg.UserInfoEndpoint) != "" &&
+ strings.TrimSpace(tokenResp.AccessToken) != "" {
+ logger.Warnf(ctx, "OIDC id_token signature not verified: no jwks_uri configured; "+
+ "relying on userinfo endpoint for claims")
+ idTokenClaims, err := decodeJWTClaims(tokenResp.IDToken)
+ if err != nil {
+ logger.Warnf(ctx, "Failed to decode OIDC id_token claims: %v", err)
+ } else {
+ for k, v := range idTokenClaims {
+ claims[k] = v
+ }
+ }
+ } else {
+ return nil, errors.New("cannot verify OIDC id_token: no jwks_uri and no userinfo endpoint configured")
}
}
@@ -1693,6 +1721,135 @@ func decodeJWTClaims(token string) (map[string]interface{}, error) {
return claims, nil
}
+// oidcJWK / oidcJWKS model the subset of a JWKS document we need to rebuild an
+// RSA signing key. Only RSA keys are supported (the overwhelmingly common OIDC
+// id_token signing type); other key types are skipped during lookup.
+type oidcJWK struct {
+ Kty string `json:"kty"`
+ Kid string `json:"kid"`
+ Alg string `json:"alg"`
+ Use string `json:"use"`
+ N string `json:"n"`
+ E string `json:"e"`
+}
+
+type oidcJWKS struct {
+ Keys []oidcJWK `json:"keys"`
+}
+
+// rsaPublicKey rebuilds an *rsa.PublicKey from the base64url modulus/exponent of
+// a JWK (RFC 7518 §6.3.1).
+func (k oidcJWK) rsaPublicKey() (*rsa.PublicKey, error) {
+ if !strings.EqualFold(k.Kty, "RSA") {
+ return nil, fmt.Errorf("unsupported JWK key type: %s", k.Kty)
+ }
+ nBytes, err := base64.RawURLEncoding.DecodeString(k.N)
+ if err != nil {
+ return nil, fmt.Errorf("invalid JWK modulus: %w", err)
+ }
+ eBytes, err := base64.RawURLEncoding.DecodeString(k.E)
+ if err != nil {
+ return nil, fmt.Errorf("invalid JWK exponent: %w", err)
+ }
+ if len(nBytes) == 0 || len(eBytes) == 0 {
+ return nil, errors.New("empty JWK modulus or exponent")
+ }
+ e := 0
+ for _, b := range eBytes {
+ e = e<<8 | int(b)
+ }
+ if e <= 0 {
+ return nil, errors.New("invalid JWK exponent value")
+ }
+ return &rsa.PublicKey{N: new(big.Int).SetBytes(nBytes), E: e}, nil
+}
+
+// fetchOIDCJWKS loads the provider's JWKS document over the SSRF-safe client.
+func (s *userService) fetchOIDCJWKS(ctx context.Context, jwksURI string) (*oidcJWKS, error) {
+ if err := validateOIDCEndpoint("jwks", jwksURI, true); err != nil {
+ return nil, err
+ }
+ req, err := http.NewRequestWithContext(ctx, http.MethodGet, jwksURI, nil)
+ if err != nil {
+ return nil, err
+ }
+ req.Header.Set("Accept", "application/json")
+
+ resp, err := newOIDCHTTPClient().Do(req)
+ if err != nil {
+ return nil, err
+ }
+ defer resp.Body.Close()
+ if resp.StatusCode < 200 || resp.StatusCode >= 300 {
+ _, _ = io.Copy(io.Discard, io.LimitReader(resp.Body, 2048))
+ return nil, fmt.Errorf("JWKS request failed: status=%d", resp.StatusCode)
+ }
+
+ var jwks oidcJWKS
+ if err := json.NewDecoder(io.LimitReader(resp.Body, 1<<20)).Decode(&jwks); err != nil {
+ return nil, fmt.Errorf("failed to decode JWKS document: %w", err)
+ }
+ if len(jwks.Keys) == 0 {
+ return nil, errors.New("JWKS document contains no keys")
+ }
+ return &jwks, nil
+}
+
+// verifyOIDCIDToken cryptographically verifies an OIDC id_token: it checks the
+// RSA signature against the provider's JWKS (matched by kid) and validates the
+// issuer, audience (client_id) and expiry. It returns the verified claims.
+//
+// Previously the id_token payload was base64-decoded without any signature
+// check (decodeJWTClaims), so a forged token could inject arbitrary identity
+// claims. Verification requires a jwks_uri (from discovery or explicit config).
+func (s *userService) verifyOIDCIDToken(
+ ctx context.Context, cfg *config.OIDCAuthConfig, idToken string,
+) (map[string]interface{}, error) {
+ if strings.TrimSpace(cfg.JwksURI) == "" {
+ return nil, errors.New("cannot verify OIDC id_token: no jwks_uri configured")
+ }
+
+ jwks, err := s.fetchOIDCJWKS(ctx, cfg.JwksURI)
+ if err != nil {
+ return nil, err
+ }
+
+ keyFunc := func(token *jwt.Token) (interface{}, error) {
+ if _, ok := token.Method.(*jwt.SigningMethodRSA); !ok {
+ return nil, fmt.Errorf("unexpected id_token signing method: %v", token.Header["alg"])
+ }
+ kid, _ := token.Header["kid"].(string)
+ for _, k := range jwks.Keys {
+ if k.Kty != "" && !strings.EqualFold(k.Kty, "RSA") {
+ continue
+ }
+ if kid != "" && k.Kid != "" && k.Kid != kid {
+ continue
+ }
+ if pub, err := k.rsaPublicKey(); err == nil {
+ return pub, nil
+ }
+ }
+ return nil, errors.New("no matching JWKS key for id_token")
+ }
+
+ parserOpts := []jwt.ParserOption{
+ jwt.WithValidMethods([]string{"RS256", "RS384", "RS512"}),
+ }
+ if iss := strings.TrimSpace(cfg.IssuerURL); iss != "" {
+ parserOpts = append(parserOpts, jwt.WithIssuer(iss))
+ }
+ if aud := strings.TrimSpace(cfg.ClientID); aud != "" {
+ parserOpts = append(parserOpts, jwt.WithAudience(aud))
+ }
+
+ claims := jwt.MapClaims{}
+ if _, err := jwt.NewParser(parserOpts...).ParseWithClaims(idToken, claims, keyFunc); err != nil {
+ return nil, fmt.Errorf("id_token verification failed: %w", err)
+ }
+ return map[string]interface{}(claims), nil
+}
+
func extractClaimAsString(claims map[string]interface{}, key string) string {
key = strings.TrimSpace(key)
if key == "" {
diff --git a/internal/application/service/user_oidc_verify_test.go b/internal/application/service/user_oidc_verify_test.go
new file mode 100644
index 000000000..6b50fe815
--- /dev/null
+++ b/internal/application/service/user_oidc_verify_test.go
@@ -0,0 +1,179 @@
+package service
+
+import (
+ "context"
+ "crypto/rand"
+ "crypto/rsa"
+ "encoding/base64"
+ "encoding/json"
+ "math/big"
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "testing"
+ "time"
+
+ "github.com/Tencent/WeKnora/internal/config"
+ "github.com/golang-jwt/jwt/v5"
+)
+
+// signedIDToken builds an RS256-signed id_token with the given claims and kid.
+func signedIDToken(t *testing.T, key *rsa.PrivateKey, kid string, claims jwt.MapClaims) string {
+ t.Helper()
+ tok := jwt.NewWithClaims(jwt.SigningMethodRS256, claims)
+ tok.Header["kid"] = kid
+ s, err := tok.SignedString(key)
+ if err != nil {
+ t.Fatalf("sign id_token: %v", err)
+ }
+ return s
+}
+
+// jwksServer serves a JWKS document exposing the public half of key under kid.
+func jwksServer(t *testing.T, key *rsa.PrivateKey, kid string) *httptest.Server {
+ t.Helper()
+ pub := key.Public().(*rsa.PublicKey)
+ eBytes := big.NewInt(int64(pub.E)).Bytes()
+ doc := oidcJWKS{Keys: []oidcJWK{{
+ Kty: "RSA",
+ Kid: kid,
+ Alg: "RS256",
+ Use: "sig",
+ N: base64.RawURLEncoding.EncodeToString(pub.N.Bytes()),
+ E: base64.RawURLEncoding.EncodeToString(eBytes),
+ }}}
+ return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
+ _ = json.NewEncoder(w).Encode(doc)
+ }))
+}
+
+func baseClaims(iss, aud string) jwt.MapClaims {
+ return jwt.MapClaims{
+ "iss": iss,
+ "aud": aud,
+ "sub": "user-123",
+ "email": "[email protected]",
+ "name": "Real User",
+ "exp": time.Now().Add(time.Hour).Unix(),
+ "iat": time.Now().Unix(),
+ }
+}
+
+// A validly signed id_token with the right issuer/audience is accepted and its
+// claims are returned.
+func TestVerifyOIDCIDToken_ValidTokenAccepted(t *testing.T) {
+ withOIDCSSRFWhitelist(t, "127.0.0.1")
+
+ key, err := rsa.GenerateKey(rand.Reader, 2048)
+ if err != nil {
+ t.Fatal(err)
+ }
+ jwks := jwksServer(t, key, "kid-1")
+ defer jwks.Close()
+
+ cfg := &config.OIDCAuthConfig{
+ JwksURI: jwks.URL,
+ IssuerURL: "https://idp.example",
+ ClientID: "weknora-client",
+ }
+ idToken := signedIDToken(t, key, "kid-1", baseClaims("https://idp.example", "weknora-client"))
+
+ svc := &userService{}
+ claims, err := svc.verifyOIDCIDToken(context.Background(), cfg, idToken)
+ if err != nil {
+ t.Fatalf("valid token rejected: %v", err)
+ }
+ if got, _ := claims["email"].(string); got != "[email protected]" {
+ t.Fatalf("email claim = %q, want [email protected]", got)
+ }
+}
+
+// A token whose payload has been tampered with (re-signed body but signature
+// from the original) must be rejected. This is the core of the vulnerability:
+// previously decodeJWTClaims would have trusted the forged claims.
+func TestVerifyOIDCIDToken_TamperedPayloadRejected(t *testing.T) {
+ withOIDCSSRFWhitelist(t, "127.0.0.1")
+
+ key, err := rsa.GenerateKey(rand.Reader, 2048)
+ if err != nil {
+ t.Fatal(err)
+ }
+ jwks := jwksServer(t, key, "kid-1")
+ defer jwks.Close()
+
+ cfg := &config.OIDCAuthConfig{JwksURI: jwks.URL, IssuerURL: "https://idp.example", ClientID: "weknora-client"}
+
+ valid := signedIDToken(t, key, "kid-1", baseClaims("https://idp.example", "weknora-client"))
+
+ // Swap the payload for a forged one ([email protected]) while keeping the
+ // original header + signature.
+ parts := strings.Split(valid, ".")
+ forgedPayload := baseClaims("https://idp.example", "weknora-client")
+ forgedPayload["email"] = "[email protected]"
+ forgedPayload["sub"] = "attacker"
+ pb, _ := json.Marshal(forgedPayload)
+ parts[1] = base64.RawURLEncoding.EncodeToString(pb)
+ forged := strings.Join(parts, ".")
+
+ svc := &userService{}
+ if _, err := svc.verifyOIDCIDToken(context.Background(), cfg, forged); err == nil {
+ t.Fatal("forged id_token was accepted; signature not verified")
+ }
+}
+
+// A token signed by a different (attacker) key must be rejected even though it
+// is a syntactically valid, self-consistent JWT.
+func TestVerifyOIDCIDToken_WrongSignerRejected(t *testing.T) {
+ withOIDCSSRFWhitelist(t, "127.0.0.1")
+
+ idpKey, _ := rsa.GenerateKey(rand.Reader, 2048)
+ attackerKey, _ := rsa.GenerateKey(rand.Reader, 2048)
+
+ jwks := jwksServer(t, idpKey, "kid-1") // JWKS only advertises the IdP key
+ defer jwks.Close()
+
+ cfg := &config.OIDCAuthConfig{JwksURI: jwks.URL, IssuerURL: "https://idp.example", ClientID: "weknora-client"}
+ // Attacker signs with their own key but claims kid-1.
+ forged := signedIDToken(t, attackerKey, "kid-1", baseClaims("https://idp.example", "weknora-client"))
+
+ svc := &userService{}
+ if _, err := svc.verifyOIDCIDToken(context.Background(), cfg, forged); err == nil {
+ t.Fatal("token signed by attacker key was accepted")
+ }
+}
+
+// Wrong audience (token minted for a different client) is rejected.
+func TestVerifyOIDCIDToken_WrongAudienceRejected(t *testing.T) {
+ withOIDCSSRFWhitelist(t, "127.0.0.1")
+
+ key, _ := rsa.GenerateKey(rand.Reader, 2048)
+ jwks := jwksServer(t, key, "kid-1")
+ defer jwks.Close()
+
+ cfg := &config.OIDCAuthConfig{JwksURI: jwks.URL, IssuerURL: "https://idp.example", ClientID: "weknora-client"}
+ other := signedIDToken(t, key, "kid-1", baseClaims("https://idp.example", "some-other-client"))
+
+ svc := &userService{}
+ if _, err := svc.verifyOIDCIDToken(context.Background(), cfg, other); err == nil {
+ t.Fatal("token with wrong audience was accepted")
+ }
+}
+
+// An expired token is rejected.
+func TestVerifyOIDCIDToken_ExpiredRejected(t *testing.T) {
+ withOIDCSSRFWhitelist(t, "127.0.0.1")
+
+ key, _ := rsa.GenerateKey(rand.Reader, 2048)
+ jwks := jwksServer(t, key, "kid-1")
+ defer jwks.Close()
+
+ cfg := &config.OIDCAuthConfig{JwksURI: jwks.URL, IssuerURL: "https://idp.example", ClientID: "weknora-client"}
+ claims := baseClaims("https://idp.example", "weknora-client")
+ claims["exp"] = time.Now().Add(-time.Hour).Unix()
+ expired := signedIDToken(t, key, "kid-1", claims)
+
+ svc := &userService{}
+ if _, err := svc.verifyOIDCIDToken(context.Background(), cfg, expired); err == nil {
+ t.Fatal("expired token was accepted")
+ }
+}
diff --git a/internal/config/config.go b/internal/config/config.go
index 2408a8dcf..4db13bf86 100644
--- a/internal/config/config.go
+++ b/internal/config/config.go
@@ -316,6 +316,7 @@ type OIDCAuthConfig struct {
AuthorizationEndpoint string `yaml:"authorization_endpoint" json:"authorization_endpoint"`
TokenEndpoint string `yaml:"token_endpoint" json:"token_endpoint"`
UserInfoEndpoint string `yaml:"user_info_endpoint" json:"user_info_endpoint"`
+ JwksURI string `yaml:"jwks_uri" json:"jwks_uri"`
Scopes []string `yaml:"scopes" json:"scopes"`
UserInfoMapping *OIDCUserInfoMapping `yaml:"user_info_mapping" json:"user_info_mapping"`
}