Repository navigation
Studio: let an approved Python call reach a host outside the sandbox allowlist - #16674
Merged
Merged
Conversation
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Member
Author
|
@codex review |
1 similar comment
Member
Author
|
@codex review |
|
Codex Review: Didn't find any major issues. Breezy! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_execre-ran the same static check and refused the call withBlocked: 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):
urlopen("https://example.com/")Blocked: host not in sandbox allowlisturlopen("https://example.com/")Blocked: host not in sandbox allowlistFix
_check_code_safetytakeshost_approved; when set it drops only theuntrusted_host_blockedfindings._python_execpasses it for a call the user approved, the same way an approvedrmis already let through in_bash_exec.unrestricted_network(see Studio sandbox: state that the python tool host allowlist is advisory at runtime #13345), so this changes no boundary.After the fix:
urlopen("https://example.com/")Blocked: host not in sandbox allowlist(unchanged)urlopen("https://example.com/")200urlopen("http://169.254.169.254/...")Blocked: cloud-metadata host(unchanged)urlopen("https://pypi.org/simple/")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.