Open-source AI coworkers that each get a computer of their own: a browser, files and tools, with every action decided before it happens and recorded after. Bring any AG-UI agent.
Turn sslmode on DATABASE_URL into TLS instead of sending it to Postgres (#746)
Guido Vizoso5971cc8e6ff5
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex a824d9a4..d955a295 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -8,6 +8,18 @@ Newest first. `Unreleased` is what is on `main` and not yet tagged.## Unreleased+### `?sslmode=require` on `DATABASE_URL` now connects to a managed database++A deployment pointed at RDS, Cloud SQL or Azure Database could not start. The server sent+`sslmode` to Postgres as a connection parameter instead of turning TLS on, so the connection went+out unencrypted and was refused with `no pg_hba.conf entry for host ... no encryption`, the very+error the Helm chart's README says `?sslmode=require` avoids. Migrations were unaffected, which is+why a deployment got as far as a migrated database and then crash-looped. `sslmode=require` now+encrypts the connection, and `sslmode=disable` turns TLS off. `sslmode=verify-full`, with an+optional `sslrootcert`, checks the certificate on Bun 1.4 and later and is refused on Bun 1.3,+which connects to any certificate. `verify-ca`, `prefer` and `allow` are refused with a message+naming the modes that work.+### A coworker the message speaks to is routed to without asking a modelAddressing a coworker by name went to the intent router like any other message, so the deploymentdiff --git a/charts/openbot/README.md b/charts/openbot/README.mdindex 236b5786..5c607558 100644--- a/charts/openbot/README.md+++ b/charts/openbot/README.md@@ -193,6 +193,12 @@ RDS has `rds.force_ssl` on by default, and Cloud SQL and Azure Database do the smigration fails with `no pg_hba.conf entry for host ... no encryption`, which names the host and theuser and not the actual problem.+The server accepts `disable`, `require` and `verify-full` (with `sslrootcert=/path/to/ca.pem`, or+the system roots without it). `require` encrypts and checks nothing, as libpq's does. `verify-full`+needs Bun 1.4 or later, because Bun 1.3 connects whatever certificate it is shown, so the server+refuses it on 1.3 rather than appear to check. `verify-ca`, `prefer` and `allow` are refused: Bun+can neither check the authority without the host name nor fall back between TLS and plain.+**The migrating role has to OWN the `vector` extension, not just be able to see it.** The firstmigration runs `CREATE EXTENSION IF NOT EXISTS vector` and migration `0010` runs`DROP EXTENSION IF EXISTS "vector"` once the document index is gone. `DROP EXTENSION` is an ownershipdiff --git a/server/src/db/client.ts b/server/src/db/client.tsindex 5248a4bf..2f7fca5c 100644--- a/server/src/db/client.ts+++ b/server/src/db/client.ts@@ -1,4 +1,5 @@import { SQL } from "bun";+import { readFileSync } from "node:fs";import { drizzle } from "drizzle-orm/bun-sql";import * as schema from "./schema";@@ -22,6 +23,82 @@ function decodePart(value: string, part: string): string {}}+/** Whether `version` is at least `major.minor`. Pre-release suffixes are ignored. */+function atLeast(version: string, major: number, minor: number): boolean {+ const [have = 0, haveMinor = 0] = version+ .split(".")+ .map((part) => Number.parseInt(part, 10));+ return have > major || (have === major && haveMinor >= minor);+}++/**+ * `sslmode` and `sslrootcert`, turned into the `tls` option Bun reads instead.+ *+ * They are libpq's client settings, not Postgres parameters. Forwarded as connection parameters+ * they were refused (`unrecognized configuration parameter "sslmode"`), and the connection was+ * opened without TLS first, which a database that requires TLS answers with `no pg_hba.conf entry+ * ... no encryption`. RDS, Cloud SQL and Azure Database all require it by default, and+ * `?sslmode=require` is what the deployment docs tell them to carry.+ *+ * Only the modes Bun can honour are accepted, each checked against a real server:+ *+ * - `require` encrypts and checks nothing, which is libpq's meaning too.+ * - `verify-full` checks the chain and the host name, against `sslrootcert` when given. Bun 1.3.x+ * connects regardless, to a self-signed server and to a certificate naming another host, so on+ * that Bun it is refused rather than allowed to look like verification.+ * - `verify-ca` would check the chain and not the name, and Bun ignores the hook that skips the+ * name, so it is refused rather than quietly made stricter.+ * - `prefer` and `allow` try one way and fall back to the other, which Bun cannot do, so they are+ * refused rather than quietly made one or the other.+ */+function tlsOf(params: URLSearchParams, bunVersion: string) {+ const mode = params.get("sslmode");+ const rootCert = params.get("sslrootcert");+ if (rootCert !== null && mode !== "verify-full") {+ throw new TypeError(+ "DATABASE_URL has sslrootcert, which is only read with sslmode=verify-full.",+ );+ }+ switch (mode) {+ case null:+ return {};+ case "disable":+ return { tls: false as const };+ case "require":+ return { tls: { rejectUnauthorized: false } };+ case "verify-full": {+ if (!atLeast(bunVersion, 1, 4)) {+ throw new TypeError(+ `DATABASE_URL has sslmode=verify-full, which needs Bun 1.4 or later to check the certificate. This is Bun ${bunVersion}. Use sslmode=require to encrypt without checking it.`,+ );+ }+ if (rootCert === null) return { tls: { rejectUnauthorized: true } };+ let ca: string;+ try {+ ca = readFileSync(rootCert, "utf8");+ } catch {+ throw new TypeError(+ `DATABASE_URL: sslrootcert ${rootCert} could not be read.`,+ );+ }+ return { tls: { rejectUnauthorized: true, ca } };+ }+ case "verify-ca":+ throw new TypeError(+ "DATABASE_URL has sslmode=verify-ca, which is not supported: Bun cannot check the certificate authority without the host name. Use sslmode=verify-full.",+ );+ case "prefer":+ case "allow":+ throw new TypeError(+ `DATABASE_URL has sslmode=${mode}, which is not supported: Bun cannot fall back between TLS and plain connections. Use sslmode=require, or sslmode=disable.`,+ );+ default:+ throw new TypeError(+ `DATABASE_URL has sslmode=${mode}, which is not one of disable, require or verify-full.`,+ );+ }+}+/*** The address, taken apart, because Bun will not take it whole on every platform.*@@ -34,8 +111,14 @@ function decodePart(value: string, part: string): string {** Passing the parts leaves nothing to parse. The behaviour is identical where the URL already* worked, since these are the same values Bun would have derived.+ *+ * Exported for its tests. `bunVersion` is a parameter so they can check both sides of the Bun 1.4+ * line from whichever Bun runs them.*/-function addressOf(databaseUrl: string) {+export function addressOf(+ databaseUrl: string,+ bunVersion: string = Bun.version,+) {let url: URL;try {url = new URL(databaseUrl);@@ -65,9 +148,15 @@ function addressOf(databaseUrl: string) {* `?application_name=…` is the one that matters here: the profile store's serialization tests* name a session that way and then look for it in `pg_stat_activity`, so losing it turns a lock* test into a three second timeout with nothing to say why. Anything else Postgres accepts on a- * URL, `sslmode` and the rest, travels the same way.+ * URL travels the same way. `sslmode` and `sslrootcert` do not: Postgres refuses them, and they+ * become the `tls` option below.*/- const connection = Object.fromEntries(url.searchParams);+ const tls = tlsOf(url.searchParams, bunVersion);+ const connection = Object.fromEntries(+ [...url.searchParams].filter(+ ([name]) => name !== "sslmode" && name !== "sslrootcert",+ ),+ );/** A port that is not a port is refused before a socket is ever opened.@@ -95,6 +184,7 @@ function addressOf(databaseUrl: string) {password: decodePart(url.password, "password"),database,...(Object.keys(connection).length > 0 ? { connection } : {}),+ ...tls,};}diff --git a/server/tests/db-client-address.test.ts b/server/tests/db-client-address.test.tsindex 8082ee04..88532d38 100644--- a/server/tests/db-client-address.test.ts+++ b/server/tests/db-client-address.test.ts@@ -1,7 +1,16 @@import { afterEach, describe, expect, test } from "bun:test";-import { createDatabase } from "../src/db/client";+import { mkdtempSync, writeFileSync } from "node:fs";+import { tmpdir } from "node:os";+import { join } from "node:path";+import { addressOf, createDatabase } from "../src/db/client";import { testDatabaseUrl } from "./support/database";+// Any readable PEM will do: these tests check what reaches Bun, not a handshake.+const caPem =+ "-----BEGIN CERTIFICATE-----\nMIIBtest\n-----END CERTIFICATE-----\n";+const caFile = join(mkdtempSync(join(tmpdir(), "openbot-ca-")), "ca.pem");+writeFileSync(caFile, caPem);+/*** The address goes to Bun in parts, and `$DATABASE_URL` does not survive the call.*@@ -129,3 +138,121 @@ describe("connection parameters on the URL", () => {}});});++/**+ * `sslmode` is a client setting, and Bun takes it as the `tls` option rather than reading it.+ *+ * It used to travel with `application_name` as a Postgres startup parameter. Postgres refuses that+ * (`unrecognized configuration parameter "sslmode"`), and against a database that requires TLS the+ * connection went out unencrypted first, which RDS answers with `no pg_hba.conf entry ... no+ * encryption`. That is every managed database, and `?sslmode=require` is what the deployment docs+ * tell a managed database to carry, so the documented configuration could not connect at all.+ */+describe("sslmode on the URL", () => {+ const base = "postgres://openbot:[email protected]:5432/openbot";++ test("require encrypts without checking the certificate, as libpq's require does", () => {+ const options = addressOf(`${base}?sslmode=require`);+ expect(options.tls).toEqual({ rejectUnauthorized: false });+ });++ test("is not sent to Postgres, which refuses it as a parameter", () => {+ const options = addressOf(`${base}?sslmode=require&application_name=probe`);+ expect(options.connection).toEqual({ application_name: "probe" });+ });++ test("disable turns TLS off explicitly", () => {+ expect(addressOf(`${base}?sslmode=disable`).tls).toBe(false);+ });++ test("absent leaves TLS as it was, so a local database without TLS still connects", () => {+ expect("tls" in addressOf(base)).toBe(false);+ });++ test("verify-full checks the certificate against sslrootcert on a Bun that verifies", () => {+ const options = addressOf(+ `${base}?sslmode=verify-full&sslrootcert=${encodeURIComponent(caFile)}`,+ "1.4.0",+ );+ expect(options.tls).toEqual({ rejectUnauthorized: true, ca: caPem });+ expect(options.connection).toBeUndefined();+ });++ test("verify-full without sslrootcert checks against the trusted roots", () => {+ expect(addressOf(`${base}?sslmode=verify-full`, "1.4.0").tls).toEqual({+ rejectUnauthorized: true,+ });+ });++ test("verify-full is refused on a Bun that does not verify, rather than pretending to", () => {+ // Observed on 1.3.14: `rejectUnauthorized: true` connects to a self-signed server and to a+ // certificate naming a different host. A setting that says it verifies and does not is worse+ // than one that refuses to start.+ expect(() => addressOf(`${base}?sslmode=verify-full`, "1.3.14")).toThrow(+ /sslmode=verify-full.*needs Bun 1\.4 or later.*1\.3\.14/,+ );+ });++ test("verify-ca is refused, since Bun cannot check the CA without the host name", () => {+ expect(() => addressOf(`${base}?sslmode=verify-ca`, "1.4.0")).toThrow(+ /sslmode=verify-ca.*not supported.*verify-full/,+ );+ });++ test.each(["prefer", "allow"])(+ "%s is refused, because Bun cannot fall back from TLS the way libpq does",+ (mode) => {+ expect(() => addressOf(`${base}?sslmode=${mode}`)).toThrow(+ new RegExp(`sslmode=${mode}.*not supported.*require`),+ );+ },+ );++ test("an unknown mode is refused, naming the ones that work", () => {+ expect(() => addressOf(`${base}?sslmode=strict`)).toThrow(+ /sslmode=strict.*not one of disable, require or verify-full/,+ );+ });++ test("sslrootcert without verify-full is refused, since nothing would check it", () => {+ expect(() =>+ addressOf(+ `${base}?sslmode=require&sslrootcert=${encodeURIComponent(caFile)}`,+ ),+ ).toThrow(/sslrootcert.*only read with sslmode=verify-full/);+ });++ test("an sslrootcert that cannot be read names the file, not the password", () => {+ const secret = "s3cr3t-p4ssw0rd";+ let message = "";+ try {+ addressOf(+ `postgres://openbot:${secret}@db.example.com:5432/openbot?sslmode=verify-full&sslrootcert=/nonexistent/ca.pem`,+ "1.4.0",+ );+ } catch (error) {+ message = error instanceof Error ? error.message : String(error);+ }+ expect(message).toMatch(+ /sslrootcert \/nonexistent\/ca\.pem could not be read/,+ );+ expect(message).not.toContain(secret);+ });+});++describe("sslmode against a real database", () => {+ test("disable connects to a database without TLS, where the parameter used to be refused", async () => {+ const address = new URL(testDatabaseUrl());+ address.searchParams.set("sslmode", "disable");+ const plain = createDatabase(address.toString());++ try {+ const rows = await plain.execute(+ "select ssl from pg_stat_ssl where pid = pg_backend_pid()",+ );+ expect((rows as Array<{ ssl: boolean }>)[0]?.ssl).toBe(false);+ } finally {+ await plain.$client.close();+ }+ });+});