From 6ddeaf0fbb51b83a85d001786b8a9a5b547f06ba Mon Sep 17 00:00:00 2001 From: Theo Browne Date: Thu, 23 Jul 2026 04:07:01 -0700 Subject: [PATCH] fix: split dev auth configuration errors --- apps/server/src/cli/config.test.ts | 45 ++++++++++++++++++++++++++++-- apps/server/src/cli/config.ts | 29 +++++++++---------- 2 files changed, 57 insertions(+), 17 deletions(-) diff --git a/apps/server/src/cli/config.test.ts b/apps/server/src/cli/config.test.ts index 74b0941d96..f94c41f56d 100644 --- a/apps/server/src/cli/config.test.ts +++ b/apps/server/src/cli/config.test.ts @@ -652,6 +652,45 @@ it.layer(NodeServices.layer)("cli config resolution", (it) => { }), ); + it.effect("requires a reusable development credential when dev auth is enabled", () => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cli-dev-auth-missing-" }); + const error = yield* resolveServerConfig( + { + mode: Option.some("web"), + port: Option.some(13_773), + host: Option.some("127.0.0.1"), + baseDir: Option.some(baseDir), + cwd: Option.none(), + devUrl: Option.none(), + noBrowser: Option.some(true), + bootstrapFd: Option.none(), + autoBootstrapProjectFromCwd: Option.none(), + logWebSocketEvents: Option.none(), + tailscaleServeEnabled: Option.none(), + tailscaleServePort: Option.none(), + }, + Option.none(), + ).pipe( + Effect.provide( + Layer.mergeAll( + ConfigProvider.layer( + ConfigProvider.fromEnv({ + env: { T3CODE_DEV_AUTH: "true" }, + }), + ), + NetService.layer, + ), + ), + Effect.flip, + ); + + expect(error._tag).toBe("MissingDevAuthKeyError"); + expect(error.message).toContain("T3CODE_DEV_AUTH_KEY is required"); + }), + ); + it.effect("rejects reusable development credentials on non-loopback hosts", () => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; @@ -689,9 +728,9 @@ it.layer(NodeServices.layer)("cli config resolution", (it) => { Effect.flip, ); - expect(error._tag).toBe("DevAuthConfigurationError"); - if (error._tag === "DevAuthConfigurationError") { - expect(error.reason).toBe("non-loopback-host"); + expect(error._tag).toBe("NonLoopbackDevAuthHostError"); + if (error._tag === "NonLoopbackDevAuthHostError") { + expect(error.host).toBe("0.0.0.0"); expect(error.message).toContain("only available on loopback hosts"); } }), diff --git a/apps/server/src/cli/config.ts b/apps/server/src/cli/config.ts index d0b74d6f5c..f2fc37255b 100644 --- a/apps/server/src/cli/config.ts +++ b/apps/server/src/cli/config.ts @@ -18,17 +18,21 @@ import { readBootstrapEnvelope } from "../bootstrap.ts"; import * as ServerConfig from "../config.ts"; import { expandHomePath, resolveBaseDir } from "../os-jank.ts"; -export class DevAuthConfigurationError extends Schema.TaggedErrorClass()( - "DevAuthConfigurationError", - { - reason: Schema.Literals(["missing-key", "non-loopback-host"]), - host: Schema.optional(Schema.String), - }, +export class MissingDevAuthKeyError extends Schema.TaggedErrorClass()( + "MissingDevAuthKeyError", + {}, ) { override get message(): string { - return this.reason === "missing-key" - ? "T3CODE_DEV_AUTH_KEY is required when T3CODE_DEV_AUTH is enabled." - : `T3CODE_DEV_AUTH is only available on loopback hosts; received ${this.host ?? "unknown"}.`; + return "T3CODE_DEV_AUTH_KEY is required when T3CODE_DEV_AUTH is enabled."; + } +} + +export class NonLoopbackDevAuthHostError extends Schema.TaggedErrorClass()( + "NonLoopbackDevAuthHostError", + { host: Schema.String }, +) { + override get message(): string { + return `T3CODE_DEV_AUTH is only available on loopback hosts; received ${this.host}.`; } } @@ -357,13 +361,10 @@ export const resolveServerConfig = ( ); const devAuthKey = env.devAuthEnabled ? env.devAuthKey?.trim() : undefined; if (env.devAuthEnabled && !devAuthKey) { - return yield* new DevAuthConfigurationError({ reason: "missing-key" }); + return yield* new MissingDevAuthKeyError(); } if (devAuthKey && !isLoopbackHost(host)) { - return yield* new DevAuthConfigurationError({ - reason: "non-loopback-host", - ...(host ? { host } : {}), - }); + return yield* new NonLoopbackDevAuthHostError({ host: host ?? "unknown" }); } const logLevel = Option.getOrElse(cliLogLevel, () => env.logLevel);