diff --git a/prisma/seed.ts b/prisma/seed.ts index f4bc97b..66dfc40 100644 --- a/prisma/seed.ts +++ b/prisma/seed.ts @@ -16,12 +16,13 @@ import { PrismaClient } from "@prisma/client"; import { PrismaPg } from "@prisma/adapter-pg"; import { Pool } from "pg"; import { getResume } from "@fastdogcoding/resume-master"; +import { pgPoolConfig } from "../src/lib/pg-config"; import { syncResume } from "../src/lib/sync-resume"; import { seedStaticSite } from "./seed-content"; const connectionString = process.env.DATABASE_URL_UNPOOLED || process.env.DATABASE_URL; -const pool = new Pool({ connectionString }); +const pool = new Pool(pgPoolConfig(connectionString)); const adapter = new PrismaPg(pool); const prisma = new PrismaClient({ adapter }); diff --git a/scripts/print-prisma-db-push-url.ts b/scripts/print-prisma-db-push-url.ts new file mode 100644 index 0000000..a534762 --- /dev/null +++ b/scripts/print-prisma-db-push-url.ts @@ -0,0 +1,7 @@ +/** + * Print the connection string `prisma db push` should use. + * Quaint cannot honor sslmode=verify-full; see prismaDbPushUrl. + */ +import { prismaDbPushUrl } from "../src/lib/pg-config"; + +process.stdout.write(prismaDbPushUrl(process.env.DATABASE_URL ?? "")); diff --git a/scripts/sync-resume.ts b/scripts/sync-resume.ts index ee9661f..d81abd2 100644 --- a/scripts/sync-resume.ts +++ b/scripts/sync-resume.ts @@ -13,6 +13,7 @@ import { PrismaClient } from "@prisma/client"; import { PrismaPg } from "@prisma/adapter-pg"; import { Pool } from "pg"; import { getResume } from "@fastdogcoding/resume-master"; +import { pgPoolConfig } from "../src/lib/pg-config"; import { syncResume } from "../src/lib/sync-resume"; import { revalidateSite } from "../src/lib/revalidate-site"; @@ -36,7 +37,7 @@ async function main() { } const projection = await getResume(apiKey); - const pool = new Pool({ connectionString }); + const pool = new Pool(pgPoolConfig(connectionString)); const prisma = new PrismaClient({ adapter: new PrismaPg(pool) }); try { diff --git a/scripts/vercel-build.sh b/scripts/vercel-build.sh index cac9959..0a052ca 100755 --- a/scripts/vercel-build.sh +++ b/scripts/vercel-build.sh @@ -19,7 +19,14 @@ if [ -z "${DATABASE_URL}" ]; then exit 1 fi -npx prisma db push +# Quaint (Prisma schema engine 7.8.0) only understands sslmode +# disable/prefer/require — verify-full is treated as prefer — and it accepts +# invalid certificates unless sslaccept=strict. pg is not used for db push. +# Keep sslmode=require and require a verified certificate. Scoped to this +# command: prisma db seed uses pgPoolConfig, which rewrites sslmode to +# verify-full for node-postgres and does not need sslaccept. +PRISMA_DB_PUSH_URL="$(npx tsx scripts/print-prisma-db-push-url.ts)" +DATABASE_URL="$PRISMA_DB_PUSH_URL" npx prisma db push if [ -n "${SEED_DATABASE:-}" ]; then echo "SEED_DATABASE is set — running prisma db seed (one-time bootstrap)." diff --git a/src/lib/pg-config.test.ts b/src/lib/pg-config.test.ts new file mode 100644 index 0000000..4b8ca57 --- /dev/null +++ b/src/lib/pg-config.test.ts @@ -0,0 +1,123 @@ +import { describe, expect, it } from "vitest"; +import { pgPoolConfig, prismaDbPushUrl } from "./pg-config"; + +const LOCAL_DATABASE_URL = "postgresql://localhost:5432/fdc_website"; +const NEON_URL = + "postgresql://u:p%40ss@ep-example.neon.tech/neondb?channel_binding=require&sslmode=require"; + +describe("pgPoolConfig", () => { + it("rewrites sslmode=require to verify-full", () => { + expect( + pgPoolConfig("postgresql://u:p@host/db?sslmode=require"), + ).toEqual({ + connectionString: "postgresql://u:p@host/db?sslmode=verify-full", + }); + }); + + it("rewrites sslmode=prefer and sslmode=verify-ca to verify-full", () => { + expect(pgPoolConfig("postgresql://u:p@host/db?sslmode=prefer")).toEqual({ + connectionString: "postgresql://u:p@host/db?sslmode=verify-full", + }); + expect(pgPoolConfig("postgresql://u:p@host/db?sslmode=verify-ca")).toEqual({ + connectionString: "postgresql://u:p@host/db?sslmode=verify-full", + }); + }); + + it("leaves sslmode=verify-full unchanged", () => { + const connectionString = + "postgresql://u:p@host/db?sslmode=verify-full&application_name=fdc"; + expect(pgPoolConfig(connectionString)).toEqual({ connectionString }); + }); + + it("leaves sslmode=disable and sslmode=allow unchanged", () => { + const disable = "postgresql://u:p@host/db?sslmode=disable"; + const allow = "postgresql://u:p@host/db?sslmode=allow"; + expect(pgPoolConfig(disable)).toEqual({ connectionString: disable }); + expect(pgPoolConfig(allow)).toEqual({ connectionString: allow }); + }); + + it("leaves a URL with no sslmode unchanged", () => { + expect(pgPoolConfig(LOCAL_DATABASE_URL)).toEqual({ + connectionString: LOCAL_DATABASE_URL, + }); + }); + + it("sets enableChannelBinding when channel_binding=require and keeps the param", () => { + expect(pgPoolConfig(NEON_URL)).toEqual({ + connectionString: + "postgresql://u:p%40ss@ep-example.neon.tech/neondb?channel_binding=require&sslmode=verify-full", + enableChannelBinding: true, + }); + }); + + it("preserves other query params and does not treat channel_binding=disable as require", () => { + const connectionString = + "postgresql://u:p@host/db?application_name=fdc&channel_binding=disable&sslmode=require&connect_timeout=10"; + expect(pgPoolConfig(connectionString)).toEqual({ + connectionString: + "postgresql://u:p@host/db?application_name=fdc&channel_binding=disable&sslmode=verify-full&connect_timeout=10", + }); + }); + + it("returns unset and empty input without throwing", () => { + expect(pgPoolConfig(undefined)).toEqual({ connectionString: undefined }); + expect(pgPoolConfig(null)).toEqual({ connectionString: undefined }); + expect(pgPoolConfig("")).toEqual({ connectionString: "" }); + }); + + it("returns invalid input unchanged", () => { + expect(pgPoolConfig("not a url")).toEqual({ connectionString: "not a url" }); + }); +}); + +describe("prismaDbPushUrl", () => { + it("appends sslaccept=strict with & when the URL already has a query", () => { + expect(prismaDbPushUrl(NEON_URL)).toBe(`${NEON_URL}&sslaccept=strict`); + expect( + prismaDbPushUrl("postgresql://u:p@host/db?sslmode=require"), + ).toBe("postgresql://u:p@host/db?sslmode=require&sslaccept=strict"); + }); + + it("uses ? when sslmode=require is present but the string has no query marker", () => { + expect(prismaDbPushUrl("postgresql://u:p@host/db&sslmode=require")).toBe( + "postgresql://u:p@host/db&sslmode=require?sslaccept=strict", + ); + }); + + it("does not override an existing sslaccept", () => { + const strict = + "postgresql://u:p@host/db?sslmode=require&sslaccept=strict"; + const loose = + "postgresql://u:p@host/db?sslaccept=accept_invalid_certs&sslmode=require"; + expect(prismaDbPushUrl(strict)).toBe(strict); + expect(prismaDbPushUrl(loose)).toBe(loose); + }); + + it("leaves URLs without sslmode=require unchanged", () => { + expect(prismaDbPushUrl(LOCAL_DATABASE_URL)).toBe(LOCAL_DATABASE_URL); + expect( + prismaDbPushUrl("postgresql://u:p@host/db?sslmode=verify-full"), + ).toBe("postgresql://u:p@host/db?sslmode=verify-full"); + expect(prismaDbPushUrl("postgresql://u:p@host/db?sslmode=prefer")).toBe( + "postgresql://u:p@host/db?sslmode=prefer", + ); + expect(prismaDbPushUrl("postgresql://u:p@host/db?sslmode=disable")).toBe( + "postgresql://u:p@host/db?sslmode=disable", + ); + }); + + it("returns unset and empty input unchanged", () => { + expect(prismaDbPushUrl(undefined)).toBe(""); + expect(prismaDbPushUrl(null)).toBe(""); + expect(prismaDbPushUrl("")).toBe(""); + }); + + it("keeps sslmode=require so quaint does not treat verify-full as prefer", () => { + const url = prismaDbPushUrl( + "postgresql://u:p@host/db?channel_binding=require&sslmode=require", + ); + expect(url).toContain("sslmode=require"); + expect(url).not.toContain("sslmode=verify-full"); + expect(url.endsWith("&sslaccept=strict")).toBe(true); + }); +}); diff --git a/src/lib/pg-config.ts b/src/lib/pg-config.ts new file mode 100644 index 0000000..4f96e02 --- /dev/null +++ b/src/lib/pg-config.ts @@ -0,0 +1,78 @@ +import type { PoolConfig } from "pg"; + +/** + * pg 8.21.0 reads channel binding only from this constructor option + * (`Client.enableChannelBinding` in pg/lib/client.js). @types/pg 8.20.0 does + * not declare it. The URL param `channel_binding` is not consulted. + */ +export interface PgPoolConfig extends PoolConfig { + enableChannelBinding?: boolean; +} + +/** + * Build a node-postgres pool config from a connection string. + * + * Neon-managed URLs end in `channel_binding=require&sslmode=require` and + * cannot be edited (the Vercel integration owns them). pg 8 treats sslmode + * `prefer`, `require`, and `verify-ca` as `verify-full` but warns that pg 9 + * will switch those aliases to weaker libpq semantics. Rewrite only those + * three values. `disable`, `allow`, `verify-full`, and a missing sslmode are + * left alone — sslmode is never added, so `postgresql://localhost:5432/fdc_website` + * is unchanged. + * + * `channel_binding=require` is copied onto the parsed config by + * pg-connection-string 2.13.0 and then ignored, so `enableChannelBinding` + * stays false unless we set it. The leftover query param does not warn, so + * it stays in the URL (stripping it would not change pg's behavior). + * + * Empty, unset, and unparseable strings are returned as a connectionString + * config and are not thrown on. Callers that require a URL still check it. + */ +export function pgPoolConfig( + connectionString: string | undefined | null, +): PgPoolConfig { + if (!connectionString) { + return { connectionString: connectionString ?? undefined }; + } + + const rewritten = connectionString.replace( + /([?&]sslmode=)(require|prefer|verify-ca)(?=&|#|$)/gi, + "$1verify-full", + ); + + const config: PgPoolConfig = { connectionString: rewritten }; + if (/(?:[?&]channel_binding=)require(?=&|#|$)/i.test(connectionString)) { + config.enableChannelBinding = true; + } + return config; +} + +/** + * Connection string for `prisma db push`. + * + * Quaint (Prisma schema engine shipped with engines 7.8.0) only understands + * sslmode `disable`, `prefer`, and `require`. `verify-full` is silently + * treated as `prefer`, and invalid certificates are accepted unless + * `sslaccept=strict`. Append that param when the URL has `sslmode=require` + * and no `sslaccept`. Leave `sslmode=require` in place. URLs without sslmode + * are unchanged. Seed still uses {@link pgPoolConfig}, which rewrites + * sslmode to `verify-full` for node-postgres. + */ +export function prismaDbPushUrl( + connectionString: string | undefined | null, +): string { + if (!connectionString) return connectionString ?? ""; + if (!/(?:[?&]sslmode=)require(?=&|#|$)/i.test(connectionString)) { + return connectionString; + } + if (/(?:[?&]sslaccept=)/i.test(connectionString)) { + return connectionString; + } + + const hashIndex = connectionString.indexOf("#"); + const base = + hashIndex === -1 ? connectionString : connectionString.slice(0, hashIndex); + const hash = hashIndex === -1 ? "" : connectionString.slice(hashIndex); + const separator = base.includes("?") ? "&" : "?"; + return `${base}${separator}sslaccept=strict${hash}`; +} diff --git a/src/lib/prisma.ts b/src/lib/prisma.ts index 874bdd2..d93f69c 100644 --- a/src/lib/prisma.ts +++ b/src/lib/prisma.ts @@ -11,6 +11,7 @@ import { PrismaClient } from "@prisma/client"; import { PrismaPg } from "@prisma/adapter-pg"; import { Pool } from "pg"; +import { pgPoolConfig } from "./pg-config"; function createPrismaClient(): PrismaClient { const connectionString = process.env.DATABASE_URL; @@ -21,7 +22,7 @@ function createPrismaClient(): PrismaClient { ); } - const pool = new Pool({ connectionString }); + const pool = new Pool(pgPoolConfig(connectionString)); const adapter = new PrismaPg(pool); return new PrismaClient({ adapter }); }