Skip to content

Studio: API keys run tools in the sandbox; Full access needs the Unsloth UI - #14345

Merged
danielhanchen merged 3 commits into
mainfrom
studio-api-key-no-full-access
Oct 20, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
studio-api-key-no-full-access

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 20, 2026 •

Copy link
Copy Markdown
Member

Hardening from a security report. Nothing changes in the Unsloth UI, Desktop or notebooks.

Problem. Studio already keeps host-local command authority to the Unsloth UI. An sk-unsloth API key cannot define a local (stdio) MCP command, cannot read provider credentials or the ambient HF token, and sees host paths redacted. Full access was the exception. An owner API key could send bypass_permissions: true (or permission_mode: "full", or disable_sandbox: true) to /v1/chat/completions, /v1/responses or /v1/messages and have terminal and python run with no sandbox, as the Studio user. With unsloth studio --secure or unsloth run --secure, that key is the only thing guarding a public Cloudflare link, and API keys are what people paste into third-party clients. Tools are on by default there, so a leaked key meant unsandboxed code execution rather than sandboxed tool calls.

Change.

  • The account context now records whether an API key or keyless API access admitted the request (via_api_key, not part of the account's identity or equality).

  • require_tool_access, which every Full access path goes through, refuses that caller with a 400 at the request door (chat completions, responses, messages, and durable chat runs before they are queued):

    Full access (bypass_permissions, permission_mode 'full', disable_sandbox) is off for API keys: tools run in the sandbox. Use permission_mode 'auto', 'ask' or 'off', or set UNSLOTH_STUDIO_ALLOW_API_FULL_ACCESS=1 on the server to allow it.

  • Unchanged:

    • API keys still run tools in the sandbox (auto, ask, off).
    • UI sessions and Desktop keep Full access.
    • Managed accounts keep their existing refusal.
  • UNSLOTH_STUDIO_ALLOW_API_FULL_ACCESS=1 restores the old behaviour, following the UNSLOTH_STUDIO_ALLOW_STDIO_MCP convention.

Tests. tests/test_api_key_full_access.py, 30 cases:

  • Real API-key and keyless authentication bind an API caller, and Full access is refused for them.
  • A real UI session JWT keeps Full access.
  • The opt-in restores it.
  • The sandboxed modes stay open to API keys.
  • The door refuses all three flags on all three routes for an API key and passes them for a UI session.
  • Durable chat runs refuse an API key's Full access before queueing.

On main, 11 of the original 26 fail (every API-key and keyless refusal), and the 4 durable-run cases also fail; all 30 pass here. The existing durable-run suites pass: 122 tests.

Evidence.

  • Live backend (main vs this branch, fresh Studio home, real login and API key; the advisory's request with bypass_permissions: true):

    caller route main this PR
    API key /v1/chat/completions passes the gate (stops at "No model loaded") 400 Full access is off for API keys
    API key /api/inference/chat/completions passes the gate 400 Full access is off for API keys
    UI session both passes the gate passes the gate (unchanged)
  • Account, auth, keyless and permission suites (83 files): 3929 passed, 2 skipped on both main and this branch.

  • The 42 other test files that touch Full access: the same 4309 passed on both. The same 13 tests in test_search_images.py fail on main too.

  • Studio regression switchboard, main vs this branch: NO_REGRESSION on auth, settings, inference settings, parallel, crawl and the tiny diffusion benches. The full_access_isolated journey, which drives the UI's Full access mode in an isolated sandbox, is SAME on all 4 steps: owner Full access, tool model load, UI toggles and the Python probe.

…oth UI

An sk-unsloth key or keyless API access asking for bypass_permissions, permission_mode 'full' or
disable_sandbox is refused with a 400 at the request door, the same rule local stdio MCP commands
already follow. UNSLOTH_STUDIO_ALLOW_API_FULL_ACCESS=1 restores the old behaviour. UI sessions and
Desktop are unchanged.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 20, 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-10-20T13:19:51.588958Z 3c11559 Manual request
🔒 Security Review ✅ Completed 2026-10-20T12:52:53.476486Z d547ff2 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 security review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: d547ff22b9

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

@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review

Security review completed. No security issues were found in this pull request.

Reviewed commit: d547ff22b9

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

@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. Chef's kiss.

Reviewed commit: 3c115596da

ℹ️ 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 17ecb2e into main Oct 20, 2026
@danielhanchen
danielhanchen deleted the studio-api-key-no-full-access branch October 20, 2026 15:46
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Oct 21, 2026
… Sandbox High (unslothai#14422)

Hardening from a security review of the Studio tool sandbox. Nothing
changes for Desktop sign-in, the first-run password change in the web UI
or Colab, or notebooks.

**Problem 1: the seeded owner password stays in the page on Desktop.**
While the default `unsloth` password is still pending a change,
`index.html` carries it for a direct loopback client so the first-run
screen can fill it in. A Desktop backend never retires it: the shell
signs in through `/api/auth/desktop-login`, which answers
`must_change_password = False`, so the pending state can last forever.
Tool code runs in a jail that shares the host network on purpose (tools
download models), so it reaches the loopback port like a local browser
and can read that password.

**Change.** A Desktop-owned backend no longer injects the seeded
password, and a successful `desktop-login` clears it from memory. The
Tauri shell loads its own bundled frontend and signs in with its own
secret; the only reader of the injected value is the change-password
form, which the shell skips. The web UI, `unsloth studio` and Colab
first-run autofill are unchanged.

- Visible difference: a Desktop user who opens `http://127.0.0.1:<port>`
in an ordinary browser before ever setting a password now sees a
"Current password" field instead of an autofilled one (the password is
in `.bootstrap_password`, as before). After the password is set in
Desktop, browser login is unchanged.

**Problem 2: API callers can still pick Sandbox Low.** unslothai#14345 keeps
API-key and keyless callers out of Full access, `bypass_permissions` and
`disable_sandbox`, but `sandbox_level: "low"` (Python and Terminal on
software safeguards only, no OS sandbox) was not part of that check.

**Change.** `require_tool_access` also refuses `sandbox_level: "low"`
for API-key and keyless callers, with the same 400 and the same
`UNSLOTH_STUDIO_ALLOW_API_FULL_ACCESS=1` opt-in. The default is `high`,
the UI (session login) keeps Sandbox Low, and one check covers
`/v1/chat/completions`, `/v1/responses`, `/v1/messages` and durable chat
runs.

Both changes are pure checks; nothing is stored, so repeating them is a
no-op.

**Tests.** `test_desktop_bootstrap_retired.py` and
`test_api_key_sandbox_level.py`: 13 of 19 fail on main, all 19 pass
here. The 90 existing modules that exercise these files give the same
result on both sides (one test,
`test_desktop_auth.py::test_health_response_reports_desktop_capability_fields`,
already fails on main).


**Before / after.** A first launch with the Desktop owner variables set,
opened in a plain browser tab. Before, the page carries the seeded
password and the setup form fills it in silently (no "Current password"
field); after, the page no longer contains it and the form asks for the
current password. The Desktop app itself signs in through
`/api/auth/desktop-login` and does not use this page.

![Desktop bootstrap before and
after](https://raw.githubusercontent.com/danielhanchen/unsloth-staging-2/pr-media/pr/14422/pr14422_ba-b704249b97b7.png)
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Oct 25, 2026
…ox (unslothai#14757)

## Problem

unslothai#14345 and unslothai#14422 set the rule that an API key (or keyless API access)
never runs Python or Terminal without the OS sandbox unless the server
sets `UNSLOTH_STUDIO_ALLOW_API_FULL_ACCESS=1`: Full access,
`bypass_permissions`, `disable_sandbox` and `sandbox_level: "low"` are
all refused for API callers.

The tool approval path was not part of that rule. In `auto` and `ask`
modes a call that names a host path outside the sandbox roots parks for
confirmation, and an approved one launches with software safeguards only
(`_prepare_tool_launch`, fault `user_approved_host_access`). `POST
/api/inference/tool-confirm` accepts an approval from any authenticated
caller, API keys included, and the request that started the run receives
the approval id in its own stream. So an API caller could approve its
own parked host-path call and get the unsandboxed launch the other
checks refuse.

A second gap made the launch side unable to tell: for an owner-scoped
API key, `account_tool_stream` ran the tool thread without rebinding the
account, so the thread lost the "admitted by an API key" flag.

## Change

- `/tool-confirm`: an `allow` from an API-key or keyless caller for a
parked call that would reach host paths gets 403, naming the env opt-in.
`deny` is accepted from everyone, and approving a call that stays in the
sandbox is unchanged.
- The three tool loops (`studio_tool_loop`, `llama_cpp`,
`safetensors_agentic`) flag a parked call as host access when it is
registered, using the same `_reaches_host_paths` check the executor
applies before honouring an approval (`approval_grants_host_access`).
The flag is set before the approval id is sent to the client.
- Launch side, independent of who approved: `_prepare_tool_launch`
ignores an approval for a call that came from an API-key request and
keeps it in the OS sandbox in `required` mode, so a sandbox that is
unavailable refuses instead of falling back to software safeguards. The
Windows cmd Terminal path applies the same check.
- `account_tool_stream` now rebinds the account for owner API-key
requests too, so the tool thread sees the same API-key flag the request
had (this also makes the existing `require_tool_access` check inside
`execute_tool` effective for owner keys).
- `UNSLOTH_STUDIO_ALLOW_API_FULL_ACCESS=1` restores the previous
behaviour for API callers. Unsloth UI and Desktop sessions are
unchanged.

## Tests

New `studio/backend/tests/test_api_key_tool_confirm.py` drives each of
the three tool loops end to end with a real approval slot, the real
`/tool-confirm` route (API-key auth faked as in
`test_api_key_sandbox_level.py`) and the real `_prepare_tool_launch`
decision (OS sandbox stubbed):

| case | main | this PR |
|---|---|---|
| API key approves a host-path call | 200, launches with software
safeguards | 403, call does not run |
| UI approves a host-path call from an API-key request | software
safeguards | OS sandbox, `required` |
| API key with `UNSLOTH_STUDIO_ALLOW_API_FULL_ACCESS=1` | software
safeguards | software safeguards |
| UI session approves its own host-path call | software safeguards |
software safeguards |
| API key denies | 200, not run | 200, not run |
| API key approves a sandboxed call (`ask` mode) | 200, OS sandbox |
200, OS sandbox |

14 of the 26 new tests fail on main and all 26 pass on this branch.

The 25 existing studio backend test files that touch tool approvals,
`host_access_approved`, the API Full access opt-in, `sandbox_level` or
`_prepare_tool_launch`, plus the new file (CPU, Linux): 4334 passed on
this branch and 4320 on main. The same 36 tests fail on both: the MCP
tests in `test_account_tool_isolation.py`, plus tests in
`test_llama_admission_compat_matrix.py`, `test_mcp_image.py`,
`test_llama_cpp_tool_loop.py::test_web_search_keeps_to_the_requested_domains`
and the Terminal cases of `test_tool_sandbox_wiring.py`. The only other
difference is the 14 new tests, which fail on main.

## Not covered

- A project folder the owner picked keeps running with software
safeguards for every call in `auto`, approved or not. That is unchanged
here, for API callers too.
- When the OS sandbox is unavailable, `auto` falls back to software
safeguards for calls that never park. That is unchanged too. Only the
approved host-path call from an API key is now refused.
- Tested on Linux with the OS sandbox stubbed. No live backend run, and
the Windows and macOS sandbox backends were not exercised.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
danielhanchen added a commit that referenced this pull request Nov 5, 2026
…allowlist (#16674)

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`.

- The allowlist is advisory today: every sandbox backend reports
`unrestricted_network` (see #13345), so this changes no boundary.
- Cloud-metadata hosts, uploads, non-literal destinations and every
other static check stay blocked, approved or not.
- File isolation is unchanged: the call still runs in the OS sandbox
unless it names host paths, which was already the rule for approved
calls.
- API-key requests keep the allowlist even after an approval, matching
#14345 / #14422.
- Run automatically and unapproved calls behave exactly as before. No UI
change.
- A remembered "Always allow" for Python in a chat counts as an approval
here, the same way it already does for host file access.

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