Skip to content

Studio: scope the seeded bootstrap password auto-fill to loopback clients - #7131

Merged
danielhanchen merged 12 commits into
unslothai:mainfrom
oobabooga:studio-loopback-bootstrap-injection
Jul 15, 2026
Merged

danielhanchen merged 12 commits into
unslothai:mainfrom
oobabooga:studio-loopback-bootstrap-injection

Conversation

@oobabooga

Copy link
Copy Markdown
Member

On a wildcard bind (unsloth studio -H 0.0.0.0), the served index.html auto-injects the seeded admin bootstrap password so the first-login form can pre-fill. The gate for that (PR #5739) only checks same-origin, not who is connecting, so a LAN peer that opens the host gets the default admin credential in-page. Studio's server-side Python and terminal tools are on by default, so that credential is a remote code-execution surface.

Fix

Also require the client to be on this machine. The gate is now same-origin AND (Colab OR loopback client). A remote peer reads the password from the terminal instead (still printed at startup and saved to .bootstrap_password); login is unchanged. Colab is exempted because its trusted forwarded headers make every peer look remote and a notebook is a single-user sandbox.

Verification

Ran a real -H 0.0.0.0 server: a loopback request still auto-fills, a request from the machine's LAN IP does not. Added regression tests for the loopback classifier; the bootstrap-origin suite stays green (34 passed).

@oobabooga
oobabooga requested a review from danielhanchen as a code owner July 14, 2026 22:34

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a security check _is_loopback_client to prevent the bootstrap admin password from being leaked to LAN peers when binding to a wildcard address (0.0.0.0), along with comprehensive unit tests. The review feedback correctly identifies a compatibility issue with ipv4_mapped on Python versions prior to 3.12 and provides a robust fallback implementation to prevent test failures on older Python environments.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread studio/backend/main.py Resolved

@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: 150f37f06a

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread studio/backend/main.py Resolved
@oobabooga

Copy link
Copy Markdown
Member Author

@codex review

@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: 95b7cc132d

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread studio/backend/main.py Resolved
@oobabooga

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: 75cb6c172d

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@danielhanchen

Copy link
Copy Markdown
Member

Thanks for this. The loopback scoping is correct, and the Cloudflare tunnel and DNS rebinding cases are both handled well.

I pushed one small addition on top: a regression test for the except ValueError branch in _is_local_bootstrap_request. request.url.hostname can raise on a malformed Host (the urllib IPv6 hardening from CVE-2024-11168), and that was the one branch in the new gate without coverage. The test confirms it falls back to remote so a bad Host can never leak the bootstrap password.

On the earlier note about ipaddress.ip_address(...).ipv4_mapped: that property is on IPv6Address from 3.9 onward (checked 3.9 through 3.13), and our requires-python is >=3.9, so it is present on every supported interpreter. The getattr(..., None) is only there for the IPv4Address case, which has no such attribute. So that path is fine as written and no change is needed.

Rest looks good to me.

@danielhanchen

Copy link
Copy Markdown
Member

Pushed a follow-up (4b71e47) to tighten the "is this a local client" gate. Three cases could still receive the seeded bootstrap password:

1. Reverse proxies other than Cloudflare. The gate only rejected CF-Connecting-IP. A same-host reverse proxy that connects over loopback and relays a remote client via Forwarded, X-Real-IP, or X-Forwarded-Host (uvicorn only consumes X-Forwarded-For, so the rest reach the app) with Host: localhost still passed. Any of those forwarding markers on a loopback peer is now treated as a proxied (remote) client, which also lines the gate up with how utils/client_ip.py already reasons about forwarded traffic.

2. Malformed or absent Host. request.url.hostname falls back to the ASGI server address when the Host header is malformed (fails Starlette's host regex) or absent (HTTP/1.0). On a wildcard bind that fallback is the loopback interface the connection landed on, so Host: e_vil or a Host-less HTTP/1.0 request from a loopback peer passed the host check. The check now reads the raw Host header directly and fails closed when it is missing or does not parse to localhost/loopback.

3. Colab shareable link. _IS_COLAB short-circuited the whole gate, so a manually started Colab Cloudflare share link (which sets CF-Connecting-IP) would serve the password to remote visitors. The Colab branch now keeps in-notebook autofill but still withholds when CF-Connecting-IP is present.

The decision is now a single _should_inject_bootstrap helper so the same-origin + local check lives in one testable place.

Verified on a real uvicorn -H 0.0.0.0 server (connecting over loopback): a legit Host: localhost request still autofills, while malformed/absent Host, X-Real-IP/Forwarded proxy, and the Colab share-tunnel cases now withhold. Added regression tests for each; the bootstrap/origin suites stay green (41 passed).

@danielhanchen

Copy link
Copy Markdown
Member

Pushed one more hardening (d602843) after a broad cross-platform and cross-version simulation.

While simulating the loopback classifier I found a small gap in my own change. _is_loopback_ip reads the raw Host header, and on Python 3.9+ ipaddress.ip_address("::1%eth0") accepts the scope id and reports is_loopback = True, so a Host: [::1%eth0] from a loopback peer was treated as local (and it was version-inconsistent, since 3.8 rejects it). A scope id never belongs on a loopback authority or a loopback socket peer, so _is_loopback_ip now rejects any % and fails closed. Added regression coverage for both the peer and Host paths.

Simulation summary, all green:

  • Before/after decision matrix, 45 scenarios, run on Python 3.9 through 3.13. Confirms the pre-PR gate served the seeded password to 25 of these (the real LAN, proxy, and DNS-rebinding leak), the first version of this PR still served 15 (malformed or absent Host, non-Cloudflare proxy headers, and the Colab share link), and the current code withholds all of them while never withholding a legitimate local client.
  • Real uvicorn server driven over raw sockets on both IPv4 (0.0.0.0 bind) and IPv6 (::1) loopback, replaying Chrome, Firefox, Safari, and Edge top-level navigations (which omit Origin) plus every attack request. 26/26 on starlette 0.37.2 + uvicorn 0.29.0 (older installs) and again on starlette 1.3.1 + uvicorn 0.51.0 (latest). Local localhost autofill still works; DNS-rebind, cross-origin, malformed or absent Host, Forwarded / X-Real-IP / X-Forwarded-For proxies, and the Cloudflare tunnel all withhold.

No dependency or API changes: the gate uses only request.client and request.headers, so old and new Studio installs behave identically. This is a request-gating change with no hardware or GPU path.

@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: 795900bb6a

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread studio/backend/main.py Outdated
Comment on lines +1401 to +1402
if host.startswith("["): # [IPv6](:port)
host = host[1:].split("]", 1)[0]

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 Fail closed on malformed loopback Host headers

This parser strips a bracketed Host down to the text before ] without validating that the bracket exists or that the suffix is empty/a port, so values like [::1]attacker or [::1 are reduced to ::1 and accepted as loopback. In the loopback-terminating proxy/tunnel case this gate is meant to defend, if the proxy omits the listed forwarding headers, a remote top-level GET has no Origin and can choose such a malformed Host to still receive the bootstrap script despite the fail-closed Host check.

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.

Good catch, fixed in 9220109. The bracket branch now requires a real closing bracket followed only by a port, so [::1]evil, [::1 and [::1]x no longer reduce to ::1 and instead fail closed. Added regression cases for the smuggling variants.

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@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: b27e8a3273

ℹ️ 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 studio/backend/main.py
Comment on lines +1417 to +1419
if any(request.headers.get(h) is not None for h in _PROXIED_CLIENT_HEADERS):
return False
return _host_header_is_loopback(request.headers.get("host"))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Block headerless loopback reverse proxies

When Studio sits behind a minimal reverse proxy on the same host, this gate still returns true for remote users: for example, NGINX with just proxy_pass http://127.0.0.1:8902; connects from 127.0.0.1 and, per the official proxy docs, defaults Host to $proxy_host while only redefining Host/Connection unless extra proxy_set_header directives are added (https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_set_header). A top-level browser GET has no Origin, none of the headers in _PROXIED_CLIENT_HEADERS are present, and the loopback Host makes _host_header_is_loopback() pass, so the bootstrap password is still embedded for a remote client through this common proxy configuration.

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.

Confirmed, this is a real gap. With a minimal same-host reverse proxy (for example nginx proxy_pass http://127.0.0.1:PORT; with no extra proxy_set_header lines) nginx connects from loopback, forwards Host: $proxy_host (a loopback authority), sets none of the forwarded-client headers, and a top-level GET carries no Origin, so _should_inject_bootstrap returns true for a remote user. No socket, Host, or port signal can separate that from a genuine local browser, because the proxy connects to the same loopback socket and can present the same values.

Two notes on scope. This is pre-existing: the same-origin-only gate before this change embedded the password for that nginx config too. And the bootstrap timeout does not arm for a loopback bind (_is_exposed_bind is false for 127.0.0.1), so a proxied loopback instance is never torn down either. This change still strictly tightens the direct-LAN, Cloudflare-tunnel, and DNS-rebinding paths.

The robust fix is to stop trusting network position and require an unforgeable local proof: a single-use setup token that Studio prints only to the local terminal and gates injection on (the same shape as Jupyter's ?token=). A proxied remote user never sees the terminal, so cannot obtain it. That reworks the first-login startup emission, so it is better as a focused follow-up than folded into this loopback-scoping change.

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.

Confirmed the nginx default (Host becomes $proxy_host and no X-Forwarded-For is added), so the observation is accurate. It is out of scope for this change for two reasons. First, it is indistinguishable at the HTTP layer from a genuine local client: a loopback peer with a loopback Host and no forwarding headers is exactly what a local browser sends, so no request-based or bind-based check can block it without also breaking the legitimate local autofill this gate exists to provide. Second, a correctly configured reverse proxy forwards the client via X-Forwarded-For (or Forwarded / X-Real-IP), all of which _PROXIED_CLIENT_HEADERS already treats as remote; the exposure only remains for a proxy that forwards no client-identifying header at all, which is a misconfiguration that also loses the real client IP in logs and breaks virtual-host routing. It is also pre-existing (the same-origin-only gate before this PR served the seed to this same setup) and limited to the pre-first-password-change window. Fully removing even the misconfigured-proxy case would mean not embedding the seed in-page at all (it is always printed to the terminal and saved to .bootstrap_password), which is a separate design decision rather than part of scoping the auto-fill to loopback.

@danielhanchen
danielhanchen merged commit d809433 into unslothai:main Jul 15, 2026
46 checks passed
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: d8094335b7

ℹ️ 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 added a commit that referenced this pull request Aug 27, 2026
Follow-up to #7131. That change scoped the in-page auto-fill of the seeded admin
bootstrap password to loopback clients, but "is this a local client" cannot be
decided from a request. A default nginx `proxy_pass http://127.0.0.1:PORT;`
forwards `Host: 127.0.0.1:PORT` from a loopback socket with no forwarding headers,
which is byte-for-byte identical to what a genuine local browser sends. No
request-based or bind-based check separates the two, so the seed could still reach
a remote user during the pre-first-password-change window.

This removes the injection entirely rather than tightening a gate that cannot
hold. The seed is saved to .bootstrap_password (0600, deleted on rotation) and the
startup banner points at it, so the operator reads it from there on first login.
That closes the LAN, tunnel and reverse-proxy exposure classes together and
deletes the Origin/loopback/Host detection code.

- studio/backend/main.py: _build_index_response serves the SPA shell without ever
  embedding window.__UNSLOTH_BOOTSTRAP__. Removes _inject_bootstrap,
  _should_inject_bootstrap, _is_local_bootstrap_request, _host_header_is_loopback,
  _is_loopback_ip, _PROXIED_CLIENT_HEADERS, _is_same_origin_request,
  _canonical_origin, _DEFAULT_PORTS and the now-unused ipaddress import.
  _CSP_SCRIPT_NONCE_HEADER stays: the vendored Swagger/ReDoc pages still use it.
- Replaces the origin/loopback suites with test_index_no_bootstrap_injection.py,
  which asserts the served page never contains the seed over the root and
  SPA-fallback routes, with a pending password change and a seed present.

The four first-boot Playwright drivers now go through a shared helper,
_playwright_robust.fill_bootstrap_current_password, which asserts
window.__UNSLOTH_BOOTSTRAP__ is undefined and that #current-password is visible and
empty BEFORE it types. The previous `if cur_pw.count():` guard passed whether or
not the injection was removed, so the browser smoke could not have detected the
injection coming back. The IME and model-config workflow steps now read, mask and
export the bootstrap password, which they previously did not need.

Behavioural notes: the index response no longer sets Vary: Origin (nothing depends
on it; Cache-Control: no-store still applies). auth-form.tsx keeps its
window.__UNSLOTH_BOOTSTRAP__ branch, which is now permanently inert; it is left in
place so a mixed-version frontend cannot lose the Current password field, and
test_auth_form_input_count.py still locks its derivation. Its docstring no longer
points at the deleted _inject_bootstrap.

tests/studio/test_model_config_bootstrap_password.py is a source-contract guard,
and no CI job currently runs tests/studio as a directory, so it and the inverted
test_composer_rtl_bidi_attribute.py guards run locally only.
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.

2 participants