Files
t3code/apps/server/src/netHost.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

66 lines
2.0 KiB
TypeScript

/**
* netHost - Pure host/hostname predicates and parsing.
*
* Dependency-free so it can be imported from auth modules, startup code, and
* HTTP layers without creating module cycles.
*
* @module netHost
*/
export const isLoopbackHost = (host: string | undefined): boolean => {
if (!host || host.length === 0) {
return true;
}
return (
host === "localhost" ||
host === "127.0.0.1" ||
host === "::1" ||
host === "[::1]" ||
host.startsWith("127.")
);
};
export const isWildcardHost = (host: string | undefined): boolean =>
host === "0.0.0.0" || host === "::" || host === "[::]";
export const formatHostForUrl = (host: string): string =>
host.includes(":") && !host.startsWith("[") ? `[${host}]` : host;
export const normalizeHost = (host: string): string =>
host.startsWith("[") && host.endsWith("]") ? host.slice(1, -1) : host;
/**
* Extracts the hostname from a raw `Host` request header, handling ports,
* bracketed IPv6 literals, and surrounding whitespace. Returns null for
* missing or malformed values.
*/
export const parseHostHeaderHostname = (hostHeader: string | undefined): string | null => {
const trimmed = hostHeader?.trim().toLowerCase();
if (!trimmed || trimmed.includes("/")) {
return null;
}
// URL handles `host[:port]` and `[v6]:port` uniformly and rejects garbage.
try {
const parsed = new URL(`http://${trimmed}`);
return parsed.hostname.length > 0 ? parsed.hostname : null;
} catch {
return null;
}
};
/**
* True when a raw `Host` request header names a loopback host. Malformed or
* missing headers are NOT loopback: this is used as an auth guard, so parse
* failures must fail closed (unlike `isLoopbackHost`, whose undefined input
* means "no bind host configured" and defaults open).
*/
export const isLoopbackHostHeader = (hostHeader: string | undefined): boolean => {
const hostname = parseHostHeaderHostname(hostHeader);
if (hostname === null) {
return false;
}
return isLoopbackHost(normalizeHost(hostname));
};