Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

fix(auth): accept OIDC issuers with a trailing slash (#6254)

CodingYangDev03b9aecfe444
diff --git a/docs/en/guides/04-authentication.md b/docs/en/guides/04-authentication.md
index 151b681c7..f416bd86c 100644
--- a/docs/en/guides/04-authentication.md
+++ b/docs/en/guides/04-authentication.md
@@ -226,7 +226,7 @@ Configure OIDC in `ov.conf`:
| Option | Description |
|--------|-------------|
-| `issuer` | OIDC provider issuer URL (required) |
+| `issuer` | OIDC provider issuer URL (required); must exactly match the token's `iss`, including any trailing `/` |
| `client_id` | OIDC client ID (optional) |
| `client_secret` | OIDC client secret (optional) |
| `audience` | Audience to validate (optional) |
diff --git a/docs/zh/guides/04-authentication.md b/docs/zh/guides/04-authentication.md
index 1dcbbc416..db6135039 100644
--- a/docs/zh/guides/04-authentication.md
+++ b/docs/zh/guides/04-authentication.md
@@ -158,6 +158,8 @@ OIDC Claims: {
### OIDC 完整配置
+`issuer` 必须与身份提供方签发的令牌中的 `iss` 完全一致,包括末尾的 `/`。
+
```json
{
"server": {
diff --git a/openviking/server/auth/oidc_config.py b/openviking/server/auth/oidc_config.py
index 76e99a255..bd31f94e4 100644
--- a/openviking/server/auth/oidc_config.py
+++ b/openviking/server/auth/oidc_config.py
@@ -33,7 +33,8 @@ class OIDCConfig(BaseModel):
@classmethod
def validate_issuer(cls, v: str) -> str:
"""Validate issuer URL."""
- v = v.rstrip("/")
- if not v:
+ # Preserve the exact issuer for token validation. Discovery URL
+ # construction handles any trailing slash separately.
+ if not v.rstrip("/"):
raise ValueError("issuer cannot be empty")
return v
diff --git a/openviking/server/auth/plugins/oidc.py b/openviking/server/auth/plugins/oidc.py
index 83392200f..684d562c3 100644
--- a/openviking/server/auth/plugins/oidc.py
+++ b/openviking/server/auth/plugins/oidc.py
@@ -242,7 +242,7 @@ class OIDCAuthPlugin(AuthPlugin):
if self._jwks_uri:
return self._jwks_uri
- discovery_url = f"{self._config.issuer}{_DISCOVERY_PATH}"
+ discovery_url = f"{self._config.issuer.rstrip('/')}{_DISCOVERY_PATH}"
logger.info("Performing OIDC discovery at %s", discovery_url)
try:
async with httpx.AsyncClient(timeout=_DISCOVERY_TIMEOUT) as client:
diff --git a/tests/server/test_identity_auth.py b/tests/server/test_identity_auth.py
index dc66b7b7b..aa65077d6 100644
--- a/tests/server/test_identity_auth.py
+++ b/tests/server/test_identity_auth.py
@@ -331,3 +331,68 @@ async def test_builtin_external_mapping_preserves_identity_and_user_role(
assert identity.role == Role.USER
ctx = await get_request_context(request, identity, None, None, None)
assert ctx.role == Role.USER
+
+
+ "configured_issuer,token_issuer,accepted",
+ [
+ ("https://issuer.test/app/", "https://issuer.test/app/", True),
+ ("https://issuer.test/app", "https://issuer.test/app", True),
+ ("https://issuer.test/app/", "https://issuer.test/app", False),
+ ("https://issuer.test/app", "https://issuer.test/app/", False),
+ ("https://issuer.test/app", "https://issuer.test/app///", False),
+ ("https://issuer.test/app", "https://attacker.test/app", False),
+ ],
+)
+async def test_oidc_issuer_matches_exactly(configured_issuer, token_issuer, accepted, monkeypatch):
+ import base64
+ import time
+
+ import httpx
+
+ jwt = pytest.importorskip("jose.jwt")
+
+ from openviking.server.auth.oidc_config import OIDCConfig
+ from openviking.server.auth.plugins import oidc as oidc_module
+
+ assert oidc_module._check_jwt_available()
+ plugin = oidc_module.OIDCAuthPlugin()
+ config = SimpleNamespace(oidc=OIDCConfig(issuer=configured_issuer, audience="openviking"))
+ plugin.validate_config(config)
+ await plugin.initialize(None, None, config)
+ secret = "test-secret"
+ encoded_key = base64.urlsafe_b64encode(secret.encode()).rstrip(b"=").decode()
+
+ def idp_response(request):
+ if str(request.url) == "https://issuer.test/app/.well-known/openid-configuration":
+ return httpx.Response(
+ 200, json={"issuer": configured_issuer, "jwks_uri": "https://issuer.test/keys"}
+ )
+ assert str(request.url) == "https://issuer.test/keys"
+ return httpx.Response(
+ 200, json={"keys": [{"kty": "oct", "k": encoded_key, "alg": "HS256", "kid": "test"}]}
+ )
+
+ client_class = httpx.AsyncClient
+ monkeypatch.setattr(
+ oidc_module.httpx,
+ "AsyncClient",
+ lambda **kwargs: client_class(transport=httpx.MockTransport(idp_response), **kwargs),
+ )
+ token = jwt.encode(
+ {
+ "iss": token_issuer,
+ "aud": "openviking",
+ "sub": "user-1",
+ "exp": int(time.time()) + 60,
+ },
+ secret,
+ algorithm="HS256",
+ headers={"kid": "test"},
+ )
+ if accepted:
+ claims = await plugin._validate_token(token)
+ assert claims["sub"] == "user-1"
+ else:
+ with pytest.raises(UnauthenticatedError, match="Invalid claims"):
+ await plugin._validate_token(token)