fix: split dev auth configuration errors
This commit is contained in:
1 parent
4bc7f56330
commit
6ddeaf0fbb
2 files changed
+57
-17
No files matched your search
@@ -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");
|
||||
}
|
||||
}),
|
||||
|
||||
@@ -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>()(
|
||||
"DevAuthConfigurationError",
|
||||
{
|
||||
reason: Schema.Literals(["missing-key", "non-loopback-host"]),
|
||||
host: Schema.optional(Schema.String),
|
||||
},
|
||||
export class MissingDevAuthKeyError extends Schema.TaggedErrorClass<MissingDevAuthKeyError>()(
|
||||
"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>()(
|
||||
"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);
|
||||
|
||||
|
||||
Reference in new issue
Block a user