Skip to content

Studio: block more metadata host spellings for approved Python - #16937

Merged
danielhanchen merged 4 commits into
unslothai:mainfrom
NilayYadav:python-metadata-host-ip-spellings
Nov 7, 2026
Merged

danielhanchen merged 4 commits into
unslothai:mainfrom
NilayYadav:python-metadata-host-ip-spellings

Conversation

@NilayYadav

@NilayYadav NilayYadav commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

Since #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 #16674 (merged 2026-11-05) for users who approve Python calls on a cloud VM.

@NilayYadav NilayYadav changed the title Studio: block metadata hosts in every IP spelling for approved Python Studio: block more metadata host spellings for approved Python Nov 6, 2026
@NilayYadav NilayYadav self-assigned this Nov 6, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 6, 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-07T11:16:45.267962Z c07e501 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.

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 4b2bc99be0

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

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: c07e501352

ℹ️ 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 58a1443 into unslothai:main Nov 7, 2026
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.

2 participants