fix: disable PostgreSQL TLS for refusing RDS endpoint
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
import { readFileSync } from "node:fs";
|
||||
import pg from "pg";
|
||||
|
||||
const { Pool } = pg;
|
||||
@@ -14,8 +13,14 @@ export function createPostgresPool({ env = process.env, applicationName = "zhini
|
||||
if (!connectionString) throw new Error("DATABASE_URL is required when ZHINIAN_DATA_BACKEND=postgres");
|
||||
const parsed = assertConnectionStringContract(connectionString);
|
||||
|
||||
for (const key of [...parsed.searchParams.keys()]) {
|
||||
if (["sslmode", "sslrootcert"].includes(key.toLowerCase())) parsed.searchParams.delete(key);
|
||||
}
|
||||
parsed.searchParams.set("sslmode", "disable");
|
||||
|
||||
const config = {
|
||||
connectionString,
|
||||
connectionString: parsed.toString(),
|
||||
ssl: false,
|
||||
max: positiveInteger(env, "DATABASE_POOL_MAX", 10),
|
||||
idleTimeoutMillis: nonNegativeInteger(env, "DATABASE_IDLE_TIMEOUT_MS", 30_000),
|
||||
connectionTimeoutMillis: positiveInteger(env, "DATABASE_CONNECTION_TIMEOUT_MS", 10_000),
|
||||
@@ -23,21 +28,6 @@ export function createPostgresPool({ env = process.env, applicationName = "zhini
|
||||
application_name: applicationName
|
||||
};
|
||||
|
||||
const sslMode = parsed.searchParams.get("sslmode")?.trim().toLowerCase() || "verify-full";
|
||||
if (sslMode === "verify-full") {
|
||||
const caPath = parsed.searchParams.get("sslrootcert")?.trim();
|
||||
config.ssl = {
|
||||
...(caPath ? { ca: readFileSync(caPath, "utf8") } : {}),
|
||||
rejectUnauthorized: true
|
||||
};
|
||||
} else if (sslMode !== "disable") {
|
||||
throw new Error("DATABASE_URL sslmode must be 'disable' or 'verify-full'");
|
||||
}
|
||||
|
||||
parsed.searchParams.delete("sslmode");
|
||||
parsed.searchParams.delete("sslrootcert");
|
||||
config.connectionString = parsed.toString();
|
||||
|
||||
return new Pool(config);
|
||||
}
|
||||
|
||||
@@ -66,7 +56,7 @@ function assertConnectionStringContract(connectionString) {
|
||||
const unsupportedSSLParameters = sslParameters.filter((key) => !["sslmode", "sslrootcert"].includes(key.toLowerCase()));
|
||||
if (unsupportedSSLParameters.length > 0) {
|
||||
throw new Error(
|
||||
`DATABASE_URL contains unsupported SSL query parameters (${unsupportedSSLParameters.join(", ")}); use sslmode and optional sslrootcert`
|
||||
`DATABASE_URL contains unsupported SSL query parameters (${unsupportedSSLParameters.join(", ")}); PostgreSQL transport is forced to sslmode=disable`
|
||||
);
|
||||
}
|
||||
const sslMode = parsed.searchParams.get("sslmode")?.trim().toLowerCase();
|
||||
|
||||
Reference in New Issue
Block a user