Skip to content

fix(studio): stop handing a wildcard bind's public IP to LAN peers - #9912

Merged
oobabooga merged 12 commits into
unslothai:mainfrom
amanyagami:fix/8868-lan-share-ip
Sep 4, 2026
Merged

oobabooga merged 12 commits into
unslothai:mainfrom
amanyagami:fix/8868-lan-share-ip

Conversation

@amanyagami

Copy link
Copy Markdown
Contributor

Summary

On -H 0.0.0.0 (or ::), Unsloth resolved the address to show for "another
device on your network" via _resolve_external_ip(), which asks
ifconfig.me (and the GCE metadata server) before falling back to a real LAN
address. That public WAN IP then got:

  • printed under "From another device on your network / to share" in the
    startup banner, and
  • used to build app.state.server_url, the frontend's "direct base for the
    API panel" -- whose own comment already says it should be "the LAN IP".

A device on the same LAN usually cannot even route to the public IP without
router-side port-forwarding, so the banner both mislabeled the address and
defeated the sharing it claimed to enable. Filed as a security issue because
it also hands an outside address to whoever the operator is sharing the
banner text with.

Closes #8868.

Root cause

studio/backend/run.py's _display_host_for_bind() was the only resolver
for a wildcard bind, and it always went through _resolve_external_ip()
(GCE metadata -> ifconfig.me -> LAN-IP-via-UDP-trick, in that order). That
function is right for the reachability probe and the Cloudflare messaging
(purpose: "is this reachable from the internet"), but wrong for "what
address can a device on my own network open" -- a different question with a
different correct answer.

Fix

  • Extracted the existing UDP-socket-trick fallback into its own
    _resolve_lan_ip() (no third-party network call).
  • Added _network_share_host_for_bind(), which calls _resolve_lan_ip()
    directly for a wildcard bind instead of going through the public-IP chain.
  • Wired the banner's "another device on your network" line and both
    app.state.server_url assignments to the new LAN-only resolver.
  • _display_host_for_bind() / _resolve_external_ip() are unchanged in
    behavior and stay wired to _verify_global_reachability and the
    Cloudflare line, where the public IP is the correct answer.

print_studio_access_banner() gained an optional network_host parameter
(defaults to display_host, so any other caller keeps its old behavior
unchanged).

Test plan

Added studio/backend/tests/test_lan_share_host_resolution.py: mocks
urllib.request.urlopen (public IP) and socket.socket (LAN IP via the UDP
trick) separately and asserts:

  • _resolve_lan_ip() never touches the network.
  • _resolve_external_ip() still prefers the public service (unchanged).
  • _network_share_host_for_bind() returns the LAN address for a wildcard
    bind, and the bind address unchanged for an explicit one.
  • The printed banner's network line contains the LAN URL and never the
    public IP.
  • A caller that only passes display_host (no network_host) keeps the old
    banner text, for backward compatibility.
$ python -m pytest tests/test_lan_share_host_resolution.py tests/test_lan_access_settings.py \
    tests/test_secure_tunnel_gate.py tests/test_tee_progress_frames.py \
    tests/test_public_check_optout.py tests/test_startup_banner_loopback.py \
    tests/test_host_defaults.py -q
180 passed in 5.91s

Also confirmed the new test module fails to even import against the
pre-fix run.py (_network_share_host_for_bind doesn't exist yet), so it
does pin the regression.

Lint

$ ruff check studio/backend/run.py studio/backend/startup_banner.py studio/backend/tests/test_lan_share_host_resolution.py
All checks passed!

(The repo's canonical formatter, scripts/run_ruff_format.py, requires
Python 3.12+ for scripts/enforce_kwargs_spacing.py's use of
tokenize.FSTRING_START; only Python 3.11 was available in my sandbox, so I
hand-matched the surrounding key = value kwarg-spacing style instead of
running that script. ruff check -- the blocking lint gate in
lint-ci.yml -- passes; ruff format --check reports the same
kwarg-spacing "drift" this file already had before my change, which
lint-ci.yml marks informational/non-blocking for exactly this reason.)

Disclosure

Investigated and written with AI assistance (Claude Code), with the actual
diff, tests, and lint/test runs verified by me in this sandbox as shown
above.

🤖 Generated with Claude Code

On -H 0.0.0.0 (or ::), _display_host_for_bind() resolved the machine's
address via _resolve_external_ip(), which tries ifconfig.me (and the GCE
metadata server) before falling back to a real LAN address. That public
WAN IP was then printed under "From another device on your network / to
share" and used to build app.state.server_url's "direct base for the API
panel" -- both places that need an address a LAN peer can actually open,
not the internet-facing one the reachability probe and Cloudflare line
legitimately want.

A LAN device usually cannot even route to the public IP without
router-side port-forwarding, so the banner both mislabeled the address
and hindered the sharing it claimed to enable -- and, per the "security"
label on unslothai#8868, told an outside address to an untrusted party.

Split the two purposes explicitly:
- _resolve_lan_ip(): the existing UDP-socket-trick fallback, extracted so
  it can be called directly with no third-party network request.
- _network_share_host_for_bind(): LAN-only, for the banner's network line
  and app.state.server_url.
- _display_host_for_bind() / _resolve_external_ip() are unchanged in
  behavior and stay wired to the reachability probe and Cloudflare
  messaging, where the public IP is the right answer.

Fixes unslothai#8868

🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Sonnet 5 <[email protected]>
chatgpt-codex-connector[bot]

This comment was marked as resolved.

@oobabooga

Copy link
Copy Markdown
Member

Pushed two commits to this branch.

b73847e covers the two review comments above. _resolve_external_ip keeps its own route lookup as step 3, so its body is identical to main again and _resolve_lan_ip is left as the sharing-only resolver, and both app.state.server_url assignments go through a new _direct_server_url, which returns None rather than publishing http://0.0.0.0:<port>.

4bd526c covers the symptom reported in #8868. The line the issue pastes under "Actual behavior", Unsloth Studio running on http://104.32.48.18:8888, comes from _install_uvicorn_startup_log_rewrite, which this PR still fed _display_host_for_bind, so a wildcard bind kept naming the WAN IP there. It now resolves its own LAN answer and takes only the bind host as an argument, so no caller can hand it the internet-facing address; when no LAN address resolves it leaves uvicorn's raw 0.0.0.0, which is the issue's stated expected behavior. _display_host_for_bind still feeds _verify_global_reachability and the Cloudflare line, where the public address is the right one.

Checked against the merge base with detect_lan_addresses and the public lookups stubbed: _display_host_for_bind and _resolve_external_ip now return the same value as main in every case tried, including WSL NAT with no public answer, a cloud VM, and a loopback-only host.

studio/backend/tests/test_lan_share_host_resolution.py gained five cases. 352 tests pass across it plus test_lan_access_settings, test_startup_banner_loopback, test_cloudflare_tunnel, test_keyless_api_access, test_keyless_api_access_adversarial, test_public_check_optout, test_host_defaults, test_secure_tunnel_gate and test_tee_progress_frames. ruff check is clean on the changed files.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: 4bd526c693

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

@oobabooga

Copy link
Copy Markdown
Member

fc0f3461 is comments only, verified with an AST diff against 79f55b42: the "not _display_host_for_bind, #8868" rationale was written out in full in five places across run.py and startup_banner.py, so it now lives once in _network_share_host_for_bind, which is the resolver the others call, and the rest state only what is local to them. All 35 removed lines came from this PR; no pre-existing comment was touched.

CI status for the record: Lint CI passes. Backend CI is red, but its failing set at 4bd526c6 is identical to the same workflow on ce99e954 (this branch's merge base) at 20 tests, so it is pre-existing on main rather than anything here. The other fork workflows are still awaiting approval; I approved only Backend CI and Lint CI, which are the ones this change can affect.

Codex reviewed 4bd526c6 clean.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: fc0f34618a

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

Copy link
Copy Markdown
Member

Confirmed this hits studio/backend/run.py where a wildcard bind still resolves through _resolve_external_ip, so the banner and app.state.server_url both name the WAN IP instead of the LAN one, and the follow-up covering _install_uvicorn_startup_log_rewrite matches the line reported in #8868. Will get this reviewed.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@oobabooga

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@oobabooga

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: 7b4695c92e

ℹ️ 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/run.py
Comment on lines +620 to +623
share_host = _network_share_host_for_bind(host)
if is_wildcard_host(share_host):
return None
return f"http://{_url_host(share_host)}:{port}"

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 Preserve the request origin for public wildcard clients

When a wildcard-bound Studio is opened through a plain public NAT or port-forward—for example, a cloud VM whose NIC is 10.x but whose public address reaches the server—this publishes the private NIC address as a non-null server_url. The frontend then prefers it over the browser's working origin in usage-examples.tsx:593-595 and history-card-grid.tsx:617-624, producing unreachable API examples and copied preview links for public clients. Keep server_url unset for wildcard binds so these clients use their request origin, while resolving the LAN address separately for the banner.

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.

Not taking this one. In this codebase server_url is defined as one LAN-reachable address to hand out, not as the base a given client should call: studio/backend/utils/lan_access_settings.py:235-237 says "run.py gives that direct base one LAN-reachable address", and the assignment site on main already carried the comment "Direct (non-tunnel) base for the API panel; resolve wildcard binds to the LAN IP". This PR is what makes the implementation match that, so unsetting the field on every wildcard bind changes the contract rather than fixing an implementation slip.

The cost lands in the share path you cite. history-card-grid.tsx:617-624 builds a link for another device and its last fallback is window.location.origin. With server_url unset, a user who runs -H 0.0.0.0 and views Studio at http://localhost:8888, which is the common case, would copy http://localhost:8888/p/.... That is the #8868 failure in the other direction. Under Tauri it is worse: the webview origin is tauri:// (the comment at api-monitor-page.tsx:679-680 says so), so the copied link is unusable for anyone, and the Agents tab and API monitor drop from the LAN address to getApiBase()'s http://127.0.0.1:<port>.

The trade you describe is real and deliberate. On a cloud VM reached over public NAT, main published the public IP from _resolve_external_ip and this PR publishes the private NIC address, so those API examples do change. That is the premise of #8868, and the reachability probe still prints the public URL. The part that is genuinely unsatisfying is that one field answers both "what base should this client call" and "what address do I hand out"; that precedence lives in the frontend and predates this PR, since main performed the same override with the public IP and broke LAN and SSH-tunnel clients. Splitting the field is not in scope here.

_direct_server_url still returns None when no LAN address resolves at all, which is the case where every consumer is better off with its own fallback.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 28, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 28, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 28, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 7b4695c92e

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

Resolves the conflict in unsloth_cli/commands/studio.py: upstream
independently restructured the runtime-launch-guard block (added a
wait= param, moved run_server() inside the with-block) around the
same lines this PR's fix touches. Kept upstream's structural change
and this branch's actual fix (_openable_host_for_bind instead of the
old _display_host_for_bind at the launch-line call site), just
re-indented to match upstream's new nesting.

Verified: tests/studio/test_cli_share_host_lines.py (8 passed) and
studio/backend/tests/test_lan_share_host_resolution.py (12 passed)
both green post-merge.

Signed-off-by: amanyagami <[email protected]>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 3, 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-09-03T15:46:58.145879Z afa0ca6 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: afa0ca62a5

ℹ️ 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/run.py
Comment on lines +191 to +192
if addresses:
return addresses[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 Avoid publishing container bridge addresses as LAN URLs

When Studio runs in the documented Docker setup with bridged networking and -H 0.0.0.0, detect_lan_addresses() returns the container's default-route address (typically 172.17.x.x) first, even though clients reach Studio through the host's published port and cannot route to that address. Returning it here makes _direct_server_url() non-null and causes the frontend's API examples, agent command, preview links, startup log, and sharing banner to advertise the unreachable container address instead of falling back to the working request origin/host mapping. Container-only bridge addresses need to be rejected (as WSL NAT addresses already are) before selecting a share host.

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.

Not taking this one, for three reasons.

It is not something this PR introduces. detect_lan_addresses owns "which of my own addresses is worth advertising", and on main it already hands 172.17.x.x to Settings > LAN access through utils/lan_access_settings.py:_launch_urls, and to _display_host_for_bind's IPv6 branch. This PR adds a consumer of that helper; the container exposure survives a revert.

In the documented Docker setup the comparison does not favour main. README.md:156 is docker run -p 8888:8888 on the default bridge, so inside the container _resolve_external_ip reaches ifconfig.me and main published the host's public WAN IP. A user viewing Studio at http://localhost:8888 got an address that reaches nothing, which is the #8868 symptom. Both values are wrong there and neither is the request origin. The one case where main was better is a cloud VM reached over public NAT, which is comment 3883815623 on this PR, where I took that trade deliberately.

The remedy is also not soundly implementable at this layer. The WSL precedent works by asking the platform: _wsl_networking_mode() runs wslinfo --networking-mode and declines only for NAT, keeping mirrored mode where WSL joins the host's network. No equivalent query exists for Docker, and the address alone cannot stand in for one. 172.17.0.0/16 sits inside RFC1918 172.16.0.0/12, which real networks use, so a range denylist drops legitimate LAN users; a container on --network host shares the host's network namespace and therefore its real addresses; macvlan attaches the container to the physical LAN with its own MAC. In the latter two the resolved address is exactly the one to advertise. Separating those from bridge-with-published-port needs container and interface introspection, which belongs in detect_lan_addresses with its own tests rather than in a resolver split.

@oobabooga

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

Reviewed commit: afa0ca62a5

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

@oobabooga

oobabooga commented Sep 3, 2026 •

Copy link
Copy Markdown
Member

Thanks @amanyagami. I ran this on a wildcard bind against my own LAN. The startup log line, the "From another device on your network" banner line and app.state.server_url all move from the public WAN address to the machine's LAN address, which is the symptom reported in #8868. _display_host_for_bind and _resolve_external_ip stay unchanged, so the reachability probe and the Cloudflare line still name the public address.

Where this came from: the public lookup was added in 5f15501, under a banner line that read "Worldwide Web Address". It was correct for that label. #4494 later relabelled the line as "From another device on your network / to share" without changing what fed it. This restores the match between the label and the value rather than changing a deliberate policy.

Loopback and explicit binds behave exactly as before. The desktop app is unaffected since it launches the backend on 127.0.0.1. Mac, Windows, Linux, Tauri and the update workflows are green on the head commit.

@oobabooga
oobabooga merged commit 5eef8a7 into unslothai:main Sep 4, 2026
86 of 87 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] [Security] -H 0.0.0.0 serves wrong IP address on MacOS

3 participants