Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.
Go agentagenticaichatbotdsh-pluginembeddingsevaluationgenerative-aigolangknowledge-basellmmulti-tenantollamaopenaiquestion-answeringragrerankingsemantic-searchvector-searchwiki
fix(security): apply MCP upload sandbox when transport is set via CLI
wizardchen26cc33542050
diff --git a/mcp-server/test_file_path_security.py b/mcp-server/test_file_path_security.pyindex e575b03ad..9948c932c 100644--- a/mcp-server/test_file_path_security.py+++ b/mcp-server/test_file_path_security.py@@ -3,15 +3,17 @@ import osimport tempfileimport unittest-from upload_paths import resolve_upload_file_path+from upload_paths import clear_active_transport, resolve_upload_file_path, set_active_transportclass ResolveUploadFilePathTest(unittest.TestCase):def setUp(self):self._old_transport = os.environ.get("MCP_TRANSPORT")self._old_roots = os.environ.get("MCP_ALLOWED_UPLOAD_DIRS")+ os.environ.pop("MCP_TRANSPORT", None)def tearDown(self):+ clear_active_transport()if self._old_transport is None:os.environ.pop("MCP_TRANSPORT", None)else:@@ -35,6 +37,21 @@ class ResolveUploadFilePathTest(unittest.TestCase):with self.assertRaises(ValueError):resolve_upload_file_path("/etc/passwd")+ def test_active_transport_without_env_blocks_path_outside_cwd(self):+ """CLI --transport http sets active transport without MCP_TRANSPORT env."""+ set_active_transport("http")+ with tempfile.TemporaryDirectory() as tmp:+ os.chdir(tmp)+ allowed = os.path.join(tmp, "note.txt")+ with open(allowed, "w", encoding="utf-8") as handle:+ handle.write("ok")++ resolved = resolve_upload_file_path("note.txt")+ self.assertEqual(resolved, os.path.realpath(allowed))++ with self.assertRaises(ValueError):+ resolve_upload_file_path("/etc/passwd")+def test_explicit_allowed_roots(self):with tempfile.TemporaryDirectory() as tmp:os.environ["MCP_ALLOWED_UPLOAD_DIRS"] = tmpdiff --git a/mcp-server/upload_paths.py b/mcp-server/upload_paths.pyindex 96b5216e5..4c72b83bb 100644--- a/mcp-server/upload_paths.py+++ b/mcp-server/upload_paths.py@@ -3,7 +3,27 @@from __future__ import annotationsimport os-from typing import List+from typing import List, Optional++_active_transport: Optional[str] = None+++def set_active_transport(transport: str) -> None:+ """Record the transport selected at server startup (CLI or run_* entry)."""+ global _active_transport+ _active_transport = transport.strip().lower()+++def clear_active_transport() -> None:+ """Reset startup transport override (for tests)."""+ global _active_transport+ _active_transport = None+++def _current_transport() -> str:+ if _active_transport is not None:+ return _active_transport+ return os.getenv("MCP_TRANSPORT", "stdio").strip().lower()def _path_within_root(resolved_path: str, root: str) -> bool:@@ -22,7 +42,7 @@ def _allowed_upload_roots() -> List[str]:if raw:return [os.path.realpath(part.strip()) for part in raw.split(",") if part.strip()]- transport = os.getenv("MCP_TRANSPORT", "stdio").strip().lower()+ transport = _current_transport()if transport in ("sse", "http"):return [os.path.realpath(os.getcwd())]return []diff --git a/mcp-server/weknora_mcp_server.py b/mcp-server/weknora_mcp_server.pyindex 9cd25ac23..ef244030e 100644--- a/mcp-server/weknora_mcp_server.py+++ b/mcp-server/weknora_mcp_server.py@@ -23,7 +23,7 @@ import requestsfrom mcp.server import NotificationOptions, Serverfrom mcp.server.models import InitializationOptionsfrom requests.exceptions import RequestException-from upload_paths import resolve_upload_file_path+from upload_paths import resolve_upload_file_path, set_active_transport# Set up logging configuration for the MCP serverlogging.basicConfig(level=logging.INFO)@@ -1338,12 +1338,14 @@ def _init_options() -> InitializationOptions:async def run_stdio():"""Run the MCP server using stdio transport"""+ set_active_transport("stdio")async with mcp.server.stdio.stdio_server() as (read_stream, write_stream):await app.run(read_stream, write_stream, _init_options())async def run_sse(host: str, port: int):"""Run the MCP server using SSE transport (legacy MCP clients)"""+ set_active_transport("sse")auth_token = require_network_transport_auth("sse")try:from mcp.server.sse import SseServerTransport@@ -1380,6 +1382,7 @@ async def run_sse(host: str, port: int):async def run_http(host: str, port: int):"""Run the MCP server using Streamable HTTP transport (MCP 2025-03-26 spec)"""+ set_active_transport("http")auth_token = require_network_transport_auth("http")try:from contextlib import asynccontextmanager