Skip to content

Studio: let an approved Python call reach a host outside the sandbox allowlist - #16674

Merged
danielhanchen merged 1 commit into
mainfrom
studio-approved-python-host
Nov 5, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
studio-approved-python-host

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

Fixes #16625 (the host allowlist part)

Problem

Python tool code that reaches a host outside the built in allowlist (for example requests.get("https://example.com/")) is flagged as risky, so in Ask for approval and Approve for me the user gets an approval prompt. Clicking Approve did nothing: _python_exec re-ran the same static check and refused the call with Blocked: host not in sandbox allowlist. The only way to reach such a host was Full access, which also turns off every file restriction. That is exactly the "soften the network while keeping file access strict" gap from the issue.

Measured on main (Linux, software safeguards):

Python code asks first approved result
urlopen("https://example.com/") yes no Blocked: host not in sandbox allowlist
urlopen("https://example.com/") yes yes Blocked: host not in sandbox allowlist

Fix

_check_code_safety takes host_approved; when set it drops only the untrusted_host_blocked findings. _python_exec passes it for a call the user approved, the same way an approved rm is already let through in _bash_exec.

After the fix:

Python code approved result
urlopen("https://example.com/") no Blocked: host not in sandbox allowlist (unchanged)
urlopen("https://example.com/") yes 200
urlopen("http://169.254.169.254/...") yes Blocked: cloud-metadata host (unchanged)
urlopen("https://pypi.org/simple/") either 200 (unchanged)

Tests

tests/test_sandbox_tools.py::TestApprovedHostLiftsOnlyTheAllowlist (5 cases) fails on main and passes here: the approved host goes through, metadata / upload / non-literal destinations stay blocked, and an API-key caller is still refused.

Base vs head over the 11 test files that touch the changed code (test_sandbox_tools, test_tool_sandbox_*, test_api_key_tool_confirm, test_approved_rm, test_permission_mode*, the tool loop tests, test_windows_cmd_terminal_profile): base 6 failed / 4336 passed, head 1 failed / 4341 passed. The 5 new tests are the only change; the remaining failure (test_llama_cpp_tool_loop.py::test_web_search_keeps_to_the_requested_domains) fails on main too.

Extra folders and per-tool allow rules from the issue are separate features and are not part of this PR.

@danielhanchen danielhanchen self-assigned this Nov 4, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-04T15:25:04.998682Z 9a7537d Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: 9a7537df5f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit e60ca31 into main Nov 5, 2026
@danielhanchen
danielhanchen deleted the studio-approved-python-host branch November 5, 2026 12:11
danielhanchen pushed a commit to Etherll/unsloth that referenced this pull request Nov 7, 2026
…thai#16937)

Since unslothai#16674, an approved Python tool call (or Python with "Always
allow" in a chat) may reach hosts off the sandbox allowlist, but cloud
metadata hosts are meant to stay blocked. That check only matched the
dotted text 169.254.169.254. The same address written as 2852039166,
0xa9fea9fe, 0251.0376.0251.0376, [::ffff:169.254.169.254], fullwidth
digits or ideographic dots, or the AWS IPv6 endpoint as
[fd00:ec2:0::254] or [fd00:ec2::254%1], counted as an ordinary unknown
host and ran once approved. So did metadata hosts Studio already refuses
for provider URLs, such as metadata.goog, instance-data.ec2.internal and
[fd20:ce::254]. On a cloud GPU machine a confused or prompt-injected
model could then read instance-role credentials from the metadata
service.

The check now normalises the host, reads a literal IP address the way
the network stack does, and also runs the metadata check Studio applies
to provider URLs. These now get "Blocked: cloud-metadata host", approved
or not. A host the parser cannot read (for example one with a NUL byte)
is blocked instead of raising. Approved calls to other hosts still run.

The check is static, so a DNS name that resolves to a metadata address,
or a redirect to one, is still out of scope.

Affects builds with unslothai#16674 (merged 2026-11-05) for users who approve
Python calls on a cloud VM.
danielhanchen added a commit that referenced this pull request Nov 7, 2026
#17024)

Follow-up to #16937 and #16674.

An approved Python tool call (or Python with "Always allow" in a chat)
may reach hosts off the sandbox allowlist, but cloud metadata hosts are
meant to stay blocked. That rule was only enforced by the static check
on the code text, so two plain cases got through:

- a redirect: `urlopen("https://some-public-site/x")` where the site
answers `302 Location: http://169.254.169.254/...`. urllib, requests and
httpx follow it.
- a DNS name that resolves to a metadata address, for example any
wildcard DNS service that maps a name to `169.254.169.254`.

On a cloud GPU machine that lets a confused or prompt-injected model
read the instance metadata service, including instance-role credentials
on hosts that still allow IMDSv1, GCP and others.

## What changes

The sandbox `sitecustomize.py` (already loaded into every sandboxed
Python process through `PYTHONPATH`) installs a `sys.addaudithook` that
looks only at `socket.connect`. Every HTTP client ends in `connect()`
with the final resolved address, so redirects and DNS aliases are both
caught there. When code passes a host name straight to `socket.connect`,
the audit event carries the name, so the hook resolves it and refuses if
any address is a metadata one. A connect to 169.254.0.0/16, the AWS /
GCP IPv6 metadata endpoints, the Alibaba addresses or a known metadata
host name raises `PermissionError("Blocked: cloud-metadata host
(...)")`, which clients surface as an ordinary connection error.
IPv4-mapped and 6to4 forms are unwrapped first.

- Enabled only for sandboxed runs: `_build_safe_env` sets
`_UNSLOTH_SANDBOX_BLOCK_METADATA=1`. Full access mode
(`_build_bypass_env`) is unchanged.
- Stdlib only, no new dependency, works on Linux, macOS and Windows
(audit hooks exist since Python 3.8).
- Idempotent: installed once per interpreter, guarded by a flag.
- The hook returns immediately for every other audit event. Importing
pandas and numpy plus 20k `open()` calls showed no measurable difference
with the hook on vs off.
- No UI change. Ordinary hosts, including loopback and LAN, connect
exactly as before.

## Measured

On an AWS VM, approved Python fetching a local URL that redirects to
`http://169.254.169.254/latest/meta-data/`:

| | Result |
|---|---|
| main | connection reached the metadata service (`HTTP Error 401:
Unauthorized`, only because this VM enforces IMDSv2 tokens) |
| this PR | `URLError: <urlopen error Blocked: cloud-metadata host
(169.254.169.254)>`, no connection made |

New tests in `TestMetadataBlockedAtRuntime`
(`studio/backend/tests/test_sandbox_tools.py`):

| Test | main | this PR |
|---|---|---|
| redirect to a metadata host through the real python tool executor |
fail | pass |
| DNS alias of a metadata host in a child using the sandbox env | fail |
pass |
| ordinary local host still connects | pass | pass |
| full access env does not enable the hook | fail | pass |
| raw `socket.connect` to a name that resolves to a metadata host
(`test_sandbox_sitecustomize.py`) | fail | pass |

## Scope

This guards Python processes started by the sandbox. A non-Python binary
run from the terminal tool (for example `curl`) still relies on the
existing static command check.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Add granularity to sandboxing

1 participant