Repository navigation
Studio: refuse sandbox connects to cloud metadata addresses at runtime - #17024
Conversation
Codex Review Summary: ✅ CompletedThis 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. |
|
@codex review |
There was a problem hiding this comment.
💡 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".
| 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]})") |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
…ata check [skip ci]
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. 🎉 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". |
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:
urlopen("https://some-public-site/x")where the site answers302 Location: http://169.254.169.254/.... urllib, requests and httpx follow it.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 throughPYTHONPATH) installs asys.addaudithookthat looks only atsocket.connect. Every HTTP client ends inconnect()with the final resolved address, so redirects and DNS aliases are both caught there. When code passes a host name straight tosocket.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 raisesPermissionError("Blocked: cloud-metadata host (...)"), which clients surface as an ordinary connection error. IPv4-mapped and 6to4 forms are unwrapped first._build_safe_envsets_UNSLOTH_SANDBOX_BLOCK_METADATA=1. Full access mode (_build_bypass_env) is unchanged.open()calls showed no measurable difference with the hook on vs off.Measured
On an AWS VM, approved Python fetching a local URL that redirects to
http://169.254.169.254/latest/meta-data/:HTTP Error 401: Unauthorized, only because this VM enforces IMDSv2 tokens)URLError: <urlopen error Blocked: cloud-metadata host (169.254.169.254)>, no connection madeNew tests in
TestMetadataBlockedAtRuntime(studio/backend/tests/test_sandbox_tools.py):socket.connectto a name that resolves to a metadata host (test_sandbox_sitecustomize.py)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.