fix: gate attachment uploads by project ACL

This commit is contained in:
nac
2026-08-14 15:01:12 +00:00
parent 56b289b53f
commit 45ee1ed2b8
11 changed files with 295 additions and 19 deletions
+69
View File
@@ -271,6 +271,21 @@ class ServerTests(unittest.TestCase):
self.assertEqual(run_asgi_request(app, path="/uploads")[0]["status"], 401)
def test_upload_route_is_not_exposed_without_an_endpoint(self) -> None:
if FastMCP is None:
self.skipTest("MCP SDK is not installed")
server = create_server(crm_service=FakeCrmService(), readonly=True)
app = create_http_app(server, "secret")
async def request_upload() -> httpx.Response:
async with httpx.AsyncClient(
transport=httpx.ASGITransport(app=app), base_url="http://testserver"
) as client:
return await client.post("/uploads", headers={"Authorization": "Bearer secret"})
self.assertEqual(asyncio.run(request_upload()).status_code, 404)
def test_upload_route_accepts_the_matching_bearer_token(self) -> None:
if FastMCP is None:
self.skipTest("MCP SDK is not installed")
@@ -428,6 +443,60 @@ class ServerTests(unittest.TestCase):
log_level="info",
)
def test_http_upload_route_is_disabled_by_default(self) -> None:
with patch.dict("os.environ", {"NOCOBASE_MCP_TOKEN": "secret"}, clear=True):
with (
patch.object(server_module, "_make_write_services") as make_services,
patch.object(server_module, "create_server") as create_server_mock,
patch.object(server_module, "UploadEndpoint") as upload_endpoint,
patch.object(server_module, "create_http_app", return_value="app") as create_http_app_mock,
patch("uvicorn.run"),
):
make_services.return_value = ("crm", "projects", "http")
run_http_server(
config_path=None,
host=DEFAULT_HTTP_HOST,
port=DEFAULT_HTTP_PORT,
token_env="NOCOBASE_MCP_TOKEN",
streamable_http_path=DEFAULT_HTTP_PATH,
allowed_hosts=None,
allowed_origins=None,
)
upload_endpoint.assert_not_called()
create_http_app_mock.assert_called_once_with(create_server_mock.return_value, "secret", upload_endpoint=None)
def test_http_upload_route_requires_explicit_local_opt_in(self) -> None:
with patch.dict(
"os.environ",
{"NOCOBASE_MCP_TOKEN": "secret", "NOCOBASE_MCP_ENABLE_UPLOADS": "1"},
clear=True,
):
with (
patch.object(server_module, "_make_write_services") as make_services,
patch.object(server_module, "create_server") as create_server_mock,
patch.object(server_module, "UploadEndpoint") as upload_endpoint,
patch.object(server_module, "create_http_app", return_value="app") as create_http_app_mock,
patch("uvicorn.run"),
):
make_services.return_value = ("crm", "projects", "http")
run_http_server(
config_path=None,
host=DEFAULT_HTTP_HOST,
port=DEFAULT_HTTP_PORT,
token_env="NOCOBASE_MCP_TOKEN",
streamable_http_path=DEFAULT_HTTP_PATH,
allowed_hosts=None,
allowed_origins=None,
)
upload_endpoint.assert_called_once()
create_http_app_mock.assert_called_once_with(
create_server_mock.return_value,
"secret",
upload_endpoint=upload_endpoint.return_value,
)
def test_config_template_tool_is_opt_in(self) -> None:
if FastMCP is None:
self.skipTest("MCP SDK is not installed")