@caelo-cms/shared 0.11.8 → 0.11.10
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/ai-tools.d.ts +2 -2
- 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/build-page.d.ts +4 -4
- package/dist/change-journal.d.ts.map +1 -1
- package/dist/change-journal.js +1 -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/content.d.ts +6 -6
- package/dist/content.d.ts.map +1 -1
- package/dist/content.js +8 -1
- package/dist/content.js.map +1 -1
- 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 +2 -15
- package/dist/database-url.d.ts.map +1 -1
- package/dist/database-url.js +14 -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/lists/fields.d.ts +6 -0
- package/dist/lists/fields.d.ts.map +1 -1
- package/dist/lists/fields.js +70 -7
- package/dist/lists/fields.js.map +1 -1
- package/dist/lists/index.d.ts +1 -0
- package/dist/lists/index.d.ts.map +1 -1
- package/dist/lists/index.js +1 -0
- package/dist/lists/index.js.map +1 -1
- package/dist/lists/list-type.d.ts +28 -6
- package/dist/lists/list-type.d.ts.map +1 -1
- package/dist/lists/list-type.js +43 -15
- package/dist/lists/list-type.js.map +1 -1
- package/dist/lists/overlay-scope.d.ts +68 -0
- package/dist/lists/overlay-scope.d.ts.map +1 -0
- package/dist/lists/overlay-scope.js +56 -0
- package/dist/lists/overlay-scope.js.map +1 -0
- package/dist/lists/query-merge.d.ts +27 -0
- package/dist/lists/query-merge.d.ts.map +1 -0
- package/dist/lists/query-merge.js +53 -0
- package/dist/lists/query-merge.js.map +1 -0
- package/dist/lists/resolve.d.ts +1 -0
- package/dist/lists/resolve.d.ts.map +1 -1
- package/dist/lists/resolve.js +11 -25
- package/dist/lists/resolve.js.map +1 -1
- package/dist/lists/scope.d.ts +4 -1
- package/dist/lists/scope.d.ts.map +1 -1
- package/dist/pool-budget.d.ts +10 -0
- package/dist/pool-budget.d.ts.map +1 -1
- package/dist/pool-budget.js +15 -0
- 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/template-engine.d.ts.map +1 -1
- package/dist/template-engine.js +4 -1
- package/dist/template-engine.js.map +1 -1
- 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 +1 -0
- package/src/client-address.test.ts +90 -0
- package/src/client-address.ts +111 -0
- package/src/content.test.ts +54 -0
- package/src/content.ts +10 -1
- 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 +49 -11
- package/src/database-url.ts +21 -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/lists/fields.ts +88 -7
- package/src/lists/index.ts +1 -0
- package/src/lists/list-type.ts +53 -18
- package/src/lists/lists.test.ts +70 -0
- package/src/lists/overlay-scope.test.ts +140 -0
- package/src/lists/overlay-scope.ts +120 -0
- package/src/lists/pagination.test.ts +15 -0
- package/src/lists/query-merge.ts +84 -0
- package/src/lists/resolve.test.ts +24 -4
- package/src/lists/resolve.ts +12 -33
- package/src/lists/scope.ts +4 -2
- package/src/pool-budget.test.ts +17 -3
- package/src/pool-budget.ts +19 -0
- 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/template-engine-lists.test.ts +27 -1
- package/src/template-engine.ts +4 -1
- package/src/version.ts +1 -1
package/src/body-cap.ts
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* P13 — request body size cap, shared by the public services (the visitor
|
|
5
|
+
* gateway and the MCP-Auth-Proxy's OAuth endpoints). Pure Web APIs.
|
|
6
|
+
*
|
|
7
|
+
* Default 64 KB ceiling; the gateway's per-plugin manifest override
|
|
8
|
+
* (`gateway.maxBodyBytes`) can raise it up to a 1 MB hard ceiling. Handlers
|
|
9
|
+
* don't need to worry about hostile payloads — anything over the cap is
|
|
10
|
+
* rejected before they see it.
|
|
11
|
+
*
|
|
12
|
+
* Bun's `req.text()` / `req.json()` will happily buffer multi-MB bodies;
|
|
13
|
+
* we instead read the stream manually with a running counter so the
|
|
14
|
+
* connection short-circuits at the first byte over the cap.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/** Cap applied when the caller names none. */
|
|
18
|
+
export const DEFAULT_MAX_BODY_BYTES = 64 * 1024;
|
|
19
|
+
/** No caller may raise the cap above this. */
|
|
20
|
+
export const HARD_MAX_BODY_BYTES = 1024 * 1024;
|
|
21
|
+
|
|
22
|
+
/** The body, read in full within the cap. */
|
|
23
|
+
export interface BodyCapResult {
|
|
24
|
+
readonly ok: true;
|
|
25
|
+
readonly body: ArrayBuffer;
|
|
26
|
+
readonly bytes: number;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** The body went over the cap; reading stopped there. */
|
|
30
|
+
export interface BodyCapTooLarge {
|
|
31
|
+
readonly ok: false;
|
|
32
|
+
readonly bytes: number;
|
|
33
|
+
readonly limit: number;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Read req.body up to `limit` bytes. If the body exceeds the limit, abort
|
|
38
|
+
* the read and return `{ok: false}` so the handler can emit 413.
|
|
39
|
+
*/
|
|
40
|
+
export async function readBodyWithCap(
|
|
41
|
+
req: Request,
|
|
42
|
+
limit: number = DEFAULT_MAX_BODY_BYTES,
|
|
43
|
+
): Promise<BodyCapResult | BodyCapTooLarge> {
|
|
44
|
+
const cap = Math.min(limit, HARD_MAX_BODY_BYTES);
|
|
45
|
+
// Cheap pre-check on Content-Length when present.
|
|
46
|
+
const contentLength = req.headers.get("content-length");
|
|
47
|
+
if (contentLength) {
|
|
48
|
+
const declared = Number.parseInt(contentLength, 10);
|
|
49
|
+
if (Number.isFinite(declared) && declared > cap) {
|
|
50
|
+
return { ok: false, bytes: declared, limit: cap };
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (!req.body) {
|
|
55
|
+
return { ok: true, body: new ArrayBuffer(0), bytes: 0 };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const reader = req.body.getReader();
|
|
59
|
+
const chunks: Uint8Array[] = [];
|
|
60
|
+
let total = 0;
|
|
61
|
+
try {
|
|
62
|
+
while (true) {
|
|
63
|
+
const { done, value } = await reader.read();
|
|
64
|
+
if (done) break;
|
|
65
|
+
total += value.byteLength;
|
|
66
|
+
if (total > cap) {
|
|
67
|
+
await reader.cancel(); // signal upstream to stop sending
|
|
68
|
+
return { ok: false, bytes: total, limit: cap };
|
|
69
|
+
}
|
|
70
|
+
chunks.push(value);
|
|
71
|
+
}
|
|
72
|
+
} finally {
|
|
73
|
+
reader.releaseLock();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const buf = new Uint8Array(total);
|
|
77
|
+
let offset = 0;
|
|
78
|
+
for (const c of chunks) {
|
|
79
|
+
buf.set(c, offset);
|
|
80
|
+
offset += c.byteLength;
|
|
81
|
+
}
|
|
82
|
+
return { ok: true, body: buf.buffer as ArrayBuffer, bytes: total };
|
|
83
|
+
}
|
package/src/change-journal.ts
CHANGED
|
@@ -208,6 +208,7 @@ export const JOURNAL_EXEMPT_TABLES: Readonly<Record<string, string>> = {
|
|
|
208
208
|
mcp_remote_connections: SECURITY,
|
|
209
209
|
mcp_remote_access_blocks: SECURITY,
|
|
210
210
|
mcp_transport_sessions: SECURITY,
|
|
211
|
+
mcp_unbound_work_sessions: SECURITY,
|
|
211
212
|
operator_oauth_client: SECURITY,
|
|
212
213
|
credential_handoffs:
|
|
213
214
|
"one-time credentials an approval left; delivered as the `credentials` topic",
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The visitor's address per deployment topology (client-address.ts). Each case
|
|
5
|
+
* builds X-Forwarded-For the way the provider's proxies do: whatever the
|
|
6
|
+
* client sent, then the entries the infrastructure appends.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { describe, expect, it } from "bun:test";
|
|
10
|
+
import {
|
|
11
|
+
ClientAddressError,
|
|
12
|
+
clientAddress,
|
|
13
|
+
forwardedClientIp,
|
|
14
|
+
TRUSTED_PROXY_HOPS_ENV,
|
|
15
|
+
trustedProxyHopsFromEnv,
|
|
16
|
+
} from "./client-address.js";
|
|
17
|
+
|
|
18
|
+
const VISITOR = "198.51.100.7";
|
|
19
|
+
const FORGED = "6.6.6.6";
|
|
20
|
+
|
|
21
|
+
function req(xff?: string): Request {
|
|
22
|
+
return new Request("http://gateway.internal/api/plugin/x/y", {
|
|
23
|
+
headers: xff === undefined ? {} : { "x-forwarded-for": xff },
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
describe("trustedProxyHopsFromEnv", () => {
|
|
28
|
+
it("reads a non-negative integer", () => {
|
|
29
|
+
expect(trustedProxyHopsFromEnv({ [TRUSTED_PROXY_HOPS_ENV]: "2" })).toBe(2);
|
|
30
|
+
expect(trustedProxyHopsFromEnv({ [TRUSTED_PROXY_HOPS_ENV]: "0" })).toBe(0);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("refuses a missing value instead of picking one, naming the fix", () => {
|
|
34
|
+
expect(() => trustedProxyHopsFromEnv({})).toThrow(ClientAddressError);
|
|
35
|
+
expect(() => trustedProxyHopsFromEnv({})).toThrow(/cms-provision upgrade/);
|
|
36
|
+
expect(() => trustedProxyHopsFromEnv({ [TRUSTED_PROXY_HOPS_ENV]: " " })).toThrow(
|
|
37
|
+
ClientAddressError,
|
|
38
|
+
);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("refuses anything but a non-negative integer", () => {
|
|
42
|
+
for (const bad of ["-1", "1.5", "two", "2x"]) {
|
|
43
|
+
expect(() => trustedProxyHopsFromEnv({ [TRUSTED_PROXY_HOPS_ENV]: bad })).toThrow(
|
|
44
|
+
/not a non-negative integer/,
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
describe("forwardedClientIp", () => {
|
|
51
|
+
it("counts from the end and trims", () => {
|
|
52
|
+
expect(forwardedClientIp(`${FORGED} , ${VISITOR},34.1.2.3`, 2)).toBe(VISITOR);
|
|
53
|
+
expect(forwardedClientIp(VISITOR, 1)).toBe(VISITOR);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("is null when the header is shorter than the hop count, or absent", () => {
|
|
57
|
+
expect(forwardedClientIp(VISITOR, 2)).toBeNull();
|
|
58
|
+
expect(forwardedClientIp(null, 1)).toBeNull();
|
|
59
|
+
expect(forwardedClientIp("", 1)).toBeNull();
|
|
60
|
+
expect(forwardedClientIp(VISITOR, 0)).toBeNull();
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
describe("clientAddress per topology", () => {
|
|
65
|
+
it("gcp — the LB appends <client>,<LB IP>: the visitor, never the LB, never a forged entry", () => {
|
|
66
|
+
const lb = "34.120.0.1";
|
|
67
|
+
expect(clientAddress(req(`${VISITOR}, ${lb}`), 2, null)).toBe(VISITOR);
|
|
68
|
+
// A client-sent header stays in front of what the LB appends.
|
|
69
|
+
expect(clientAddress(req(`${FORGED}, ${VISITOR}, ${lb}`), 2, null)).toBe(VISITOR);
|
|
70
|
+
// Two visitors behind the same LB stay apart (the last-hop rule pooled them).
|
|
71
|
+
expect(clientAddress(req(`203.0.113.9, ${lb}`), 2, null)).not.toBe(
|
|
72
|
+
clientAddress(req(`${VISITOR}, ${lb}`), 2, null),
|
|
73
|
+
);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("gcp-firebase / self-hosted / azure — one appended entry: forged prefixes are ignored", () => {
|
|
77
|
+
expect(clientAddress(req(VISITOR), 1, null)).toBe(VISITOR);
|
|
78
|
+
expect(clientAddress(req(`${FORGED}, ${VISITOR}`), 1, null)).toBe(VISITOR);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("no proxy (local dev) — the TCP peer, whatever the header says", () => {
|
|
82
|
+
expect(clientAddress(req(FORGED), 0, VISITOR)).toBe(VISITOR);
|
|
83
|
+
expect(() => clientAddress(req(), 0, null)).toThrow(ClientAddressError);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("fails loudly when a request lacks what the topology always appends", () => {
|
|
87
|
+
expect(() => clientAddress(req(VISITOR), 2, null)).toThrow(/fewer than 2 X-Forwarded-For/);
|
|
88
|
+
expect(() => clientAddress(req(), 1, VISITOR)).toThrow(ClientAddressError);
|
|
89
|
+
});
|
|
90
|
+
});
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The client's address, as the infrastructure in front of a public service
|
|
5
|
+
* vouches for it — one rule for every Caelo service that faces the internet
|
|
6
|
+
* (the visitor data gateway, apps/api-gateway; the MCP-Auth-Proxy,
|
|
7
|
+
* apps/mcp-auth-proxy). Pure Web APIs, no runtime dependency.
|
|
8
|
+
*
|
|
9
|
+
* Every proxy on the path APPENDS to `X-Forwarded-For`; everything before
|
|
10
|
+
* the entries our own infrastructure appended came from the client and can
|
|
11
|
+
* be forged. So the client address is not "the first entry" (forgeable) and
|
|
12
|
+
* not blindly "the last entry" either (behind a load balancer that is the
|
|
13
|
+
* load balancer, for every client): it is the entry `hops` places from the
|
|
14
|
+
* end, where `hops` is how many entries the deployment's proxies append,
|
|
15
|
+
* counting the client's own. That number is a property of the topology in
|
|
16
|
+
* front of each service, so the provisioner sets it per provider and service
|
|
17
|
+
* (`CAELO_TRUSTED_PROXY_HOPS`, packages/provisioning/src/stack-contract.ts):
|
|
18
|
+
*
|
|
19
|
+
* - gcp: external Application LB appends `<client>,<LB IP>` and
|
|
20
|
+
* the service's ingress is internal-and-LB only → 2.
|
|
21
|
+
* - gcp-firebase: Cloud Run's front end appends the address it got the
|
|
22
|
+
* connection from → 1 (behind the Firebase Hosting
|
|
23
|
+
* rewrite that is the Hosting CDN edge, see the gateway's
|
|
24
|
+
* client-ip.ts).
|
|
25
|
+
* - self-hosted: Caddy (no `trusted_proxies`) replaces the header with
|
|
26
|
+
* the peer address; the service is reachable only on the
|
|
27
|
+
* Compose network → 1.
|
|
28
|
+
* - azure: Container Apps ingress appends the peer → 1.
|
|
29
|
+
* - 0: nothing in front (local dev): the TCP peer address.
|
|
30
|
+
*
|
|
31
|
+
* The MCP-Auth-Proxy is reached the same way as the gateway on every
|
|
32
|
+
* provider (on gcp-firebase directly on its run.app URL, on gcp through the
|
|
33
|
+
* load balancer), so the same counts apply to it.
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
/** The env var carrying the deployment's trusted proxy hop count. */
|
|
37
|
+
export const TRUSTED_PROXY_HOPS_ENV = "CAELO_TRUSTED_PROXY_HOPS";
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The service cannot tell who the client is: the hop count is missing or
|
|
41
|
+
* invalid, or a request arrived with fewer forwarded entries than the
|
|
42
|
+
* deployment's proxies always append. Both are deployment errors; the
|
|
43
|
+
* message names the fix.
|
|
44
|
+
*/
|
|
45
|
+
export class ClientAddressError extends Error {
|
|
46
|
+
override readonly name = "ClientAddressError";
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The trusted proxy hop count from `env`.
|
|
51
|
+
*
|
|
52
|
+
* @throws ClientAddressError when it is unset or not a non-negative integer —
|
|
53
|
+
* guessing would either pool every client into one rate-limit bucket or
|
|
54
|
+
* let a client pick its own.
|
|
55
|
+
*/
|
|
56
|
+
export function trustedProxyHopsFromEnv(env: Readonly<Record<string, string | undefined>>): number {
|
|
57
|
+
const raw = env[TRUSTED_PROXY_HOPS_ENV];
|
|
58
|
+
if (raw === undefined || raw.trim() === "") {
|
|
59
|
+
throw new ClientAddressError(
|
|
60
|
+
`${TRUSTED_PROXY_HOPS_ENV} is not set, so this service cannot tell which X-Forwarded-For entry is the client. Cloud and self-hosted installs: run \`cms-provision upgrade\` (the provisioner sets it per provider). Local dev without a proxy: set it to 0.`,
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
if (!/^\d+$/.test(raw.trim())) {
|
|
64
|
+
throw new ClientAddressError(
|
|
65
|
+
`${TRUSTED_PROXY_HOPS_ENV}="${raw}" is not a non-negative integer (the number of X-Forwarded-For entries the proxies in front of this service append, counting the client's).`,
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
return Number.parseInt(raw.trim(), 10);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* The entry `hops` places from the end of `xForwardedFor` (1 = the last), or
|
|
73
|
+
* null when the header has fewer entries than that.
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* forwardedClientIp("6.6.6.6, 198.51.100.7, 34.1.2.3", 2) // → "198.51.100.7"
|
|
77
|
+
*/
|
|
78
|
+
export function forwardedClientIp(xForwardedFor: string | null, hops: number): string | null {
|
|
79
|
+
if (hops < 1 || !xForwardedFor) return null;
|
|
80
|
+
const entries = xForwardedFor
|
|
81
|
+
.split(",")
|
|
82
|
+
.map((s) => s.trim())
|
|
83
|
+
.filter(Boolean);
|
|
84
|
+
return entries[entries.length - hops] ?? null;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* The client address of `req` under `hops`.
|
|
89
|
+
*
|
|
90
|
+
* @param peerAddress the TCP peer (Bun `server.requestIP`); used only when
|
|
91
|
+
* `hops` is 0.
|
|
92
|
+
* @throws ClientAddressError when the request does not carry what the
|
|
93
|
+
* configured topology always adds.
|
|
94
|
+
*/
|
|
95
|
+
export function clientAddress(req: Request, hops: number, peerAddress: string | null): string {
|
|
96
|
+
if (hops === 0) {
|
|
97
|
+
if (!peerAddress) {
|
|
98
|
+
throw new ClientAddressError(
|
|
99
|
+
`${TRUSTED_PROXY_HOPS_ENV}=0 (no proxy) but the connection's peer address is unknown.`,
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
return peerAddress;
|
|
103
|
+
}
|
|
104
|
+
const ip = forwardedClientIp(req.headers.get("x-forwarded-for"), hops);
|
|
105
|
+
if (!ip) {
|
|
106
|
+
throw new ClientAddressError(
|
|
107
|
+
`the request carries fewer than ${hops} X-Forwarded-For entries, but ${TRUSTED_PROXY_HOPS_ENV}=${hops} says the proxies in front of this service always append that many. Either a request reached the service around its proxy, or ${TRUSTED_PROXY_HOPS_ENV} does not match the deployment.`,
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
return ip;
|
|
111
|
+
}
|
package/src/content.test.ts
CHANGED
|
@@ -251,6 +251,60 @@ describe("moduleFields list kind", () => {
|
|
|
251
251
|
]);
|
|
252
252
|
});
|
|
253
253
|
|
|
254
|
+
// #1304 — `editable` is optional (none); #1281 — an incomplete query is
|
|
255
|
+
// refused at write time instead of rendering a list-malformed marker.
|
|
256
|
+
const queryField = (source: Record<string, unknown>, extra: Record<string, unknown> = {}) => ({
|
|
257
|
+
name: "guides",
|
|
258
|
+
label: "G",
|
|
259
|
+
kind: "list",
|
|
260
|
+
listType: "links",
|
|
261
|
+
bind: "source",
|
|
262
|
+
source: { kind: "query", ...source },
|
|
263
|
+
...extra,
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
it("a query source without editable means none", () => {
|
|
267
|
+
const r = mod([
|
|
268
|
+
queryField({ defaults: { page_type: "guide", order: "title_asc", limit: 10 } }),
|
|
269
|
+
]);
|
|
270
|
+
expect(r.success).toBe(true);
|
|
271
|
+
const field = r.success ? r.data.fields[0] : undefined;
|
|
272
|
+
expect(field?.kind === "list" ? field.source : undefined).toEqual({
|
|
273
|
+
kind: "query",
|
|
274
|
+
editable: [],
|
|
275
|
+
defaults: { page_type: "guide", order: "title_asc", limit: 10 },
|
|
276
|
+
});
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
it("refuses a query source whose defaults lack order or limit, naming the fix", () => {
|
|
280
|
+
const noOrder = messages(
|
|
281
|
+
mod([queryField({ editable: ["order"], defaults: { page_type: "guide", limit: 10 } })]),
|
|
282
|
+
);
|
|
283
|
+
expect(noOrder).toHaveLength(1);
|
|
284
|
+
expect(noOrder[0]).toContain('list field "guides": a query source needs order');
|
|
285
|
+
expect(noOrder[0]).toContain("in source.defaults");
|
|
286
|
+
const bare = messages(mod([queryField({ defaults: { page_type: "guide" } })]));
|
|
287
|
+
expect(bare).toHaveLength(1);
|
|
288
|
+
expect(bare[0]).toContain("needs order");
|
|
289
|
+
expect(bare[0]).toContain("and limit (1–50; for an overview of every matching page");
|
|
290
|
+
expect(bare[0]).toContain("paginate:{}");
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
it("a paginated query needs order but no limit", () => {
|
|
294
|
+
const paginated = { paginate: {} };
|
|
295
|
+
expect(
|
|
296
|
+
mod([queryField({ defaults: { page_type: "guide", order: "title_asc" } }, paginated)])
|
|
297
|
+
.success,
|
|
298
|
+
).toBe(true);
|
|
299
|
+
const noOrder = messages(mod([queryField({ defaults: { page_type: "guide" } }, paginated)]));
|
|
300
|
+
expect(noOrder).toHaveLength(1);
|
|
301
|
+
expect(noOrder[0]).toContain("needs order");
|
|
302
|
+
expect(noOrder[0]).not.toContain("limit (1–50");
|
|
303
|
+
// The example must be one the next write accepts: no limit next to paginate.
|
|
304
|
+
expect(noOrder[0]).not.toContain("limit:");
|
|
305
|
+
expect(noOrder[0]).toContain("paginate:{}");
|
|
306
|
+
});
|
|
307
|
+
|
|
254
308
|
it("refuses the removed text-list / link-list kinds", () => {
|
|
255
309
|
expect(mod([{ name: "t", label: "T", kind: "text-list" }]).success).toBe(false);
|
|
256
310
|
expect(mod([{ name: "l", label: "L", kind: "link-list" }]).success).toBe(false);
|
package/src/content.ts
CHANGED
|
@@ -14,7 +14,7 @@ import { z } from "zod";
|
|
|
14
14
|
import { hasEscapedLineBreaks } from "./escaped-line-breaks.js";
|
|
15
15
|
import { DEFAULT_LIST_TYPES, SITE_TEXTS_LIST_TYPE } from "./lists/default-types.js";
|
|
16
16
|
import { listItemInputSchema } from "./lists/items.js";
|
|
17
|
-
import { LIST_MAX_ITEMS, sourceDefSchema } from "./lists/list-type.js";
|
|
17
|
+
import { LIST_MAX_ITEMS, missingQueryDefaults, sourceDefSchema } from "./lists/list-type.js";
|
|
18
18
|
import { pageTypeDefSchema } from "./lists/page-type.js";
|
|
19
19
|
import { listPaginationSchema } from "./lists/pagination.js";
|
|
20
20
|
import { LIST_REF_RE } from "./lists/refs.js";
|
|
@@ -424,6 +424,15 @@ function listFieldIssues(f: z.infer<typeof moduleFieldListSchema>): string[] {
|
|
|
424
424
|
issues.push(`list field "${f.name}": min is greater than max.`);
|
|
425
425
|
}
|
|
426
426
|
if (f.paginate !== undefined) issues.push(...paginateIssues(f));
|
|
427
|
+
// #1281 — a query whose defaults lack order/limit renders nothing but a
|
|
428
|
+
// `list-malformed` marker on every page; refuse it here, with the fix.
|
|
429
|
+
if (bind === "source" && f.source?.kind === "query") {
|
|
430
|
+
const missing = missingQueryDefaults(
|
|
431
|
+
f.source.defaults,
|
|
432
|
+
f.paginate !== undefined ? "paginated" : "field",
|
|
433
|
+
);
|
|
434
|
+
if (missing !== null) issues.push(`list field "${f.name}": ${missing}`);
|
|
435
|
+
}
|
|
427
436
|
return issues;
|
|
428
437
|
}
|
|
429
438
|
|
package/src/context.ts
CHANGED
|
@@ -37,6 +37,14 @@ export interface ExecutionContext {
|
|
|
37
37
|
* only while exactly this artifact is the active, granted one.
|
|
38
38
|
*/
|
|
39
39
|
readonly pluginArtifactDigest?: string;
|
|
40
|
+
/**
|
|
41
|
+
* #650 — the public origin of the static build a plugin's render-time
|
|
42
|
+
* call belongs to. `pages.resolve_public_urls` builds URLs on it
|
|
43
|
+
* instead of the stored site base URL, so a Stage with its own staging
|
|
44
|
+
* host gets staging URLs from plugins too. Set by the plugin host from
|
|
45
|
+
* the render invocation only.
|
|
46
|
+
*/
|
|
47
|
+
readonly renderSiteBaseUrl?: string;
|
|
40
48
|
/**
|
|
41
49
|
* P10.5: parent attribution for subagent invocations. When the
|
|
42
50
|
* `spawn_subagent` tool calls runChatTurn for the child, it carries
|
|
@@ -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
|
+
}
|