Skip to content

Studio desktop: serve the web UI behind an allowlisted reverse proxy (fixes #17420) - #17435

Merged
danielhanchen merged 5 commits into
unslothai:mainfrom
debojyoti-dev:fix/desktop-proxy-hosts-17420
Nov 11, 2026
Merged

danielhanchen merged 5 commits into
unslothai:mainfrom
debojyoti-dev:fix/desktop-proxy-hosts-17420

Conversation

@debojyoti-dev

Copy link
Copy Markdown
Contributor

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:

  • arrived on the loopback listener from a loopback peer,
  • carries exactly one Host, which is either allowlisted or loopback,
  • and names an allowlisted hostname, either as Host (cloudflared's default) or in X-Forwarded-Host, with every X-Forwarded-Host entry allowlisted.

A page on a rebound name sends that name as Host, so it cannot borrow the allowlist by setting X-Forwarded-Host itself. 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 at http://studio.example.com/:

main (empty 404) with UNSLOTH_STUDIO_PROXY_HOSTS=studio.example.com

After logging in through the proxy:

Testing

Real desktop-mode servers (run.py --api-only --no-cloudflare with the desktop owner env set) on Windows 11, one on main and one on this branch, with the issue's curl matrix:

Request main this branch
Local baseline 200 200
Host: studio.example.com 404 200
X-Forwarded-Host only 404 200
X-Forwarded-Proto only 200 200
Both forwarded headers 404 200
Host: evil.example + X-Forwarded-Host: studio.example.com 404 404
Host: other.example.com 404 404
  • Headless Chromium mapped studio.example.com to 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 loopback Host and sends X-Forwarded-Host.
  • New tests/test_trusted_proxy_hosts.py (22 tests): setting parsing, admitted and refused header combinations, loopback-only listener, the real setup_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 in test_middleware are the same on clean main here (backports.zstd isn't installed).
  • ruff check passes on the changed files.

Not tested against a real Cloudflare Named Tunnel (no custom domain available); the local proxy reproduces the headers it sends.

@debojyoti-dev
debojyoti-dev force-pushed the fix/desktop-proxy-hosts-17420 branch from e8ac695 to 185f8ee Compare November 10, 2026 19:14
debojyoti-dev and others added 2 commits November 11, 2026 00:44
…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.
danielhanchen added a commit that referenced this pull request Nov 10, 2026
…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

Mknoops commented Nov 10, 2026

Copy link
Copy Markdown

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!

@danielhanchen danielhanchen added edge case# new-feature# security# Moves a security boundary: sandbox, allowlist, permission or network guard labels Nov 11, 2026
@danielhanchen danielhanchen self-assigned this Nov 11, 2026
…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]
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 11, 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-11T09:50:21.203335Z 7c19420 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 chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread README.md Outdated

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 7c19420: the README now uses export on Linux, launchctl setenv on macOS and setx on Windows, then a restart of Desktop.

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

Reviewed commit: 7c194201f2

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

edge case# new-feature# security# Moves a security boundary: sandbox, allowlist, permission or network guard

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug / Feature Request] Support secure custom domains and reverse proxies in Unsloth Desktop

3 participants