feat(web): enforce localhost-only binding; expand loopback origin allow-list (#1796)
Closes #1795. Enforce localhost-only binding in code (the "never 0.0.0.0" rule was only a spec note): refuse an all-interfaces HOST — across every spelling the OS binds as the wildcard (inet_aton decimal/octal/hex, IPv6, IPv4-mapped, zone-scoped, IDNA/Unicode) — unless DANGEROUSLY_BIND_ALL_INTERFACES=true (Docker's sanctioned opt-in). Fixes the reported stdio-connect 403 by expanding the default loopback origin allow-list to all three interchangeable forms. Hardening surfaced across an extended review: closes an IDNA/fullwidth wildcard bypass, a blank-ALLOWED_ORIGINS fail-open, and the CLI/TUI OAuth-callback wildcard bind (loopback-only now, RFC 8252); canonicalizes the origin allow-list the way a browser does; derives frame-ancestors from the real embedder list (IPv6-literal exclusion verified in headless Chromium); strictPort on all three bind points; and preserves the CLI exit-code map + JSON envelope through the mcp-inspector --cli launcher path. Shared host normalization lives in core/node/hostUrl.ts. Docs + tests + smokes throughout. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
This commit is contained in:
parent
33e88eac9f
commit
cea22d86b8
1 changed files with 1610 additions and 86 deletions
|
|
@ -0,0 +1,91 @@
|
|||
import { describe, it, expect } from "vitest";
|
||||
import {
|
||||
BIND_ALL_INTERFACES_ENV,
|
||||
resolveBindHostname,
|
||||
} from "../../../../server/resolve-bind-host.js";
|
||||
|
||||
describe("resolveBindHostname", () => {
|
||||
it("defaults to localhost when HOST is unset", () => {
|
||||
expect(resolveBindHostname({})).toBe("localhost");
|
||||
});
|
||||
|
||||
it("returns a loopback HOST unchanged", () => {
|
||||
expect(resolveBindHostname({ HOST: "127.0.0.1" })).toBe("127.0.0.1");
|
||||
});
|
||||
|
||||
it("trims the returned host so detection and the bind value agree", () => {
|
||||
expect(resolveBindHostname({ HOST: " 127.0.0.1 " })).toBe("127.0.0.1");
|
||||
});
|
||||
|
||||
it("returns a bracketed IPv6 HOST bare so listen() can bind it", () => {
|
||||
expect(resolveBindHostname({ HOST: "[::1]" })).toBe("::1");
|
||||
});
|
||||
|
||||
it("keeps the zone index on a link-local HOST for listen()", () => {
|
||||
// The guard must not throw on a zone-scoped host, and must return it with
|
||||
// the zone intact (listen() needs the zone to pick the interface).
|
||||
expect(resolveBindHostname({ HOST: "fe80::1%eth0" })).toBe("fe80::1%eth0");
|
||||
});
|
||||
|
||||
it.each([
|
||||
"0.0.0.0",
|
||||
"::",
|
||||
"",
|
||||
"0",
|
||||
"0x0.0.0.0",
|
||||
"::ffff:0.0.0.0",
|
||||
" 0 ",
|
||||
"0", // fullwidth "0" — the resolver binds it as 0.0.0.0, so the guard must refuse it
|
||||
])("refuses the all-interfaces host %j without the opt-in", (host) => {
|
||||
expect(() => resolveBindHostname({ HOST: host })).toThrow(
|
||||
new RegExp(BIND_ALL_INTERFACES_ENV),
|
||||
);
|
||||
});
|
||||
|
||||
it("names the resolved wildcard address when the spelling differs (fullwidth)", () => {
|
||||
// `HOST="0"` renders like `0`, so the message shows it resolves to 0.0.0.0.
|
||||
expect(() => resolveBindHostname({ HOST: "0" })).toThrow(
|
||||
/resolves to 0\.0\.0\.0/,
|
||||
);
|
||||
});
|
||||
|
||||
it("does not add a resolved-address hint for an already-canonical bracketed HOST", () => {
|
||||
// `HOST="[::]"` is accepted-then-refused as the wildcard; the message must
|
||||
// not read "(resolves to [::])" — same value, just bracketed. Capture the
|
||||
// message outside try/catch so a stopped-throwing regression reports clearly.
|
||||
let message = "";
|
||||
expect(() => {
|
||||
try {
|
||||
resolveBindHostname({ HOST: "[::]" });
|
||||
} catch (err) {
|
||||
message = (err as Error).message;
|
||||
throw err;
|
||||
}
|
||||
}).toThrow(BIND_ALL_INTERFACES_ENV);
|
||||
expect(message).not.toContain("resolves to");
|
||||
});
|
||||
|
||||
it.each(["true", "TRUE", "1", " true "])(
|
||||
"allows 0.0.0.0 when the opt-in is %j",
|
||||
(flag) => {
|
||||
expect(
|
||||
resolveBindHostname({
|
||||
HOST: "0.0.0.0",
|
||||
[BIND_ALL_INTERFACES_ENV]: flag,
|
||||
}),
|
||||
).toBe("0.0.0.0");
|
||||
},
|
||||
);
|
||||
|
||||
it.each(["false", "0", "", "yes", "no"])(
|
||||
"still refuses 0.0.0.0 when the opt-in reads %j",
|
||||
(flag) => {
|
||||
expect(() =>
|
||||
resolveBindHostname({
|
||||
HOST: "0.0.0.0",
|
||||
[BIND_ALL_INTERFACES_ENV]: flag,
|
||||
}),
|
||||
).toThrow();
|
||||
},
|
||||
);
|
||||
});
|
||||
Loading…
Reference in a new issue