From 8b5ee21ec117aaa501276a8f15487c1b036fe0ae Mon Sep 17 00:00:00 2001 From: AbelTomato Date: Wed, 18 Nov 2026 00:48:55 +0800 Subject: [PATCH] fix(connectors): enforce custom MCP permission --- src/octop/api/routers/connectors.py | 25 ++++- tests/integration/test_connectors_api.py | 130 +++++++++++++++++++++++ 2 files changed, 150 insertions(+), 5 deletions(-) diff --git a/src/octop/api/routers/connectors.py b/src/octop/api/routers/connectors.py index 0552e5c752..8105fa9996 100644 --- a/src/octop/api/routers/connectors.py +++ b/src/octop/api/routers/connectors.py @@ -75,6 +75,7 @@ RESOURCE_CONNECTOR, GroupAccess, ) +from octop.infra.users.permissions import user_has_permission from octop.infra.utils.locale import resolve_request_locale from octop.infra.utils.ulid import new_ulid @@ -292,7 +293,9 @@ async def _begin_oauth_flow( ErrorCode.CONNECTOR_INVALID_CREDENTIALS, f"OAuth for {kind} is not available", ) - elif target_type != "custom_mcp": + elif target_type == "custom_mcp": + _assert_custom_mcp_permission(user) + else: raise OctopError(ErrorCode.CONNECTOR_INVALID_CREDENTIALS, "unsupported oauth target") state = secrets.token_urlsafe(24) @@ -607,6 +610,15 @@ def _assert_can_manage_connector(inst: Any, user: Any) -> None: raise OctopError(ErrorCode.FORBIDDEN, "not your connector instance") +def _assert_custom_mcp_permission(user: Any) -> None: + if not user_has_permission(user, "connector_custom"): + raise OctopError( + ErrorCode.FORBIDDEN, + "permission required", + details={"permission": "connector_custom"}, + ) + + def _raise_name_taken(name: str) -> None: raise OctopError( ErrorCode.CONNECTOR_NAME_TAKEN, @@ -704,7 +716,7 @@ async def list_instances( @router.get("/connectors/custom-mcp", summary="Get custom MCP servers") async def get_custom_mcp( - user: Any = Depends(current_user), + user: Any = Depends(require_permission("connector_custom")), server: Any = Depends(get_server), ) -> dict[str, Any]: """Return the user's custom MCP server map (langchain-mcp-adapters shape).""" @@ -715,7 +727,7 @@ async def get_custom_mcp( @router.put("/connectors/custom-mcp", summary="Save custom MCP servers") async def put_custom_mcp( body: CustomMcpPutBody, - user: Any = Depends(current_user), + user: Any = Depends(require_permission("connector_custom")), server: Any = Depends(get_server), ) -> dict[str, Any]: """Replace the user's custom MCP servers document and reload agents.""" @@ -749,7 +761,7 @@ async def put_custom_mcp( async def patch_custom_mcp_server( server_name: str, body: CustomMcpServerPatchBody, - user: Any = Depends(current_user), + user: Any = Depends(require_permission("connector_custom")), server: Any = Depends(get_server), ) -> dict[str, Any]: """Update ``enabled`` and/or ``default_open`` for one custom MCP server.""" @@ -775,7 +787,7 @@ async def patch_custom_mcp_server( @router.post("/connectors/custom-mcp/test", summary="Probe a custom MCP server") async def test_custom_mcp( body: CustomMcpTestBody, - user: Any = Depends(current_user), + user: Any = Depends(require_permission("connector_custom")), server: Any = Depends(get_server), ) -> dict[str, Any]: """Probe connectivity for one custom MCP server (saved name or inline spec).""" @@ -939,6 +951,7 @@ async def patch_instance( """Edit a connector instance without replacing its identity.""" custom_target = _resolve_custom_target(instance_id, user=user, server=server) if custom_target is not None: + _assert_custom_mcp_permission(user) custom_user_id, synthetic_name = custom_target svc = _connector_service(server) if body.status is None and body.default_open is None and body.shared is None: @@ -1081,6 +1094,7 @@ async def delete_instance( """Disconnect and delete stored credentials for a connector instance.""" custom_target = _resolve_custom_target(instance_id, user=user, server=server) if custom_target is not None: + _assert_custom_mcp_permission(user) custom_user_id, synthetic_name = custom_target svc = _connector_service(server) servers = dict(svc.get_custom_servers(custom_user_id)) @@ -1161,6 +1175,7 @@ async def test_instance( """Probe the connector with stored credentials and return success or error details.""" custom_target = _resolve_custom_target(instance_id, user=user, server=server) if custom_target is not None: + _assert_custom_mcp_permission(user) custom_user_id, synthetic_name = custom_target svc = _connector_service(server) saved = svc.get_custom_servers(custom_user_id) diff --git a/tests/integration/test_connectors_api.py b/tests/integration/test_connectors_api.py index 3bec141fc2..a99b4136e2 100644 --- a/tests/integration/test_connectors_api.py +++ b/tests/integration/test_connectors_api.py @@ -5,6 +5,7 @@ import json import os import re +import sys from html import escape from pathlib import Path from unittest.mock import AsyncMock, patch @@ -663,6 +664,135 @@ async def test_patch_custom_mcp_server_default_open_only(env): assert "default_open" not in patch_off.json()["servers"]["linear"] +async def test_custom_mcp_routes_require_connector_custom_permission(env): + c, _, admin_auth, _ = env + user_auth = await create_user( + c, + admin_auth, + username="custom_mcp_routes_denied", + permissions=[], + ) + + listed = await c.get("/api/connectors/custom-mcp", headers=user_auth) + oauth = await c.post( + "/api/connectors/oauth/start", + headers=user_auth, + json={"target": {"type": "custom_mcp", "server_name": "linear"}}, + ) + saved = await c.put( + "/api/connectors/custom-mcp", + headers=user_auth, + json={ + "servers": { + "linear": { + "transport": "streamable_http", + "url": "https://mcp.example.com/mcp", + } + } + }, + ) + + assert [listed.status_code, oauth.status_code, saved.status_code] == [403, 403, 403] + + +async def test_custom_mcp_patch_and_delete_require_connector_custom_permission(env): + c, server, admin_auth, _ = env + user_auth = await create_user( + c, + admin_auth, + username="custom_mcp_mutation_denied", + permissions=[], + ) + user_id = await resolve_user_id(c, admin_auth, "custom_mcp_mutation_denied") + svc = _connector_service(server) + svc.put_custom_servers( + user_id, + { + "linear": { + "transport": "streamable_http", + "url": "https://mcp.example.com/mcp", + } + }, + ) + + direct_patch = await c.patch( + "/api/connectors/custom-mcp/servers/linear", + headers=user_auth, + json={"enabled": False}, + ) + instance_patch = await c.patch( + "/api/connector-instances/custom:linear", + headers=user_auth, + json={"status": "disabled"}, + ) + deleted = await c.delete("/api/connector-instances/custom:linear", headers=user_auth) + + assert [direct_patch.status_code, instance_patch.status_code, deleted.status_code] == [ + 403, + 403, + 403, + ] + assert "linear" in svc.get_custom_servers(user_id) + + +async def test_custom_mcp_probe_requires_permission_before_starting_stdio(env, tmp_path: Path): + c, _, admin_auth, _ = env + user_auth = await create_user( + c, + admin_auth, + username="custom_mcp_probe_denied", + permissions=[], + ) + marker = tmp_path / "stdio-started" + script = f"from pathlib import Path; Path({str(marker)!r}).write_text('started')" + + response = await c.post( + "/api/connectors/custom-mcp/test", + headers=user_auth, + json={ + "server": { + "transport": "stdio", + "command": sys.executable, + "args": ["-c", script], + } + }, + ) + + assert (response.status_code, marker.exists()) == (403, False) + + +async def test_custom_mcp_instance_probe_requires_permission_before_starting_stdio( + env, tmp_path: Path +): + c, server, admin_auth, _ = env + user_auth = await create_user( + c, + admin_auth, + username="custom_mcp_instance_probe_denied", + permissions=[], + ) + user_id = await resolve_user_id(c, admin_auth, "custom_mcp_instance_probe_denied") + marker = tmp_path / "instance-stdio-started" + script = f"from pathlib import Path; Path({str(marker)!r}).write_text('started')" + _connector_service(server).put_custom_servers( + user_id, + { + "linear": { + "transport": "stdio", + "command": sys.executable, + "args": ["-c", script], + } + }, + ) + + response = await c.post( + "/api/connector-instances/custom:linear/test", + headers=user_auth, + ) + + assert (response.status_code, marker.exists()) == (403, False) + + async def test_shared_custom_mcp_is_visible_with_collision_safe_name(env): c, _, admin_auth, _ = env put = await c.put(