Skip to content

Studio: refuse sandbox connects to cloud metadata addresses at runtime - #17024

Merged
danielhanchen merged 2 commits into
mainfrom
studio-sandbox-runtime-metadata-block
Nov 7, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
studio-sandbox-runtime-metadata-block

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

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.

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

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

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-07T13:12:42.708872Z 1ba2d63 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

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c7de516d9b

ℹ️ 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".

Comment on lines +479 to +481
address = args[1] if len(args) > 1 else None
if isinstance(address, tuple) and address and _metadata_address(address[0]):
raise PermissionError(f"Blocked: cloud-metadata host ({address[0]})")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Validate the resolved destination rather than the audit argument

For approved sandbox code that passes a DNS hostname directly to a socket connection, this hook does not enforce the metadata restriction: CPython's socket.connect audit event exposes the caller-supplied address, not necessarily the resolved IP. I verified locally that a connection to localhost emits ('localhost', port) even though its peer is 127.0.0.1. An unlisted hostname therefore passes _metadata_address regardless of its eventual destination, leaving the DNS-alias case this change intends to close unprotected for that connection path. Enforce the check against the actual resolved destination and ensure the connection uses that validated address; the current DNS test only covers urllib's separate, earlier resolution.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 16aae2f: a non-literal host given to socket.connect is now resolved in the hook and refused if any address is a metadata one. Clients that resolve first (urllib, requests, httpx, aiohttp) already pass the IP.

@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. 🎉

Reviewed commit: 1ba2d63299

ℹ️ 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 added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Nov 7, 2026
@danielhanchen
danielhanchen merged commit ff62901 into main Nov 7, 2026
@danielhanchen
danielhanchen deleted the studio-sandbox-runtime-metadata-block branch November 7, 2026 13:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant