Files
t3code/apps/server/src/netHost.test.ts
T
Theo BrowneandClaude Fable 5 03a9e682f1 feat: silent pairing for local web-dev servers
Local web dev previously required scraping a one-time, 5-minute
/pair#token URL from dev-runner stdout for every fresh browser context,
which made parallel-instance work and agent browser verification
painful. The web app now authenticates automatically against eligible
dev servers via a guarded dev-only endpoint.

Server: POST /api/auth/dev-pairing-token mints a one-time administrative
credential (subject dev-auto-bootstrap, hidden from pairing-link
listings) only when the server is web-mode, loopback-bound
(loopback-browser policy), and configured with a loopback dev URL.
Requests must carry an Origin exactly matching the dev origin (survives
the Vite proxy, unlike Host) and a loopback Host header (DNS-rebinding
defense). Ineligible configs answer an explicit 404 — the handler is
always mounted so the SPA/redirect wildcard can't shadow it.

Startup: eligible dev servers now open the plain dev URL instead of
minting a startup pairing credential the silent path would leave
unconsumed in the URL and scrollback.

Web: the auth-gate bootstrap silently tries the endpoint when
unauthenticated in dev (never in hosted/desktop contexts, never when an
explicit #token is present) and falls back to the pairing screen on any
failure. import.meta.env.DEV keeps the request out of prod bundles.

Extracted host parsing into netHost.ts (auth/http.ts importing the old
helper from http.ts would have been a module cycle; the old helper also
mishandled Host headers with ports).

Docs: AGENTS.md browser-verification section, dev-instance auth notes in
scripts.md (plus stale T3CODE_STATE_DIR fix), and a security spec in
environment-auth.md.

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-07-13 18:12:16 -07:00

60 lines
2.6 KiB
TypeScript

import { assert, describe, it } from "@effect/vitest";
import { isLoopbackHost, isLoopbackHostHeader, parseHostHeaderHostname } from "./netHost.ts";
describe("parseHostHeaderHostname", () => {
it("extracts hostnames from host headers with and without ports", () => {
assert.equal(parseHostHeaderHostname("localhost"), "localhost");
assert.equal(parseHostHeaderHostname("localhost:13773"), "localhost");
assert.equal(parseHostHeaderHostname("127.0.0.1:13773"), "127.0.0.1");
assert.equal(parseHostHeaderHostname("[::1]:13773"), "[::1]");
assert.equal(parseHostHeaderHostname("example.com:8080"), "example.com");
assert.equal(parseHostHeaderHostname(" LOCALHOST:13773 "), "localhost");
});
it("returns null for missing or malformed host headers", () => {
assert.equal(parseHostHeaderHostname(undefined), null);
assert.equal(parseHostHeaderHostname(""), null);
assert.equal(parseHostHeaderHostname(" "), null);
assert.equal(parseHostHeaderHostname("exa mple.com"), null);
assert.equal(parseHostHeaderHostname("http://example.com"), null);
});
});
describe("isLoopbackHostHeader", () => {
it("accepts loopback host headers, with ports and brackets", () => {
assert.isTrue(isLoopbackHostHeader("localhost"));
assert.isTrue(isLoopbackHostHeader("localhost:13773"));
assert.isTrue(isLoopbackHostHeader("127.0.0.1:13773"));
assert.isTrue(isLoopbackHostHeader("127.5.5.5"));
assert.isTrue(isLoopbackHostHeader("127.5.5.5:8080"));
assert.isTrue(isLoopbackHostHeader("[::1]:13773"));
assert.isTrue(isLoopbackHostHeader("[::1]"));
});
it("rejects non-loopback and malformed host headers (fails closed)", () => {
assert.isFalse(isLoopbackHostHeader("evil.example"));
assert.isFalse(isLoopbackHostHeader("evil.example:13773"));
assert.isFalse(isLoopbackHostHeader("localhost.evil.example"));
assert.isFalse(isLoopbackHostHeader("10.0.0.5:13773"));
assert.isFalse(isLoopbackHostHeader("[::2]:13773"));
assert.isFalse(isLoopbackHostHeader(undefined));
assert.isFalse(isLoopbackHostHeader(""));
assert.isFalse(isLoopbackHostHeader("exa mple.com"));
});
});
describe("isLoopbackHost", () => {
it("keeps the config-host semantics: undefined means loopback", () => {
assert.isTrue(isLoopbackHost(undefined));
assert.isTrue(isLoopbackHost(""));
assert.isTrue(isLoopbackHost("localhost"));
assert.isTrue(isLoopbackHost("127.0.0.1"));
assert.isTrue(isLoopbackHost("127.99.0.1"));
assert.isTrue(isLoopbackHost("::1"));
assert.isTrue(isLoopbackHost("[::1]"));
assert.isFalse(isLoopbackHost("0.0.0.0"));
assert.isFalse(isLoopbackHost("192.168.1.10"));
});
});