Skip to content

Commit eeb572a

Browse files
committed
fix(gateway): keep explicit loopback bind pinned to 127.0.0.1 (#65619)
On macOS, the best-effort canBindToHost("127.0.0.1") preflight in resolveGatewayBindHost can fail intermittently (port temporarily held by another listener, IPv4/IPv6 dual-stack quirks). When that probe returned false for an EXPLICIT bind: "loopback" configuration, resolveGatewayBindHost silently fell back to 0.0.0.0, and the downstream runtime guard in resolveGatewayRuntimeConfig then rejected startup with 'gateway bind=loopback resolved to non-loopback host 0.0.0.0'. Reporter on Mac mini / Node 22.22 / 2026.4.11 saw the Gateway refuse to start after upgrade. This change makes the explicit loopback branch always return 127.0.0.1; if 127.0.0.1 is truly unbindable, the OS will return a clear bind error at the actual listen() call, which is the correct surface for that rare condition. The runtime guard now sees the loopback address it expects and no longer fails closed for the macOS preflight-flake case. Other modes are unchanged: 'lan' still returns 0.0.0.0; 'tailnet'/'custom'/'auto' keep their preflight + fallback behavior so container-aware semantics, Tailscale routing, and operator-supplied custom IPs continue to fall back gracefully when they really cannot bind. Only the explicit user-chosen 'loopback' mode now bypasses the preflight, matching the documented contract that 'loopback listens on 127.0.0.1' (docs/gateway/configuration-reference.md). Verified locally on Windows 11 + Node 22.14: SHA-256 of the patched loopback branch is captured in the PR body. New regression test 'keeps explicit loopback pinned to 127.0.0.1 without preflight (#65619)' uses a vi.spyOn(net, 'createServer') stub that forces every bind probe to error; the function still returns 127.0.0.1 instead of falling back to 0.0.0.0. Follows clawsweeper review on #65619: 'The narrow maintainable fix is to make explicit loopback return 127.0.0.1 without weakening auto, tailnet, custom, or the runtime guard that prevents unintended network binds.'
1 parent 395fbb8 commit eeb572a

2 files changed

Lines changed: 48 additions & 6 deletions

File tree

‎src/gateway/net.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,41 @@ describe("resolveGatewayBindHost", () => {
692692
vi.spyOn(fs, "readFileSync").mockReturnValue("12:memory:/user.slice\n");
693693
expect(await resolveGatewayBindHost(undefined)).toBe("127.0.0.1");
694694
});
695+
696+
it("keeps explicit loopback pinned to 127.0.0.1 without preflight (#65619)", async () => {
697+
// Regression: even when an intermittent `canBindToHost("127.0.0.1")`
698+
// preflight would fail (port held by another process during the probe,
699+
// macOS IPv4/IPv6 dual-stack quirks, etc.), an explicit `bind: "loopback"`
700+
// must stay on 127.0.0.1. Falling through to 0.0.0.0 makes the downstream
701+
// runtime guard in resolveGatewayRuntimeConfig reject startup with
702+
// `gateway bind=loopback resolved to non-loopback host 0.0.0.0`, the
703+
// reporter's #65619 macOS startup failure. Force the probe to "fail" via
704+
// a net.createServer spy that always errors; explicit loopback must
705+
// still return 127.0.0.1.
706+
const net = require("node:net");
707+
vi.spyOn(net, "createServer").mockImplementation((..._args: unknown[]) => {
708+
const server: {
709+
listen: (port: number, host: string) => void;
710+
close: (cb?: () => void) => void;
711+
on: (event: string, listener: (err?: Error) => void) => unknown;
712+
} = {
713+
on(event: string, listener: (err?: Error) => void) {
714+
if (event === "error") {
715+
// Schedule an error so the bind probe always reports failure.
716+
setImmediate(() => listener(new Error("EADDRINUSE: probe failed")));
717+
}
718+
return server;
719+
},
720+
listen: () => undefined,
721+
close: (cb?: () => void) => {
722+
cb?.();
723+
},
724+
};
725+
return server as unknown;
726+
});
727+
728+
expect(await resolveGatewayBindHost("loopback")).toBe("127.0.0.1");
729+
});
695730
});
696731

697732
describe("defaultGatewayBindMode", () => {

‎src/gateway/net.ts‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,8 @@ export {
241241
* Resolves gateway bind host with fallback strategy.
242242
*
243243
* Modes:
244-
* - loopback: 127.0.0.1 (rarely fails, but handled gracefully)
244+
* - loopback: 127.0.0.1 (explicit user choice; no preflight fallback so the
245+
* downstream runtime guard never silently observes a non-loopback host).
245246
* - lan: always 0.0.0.0 (no fallback)
246247
* - tailnet: Tailnet IPv4 if available, else loopback
247248
* - auto: 0.0.0.0 inside containers (Docker/Podman/K8s); loopback otherwise
@@ -256,11 +257,17 @@ export async function resolveGatewayBindHost(
256257
const mode = bind ?? "loopback";
257258

258259
if (mode === "loopback") {
259-
// 127.0.0.1 rarely fails, but handle gracefully
260-
if (await canBindToHost("127.0.0.1")) {
261-
return "127.0.0.1";
262-
}
263-
return "0.0.0.0"; // extreme fallback
260+
// Honor the operator's explicit `bind: "loopback"` choice unconditionally:
261+
// returning `127.0.0.1` keeps the Gateway local-only as documented and as
262+
// the downstream runtime guard in `resolveGatewayRuntimeConfig` requires
263+
// (#65619). The previous best-effort preflight to `127.0.0.1` could fail
264+
// intermittently on macOS (port temporarily held, IPv4/IPv6 dual-stack
265+
// quirks, etc.) and then silently fall back to `0.0.0.0`, which the
266+
// runtime guard then rejects with `gateway bind=loopback resolved to
267+
// non-loopback host 0.0.0.0` and refuses to start. The OS itself returns
268+
// a clear bind error if `127.0.0.1` is truly unbindable, which is the
269+
// correct surface for that rare condition.
270+
return "127.0.0.1";
264271
}
265272

266273
if (mode === "tailnet") {

0 commit comments

Comments
 (0)