Repository navigation
Studio: API keys run tools in the sandbox; Full access needs the Unsloth UI - #14345
Conversation
…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.
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. |
|
@codex security review |
|
Codex Review: Didn't find any major issues. Chef's kiss. 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". |
🛡️ Codex Security ReviewSecurity review completed. No security issues were found in this pull request. Reviewed commit: Only the user who started this review can view the report in Codex. ℹ️ About Codex security reviews in GitHubThis is an experimental Codex feature. Security reviews are triggered when:
Once complete, Codex will leave suggestions, or a comment if no findings are found. |
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Chef's kiss. 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". |
… 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. 
…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>
…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.
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-unslothAPI 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 sendbypass_permissions: true(orpermission_mode: "full", ordisable_sandbox: true) to/v1/chat/completions,/v1/responsesor/v1/messagesand haveterminalandpythonrun with no sandbox, as the Studio user. Withunsloth studio --secureorunsloth 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):Unchanged:
auto,ask,off).UNSLOTH_STUDIO_ALLOW_API_FULL_ACCESS=1restores the old behaviour, following theUNSLOTH_STUDIO_ALLOW_STDIO_MCPconvention.Tests.
tests/test_api_key_full_access.py, 30 cases: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):/v1/chat/completions/api/inference/chat/completionsAccount, 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.pyfail 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_isolatedjourney, 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.