Skip to content

Commit abf2768

Browse files
committed
net_allow_hosts implies net_connect: [80, 443]
Signed-off-by: Cong Wang <cwang@multikernel.io>
1 parent 05d7c8d commit abf2768

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

examples/mcp_agent.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,7 @@ async def run_agent(user_prompt: str, workspace: str):
158158
"web_fetch", web_fetch,
159159
description="Fetch a URL and return the response body. Only httpbin.org is allowed.",
160160
capabilities={
161-
"net_connect": [443],
162-
"net_allow_hosts": ["httpbin.org"], # DNS restricted to this host
161+
"net_allow_hosts": ["httpbin.org"], # implies net_connect: [80, 443]
163162
},
164163
input_schema={
165164
"type": "object",

src/sandlock/mcp/_policy.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ def policy_for_tool(
6262
if key in _POLICY_FIELDS:
6363
kwargs[key] = value
6464

65+
# net_allow_hosts implies net_connect: [80, 443] unless explicit
66+
if "net_allow_hosts" in capabilities and "net_connect" not in capabilities:
67+
kwargs["net_connect"] = [80, 443]
68+
6569
return Policy(**kwargs)
6670

6771

tests/test_mcp.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,25 @@ def test_multiple(self):
6969
assert 8080 in policy.net_connect
7070
assert policy.max_memory == "256M"
7171

72+
def test_net_allow_hosts_implies_net_connect(self):
73+
policy = policy_for_tool(
74+
workspace="/tmp/ws",
75+
capabilities={"net_allow_hosts": ["example.com"]},
76+
)
77+
assert "example.com" in policy.net_allow_hosts
78+
assert 80 in policy.net_connect
79+
assert 443 in policy.net_connect
80+
81+
def test_net_allow_hosts_with_explicit_net_connect(self):
82+
policy = policy_for_tool(
83+
workspace="/tmp/ws",
84+
capabilities={
85+
"net_allow_hosts": ["example.com"],
86+
"net_connect": [8443],
87+
},
88+
)
89+
assert policy.net_connect == [8443] # explicit wins
90+
7291
def test_unknown_field_ignored(self):
7392
policy = policy_for_tool(
7493
workspace="/tmp/ws",

0 commit comments

Comments
 (0)