Skip to content

Bind --host localhost to 127.0.0.1 and refuse unparsed addresses - #1335

Merged
ddalcu merged 1 commit into
ddalcu:mainfrom
viktortnk:host-refuses-unparsed-address
Nov 29, 2026
Merged

ddalcu merged 1 commit into
ddalcu:mainfrom
viktortnk:host-refuses-unparsed-address

Conversation

@viktortnk

Copy link
Copy Markdown
Contributor

--host localhost binds 0.0.0.0 today, and so do --host ::1, an empty host and any hostname. parseIp4 defaults missing or unparsed octets to 0, and 0.0.0.0 is the wildcard.

So a user who asks for a local-only server gets one that every device on the network can reach. With no --api-key and no --lan-share, a LAN peer reaches the router, including /v1/load-model and the provider proxies. The open-bind warning does not print, because the caller passed --host. The app passes a non-empty Settings host field straight to --host, so localhost typed there does the same.

What changes

  • localhost, in any case, binds 127.0.0.1.
  • Any other value must be an IPv4 address that std.Io.net.Ip4Address.parse accepts. main exits 1 at the flag and prints, for example, --host takes an IPv4 address or localhost, got '::1'. serve() returns error.InvalidHost, which covers ios_lib.
  • parseIp4 refuses ::1 instead of mapping it to 127.0.0.1. The server binds IPv4 only. The app formats ::1 as http://[::1]:<port>, which cannot reach an IPv4-only listener.
  • --help, docs/cli.md and docs/zh-CN/cli.md name the accepted forms.

Every --host in the repo's tests and scripts is an IPv4 literal, and the app turns an empty field into 0.0.0.0. Existing callers keep working. Only an empty --host on the command line, a malformed IPv4, a hostname or an IPv6 address now fails.

Testing

On a Mac Studio M5 Ultra, 96 GB, macOS 27.2.1:

  • The new parseIp4 test failed before the fix, with localhost read as 0.0.0.0. It passes after.
  • zig build test: 5655 of 6074 passed, 419 skipped, 0 failed. An earlier full run had one failure in deepseek_v41 served: DSpark rounds (greedy, then seeded sampling). That test passes when run alone, both on main and on this branch.
  • python3 scripts/lint.py --strict exits 0.
  • I served a ReleaseFast build with an empty model folder and curled /health from loopback and from the machine's LAN address:
--host main this branch
localhost listens on *:port, the LAN gets 200 listens on 127.0.0.1:port, the LAN cannot connect
::1, '', my-mac.local listens on *:port, the LAN gets 200 exits 1 and names the value

On this branch, --host 127.0.0.1 still listens on 127.0.0.1, and the default still listens on every interface.

… parse

parseIp4 defaulted missing or unparsed octets to 0, so --host localhost, --host ::1, an empty host
and any hostname bound 0.0.0.0. With no --api-key and no --lan-share, every device on the network
then reached the router, including /v1/load-model and the provider proxies. The open-bind warning
did not print, because the host was explicit.

localhost now binds 127.0.0.1. Any other value must be an IPv4 address that
std.Io.net.Ip4Address.parse accepts. main logs the rejected host at the flag and exits 1. serve()
returns error.InvalidHost, which covers the iOS entry. parseIp4 refuses ::1 instead of mapping it
to 127.0.0.1, because the server binds IPv4 only.

This commit also moves serve()'s doc comment back to serve() from listen.zig.

ddalcu commented Nov 26, 2026

Copy link
Copy Markdown
Owner

🤖 MLXServe-PRBot approved — reviewed at head 822d8f44, no issues found.

The bug is real, and it is a security bug: parseIp4 read any octet it could not parse as 0. So --host localhost, ::1, "" or a hostname all bound the wildcard, while the user believed the server was local-only. The fix is the plain one:

  • localhost maps to loopback;
  • everything else goes through std.Io.net.Ip4Address.parse;
  • a refusal is null, never 0.0.0.0.

Checked:

  • Both callers handle the optional: main exits 1 at the flag and names the value, and serve() returns error.InvalidHost (the iOS entry path).
  • server.zig:152 re-exports the same function, so nothing is left on the old signature.
  • The test pins loopback, the explicit wildcard, and the three refusals.
  • serve's doc comment, which had drifted onto parseIp4, is back on serve.

Note, not blocking: a host the app saved before this change (a .local name, say) now makes the server exit at launch instead of quietly binding everything. That is the right direction, but the app should surface the error line rather than a bare "server failed to start".

No conflicts with main. CI has not run on this fork PR yet (workflows await approval); merge only on green.


Generated by Claude Code

@ddalcu ddalcu added bug Something isn't working documentation Improvements or additions to documentation engine security Approved labels Nov 26, 2026 — with Claude
@ddalcu
ddalcu merged commit 4e83aa5 into ddalcu:main Nov 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Approved bug Something isn't working documentation Improvements or additions to documentation engine security

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants