Skip to content

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

fix(openwebui): validate session IDs before forwarding requests (#6059)

Qin Haojief047d8051628
diff --git a/examples/openwebui-plugin/README.md b/examples/openwebui-plugin/README.md
index 9f9803824..5f64f2fd9 100644
--- a/examples/openwebui-plugin/README.md
+++ b/examples/openwebui-plugin/README.md
@@ -113,6 +113,11 @@ the OV server validates the source.
Returns session metadata for a given session ID — message counts, archive
state, pending tokens. Takes `{session_id}`.
+The ID must be a non-empty single path segment. Standalone `.` / `..`,
+`/`, `\`, `?`, `#`, `%`, and ASCII control characters are rejected with
+HTTP 422 before contacting OpenViking. Dots within an ID and Unicode IDs
+remain supported.
+
## Tests
```bash
diff --git a/examples/openwebui-plugin/openviking_openwebui/tools.py b/examples/openwebui-plugin/openviking_openwebui/tools.py
index f465fa492..9d1c4eae7 100644
--- a/examples/openwebui-plugin/openviking_openwebui/tools.py
+++ b/examples/openwebui-plugin/openviking_openwebui/tools.py
@@ -4,6 +4,7 @@
from __future__ import annotations
+import re
from typing import Any, Dict, List, Literal, Optional
from fastapi import APIRouter, Depends, HTTPException, Request
@@ -289,7 +290,14 @@ async def ov_session_status(
client: OVClient = Depends(get_client),
) -> Dict[str, Any]:
"""GET /api/v1/sessions/{session_id}."""
+ session_id = body.session_id
+ # Reject percent escapes as well: upstream routers may decode the path.
+ if session_id in {".", ".."} or not re.fullmatch(r"[^/\\?#%\x00-\x1f\x7f]+", session_id):
+ raise HTTPException(
+ status_code=422,
+ detail="session_id must be a single path segment without URL syntax or control characters",
+ )
try:
- return await client.get(f"/api/v1/sessions/{body.session_id}")
+ return await client.get(f"/api/v1/sessions/{session_id}")
except OVError as exc:
raise _forward(exc) from exc
diff --git a/examples/openwebui-plugin/tests/test_tools.py b/examples/openwebui-plugin/tests/test_tools.py
index c25bfdf9d..73c03768f 100644
--- a/examples/openwebui-plugin/tests/test_tools.py
+++ b/examples/openwebui-plugin/tests/test_tools.py
@@ -150,16 +150,45 @@ async def test_ov_add_resource_posts_resources(client: httpx.AsyncClient) -> Non
assert '"wait":true' in raw.replace(" ", "")
-async def test_ov_session_status_gets_session(client: httpx.AsyncClient) -> None:
- route = respx.get("http://ov.test/api/v1/sessions/sess-42").mock(
- return_value=httpx.Response(200, json={"status": "ok", "result": {"session_id": "sess-42"}})
+ ("session_id", "expected_status"),
+ [
+ ("sess-42", 200),
+ ("release.v1_42", 200),
+ ("会话-42", 200),
+ ("", 422),
+ (".", 422),
+ ("..", 422),
+ ("../admin/accounts", 422),
+ (r"..\admin\accounts", 422),
+ ("%2e%2e%2fadmin%2faccounts", 422),
+ ("sess-42?auto_create=true", 422),
+ ("sess-42#fragment", 422),
+ ("sess-42\n", 422),
+ ],
+)
[email protected](assert_all_called=False)
+async def test_ov_session_status_gets_session(
+ client: httpx.AsyncClient,
+ session_id: str,
+ expected_status: int,
+ respx_mock: respx.MockRouter,
+) -> None:
+ route = respx_mock.get().mock(
+ return_value=httpx.Response(
+ 200, json={"status": "ok", "result": {"session_id": session_id}}
+ )
)
- resp = await client.post("/tools/ov_session_status", json={"session_id": "sess-42"})
- assert resp.status_code == 200
+ resp = await client.post("/tools/ov_session_status", json={"session_id": session_id})
+ assert resp.status_code == expected_status
+ if expected_status == 422:
+ assert not route.called
+ return
sent = route.calls.last.request
_assert_headers(sent)
- assert sent.url.path == "/api/v1/sessions/sess-42"
+ assert sent.url.path == f"/api/v1/sessions/{session_id}"
+ assert sent.url.query == b""
+ assert sent.url.fragment == ""
@respx.mock