fix(journal): fail loud in production when secrets are unset

Adds `requireSecret(name, devFallback)` in lib/config.server.ts and
`getDatabaseUrl()` in @trails-cool/db. Both:
- return the env var when set,
- fall back to the dev default in non-production,
- throw at boot in production if the env var is missing OR matches the
  known dev fallback (which would otherwise silently ship a public
  secret / point at localhost).

Applied to:
- JWT_SECRET (lib/jwt.server.ts) — was `?? "dev-jwt-secret-change-in-production"`
- SESSION_SECRET (lib/auth/session.server.ts) — was `?? "dev-secret-change-in-production"`
- DATABASE_URL (server.ts health + boss; packages/db migrate-data) — was
  `?? "postgres://trails:trails@localhost:5432/trails"`

Why: these strings are in the repo and known to attackers. A
misconfigured prod deploy that forgot to set them would either run with
guessable signing keys (full session/JWT forgery) or connect to a
non-existent localhost DB. Better to refuse to start than to silently
operate insecurely.

Tests:
- packages/db/src/get-database-url.test.ts (5 cases)
- lib/config.server.test.ts gains `requireSecret` cases (4 new)

Full repo: pnpm typecheck, pnpm lint, pnpm test all green.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ullrich Schäfer 2026-05-24 11:37:20 +02:00
parent 742065c319
commit 5a7bb76ff1
No known key found for this signature in database
GPG key ID: A32FF691A0F752D9
8 changed files with 138 additions and 10 deletions

View file

@ -9,8 +9,9 @@ import { createCookieSessionStorage, redirect } from "react-router";
import { eq } from "drizzle-orm";
import { users } from "@trails-cool/db/schema/journal";
import { getDb } from "../db.ts";
import { requireSecret } from "../config.server.ts";
const sessionSecret = process.env.SESSION_SECRET ?? "dev-secret-change-in-production";
const sessionSecret = requireSecret("SESSION_SECRET", "dev-secret-change-in-production");
export const sessionStorage = createCookieSessionStorage({
cookie: {

View file

@ -17,3 +17,38 @@ describe("getOrigin", () => {
expect(getOrigin()).toBe("http://localhost:3000");
});
});
describe("requireSecret", () => {
beforeEach(() => {
vi.resetModules();
vi.unstubAllEnvs();
});
it("returns the env value when set in any environment", async () => {
vi.stubEnv("NODE_ENV", "production");
vi.stubEnv("MY_SECRET", "real-secret");
const { requireSecret } = await import("./config.server.ts");
expect(requireSecret("MY_SECRET", "dev-fallback")).toBe("real-secret");
});
it("returns the dev fallback when unset in development", async () => {
vi.stubEnv("NODE_ENV", "development");
delete process.env.MY_SECRET;
const { requireSecret } = await import("./config.server.ts");
expect(requireSecret("MY_SECRET", "dev-fallback")).toBe("dev-fallback");
});
it("throws in production when the secret is unset", async () => {
vi.stubEnv("NODE_ENV", "production");
delete process.env.MY_SECRET;
const { requireSecret } = await import("./config.server.ts");
expect(() => requireSecret("MY_SECRET", "dev-fallback")).toThrow(/MY_SECRET/);
});
it("throws in production when the secret matches the dev fallback", async () => {
vi.stubEnv("NODE_ENV", "production");
vi.stubEnv("MY_SECRET", "dev-fallback");
const { requireSecret } = await import("./config.server.ts");
expect(() => requireSecret("MY_SECRET", "dev-fallback")).toThrow(/dev fallback/);
});
});

View file

@ -5,3 +5,28 @@
export function getOrigin(): string {
return process.env.ORIGIN ?? "http://localhost:3000";
}
/**
* Read a required secret from the environment. Returns the env value when
* set. In production, throws if the env var is missing or matches the
* known-public dev fallback silently shipping a default secret to prod
* is a credential leak. In dev/test, returns the supplied fallback so the
* local loop keeps working without ceremony.
*
* Use this for any value where a leaked default would be a security
* incident: signing keys, session secrets, database credentials.
*/
export function requireSecret(name: string, devFallback: string): string {
const value = process.env[name];
const isProd = process.env.NODE_ENV === "production";
if (isProd) {
if (!value || value === devFallback) {
throw new Error(
`Refusing to start: ${name} is unset or matches the known-public dev fallback. ` +
`Set ${name} to a strong, unique value in production.`,
);
}
return value;
}
return value ?? devFallback;
}

View file

@ -1,8 +1,8 @@
import { SignJWT, jwtVerify } from "jose";
import { getOrigin } from "./config.server.ts";
import { getOrigin, requireSecret } from "./config.server.ts";
const JWT_SECRET = new TextEncoder().encode(
process.env.JWT_SECRET ?? "dev-jwt-secret-change-in-production",
requireSecret("JWT_SECRET", "dev-jwt-secret-change-in-production"),
);
const ISSUER = getOrigin();