Repository navigation
Studio desktop: serve the web UI behind an allowlisted reverse proxy (fixes #17420) - #17435
Conversation
e8ac695 to
185f8ee
Compare
…ixes unslothai#17420) The desktop backend only serves its web UI to its own Cloudflare quick tunnel, the LAN listener, or a direct loopback browser, so a user-managed proxy such as a Cloudflare Named Tunnel got an empty 404. UNSLOTH_STUDIO_PROXY_HOSTS lists public hostnames (comma or space separated). A loopback connection is admitted when its Host is allowlisted, or is loopback with every X-Forwarded-Host entry allowlisted. A rebound page sends its own name as Host, so it cannot borrow the allowlist. Unset by default; the bootstrap password gate is unchanged.
…helper (#17442) ## Summary `Source lint` fails on main since #17256, and on every PR based on it (seen on #17429 and #17435): ``` duplicate definitions (1): studio/backend/core/inference/tools.py:9806: _substitution_end is defined twice (def at line 6738, def here) ``` These are not two copies of one function. They are two different helpers that ended up with the same name: | | Added by | Returns for `$( ... )` | Unterminated | Quotes inside | |---|---|---|---|---| | line 6738 | #17256 (heredoc screening), used by `_quote_end` | one past the `)` | `-1` | `_quote_end`, which handles `\"` and nested `$(` | | line 9806 | #17010 (Windows `2>nul` rewrite), used by `_bash_nul_to_dev_null` | the index of the `)` | `len(command)` | naive `find` of the same quote | The later definition wins at module scope. So since #17256 merged, its `_quote_end` has been running #17010's helper rather than the one written for it, while #17010's nul rewrite keeps its own. The lint is right, and both PRs are correct on their own. The fix is to give #17256's helper its own name. The practical effect is small but real: the two disagree on some quoting. For example, `"( )'$("`")"` reads as unterminated with the intended helper and as closed with the shadowing one, which changes how the Terminal heredoc screen splits that command. ## Change - The helper at line 6738 is renamed `_quoted_substitution_end`, and its one caller in `_quote_end` uses it. That is the function #17256 wrote and tested. - #17010's `_substitution_end` and the nul rewrite are untouched. - `test_the_heredoc_quote_scanner_keeps_its_own_substitution_helper` in `studio/backend/tests/test_permission_mode.py` pins both contracts: - `_quoted_substitution_end` returns one past the `)` and `-1` when unterminated; - `_quote_end` closes `"$(echo "a)")"` at its last quote and keeps the case above unterminated; - `_substitution_end` keeps the nul rewrite's own return values. It fails on main, where the name is shadowed. ## Testing - `scripts/lint_duplicate_definitions.py` on `tools.py`: flags main, clean here - `studio/backend/tests/test_permission_mode.py` and `test_windows_bash_nul_redirect.py`: 2637 passed, 1 skipped. The new test fails on main. - `tests/test_lint_duplicate_definitions.py`: 12 passed
|
Thanks for looking into this so quickly and putting together a fix! The proposed solution looks great, especially keeping the security protections intact while allowing custom reverse proxies. I'm running Unsloth Desktop on Windows behind a Cloudflare Named Tunnel with Cloudflare Access, so I'd be happy to test the fix with a real custom domain once it's available. Really appreciate your work on this! |
…RDED uvicorn's proxy_headers replaces the loopback peer with the X-Forwarded-For visitor, so the allowlist refused every cloudflared request when both were set. The listener is still required to be loopback. README: keep cloudflared's Host and where the remote password lives. [skip ci]
|
@codex review |
1 similar comment
|
@codex review |
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. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 3da076a812
ℹ️ 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".
|
|
||
| **Your own reverse proxy or custom domain** (Cloudflare Named Tunnel, nginx, Caddy) running on the same computer: Unsloth Desktop only serves its web UI to a proxy for hostnames you list, so set them before starting it: | ||
| ```bash | ||
| UNSLOTH_STUDIO_PROXY_HOSTS=studio.example.com # comma separated; on Windows: setx UNSLOTH_STUDIO_PROXY_HOSTS studio.example.com |
There was a problem hiding this comment.
Export the proxy allowlist in the Unix setup example
For a user following this snippet in a fresh Bash shell, the standalone assignment creates a shell variable but does not pass it to a subsequently launched Studio process. Bash's help export describes the export attribute as enabling “automatic export to the environment of subsequently executed commands.” Without it, trusted_proxy_hosts() reads an empty value from os.environ, so the documented setup still produces the original proxy-facing 404. Use export UNSLOTH_STUDIO_PROXY_HOSTS=studio.example.com and launch Studio from that shell, or put the assignment directly before the launch command.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 7c19420: the README now uses export on Linux, launchctl setenv on macOS and setx on Windows, then a restart of Desktop.
|
@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". |
Fixes #17420
What this changes
The desktop backend (
--api-only, desktop-owned) serves its packaged web UI only to its own Cloudflare quick tunnel, the LAN listener, or a direct loopback browser. A user-managed reverse proxy on the same computer (Cloudflare Named Tunnel with a custom domain, nginx, Caddy) gets an empty 404, even though login and the API already work through it.This adds an explicit opt-in,
UNSLOTH_STUDIO_PROXY_HOSTS: public hostnames, comma or space separated. When it is set, the web UI (/, SPA routes and/assets) is also served to a request that:Host, which is either allowlisted or loopback,Host(cloudflared's default) or inX-Forwarded-Host, with everyX-Forwarded-Hostentry allowlisted.A page on a rebound name sends that name as
Host, so it cannot borrow the allowlist by settingX-Forwarded-Hostitself. Entries that are IP literals,localhost, wildcards or not plain DNS names are ignored. Nothing changes when the variable is unset, and the bootstrap password gate (is_direct_local_request) is untouched, so a proxied request never counts as local. The README gets a short note under Remote HTTPS & LAN Access.Before / after
Same request through a local proxy that forwards like cloudflared (
Host: studio.example.com,CF-Connecting-IP,X-Forwarded-For,X-Forwarded-Proto: https), opened in Chromium athttp://studio.example.com/:UNSLOTH_STUDIO_PROXY_HOSTS=studio.example.comAfter logging in through the proxy:
Testing
Real desktop-mode servers (
run.py --api-only --no-cloudflarewith the desktop owner env set) on Windows 11, one on main and one on this branch, with the issue's curl matrix:Host: studio.example.comX-Forwarded-HostonlyX-Forwarded-ProtoonlyHost: evil.example+X-Forwarded-Host: studio.example.comHost: other.example.comstudio.example.comto a local proxy in front of each server: main shows the blank 404; this branch loads the login page, logs in and reaches/chat. Same result with a proxy that keeps a loopbackHostand sendsX-Forwarded-Host.tests/test_trusted_proxy_hosts.py(22 tests): setting parsing, admitted and refused header combinations, loopback-only listener, the realsetup_frontend(tunnel_only=True)mount over ASGI, and the bootstrap gate staying closed.test_index_bootstrap_loopback,test_frontend_resolution,test_lan_access_settings,test_forwarded_header_trust,test_keyless_loopback_proxy,test_login_forwarded_client_ip,test_middleware: 336 passed. The 4 zstd errors intest_middlewareare the same on clean main here (backports.zstdisn't installed).ruff checkpasses on the changed files.Not tested against a real Cloudflare Named Tunnel (no custom domain available); the local proxy reproduces the headers it sends.