fix(security): apply MCP upload sandbox when transport is set via CLI
Record the active transport at run_stdio/run_sse/run_http startup so create_knowledge_from_file path checks apply even when MCP_TRANSPORT is not set in the environment.
This commit is contained in:
@@ -3,15 +3,17 @@ import os
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
from upload_paths import resolve_upload_file_path
|
||||
from upload_paths import clear_active_transport, resolve_upload_file_path, set_active_transport
|
||||
|
||||
|
||||
class 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"] = tmp
|
||||
|
||||
Reference in New Issue
Block a user