mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 08:30:05 +08:00
fix: deny ungranted MCP tools for non-admin users
This commit is contained in:
@@ -435,15 +435,19 @@ class AgenticChatPipeline:
|
||||
|
||||
await get_mcp_manager().ensure_started()
|
||||
# Caller-scoped whitelist (e.g. a partner's configured MCP tools)
|
||||
# intersected with the current user's admin grant: ``None`` = all
|
||||
# deferred tools, a set = only these. Either side can narrow.
|
||||
# intersected with the current user's admin grant. ``None`` means
|
||||
# unrestricted; a set narrows the deferred tools. Real non-admin
|
||||
# users fail closed when no MCP grant is present, while partner
|
||||
# turns use their owner-scoped metadata whitelist as the authority.
|
||||
from deeptutor.multi_user.tool_access import allowed_mcp_tools, combine_whitelists
|
||||
|
||||
raw_filter = context.metadata.get("mcp_tools_filter")
|
||||
allowed: set[str] | None = combine_whitelists(
|
||||
{str(name) for name in raw_filter} if isinstance(raw_filter, list) else None,
|
||||
allowed_mcp_tools(),
|
||||
caller_allowed = (
|
||||
{str(name) for name in raw_filter} if isinstance(raw_filter, list) else None
|
||||
)
|
||||
is_partner = str((context.metadata or {}).get("source")) == "partner"
|
||||
user_allowed = None if is_partner else allowed_mcp_tools()
|
||||
allowed: set[str] | None = combine_whitelists(caller_allowed, user_allowed)
|
||||
pool = self.registry.deferred_tools()
|
||||
if allowed is not None:
|
||||
pool = [t for t in pool if t.get_definition().name in allowed]
|
||||
|
||||
@@ -20,12 +20,15 @@ def empty_grant(user_id: str) -> dict[str, Any]:
|
||||
"models": {"llm": []},
|
||||
"knowledge_bases": [],
|
||||
"skills": [],
|
||||
# Tool whitelists share the partner-config semantics: ``None`` means
|
||||
# "default" (every tool in the pool), ``[]`` means none, a list is an
|
||||
# explicit whitelist. ``exec_enabled`` is a tri-state override on top
|
||||
# of the deployment exec policy: ``None`` follows the policy, ``False``
|
||||
# always denies, ``True`` is only honored where the sandbox can
|
||||
# actually isolate users (SYSTEM isolation).
|
||||
# Tool whitelists share the partner-config semantics for built-ins:
|
||||
# ``enabled_tools=None`` means "default" (every tool in the pool),
|
||||
# ``[]`` means none, a list is an explicit whitelist. MCP tools can
|
||||
# proxy host-side capabilities, so non-admin runtime access treats
|
||||
# ``mcp_tools=None`` as deny-by-default until an admin grants explicit
|
||||
# names. ``exec_enabled`` is a tri-state override on top of the
|
||||
# deployment exec policy: ``None`` follows the policy, ``False`` always
|
||||
# denies, ``True`` is only honored where the sandbox can actually
|
||||
# isolate users (SYSTEM isolation).
|
||||
"enabled_tools": None,
|
||||
"mcp_tools": None,
|
||||
"exec_enabled": None,
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
"""Per-user tool and exec access resolution (grant v2).
|
||||
|
||||
Mirrors the partner config semantics for real users: ``None`` means
|
||||
"unrestricted / follow defaults", a set is an explicit whitelist. Admins
|
||||
are never restricted. Synthetic scopes (partners) have no grant file and
|
||||
resolve to unrestricted here — their whitelists travel through the chat
|
||||
context metadata instead (``mcp_tools_filter`` / ``enabled_tools``).
|
||||
Optional built-in tools keep the partner config semantics for real users:
|
||||
``None`` means "unrestricted / follow defaults", a set is an explicit
|
||||
whitelist. MCP tools are different because they can proxy host-side
|
||||
capabilities through configured MCP servers. For non-admin real users an
|
||||
absent MCP grant is therefore deny-by-default; administrators remain
|
||||
unrestricted. Synthetic scopes (partners) are handled by the chat pipeline,
|
||||
where their owner-scoped whitelist travels through context metadata
|
||||
(``mcp_tools_filter`` / ``enabled_tools``).
|
||||
|
||||
Enforcement points:
|
||||
|
||||
@@ -13,7 +16,9 @@ Enforcement points:
|
||||
filters the /settings/tools listing so the UI matches.
|
||||
* ``allowed_mcp_tools`` — the chat pipeline intersects this with any
|
||||
caller-scoped ``mcp_tools_filter`` before building the deferred-tool
|
||||
loader, so a granted-away MCP tool can be neither listed nor loaded.
|
||||
loader, so a granted-away MCP tool can be neither listed nor loaded. For
|
||||
real non-admin users, missing ``mcp_tools`` means no MCP tools are listed
|
||||
or loadable until an admin grants specific names.
|
||||
* ``exec_override`` — layered on top of the deployment exec policy in the
|
||||
chat pipeline's exec gate and in the exec tool itself.
|
||||
"""
|
||||
@@ -44,13 +49,19 @@ def allowed_optional_tools() -> set[str] | None:
|
||||
|
||||
|
||||
def allowed_mcp_tools() -> set[str] | None:
|
||||
"""Whitelist of MCP (deferred) tool names, ``None`` = unrestricted."""
|
||||
"""Whitelist of MCP (deferred) tool names.
|
||||
|
||||
``None`` means unrestricted and is reserved for administrators. Real
|
||||
non-admin users fail closed when the grant omits ``mcp_tools`` so a chat
|
||||
turn cannot discover or load deployment-wide MCP host tools until an admin
|
||||
explicitly grants the tool names.
|
||||
"""
|
||||
grant = _current_grant()
|
||||
if grant is None:
|
||||
return None
|
||||
value = grant.get("mcp_tools")
|
||||
if value is None:
|
||||
return None
|
||||
return set()
|
||||
return {str(name) for name in value}
|
||||
|
||||
|
||||
|
||||
@@ -73,10 +73,10 @@ def test_admin_is_never_restricted(as_user):
|
||||
assert exec_override() is None
|
||||
|
||||
|
||||
def test_user_without_grant_is_unrestricted(as_user, mu_isolated_root):
|
||||
def test_user_without_grant_keeps_builtins_unrestricted_but_denies_mcp(as_user, mu_isolated_root):
|
||||
with as_user("u_alice"):
|
||||
assert allowed_optional_tools() is None
|
||||
assert allowed_mcp_tools() is None
|
||||
assert allowed_mcp_tools() == set()
|
||||
assert exec_override() is None
|
||||
|
||||
|
||||
@@ -85,13 +85,13 @@ def test_user_whitelists_resolve_from_grant(as_user, grantable_alice):
|
||||
grantable_alice,
|
||||
{
|
||||
"enabled_tools": ["web_search"],
|
||||
"mcp_tools": [],
|
||||
"mcp_tools": ["mcp_demo_search", "mcp_demo_write"],
|
||||
"exec_enabled": False,
|
||||
},
|
||||
)
|
||||
with as_user(grantable_alice):
|
||||
assert allowed_optional_tools() == {"web_search"}
|
||||
assert allowed_mcp_tools() == set()
|
||||
assert allowed_mcp_tools() == {"mcp_demo_search", "mcp_demo_write"}
|
||||
assert exec_override() is False
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user