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>
28 lines
873 B
TypeScript
28 lines
873 B
TypeScript
import { SignJWT, jwtVerify } from "jose";
|
|
import { getOrigin, requireSecret } from "./config.server.ts";
|
|
|
|
const JWT_SECRET = new TextEncoder().encode(
|
|
requireSecret("JWT_SECRET", "dev-jwt-secret-change-in-production"),
|
|
);
|
|
|
|
const ISSUER = getOrigin();
|
|
|
|
export async function createRouteToken(routeId: string, permissions: string[] = ["read", "write"]): Promise<string> {
|
|
return new SignJWT({ route_id: routeId, permissions })
|
|
.setProtectedHeader({ alg: "HS256" })
|
|
.setIssuer(ISSUER)
|
|
.setExpirationTime("7d")
|
|
.setIssuedAt()
|
|
.sign(JWT_SECRET);
|
|
}
|
|
|
|
export async function verifyRouteToken(token: string): Promise<{ routeId: string; permissions: string[] }> {
|
|
const { payload } = await jwtVerify(token, JWT_SECRET, {
|
|
issuer: ISSUER,
|
|
});
|
|
|
|
return {
|
|
routeId: payload.route_id as string,
|
|
permissions: payload.permissions as string[],
|
|
};
|
|
}
|