Repository navigation
fix(studio): stop handing a wildcard bind's public IP to LAN peers - #9912
Conversation
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]>
|
Pushed two commits to this branch.
Checked against the merge base with
|
|
Codex Review: Didn't find any major issues. What shall we delve into next? 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". |
|
CI status for the record: Lint CI passes. Backend CI is red, but its failing set at Codex reviewed |
|
Codex Review: Didn't find any major issues. Keep them coming! 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". |
|
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. |
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 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".
| share_host = _network_share_host_for_bind(host) | ||
| if is_wildcard_host(share_host): | ||
| return None | ||
| return f"http://{_url_host(share_host)}:{port}" |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
|
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". |
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]>
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. |
There was a problem hiding this comment.
💡 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".
| if addresses: | ||
| return addresses[0] |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
|
@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". |
|
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 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. |
Summary
On
-H 0.0.0.0(or::), Unsloth resolved the address to show for "anotherdevice on your network" via
_resolve_external_ip(), which asksifconfig.me(and the GCE metadata server) before falling back to a real LANaddress. That public WAN IP then got:
startup banner, and
app.state.server_url, the frontend's "direct base for theAPI 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 resolverfor a wildcard bind, and it always went through
_resolve_external_ip()(GCE metadata ->
ifconfig.me-> LAN-IP-via-UDP-trick, in that order). Thatfunction 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
_resolve_lan_ip()(no third-party network call)._network_share_host_for_bind(), which calls_resolve_lan_ip()directly for a wildcard bind instead of going through the public-IP chain.
app.state.server_urlassignments to the new LAN-only resolver._display_host_for_bind()/_resolve_external_ip()are unchanged inbehavior and stay wired to
_verify_global_reachabilityand theCloudflare line, where the public IP is the correct answer.
print_studio_access_banner()gained an optionalnetwork_hostparameter(defaults to
display_host, so any other caller keeps its old behaviorunchanged).
Test plan
Added
studio/backend/tests/test_lan_share_host_resolution.py: mocksurllib.request.urlopen(public IP) andsocket.socket(LAN IP via the UDPtrick) 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 wildcardbind, and the bind address unchanged for an explicit one.
public IP.
display_host(nonetwork_host) keeps the oldbanner text, for backward compatibility.
Also confirmed the new test module fails to even import against the
pre-fix
run.py(_network_share_host_for_binddoesn't exist yet), so itdoes pin the regression.
Lint
(The repo's canonical formatter,
scripts/run_ruff_format.py, requiresPython 3.12+ for
scripts/enforce_kwargs_spacing.py's use oftokenize.FSTRING_START; only Python 3.11 was available in my sandbox, so Ihand-matched the surrounding
key = valuekwarg-spacing style instead ofrunning that script.
ruff check-- the blocking lint gate inlint-ci.yml-- passes;ruff format --checkreports the samekwarg-spacing "drift" this file already had before my change, which
lint-ci.ymlmarks 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