fix: enforce production transport policy
Every PostgreSQL connection this repository opened was plaintext, in every environment: no connection site set `ssl` at all, so credentials, session tokens, and row data crossed the network in the clear and nothing in configuration, startup, or `pnpm doctor` said so. The public identity URL boundary was also looser than it appeared. `core/src/database/transport.ts` is now the one typed policy. It has exactly two modes: `verify-full` (TLS required, certificate chain verified, and server identity verified against the configured host — the production default) and `disable` (the development/test default, refused outright in production). There is deliberately no encryption-only `require` mode, no chain-only `verify-ca`, and no `rejectUnauthorized: false` escape hatch anywhere in the configuration surface; every loose or aliased spelling is rejected in both environments, so a typo never quietly becomes a weaker mode. Production has no plaintext exception at all, not even for a loopback database — a connection string cannot prove the database is process-local. Verification is certificate *and* hostname. The policy supplies an explicit `checkServerIdentity` pinned to the host parsed from DATABASE_URL, because the pinned driver sets TLS `servername` only for non-IP hosts and Node performs no identity check when it is unset — so `rejectUnauthorized: true` alone accepts a certificate from any host the trusted CA ever signed when connecting by IP. Both halves are proven against a real TLS-enabled PostgreSQL, including that the same connection without the pinned callback succeeds. Every CA, certificate, and key is generated at test runtime and deleted afterwards. A DATABASE_URL may no longer carry its own TLS parameters. The pinned driver merges parsed connection-string values over explicitly passed options, so a `?sslmode=disable` in the URL would silently replace the policy's verified `ssl` object; that now fails with a stable code, reported by both `pnpm doctor` and the migration CLIs rather than as "failed unexpectedly". One shared environment reader keeps `process.env` out of Core's reusable constructors, and every executable that opens a connection resolves the policy identically: the API (validated before any pool, Better Auth instance, module runtime, or listener exists), both migration commands, the bootstrap CLI, and Doctor's own connection. Doctor reports it as a blocking failure, never a warning, naming only the effective mode. Separately, the identity base URL and every trusted origin must now be exactly one canonical origin — the WHATWG origin serialization must equal the supplied string — so a path, trailing slash, credentials, explicit default port, or non-canonical case is rejected rather than normalized into something that no longer matches what a browser sends. Production's loopback rejection now classifies the address instead of comparing against three literals, covering `.localhost`, all of 127.0.0.0/8, 0.0.0.0, `::`, expanded and IPv4-mapped forms. This is a configuration-shape guarantee only: it does not resolve DNS or prove reachability, and TLS protects the connection, not the exposure. Co-Authored-By: Claude Opus 5 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5
parent
7a0f624fc3
commit
6b6b77c105
@@ -179,6 +179,126 @@ describe("Core identity configuration", () => {
|
||||
expect(config.baseUrl).toBe("http://localhost:3000");
|
||||
});
|
||||
|
||||
it("requires the base URL to be exactly one canonical origin, rejecting every non-origin component", () => {
|
||||
for (const baseUrl of [
|
||||
"https://id.example.test/", // a bare trailing slash is a path; `URL.origin` is slashless
|
||||
"https://id.example.test/auth", // Better Auth is never mounted beneath a configurable path
|
||||
"https://id.example.test?a=1",
|
||||
"https://id.example.test#fragment",
|
||||
"https://user:[email protected]",
|
||||
"https://:[email protected]",
|
||||
" https://id.example.test ",
|
||||
"https://ID.EXAMPLE.TEST", // non-canonical hostname case
|
||||
"https://id.example.test:443", // redundant default port
|
||||
"http://id.example.test:80",
|
||||
"ftp://id.example.test",
|
||||
"id.example.test",
|
||||
]) {
|
||||
let captured: unknown;
|
||||
try {
|
||||
createIdentityConfig({ ...validInput(), baseUrl }, { environment: "production" });
|
||||
} catch (error) {
|
||||
captured = error;
|
||||
}
|
||||
expect(captured, `expected ${JSON.stringify(baseUrl)} to be rejected`).toMatchObject({
|
||||
code: "BASE_URL_INVALID",
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
it("stores the base URL and trusted origins as one identical canonical origin representation", () => {
|
||||
const config = createIdentityConfig(
|
||||
{
|
||||
...validInput(),
|
||||
baseUrl: "https://id.example.test",
|
||||
trustedOrigins: ["https://id.example.test", "https://dashboard.example.test:8443"],
|
||||
},
|
||||
{ environment: "production" },
|
||||
);
|
||||
expect(config.baseUrl).toBe("https://id.example.test");
|
||||
expect(config.trustedOrigins).toEqual([
|
||||
"https://id.example.test",
|
||||
"https://dashboard.example.test:8443",
|
||||
]);
|
||||
});
|
||||
|
||||
it("rejects every loopback and unspecified-host spelling in production, for the base URL and trusted origins alike", () => {
|
||||
// Canonical spellings the WHATWG parser preserves — these must be rejected as production
|
||||
// policy failures, by the identical classifier on both fields.
|
||||
const canonicalLoopback = [
|
||||
"https://127.0.0.1",
|
||||
"https://127.0.0.2", // the full 127.0.0.0/8 block, not just .1
|
||||
"https://127.255.255.254",
|
||||
"https://[::1]", // IPv6 loopback, compressed
|
||||
"https://[::ffff:7f00:1]", // IPv4-mapped loopback, hexadecimal notation
|
||||
"https://localhost",
|
||||
"https://app.localhost", // RFC 6761 reserved namespace
|
||||
"https://0.0.0.0",
|
||||
"https://[::]", // IPv6 unspecified
|
||||
"https://[::ffff:0:0]", // IPv4-mapped unspecified
|
||||
];
|
||||
for (const value of canonicalLoopback) {
|
||||
let baseCaptured: unknown;
|
||||
try {
|
||||
createIdentityConfig({ ...validInput(), baseUrl: value }, { environment: "production" });
|
||||
} catch (error) {
|
||||
baseCaptured = error;
|
||||
}
|
||||
expect(baseCaptured, `expected base URL ${value} to be rejected`).toMatchObject({
|
||||
code: "BASE_URL_LOOPBACK_IN_PRODUCTION",
|
||||
});
|
||||
|
||||
let originCaptured: unknown;
|
||||
try {
|
||||
createIdentityConfig(
|
||||
{ ...validInput(), trustedOrigins: [value] },
|
||||
{ environment: "production" },
|
||||
);
|
||||
} catch (error) {
|
||||
originCaptured = error;
|
||||
}
|
||||
expect(originCaptured, `expected trusted origin ${value} to be rejected`).toMatchObject({
|
||||
code: "TRUSTED_ORIGIN_LOOPBACK_IN_PRODUCTION",
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
it("rejects non-canonical loopback spellings structurally, never as a misleading production-policy failure", () => {
|
||||
// WHATWG canonicalization folds some of these into `127.0.0.1`; others simply are not the
|
||||
// canonical origin text. Either way the honest answer is "this is not a canonical origin".
|
||||
for (const baseUrl of [
|
||||
"https://127.1",
|
||||
"https://2130706433",
|
||||
"https://0x7f000001",
|
||||
"https://[0:0:0:0:0:0:0:1]", // expanded IPv6 loopback
|
||||
"https://[::ffff:127.0.0.1]", // mixed IPv4-mapped notation
|
||||
"https://LOCALHOST",
|
||||
]) {
|
||||
let captured: unknown;
|
||||
try {
|
||||
createIdentityConfig({ ...validInput(), baseUrl }, { environment: "production" });
|
||||
} catch (error) {
|
||||
captured = error;
|
||||
}
|
||||
expect(captured, `expected ${baseUrl} to be structurally rejected`).toMatchObject({
|
||||
code: "BASE_URL_INVALID",
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
it("still accepts canonical loopback development origins", () => {
|
||||
const config = createIdentityConfig(
|
||||
{
|
||||
...validInput(),
|
||||
baseUrl: "http://localhost:3000",
|
||||
trustedOrigins: ["http://localhost:3000", "http://127.0.0.1:5173"],
|
||||
},
|
||||
{ environment: "development" },
|
||||
);
|
||||
expect(config.baseUrl).toBe("http://localhost:3000");
|
||||
expect(config.trustedOrigins).toEqual(["http://localhost:3000", "http://127.0.0.1:5173"]);
|
||||
});
|
||||
|
||||
it("defaults publicSignupEnabled to false when entirely omitted", () => {
|
||||
const config = createIdentityConfig(validInput(), { environment: "production" });
|
||||
expect(config.publicSignupEnabled).toBe(false);
|
||||
|
||||
Reference in New Issue
Block a user