@caelo-cms/shared 0.11.9 → 0.11.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/certs/aws-rds-global.pem +2888 -0
- package/certs/cloudsql-global.pem +1897 -0
- package/dist/body-cap.d.ts +35 -0
- package/dist/body-cap.d.ts.map +1 -0
- package/dist/body-cap.js +63 -0
- package/dist/body-cap.js.map +1 -0
- package/dist/change-journal.d.ts.map +1 -1
- package/dist/change-journal.js +3 -0
- package/dist/change-journal.js.map +1 -1
- package/dist/client-address.d.ts +69 -0
- package/dist/client-address.d.ts.map +1 -0
- package/dist/client-address.js +99 -0
- package/dist/client-address.js.map +1 -0
- package/dist/context.d.ts +8 -0
- package/dist/context.d.ts.map +1 -1
- package/dist/database-tls.d.ts +64 -0
- package/dist/database-tls.d.ts.map +1 -0
- package/dist/database-tls.js +95 -0
- package/dist/database-tls.js.map +1 -0
- package/dist/database-url.d.ts +19 -15
- package/dist/database-url.d.ts.map +1 -1
- package/dist/database-url.js +37 -1
- package/dist/database-url.js.map +1 -1
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -1
- package/dist/interim-forwarded-host.d.ts +15 -0
- package/dist/interim-forwarded-host.d.ts.map +1 -0
- package/dist/interim-forwarded-host.js +16 -0
- package/dist/interim-forwarded-host.js.map +1 -0
- package/dist/json-security-headers.d.ts +23 -0
- package/dist/json-security-headers.d.ts.map +1 -0
- package/dist/json-security-headers.js +53 -0
- package/dist/json-security-headers.js.map +1 -0
- package/dist/live-events.d.ts.map +1 -1
- package/dist/live-events.js +6 -3
- package/dist/live-events.js.map +1 -1
- package/dist/media.d.ts +8 -0
- package/dist/media.d.ts.map +1 -1
- package/dist/media.js +11 -0
- package/dist/media.js.map +1 -1
- package/dist/pool-budget.d.ts +54 -8
- package/dist/pool-budget.d.ts.map +1 -1
- package/dist/pool-budget.js +60 -8
- package/dist/pool-budget.js.map +1 -1
- package/dist/redirect-emit-gcp.d.ts +16 -8
- package/dist/redirect-emit-gcp.d.ts.map +1 -1
- package/dist/redirect-emit-gcp.js +22 -11
- package/dist/redirect-emit-gcp.js.map +1 -1
- package/dist/release-manifest.d.ts +18 -2
- package/dist/release-manifest.d.ts.map +1 -1
- package/dist/release-manifest.js +32 -4
- package/dist/release-manifest.js.map +1 -1
- package/dist/session-cookie.d.ts +88 -0
- package/dist/session-cookie.d.ts.map +1 -0
- package/dist/session-cookie.js +151 -0
- package/dist/session-cookie.js.map +1 -0
- package/dist/staging-plugin-stub.d.ts +28 -0
- package/dist/staging-plugin-stub.d.ts.map +1 -0
- package/dist/staging-plugin-stub.js +34 -0
- package/dist/staging-plugin-stub.js.map +1 -0
- package/dist/version.d.ts +2 -2
- package/dist/version.d.ts.map +1 -1
- package/dist/version.js +1 -1
- package/dist/version.js.map +1 -1
- package/package.json +2 -1
- package/src/body-cap.test.ts +44 -0
- package/src/body-cap.ts +83 -0
- package/src/change-journal.ts +5 -0
- package/src/client-address.test.ts +90 -0
- package/src/client-address.ts +111 -0
- package/src/context.ts +8 -0
- package/src/database-tls.test.ts +83 -0
- package/src/database-tls.ts +99 -0
- package/src/database-url.test.ts +73 -11
- package/src/database-url.ts +57 -1
- package/src/index.ts +7 -0
- package/src/interim-forwarded-host.ts +16 -0
- package/src/json-security-headers.test.ts +31 -0
- package/src/json-security-headers.ts +53 -0
- package/src/live-events.ts +6 -3
- package/src/media.test.ts +27 -0
- package/src/media.ts +13 -0
- package/src/pool-budget.test.ts +52 -10
- package/src/pool-budget.ts +82 -9
- package/src/redirect-emit-gcp.ts +24 -11
- package/src/redirect-emit.test.ts +6 -5
- package/src/release-manifest.ts +33 -4
- package/src/session-cookie.test.ts +96 -0
- package/src/session-cookie.ts +163 -0
- package/src/staging-plugin-stub.test.ts +23 -0
- package/src/staging-plugin-stub.ts +37 -0
- package/src/version.ts +1 -1
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
|
|
3
|
+
import { describe, expect, it } from "bun:test";
|
|
4
|
+
import { X509Certificate } from "node:crypto";
|
|
5
|
+
import { resolve } from "node:path";
|
|
6
|
+
import {
|
|
7
|
+
AWS_RDS_CA_BUNDLE,
|
|
8
|
+
CLOUD_SQL_CA_BUNDLE,
|
|
9
|
+
DATABASE_CA_DIR,
|
|
10
|
+
databaseTlsProblem,
|
|
11
|
+
isIpLiteral,
|
|
12
|
+
verifyFullQuery,
|
|
13
|
+
} from "./database-tls.js";
|
|
14
|
+
|
|
15
|
+
const CERTS = resolve(import.meta.dir, "../certs");
|
|
16
|
+
|
|
17
|
+
/** The certificates of a PEM bundle in the repo. */
|
|
18
|
+
async function bundle(name: string): Promise<X509Certificate[]> {
|
|
19
|
+
const pem = await Bun.file(resolve(CERTS, name)).text();
|
|
20
|
+
const blocks = pem.match(/-----BEGIN CERTIFICATE-----[\s\S]+?-----END CERTIFICATE-----/g) ?? [];
|
|
21
|
+
return blocks.map((b) => new X509Certificate(b));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
describe("shipped database CA bundles", () => {
|
|
25
|
+
// The images copy packages/shared/certs/ to DATABASE_CA_DIR; the constants
|
|
26
|
+
// name files there, so each must exist in the repo under the same name.
|
|
27
|
+
for (const path of [CLOUD_SQL_CA_BUNDLE, AWS_RDS_CA_BUNDLE]) {
|
|
28
|
+
const name = path.slice(DATABASE_CA_DIR.length + 1);
|
|
29
|
+
it(`${name} parses, holds a root, and no certificate expires within two years`, async () => {
|
|
30
|
+
const certs = await bundle(name);
|
|
31
|
+
expect(certs.length).toBeGreaterThan(0);
|
|
32
|
+
expect(certs.some((c) => c.subject === c.issuer)).toBe(true);
|
|
33
|
+
// A bundle that is about to expire breaks every connection of every
|
|
34
|
+
// install on that day: refresh it from the provider (see the file
|
|
35
|
+
// comment of database-tls.ts for the URLs) well before.
|
|
36
|
+
const horizon = Date.now() + 2 * 365 * 24 * 3600 * 1000;
|
|
37
|
+
for (const c of certs) expect(Date.parse(c.validTo)).toBeGreaterThan(horizon);
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
it("holds no private key", async () => {
|
|
42
|
+
for (const name of ["cloudsql-global.pem", "aws-rds-global.pem"]) {
|
|
43
|
+
expect(await Bun.file(resolve(CERTS, name)).text()).not.toMatch(/PRIVATE KEY/);
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
describe("verifyFullQuery", () => {
|
|
49
|
+
it("names the CA bundle, or relies on the public roots without one", () => {
|
|
50
|
+
expect(verifyFullQuery(CLOUD_SQL_CA_BUNDLE)).toBe(
|
|
51
|
+
"sslmode=verify-full&sslrootcert=/etc/caelo/db-ca/cloudsql-global.pem",
|
|
52
|
+
);
|
|
53
|
+
expect(verifyFullQuery()).toBe("sslmode=verify-full");
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
describe("databaseTlsProblem", () => {
|
|
58
|
+
it("accepts verify-full to a remote host", () => {
|
|
59
|
+
expect(databaseTlsProblem(`postgres://a@db.example:5432/d?${verifyFullQuery()}`)).toBeNull();
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("refuses every weaker mode to a remote host", () => {
|
|
63
|
+
for (const q of ["", "?sslmode=require", "?sslmode=verify-ca", "?sslmode=prefer"]) {
|
|
64
|
+
expect(databaseTlsProblem(`postgres://a@10.0.0.3:5432/d${q}`, "X_URL")).toMatch(
|
|
65
|
+
/^X_URL reaches 10\.0\.0\.3 /,
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("refuses a URL that does not parse", () => {
|
|
71
|
+
expect(databaseTlsProblem("not a url", "X_URL")).toBe("X_URL is not a valid URL");
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
describe("isIpLiteral", () => {
|
|
76
|
+
it("tells IP literals from host names", () => {
|
|
77
|
+
expect(isIpLiteral("10.20.0.3")).toBe(true);
|
|
78
|
+
expect(isIpLiteral("[fd00::3]")).toBe(true);
|
|
79
|
+
expect(isIpLiteral("fd00::3")).toBe(true);
|
|
80
|
+
expect(isIpLiteral("i1.p.europe-west1.sql-psa.goog")).toBe(false);
|
|
81
|
+
expect(isIpLiteral("postgres")).toBe(false);
|
|
82
|
+
});
|
|
83
|
+
});
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* TLS of every database connection (#1338, gateway hardening L6).
|
|
5
|
+
*
|
|
6
|
+
* Every connection to a database on another machine uses
|
|
7
|
+
* `sslmode=verify-full`: the server's certificate must chain to a CA the
|
|
8
|
+
* install trusts AND name the host the client dialled. `sslmode=require`
|
|
9
|
+
* (what every provider used before) encrypts but checks neither, so anyone
|
|
10
|
+
* who can answer on the database's address can read the passwords and the
|
|
11
|
+
* data. There is no weaker mode for a remote host and no automatic downgrade
|
|
12
|
+
* (CLAUDE.md §2, no fallbacks): a URL that asks for less is refused with
|
|
13
|
+
* {@link databaseTlsProblem} wherever a process reads one from its
|
|
14
|
+
* environment (`databaseUrlFromEnv`).
|
|
15
|
+
*
|
|
16
|
+
* Which CA each provider's server presents, and so which bundle the client
|
|
17
|
+
* passes as `sslrootcert`:
|
|
18
|
+
*
|
|
19
|
+
* - gcp / gcp-firebase: Cloud SQL with the shared CA
|
|
20
|
+
* (`GOOGLE_MANAGED_CAS_CA`); its server certificate names the instance's
|
|
21
|
+
* DNS name, not its IP, so clients dial that name (resolved by a private
|
|
22
|
+
* Cloud DNS zone in the install's VPC) and trust Google's published root
|
|
23
|
+
* + regional bundle ({@link CLOUD_SQL_CA_BUNDLE}).
|
|
24
|
+
* - aws: RDS, the endpoint host name, Amazon's RDS bundle
|
|
25
|
+
* ({@link AWS_RDS_CA_BUNDLE}).
|
|
26
|
+
* - azure: Flexible Server, the server's FQDN; its certificate chains to
|
|
27
|
+
* public roots (DigiCert Global Root G2 / Microsoft RSA Root CA 2017)
|
|
28
|
+
* that Bun trusts out of the box, so no `sslrootcert`.
|
|
29
|
+
* - self-hosted: the compose Postgres serves a certificate for the host
|
|
30
|
+
* name `postgres`, signed by a CA the installer generates once per
|
|
31
|
+
* install ({@link SELF_HOSTED_CA_FILE}).
|
|
32
|
+
*
|
|
33
|
+
* The bundles ship in every image at {@link DATABASE_CA_DIR}
|
|
34
|
+
* (packages/shared/certs/ in the repo), so a URL built on the operator's
|
|
35
|
+
* machine names a path that exists in the container.
|
|
36
|
+
*
|
|
37
|
+
* Pure module — no I/O.
|
|
38
|
+
*/
|
|
39
|
+
|
|
40
|
+
/** Where every Caelo image carries the database CA bundles (copied from packages/shared/certs/). */
|
|
41
|
+
export const DATABASE_CA_DIR = "/etc/caelo/db-ca";
|
|
42
|
+
|
|
43
|
+
/** Google's Cloud SQL shared-CA bundle (all regions), `cloudsql-ca-bundles/global.pem`. */
|
|
44
|
+
export const CLOUD_SQL_CA_BUNDLE = `${DATABASE_CA_DIR}/cloudsql-global.pem`;
|
|
45
|
+
|
|
46
|
+
/** Amazon's RDS bundle (all regions), `truststore.pki.rds.amazonaws.com/global/global-bundle.pem`. */
|
|
47
|
+
export const AWS_RDS_CA_BUNDLE = `${DATABASE_CA_DIR}/aws-rds-global.pem`;
|
|
48
|
+
|
|
49
|
+
/** The self-hosted install's own CA, mounted read-only into every container that connects. */
|
|
50
|
+
export const SELF_HOSTED_CA_FILE = `${DATABASE_CA_DIR}/self-hosted-ca.pem`;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The query string of a verify-full connection, with `caFile` as the trust
|
|
54
|
+
* anchor (omit it for a server whose chain ends in a public root).
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* verifyFullQuery(CLOUD_SQL_CA_BUNDLE)
|
|
58
|
+
* // → "sslmode=verify-full&sslrootcert=/etc/caelo/db-ca/cloudsql-global.pem"
|
|
59
|
+
*/
|
|
60
|
+
export function verifyFullQuery(caFile?: string): string {
|
|
61
|
+
return caFile ? `sslmode=verify-full&sslrootcert=${caFile}` : "sslmode=verify-full";
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Whether `host` is an IPv4 or IPv6 literal (with or without URL brackets). */
|
|
65
|
+
export function isIpLiteral(host: string): boolean {
|
|
66
|
+
const h = host.replace(/^\[|\]$/g, "");
|
|
67
|
+
return /^\d{1,3}(\.\d{1,3}){3}$/.test(h) || (h.includes(":") && /^[0-9a-f:.]+$/i.test(h));
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Hosts on this machine: a connection to them never crosses a network, so
|
|
72
|
+
* dev, CI and the test suites connect without TLS.
|
|
73
|
+
*/
|
|
74
|
+
function isLoopbackHost(host: string): boolean {
|
|
75
|
+
const h = host.replace(/^\[|\]$/g, "").toLowerCase();
|
|
76
|
+
return h === "localhost" || h === "::1" || /^127\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(h);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Why `url` may not be used to reach a database, or null when it may: a
|
|
81
|
+
* remote host needs `sslmode=verify-full`. Loopback hosts and unix sockets
|
|
82
|
+
* (`?host=/path`) are exempt. The message names the variable and the fix.
|
|
83
|
+
*
|
|
84
|
+
* @param label how the URL is known to the operator, e.g. `ADMIN_DATABASE_URL`
|
|
85
|
+
*/
|
|
86
|
+
export function databaseTlsProblem(url: string, label = "database URL"): string | null {
|
|
87
|
+
let parsed: URL;
|
|
88
|
+
try {
|
|
89
|
+
parsed = new URL(url);
|
|
90
|
+
} catch {
|
|
91
|
+
return `${label} is not a valid URL`;
|
|
92
|
+
}
|
|
93
|
+
const socket = parsed.searchParams.get("host");
|
|
94
|
+
if (socket?.startsWith("/")) return null;
|
|
95
|
+
if (parsed.hostname === "" || isLoopbackHost(parsed.hostname)) return null;
|
|
96
|
+
const mode = parsed.searchParams.get("sslmode");
|
|
97
|
+
if (mode === "verify-full") return null;
|
|
98
|
+
return `${label} reaches ${parsed.hostname} with sslmode=${mode ?? "(none)"}; every connection to a database on another machine must use sslmode=verify-full, which checks the server's certificate and host name (#1338). Cloud and self-hosted installs: run \`cms-provision upgrade\` (it sets the URLs and the database's certificate up). Local development: connect to localhost.`;
|
|
99
|
+
}
|
package/src/database-url.test.ts
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
import { describe, expect, it } from "bun:test";
|
|
4
4
|
import {
|
|
5
5
|
cloudSqlIamDatabaseUrls,
|
|
6
|
+
cloudSqlIamIpDatabaseUrls,
|
|
6
7
|
databasePasswordVar,
|
|
7
8
|
databaseUrlFromEnv,
|
|
8
9
|
withDatabasePassword,
|
|
@@ -72,34 +73,35 @@ describe("databaseUrlFromEnv", () => {
|
|
|
72
73
|
|
|
73
74
|
it("composes the URL from the plain URL var + its secret-mounted password var (cloud)", () => {
|
|
74
75
|
const env = {
|
|
75
|
-
ADMIN_DATABASE_URL:
|
|
76
|
+
ADMIN_DATABASE_URL:
|
|
77
|
+
"postgresql://admin_role@i1.p.europe-west1.sql-psa.goog:5432/cms_admin?sslmode=verify-full&sslrootcert=/etc/caelo/db-ca/cloudsql-global.pem",
|
|
76
78
|
ADMIN_DATABASE_PASSWORD: "s3cret",
|
|
77
79
|
};
|
|
78
80
|
expect(databaseUrlFromEnv(["ADMIN_DATABASE_URL"], env)).toBe(
|
|
79
|
-
"postgresql://admin_role:s3cret@
|
|
81
|
+
"postgresql://admin_role:s3cret@i1.p.europe-west1.sql-psa.goog:5432/cms_admin?sslmode=verify-full&sslrootcert=/etc/caelo/db-ca/cloudsql-global.pem",
|
|
80
82
|
);
|
|
81
83
|
});
|
|
82
84
|
|
|
83
85
|
it("uses the password var that belongs to the URL var it picked", () => {
|
|
84
86
|
const env = {
|
|
85
|
-
PUBLIC_DATABASE_URL: "postgresql://public_role@h:5432/cms_public",
|
|
87
|
+
PUBLIC_DATABASE_URL: "postgresql://public_role@h:5432/cms_public?sslmode=verify-full",
|
|
86
88
|
PUBLIC_DATABASE_PASSWORD: "pub",
|
|
87
89
|
// Not consulted: it pairs with PUBLIC_ADMIN_DATABASE_URL, which is unset.
|
|
88
90
|
PUBLIC_ADMIN_DATABASE_PASSWORD: "adm",
|
|
89
91
|
};
|
|
90
92
|
expect(databaseUrlFromEnv(["PUBLIC_ADMIN_DATABASE_URL", "PUBLIC_DATABASE_URL"], env)).toBe(
|
|
91
|
-
"postgresql://public_role:pub@h:5432/cms_public",
|
|
93
|
+
"postgresql://public_role:pub@h:5432/cms_public?sslmode=verify-full",
|
|
92
94
|
);
|
|
93
95
|
});
|
|
94
96
|
|
|
95
97
|
it("prefers the first URL var that is set", () => {
|
|
96
98
|
const env = {
|
|
97
|
-
PUBLIC_ADMIN_DATABASE_URL: "postgresql://admin_role@h:5432/cms_public",
|
|
99
|
+
PUBLIC_ADMIN_DATABASE_URL: "postgresql://admin_role@h:5432/cms_public?sslmode=verify-full",
|
|
98
100
|
PUBLIC_ADMIN_DATABASE_PASSWORD: "adm",
|
|
99
|
-
PUBLIC_DATABASE_URL: "postgresql://public_role@h:5432/cms_public",
|
|
101
|
+
PUBLIC_DATABASE_URL: "postgresql://public_role@h:5432/cms_public?sslmode=verify-full",
|
|
100
102
|
};
|
|
101
103
|
expect(databaseUrlFromEnv(["PUBLIC_ADMIN_DATABASE_URL", "PUBLIC_DATABASE_URL"], env)).toBe(
|
|
102
|
-
"postgresql://admin_role:adm@h:5432/cms_public",
|
|
104
|
+
"postgresql://admin_role:adm@h:5432/cms_public?sslmode=verify-full",
|
|
103
105
|
);
|
|
104
106
|
});
|
|
105
107
|
|
|
@@ -111,7 +113,7 @@ describe("databaseUrlFromEnv", () => {
|
|
|
111
113
|
|
|
112
114
|
it("fails loudly when both the URL and the password var carry a password", () => {
|
|
113
115
|
const env = {
|
|
114
|
-
ADMIN_DATABASE_URL: "postgres://admin_role:inline@h/cms_admin",
|
|
116
|
+
ADMIN_DATABASE_URL: "postgres://admin_role:inline@h/cms_admin?sslmode=verify-full",
|
|
115
117
|
ADMIN_DATABASE_PASSWORD: "mounted",
|
|
116
118
|
};
|
|
117
119
|
expect(() => databaseUrlFromEnv(["ADMIN_DATABASE_URL"], env)).toThrow(
|
|
@@ -122,10 +124,70 @@ describe("databaseUrlFromEnv", () => {
|
|
|
122
124
|
|
|
123
125
|
describe("cloudSqlIamDatabaseUrls", () => {
|
|
124
126
|
it("logs in as the service account's IAM database user with the token as password", () => {
|
|
125
|
-
expect(
|
|
127
|
+
expect(
|
|
128
|
+
cloudSqlIamDatabaseUrls(
|
|
129
|
+
"i1.p.europe-west1.sql-psa.goog",
|
|
130
|
+
"job@p.iam.gserviceaccount.com",
|
|
131
|
+
"ya29/x",
|
|
132
|
+
),
|
|
133
|
+
).toEqual({
|
|
126
134
|
user: "job@p.iam",
|
|
127
|
-
admin:
|
|
128
|
-
|
|
135
|
+
admin:
|
|
136
|
+
"postgresql://job%40p.iam:ya29%2Fx@i1.p.europe-west1.sql-psa.goog:5432/cms_admin?sslmode=verify-full&sslrootcert=/etc/caelo/db-ca/cloudsql-global.pem",
|
|
137
|
+
public:
|
|
138
|
+
"postgresql://job%40p.iam:ya29%2Fx@i1.p.europe-west1.sql-psa.goog:5432/cms_public?sslmode=verify-full&sslrootcert=/etc/caelo/db-ca/cloudsql-global.pem",
|
|
129
139
|
});
|
|
130
140
|
});
|
|
141
|
+
|
|
142
|
+
it("refuses an IP host: the shared-CA certificate names only the instance's DNS name", () => {
|
|
143
|
+
expect(() => cloudSqlIamDatabaseUrls("10.0.0.3", "job@p.iam.gserviceaccount.com", "t")).toThrow(
|
|
144
|
+
/names only its DNS name.*cms-provision upgrade/,
|
|
145
|
+
);
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
describe("cloudSqlIamIpDatabaseUrls (#1373)", () => {
|
|
150
|
+
it("dials the IP with verify-full and no trust anchor: the caller passes the server checks", () => {
|
|
151
|
+
expect(cloudSqlIamIpDatabaseUrls("10.0.0.3", "job@p.iam.gserviceaccount.com", "t").admin).toBe(
|
|
152
|
+
"postgresql://job%40p.iam:t@10.0.0.3:5432/cms_admin?sslmode=verify-full",
|
|
153
|
+
);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it("brackets an IPv6 literal in the authority", () => {
|
|
157
|
+
for (const ip of ["fd00::3", "[fd00::3]"]) {
|
|
158
|
+
const url = cloudSqlIamIpDatabaseUrls(ip, "job@p.iam.gserviceaccount.com", "t").admin;
|
|
159
|
+
expect(url).toBe("postgresql://job%40p.iam:t@[fd00::3]:5432/cms_admin?sslmode=verify-full");
|
|
160
|
+
expect(new URL(url).hostname).toBe("[fd00::3]");
|
|
161
|
+
expect(new URL(url).port).toBe("5432");
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it("refuses a host name, which takes the plain verify-full URLs", () => {
|
|
166
|
+
expect(() =>
|
|
167
|
+
cloudSqlIamIpDatabaseUrls("i1.p.europe-west1.sql-psa.goog", "job@p.iam", "t"),
|
|
168
|
+
).toThrow(/cloudSqlIamDatabaseUrls/);
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
describe("databaseUrlFromEnv TLS (#1338)", () => {
|
|
173
|
+
it("refuses a remote host without verify-full and names the variable", () => {
|
|
174
|
+
for (const mode of ["", "?sslmode=require", "?sslmode=verify-ca", "?sslmode=disable"]) {
|
|
175
|
+
expect(() =>
|
|
176
|
+
databaseUrlFromEnv(["PUBLIC_DATABASE_URL"], {
|
|
177
|
+
PUBLIC_DATABASE_URL: `postgres://public_role:x@postgres:5432/cms_public${mode}`,
|
|
178
|
+
}),
|
|
179
|
+
).toThrow(/PUBLIC_DATABASE_URL reaches postgres .*verify-full/);
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it("lets loopback hosts and unix sockets connect without TLS (dev, CI, tests)", () => {
|
|
184
|
+
for (const url of [
|
|
185
|
+
"postgres://a:x@localhost:5432/d",
|
|
186
|
+
"postgres://a:x@127.0.0.1:5541/d",
|
|
187
|
+
"postgres://a:x@[::1]:5432/d",
|
|
188
|
+
"postgres://a:x@sockethost/d?host=/var/run/postgresql",
|
|
189
|
+
]) {
|
|
190
|
+
expect(databaseUrlFromEnv(["U_URL"], { U_URL: url })).toBe(url);
|
|
191
|
+
}
|
|
192
|
+
});
|
|
131
193
|
});
|
package/src/database-url.ts
CHANGED
|
@@ -13,9 +13,20 @@
|
|
|
13
13
|
* password is a Secret Manager reference, so the password never shows up in
|
|
14
14
|
* the service's configuration or its revision history.
|
|
15
15
|
*
|
|
16
|
+
* A URL that reaches a database on another machine must ask for
|
|
17
|
+
* `sslmode=verify-full` (database-tls.ts, #1338); {@link databaseUrlFromEnv}
|
|
18
|
+
* refuses one that does not.
|
|
19
|
+
*
|
|
16
20
|
* Pure module — no I/O.
|
|
17
21
|
*/
|
|
18
22
|
|
|
23
|
+
import {
|
|
24
|
+
CLOUD_SQL_CA_BUNDLE,
|
|
25
|
+
databaseTlsProblem,
|
|
26
|
+
isIpLiteral,
|
|
27
|
+
verifyFullQuery,
|
|
28
|
+
} from "./database-tls.js";
|
|
29
|
+
|
|
19
30
|
/** The `<NAME>_PASSWORD` var that pairs with a `<NAME>_URL` var. */
|
|
20
31
|
export function databasePasswordVar(urlVar: string): string {
|
|
21
32
|
if (!urlVar.endsWith("_URL")) {
|
|
@@ -75,6 +86,8 @@ export function databaseUrlFromEnv(
|
|
|
75
86
|
for (const urlVar of urlVars) {
|
|
76
87
|
const url = env[urlVar];
|
|
77
88
|
if (!url) continue;
|
|
89
|
+
const tls = databaseTlsProblem(url, urlVar);
|
|
90
|
+
if (tls) throw new Error(tls);
|
|
78
91
|
const passwordVar = databasePasswordVar(urlVar);
|
|
79
92
|
const password = env[passwordVar];
|
|
80
93
|
return password ? withDatabasePassword(url, password, `${urlVar} (with ${passwordVar})`) : url;
|
|
@@ -87,6 +100,8 @@ export function databaseUrlFromEnv(
|
|
|
87
100
|
* how the in-cloud jobs (the operator-access sync job, the upgrader job)
|
|
88
101
|
* connect: the user is the service account email without
|
|
89
102
|
* `.gserviceaccount.com`, the password a short-lived OAuth access token.
|
|
103
|
+
* TLS is verify-full against Google's Cloud SQL shared-CA bundle, so `host`
|
|
104
|
+
* must be the instance's DNS name (its certificate names no IP).
|
|
90
105
|
*
|
|
91
106
|
* @example
|
|
92
107
|
* cloudSqlIamDatabaseUrls("10.0.0.3", "job@p.iam.gserviceaccount.com", "ya29.x").user
|
|
@@ -96,9 +111,50 @@ export function cloudSqlIamDatabaseUrls(
|
|
|
96
111
|
host: string,
|
|
97
112
|
serviceAccountEmail: string,
|
|
98
113
|
accessToken: string,
|
|
114
|
+
): { admin: string; public: string; user: string } {
|
|
115
|
+
if (isIpLiteral(host)) {
|
|
116
|
+
throw new Error(
|
|
117
|
+
`the Cloud SQL host is the IP address ${host}, but the instance's certificate names only its DNS name, so a verify-full connection to it cannot succeed (#1338). Run the converge (\`cms-provision upgrade\`, or Install update for a cloud-setup release) — it moves the services onto the DNS name — then retry.`,
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
return iamUrls(host, serviceAccountEmail, accessToken, verifyFullQuery(CLOUD_SQL_CA_BUNDLE));
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* #1373 — {@link cloudSqlIamDatabaseUrls} for the instance's private IP,
|
|
125
|
+
* for the one connection that dials it: the converge job's, before the
|
|
126
|
+
* install's TLS step has given the instance a DNS name the clients can
|
|
127
|
+
* resolve. The URLs ask for `sslmode=verify-full` and name no trust anchor;
|
|
128
|
+
* the caller must pass the server checks itself (`DatabaseAdapter`'s
|
|
129
|
+
* `tls`, from provisioning converge-db-trust.ts), since a certificate
|
|
130
|
+
* never names an IP.
|
|
131
|
+
*
|
|
132
|
+
* @throws when `ip` is not an IP literal: a DNS host takes the verify-full
|
|
133
|
+
* URLs of {@link cloudSqlIamDatabaseUrls}
|
|
134
|
+
*/
|
|
135
|
+
export function cloudSqlIamIpDatabaseUrls(
|
|
136
|
+
ip: string,
|
|
137
|
+
serviceAccountEmail: string,
|
|
138
|
+
accessToken: string,
|
|
139
|
+
): { admin: string; public: string; user: string } {
|
|
140
|
+
if (!isIpLiteral(ip)) {
|
|
141
|
+
throw new Error(
|
|
142
|
+
`${ip} is a host name; connect to it with verify-full (cloudSqlIamDatabaseUrls), not with the IP-address checks`,
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
// An IPv6 literal goes into the URI authority in brackets (RFC 3986 §3.2.2).
|
|
146
|
+
const authority = ip.includes(":") && !ip.startsWith("[") ? `[${ip}]` : ip;
|
|
147
|
+
return iamUrls(authority, serviceAccountEmail, accessToken, verifyFullQuery());
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function iamUrls(
|
|
151
|
+
host: string,
|
|
152
|
+
serviceAccountEmail: string,
|
|
153
|
+
accessToken: string,
|
|
154
|
+
tlsQuery: string,
|
|
99
155
|
): { admin: string; public: string; user: string } {
|
|
100
156
|
const user = serviceAccountEmail.replace(/\.gserviceaccount\.com$/, "");
|
|
101
157
|
const url = (db: string) =>
|
|
102
|
-
`postgresql://${encodeURIComponent(user)}:${encodeURIComponent(accessToken)}@${host}:5432/${db}
|
|
158
|
+
`postgresql://${encodeURIComponent(user)}:${encodeURIComponent(accessToken)}@${host}:5432/${db}?${tlsQuery}`;
|
|
103
159
|
return { admin: url("cms_admin"), public: url("cms_public"), user };
|
|
104
160
|
}
|
package/src/index.ts
CHANGED
|
@@ -6,10 +6,12 @@ export * from "./ai-tools.js";
|
|
|
6
6
|
export * from "./auth-forms.js";
|
|
7
7
|
export * from "./auto-update.js";
|
|
8
8
|
export * from "./base-css.js";
|
|
9
|
+
export * from "./body-cap.js";
|
|
9
10
|
export * from "./bug-intake.js";
|
|
10
11
|
export * from "./build-page.js";
|
|
11
12
|
export * from "./cap-failures.js";
|
|
12
13
|
export * from "./change-journal.js";
|
|
14
|
+
export * from "./client-address.js";
|
|
13
15
|
export * from "./cloud-shell-install.js";
|
|
14
16
|
export * from "./content.js";
|
|
15
17
|
export * from "./context.js";
|
|
@@ -17,6 +19,7 @@ export * from "./css-gradient-scan.js";
|
|
|
17
19
|
export * from "./css-scope-scan.js";
|
|
18
20
|
export * from "./css-var-aliases.js";
|
|
19
21
|
export * from "./css-var-scan.js";
|
|
22
|
+
export * from "./database-tls.js";
|
|
20
23
|
export * from "./database-url.js";
|
|
21
24
|
export * from "./design-draft-shell.js";
|
|
22
25
|
export * from "./document-language.js";
|
|
@@ -33,6 +36,8 @@ export * from "./google-oauth.js";
|
|
|
33
36
|
export * from "./id-safe-truncate.js";
|
|
34
37
|
export * from "./image-capabilities.js";
|
|
35
38
|
export * from "./interactions.js";
|
|
39
|
+
export * from "./interim-forwarded-host.js";
|
|
40
|
+
export * from "./json-security-headers.js";
|
|
36
41
|
export * from "./link-canonicalise.js";
|
|
37
42
|
export * from "./link-refs.js";
|
|
38
43
|
export * from "./lists/index.js";
|
|
@@ -76,11 +81,13 @@ export * from "./run-batched.js";
|
|
|
76
81
|
export * from "./safe-keys.js";
|
|
77
82
|
export * from "./screenshot-capture.js";
|
|
78
83
|
export * from "./seo.js";
|
|
84
|
+
export * from "./session-cookie.js";
|
|
79
85
|
export * from "./skills.js";
|
|
80
86
|
export * from "./snapshots.js";
|
|
81
87
|
export * from "./source-text-transport.js";
|
|
82
88
|
export * from "./stage-status.js";
|
|
83
89
|
export * from "./staging-gate.js";
|
|
90
|
+
export * from "./staging-plugin-stub.js";
|
|
84
91
|
export * from "./static-cache-policy.js";
|
|
85
92
|
export * from "./static-security-headers.js";
|
|
86
93
|
export * from "./strip-cdata.js";
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* FIXME(issue #1331): the header the MCP-Auth-Proxy's interim forwarder
|
|
5
|
+
* (apps/mcp-auth-proxy server.ts) sets to the host a client asked for, and
|
|
6
|
+
* the visitor gateway's staging gate (apps/api-gateway staging-proxy.ts)
|
|
7
|
+
* reads. One name for both ends of the contract.
|
|
8
|
+
*
|
|
9
|
+
* On gcp-firebase the Cloud Run service `gateway` became the MCP-Auth-Proxy
|
|
10
|
+
* (#1328, E13 a), while an existing staging domain mapping (#650) and the
|
|
11
|
+
* live release's Firebase `/api/**` rewrite still point at it. The proxy
|
|
12
|
+
* streams that traffic to the data gateway's run.app URL; Cloud Run routes
|
|
13
|
+
* by Host, so the original host travels in this header. Removed with the
|
|
14
|
+
* staging cutover (P3b-3).
|
|
15
|
+
*/
|
|
16
|
+
export const INTERIM_FORWARDED_HOST_HEADER = "x-caelo-forwarded-host";
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
|
|
3
|
+
import { describe, expect, it } from "bun:test";
|
|
4
|
+
import { JSON_SECURITY_HEADERS, withJsonSecurityHeaders } from "./json-security-headers.js";
|
|
5
|
+
|
|
6
|
+
describe("withJsonSecurityHeaders", () => {
|
|
7
|
+
it("adds every header to a JSON answer and keeps one already set", () => {
|
|
8
|
+
const res = withJsonSecurityHeaders(
|
|
9
|
+
new Response("{}", {
|
|
10
|
+
headers: { "content-type": "application/json; charset=utf-8", "referrer-policy": "origin" },
|
|
11
|
+
}),
|
|
12
|
+
);
|
|
13
|
+
expect(res.headers.get("referrer-policy")).toBe("origin");
|
|
14
|
+
expect(res.headers.get("x-frame-options")).toBe(JSON_SECURITY_HEADERS["X-Frame-Options"] ?? "");
|
|
15
|
+
expect(res.headers.get("x-content-type-options")).toBe("nosniff");
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("leaves a non-JSON answer as it is", () => {
|
|
19
|
+
const res = withJsonSecurityHeaders(
|
|
20
|
+
new Response("data: x\n\n", { headers: { "content-type": "text/event-stream" } }),
|
|
21
|
+
);
|
|
22
|
+
expect(res.headers.get("x-frame-options")).toBeNull();
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("rebuilds an answer with immutable headers around the same body", async () => {
|
|
26
|
+
const upstream = Response.json({ ok: true });
|
|
27
|
+
const res = withJsonSecurityHeaders(upstream);
|
|
28
|
+
expect(res.headers.get("content-security-policy")).toContain("default-src 'none'");
|
|
29
|
+
expect(await res.json()).toEqual({ ok: true });
|
|
30
|
+
});
|
|
31
|
+
});
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Security headers of a public service's JSON answers (#1326, #763 L2): the
|
|
5
|
+
* visitor gateway's and the MCP-Auth-Proxy's (#1328).
|
|
6
|
+
*
|
|
7
|
+
* A JSON answer is data, never a document: it must not be sniffed into
|
|
8
|
+
* HTML or script, framed, or handed to another origin's page. The headers
|
|
9
|
+
* say so to the browser on every JSON response, whichever route produced
|
|
10
|
+
* it. Pure Web APIs, so both services share the one definition.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/** The headers every JSON response carries. */
|
|
14
|
+
export const JSON_SECURITY_HEADERS: Readonly<Record<string, string>> = {
|
|
15
|
+
"X-Content-Type-Options": "nosniff",
|
|
16
|
+
// Nothing in a JSON answer may load or run anything, nor be framed.
|
|
17
|
+
"Content-Security-Policy": "default-src 'none'; frame-ancestors 'none'",
|
|
18
|
+
"X-Frame-Options": "DENY",
|
|
19
|
+
"Referrer-Policy": "no-referrer",
|
|
20
|
+
"Cross-Origin-Resource-Policy": "same-origin",
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/** Whether `res` is a JSON answer (any `application/json` or `+json` type). */
|
|
24
|
+
export function isJsonResponse(res: Response): boolean {
|
|
25
|
+
const type = (res.headers.get("content-type") ?? "").split(";")[0]?.trim().toLowerCase() ?? "";
|
|
26
|
+
return type === "application/json" || type.endsWith("+json");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Set the {@link JSON_SECURITY_HEADERS} on `headers`, keeping a value already set. */
|
|
30
|
+
export function addJsonSecurityHeaders(headers: Headers): void {
|
|
31
|
+
for (const [name, value] of Object.entries(JSON_SECURITY_HEADERS)) {
|
|
32
|
+
if (!headers.has(name)) headers.set(name, value);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* `res` with the JSON security headers when it is JSON, else `res` as it
|
|
38
|
+
* is. A response whose headers are immutable (one passed through from a
|
|
39
|
+
* `fetch`) is rebuilt around the same body stream, so a streamed answer
|
|
40
|
+
* stays streamed.
|
|
41
|
+
*/
|
|
42
|
+
export function withJsonSecurityHeaders(res: Response): Response {
|
|
43
|
+
if (!isJsonResponse(res)) return res;
|
|
44
|
+
if (Object.keys(JSON_SECURITY_HEADERS).every((h) => res.headers.has(h))) return res;
|
|
45
|
+
try {
|
|
46
|
+
addJsonSecurityHeaders(res.headers);
|
|
47
|
+
return res;
|
|
48
|
+
} catch {
|
|
49
|
+
const headers = new Headers(res.headers);
|
|
50
|
+
addJsonSecurityHeaders(headers);
|
|
51
|
+
return new Response(res.body, { status: res.status, statusText: res.statusText, headers });
|
|
52
|
+
}
|
|
53
|
+
}
|
package/src/live-events.ts
CHANGED
|
@@ -113,11 +113,12 @@ const EXACT: Readonly<Record<string, readonly LiveTopic[]>> = {
|
|
|
113
113
|
"imports.create_run": ["crawl"],
|
|
114
114
|
"imports.update_run_status": ["crawl"],
|
|
115
115
|
"imports.write_extracted_pages": ["crawl"],
|
|
116
|
-
"deploy.trigger": ["deploys"],
|
|
116
|
+
"deploy.trigger": ["deploys", "quality"],
|
|
117
117
|
// #1211 — the Stage's steps: its run appears, ends failed, ends recorded.
|
|
118
118
|
"deploy.start_trigger": ["deploys"],
|
|
119
119
|
"deploy.fail_run": ["deploys"],
|
|
120
|
-
|
|
120
|
+
// #1319 — a staging build's audit row is queued in this step.
|
|
121
|
+
"deploy.finish_trigger": ["deploys", "quality"],
|
|
121
122
|
"deploy.update_progress": ["deploys"],
|
|
122
123
|
"deploy.promote": ["deploys"],
|
|
123
124
|
// #1225 — Publish live's steps, and an approved card's claim and record.
|
|
@@ -126,7 +127,9 @@ const EXACT: Readonly<Record<string, readonly LiveTopic[]>> = {
|
|
|
126
127
|
"deploy.begin_execute_proposal": ["pending", "deploys"],
|
|
127
128
|
"deploy.end_execute_proposal": ["pending", "deploys"],
|
|
128
129
|
"deploy.rollback": ["deploys"],
|
|
129
|
-
|
|
130
|
+
// #1250 — the rollback's steps: its run appears, ends recorded.
|
|
131
|
+
"deploy.start_rollback": ["deploys"],
|
|
132
|
+
"deploy.finish_rollback": ["deploys"],
|
|
130
133
|
"deploy.build_plugin_data": ["deploys"],
|
|
131
134
|
// #1220 — the plugin-data build's steps that start and record a run.
|
|
132
135
|
"deploy.begin_plugin_data_run": ["deploys"],
|
package/src/media.test.ts
CHANGED
|
@@ -2,9 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
import { describe, expect, it } from "bun:test";
|
|
4
4
|
import {
|
|
5
|
+
buildChatImageKey,
|
|
5
6
|
buildMediaUrl,
|
|
6
7
|
buildStorageKey,
|
|
7
8
|
extractMediaRefs,
|
|
9
|
+
isWellFormedChatImageKey,
|
|
8
10
|
MEDIA_ALLOWED_MIMES,
|
|
9
11
|
MEDIA_HARD_LIMIT_BYTES,
|
|
10
12
|
MEDIA_SIZE_CAPS,
|
|
@@ -197,3 +199,28 @@ describe("media schemas", () => {
|
|
|
197
199
|
expect(mediaSetCdnInputSchema.safeParse({ enabled: true, threshold: 0 }).success).toBe(false);
|
|
198
200
|
});
|
|
199
201
|
});
|
|
202
|
+
|
|
203
|
+
describe("chat image keys (#359)", () => {
|
|
204
|
+
const sha = "a".repeat(64);
|
|
205
|
+
|
|
206
|
+
it("accepts every key buildChatImageKey writes for a raster image", () => {
|
|
207
|
+
for (const ext of ["png", "jpeg", "webp", "gif"]) {
|
|
208
|
+
expect(isWellFormedChatImageKey(buildChatImageKey("2026-11-03", sha, ext))).toBe(true);
|
|
209
|
+
}
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
it("rejects traversal, foreign prefixes and non-image objects", () => {
|
|
213
|
+
for (const key of [
|
|
214
|
+
`chat-images/../${sha}.png`,
|
|
215
|
+
`chat-images/2026-11-03/../../secrets/${sha}.png`,
|
|
216
|
+
`${sha}/orig.png`,
|
|
217
|
+
`chat-images/2026-11-03/${sha}.svg`,
|
|
218
|
+
`chat-images/2026-11-03/${sha}.png/x`,
|
|
219
|
+
`chat-images/2026-11-03/${"A".repeat(64)}.png`,
|
|
220
|
+
`/chat-images/2026-11-03/${sha}.png`,
|
|
221
|
+
`chat-images/2026-11-03/${sha}.png\n`,
|
|
222
|
+
]) {
|
|
223
|
+
expect(isWellFormedChatImageKey(key)).toBe(false);
|
|
224
|
+
}
|
|
225
|
+
});
|
|
226
|
+
});
|
package/src/media.ts
CHANGED
|
@@ -502,6 +502,19 @@ export function isChatImageKey(key: string): boolean {
|
|
|
502
502
|
return key.startsWith(`${CHAT_IMAGE_PREFIX}/`);
|
|
503
503
|
}
|
|
504
504
|
|
|
505
|
+
const CHAT_IMAGE_KEY_SHAPE = /^chat-images\/\d{4}-\d{2}-\d{2}\/[0-9a-f]{64}\.(png|jpeg|webp|gif)$/;
|
|
506
|
+
|
|
507
|
+
/**
|
|
508
|
+
* True only for a key with exactly the shape {@link buildChatImageKey} writes
|
|
509
|
+
* for a raster image. The chat-image route serves a key read from a message
|
|
510
|
+
* row; checking the full shape (not just the prefix) means a tampered or
|
|
511
|
+
* legacy row can never point the route at `..`, another prefix, or a
|
|
512
|
+
* non-image object.
|
|
513
|
+
*/
|
|
514
|
+
export function isWellFormedChatImageKey(key: string): boolean {
|
|
515
|
+
return CHAT_IMAGE_KEY_SHAPE.test(key);
|
|
516
|
+
}
|
|
517
|
+
|
|
505
518
|
/** Build the canonical storage key for a given asset variant. */
|
|
506
519
|
export function buildStorageKey(
|
|
507
520
|
sha256: string,
|