@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,163 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The one cookie the API gateway reads from a visitor's browser:
|
|
5
|
+
* `__session`, a container of named entries (#1343).
|
|
6
|
+
*
|
|
7
|
+
* ## Why one cookie, and why this name
|
|
8
|
+
*
|
|
9
|
+
* Firebase Hosting forwards exactly one cookie over a rewrite to Cloud Run,
|
|
10
|
+
* the one named `__session`, and strips every other
|
|
11
|
+
* (docs/dev/firebase-hosting-behaviour.md, section 1). On gcp-firebase a
|
|
12
|
+
* gateway cookie by any other name is set in the browser and never comes
|
|
13
|
+
* back. Every provider uses the same container (CLAUDE.md §11.B: one shape
|
|
14
|
+
* per tier), so no provider has a code path of its own and a site moved
|
|
15
|
+
* between providers keeps its visitors' state.
|
|
16
|
+
*
|
|
17
|
+
* ## Shape
|
|
18
|
+
*
|
|
19
|
+
* The cookie value is a URI-encoded `URLSearchParams` string, one parameter
|
|
20
|
+
* per entry: `<key>=<expiresAt>.<value>`. Known keys are in
|
|
21
|
+
* {@link SESSION_ENTRY}; each belongs to one owner, which writes, reads and
|
|
22
|
+
* verifies its own entry's value:
|
|
23
|
+
*
|
|
24
|
+
* - `vid` the visitor id (gateway, only with consent, #677)
|
|
25
|
+
* - `login` the visitor's login session (gateway, for the auth plugin)
|
|
26
|
+
* - `staging` reserved for the staging gate's session (E13 (a), #763)
|
|
27
|
+
*
|
|
28
|
+
* A component that updates one entry keeps every other entry it received,
|
|
29
|
+
* so entries of different owners never clash.
|
|
30
|
+
*
|
|
31
|
+
* ## What is and is not protected
|
|
32
|
+
*
|
|
33
|
+
* The container itself is not signed: `expiresAt` is metadata that sets the
|
|
34
|
+
* browser's Max-Age and lets the reader drop an entry that has run out.
|
|
35
|
+
* Changing it buys a client nothing it could not do anyway (keep or drop its
|
|
36
|
+
* own cookie). Every entry VALUE must carry its own integrity check, made
|
|
37
|
+
* and checked by its owner (the gateway HMAC-signs `vid` and `login` with
|
|
38
|
+
* its cookie secret and bounds their age itself).
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
/** The cookie name, the only one Firebase Hosting passes through a rewrite. */
|
|
42
|
+
export const SESSION_COOKIE_NAME = "__session";
|
|
43
|
+
|
|
44
|
+
/** The entries the container holds, by owner. */
|
|
45
|
+
export const SESSION_ENTRY = {
|
|
46
|
+
/** The gateway's visitor id, set only with the visitor's consent (#677). */
|
|
47
|
+
visitorId: "vid",
|
|
48
|
+
/** The gateway's signed visitor login session (auth plugin). */
|
|
49
|
+
login: "login",
|
|
50
|
+
/** Reserved for the staging gate's session cookie (E13 (a), #763). */
|
|
51
|
+
staging: "staging",
|
|
52
|
+
} as const;
|
|
53
|
+
|
|
54
|
+
/** One entry: an owner-verified value and when it runs out. */
|
|
55
|
+
export interface SessionEntry {
|
|
56
|
+
readonly value: string;
|
|
57
|
+
/** Unix seconds. Not authenticated; see the module comment. */
|
|
58
|
+
readonly expiresAt: number;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Entries by key, as {@link readSessionCookie} returns them. */
|
|
62
|
+
export type SessionEntries = ReadonlyMap<string, SessionEntry>;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* The only keys the container holds. An unknown key is visitor input with
|
|
66
|
+
* no owner: reading leaves it out, so the container never grows past these
|
|
67
|
+
* three and a gateway write can never overflow it (PR #1354 review). A new
|
|
68
|
+
* owner adds its key to {@link SESSION_ENTRY}.
|
|
69
|
+
*/
|
|
70
|
+
const KNOWN_KEYS: ReadonlySet<string> = new Set(Object.values(SESSION_ENTRY));
|
|
71
|
+
/** Values the entries' owners write: signed tokens, no separators of ours. */
|
|
72
|
+
const ENTRY_VALUE = /^[A-Za-z0-9._~-]{1,512}$/;
|
|
73
|
+
|
|
74
|
+
/** The raw `__session` value of a `Cookie` header, or null. */
|
|
75
|
+
function rawSessionCookie(cookieHeader: string | null): string | null {
|
|
76
|
+
if (!cookieHeader) return null;
|
|
77
|
+
for (const part of cookieHeader.split(/;\s*/)) {
|
|
78
|
+
const eq = part.indexOf("=");
|
|
79
|
+
if (eq > 0 && part.slice(0, eq).trim() === SESSION_COOKIE_NAME) {
|
|
80
|
+
return part.slice(eq + 1).trim();
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Parse the request's `__session` container.
|
|
88
|
+
*
|
|
89
|
+
* Visitor-controlled input: a malformed container, entry or value, a key
|
|
90
|
+
* not in {@link SESSION_ENTRY} and a repeated key are left out rather than
|
|
91
|
+
* failing the request, and an entry past its `expiresAt` is left out too.
|
|
92
|
+
* The caller still verifies each value it uses.
|
|
93
|
+
*
|
|
94
|
+
* @param cookieHeader the request's `Cookie` header.
|
|
95
|
+
* @param nowSeconds the current time, unix seconds.
|
|
96
|
+
*/
|
|
97
|
+
export function readSessionCookie(cookieHeader: string | null, nowSeconds: number): SessionEntries {
|
|
98
|
+
const out = new Map<string, SessionEntry>();
|
|
99
|
+
const raw = rawSessionCookie(cookieHeader);
|
|
100
|
+
if (!raw) return out;
|
|
101
|
+
let decoded: string;
|
|
102
|
+
try {
|
|
103
|
+
decoded = decodeURIComponent(raw);
|
|
104
|
+
} catch {
|
|
105
|
+
return out;
|
|
106
|
+
}
|
|
107
|
+
for (const [key, entry] of new URLSearchParams(decoded)) {
|
|
108
|
+
if (!KNOWN_KEYS.has(key) || out.has(key)) continue;
|
|
109
|
+
const dot = entry.indexOf(".");
|
|
110
|
+
const expRaw = entry.slice(0, dot);
|
|
111
|
+
const value = entry.slice(dot + 1);
|
|
112
|
+
if (dot <= 0 || !/^\d{1,12}$/.test(expRaw) || !ENTRY_VALUE.test(value)) continue;
|
|
113
|
+
const expiresAt = Number(expRaw);
|
|
114
|
+
if (expiresAt <= nowSeconds) continue;
|
|
115
|
+
out.set(key, { value, expiresAt });
|
|
116
|
+
}
|
|
117
|
+
return out;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Serialise entries into a `__session` cookie value (URI-encoded).
|
|
122
|
+
*
|
|
123
|
+
* @throws on a key or value {@link readSessionCookie} would not read back:
|
|
124
|
+
* an owner writing one is a bug, not visitor input.
|
|
125
|
+
*/
|
|
126
|
+
export function sessionCookieValue(entries: SessionEntries): string {
|
|
127
|
+
const params = new URLSearchParams();
|
|
128
|
+
for (const [key, e] of [...entries].sort(([a], [b]) => a.localeCompare(b))) {
|
|
129
|
+
if (!KNOWN_KEYS.has(key)) throw new Error(`session cookie: unknown entry key "${key}"`);
|
|
130
|
+
if (!ENTRY_VALUE.test(e.value)) throw new Error(`session cookie: invalid value for "${key}"`);
|
|
131
|
+
if (!Number.isInteger(e.expiresAt) || e.expiresAt <= 0) {
|
|
132
|
+
throw new Error(`session cookie: invalid expiresAt for "${key}"`);
|
|
133
|
+
}
|
|
134
|
+
params.append(key, `${e.expiresAt}.${e.value}`);
|
|
135
|
+
}
|
|
136
|
+
return encodeURIComponent(params.toString());
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* The `Set-Cookie` header that stores `entries` as the `__session`
|
|
141
|
+
* container, or expires it when no entry is left.
|
|
142
|
+
*
|
|
143
|
+
* HttpOnly and SameSite=Lax always; Secure when `secure` (every deployed
|
|
144
|
+
* environment — plain-http local development cannot keep a Secure cookie).
|
|
145
|
+
* Max-Age follows the longest-lived entry.
|
|
146
|
+
*/
|
|
147
|
+
export function sessionSetCookieHeader(
|
|
148
|
+
entries: SessionEntries,
|
|
149
|
+
opts: { readonly nowSeconds: number; readonly secure: boolean },
|
|
150
|
+
): string {
|
|
151
|
+
const flags =
|
|
152
|
+
entries.size === 0
|
|
153
|
+
? [`${SESSION_COOKIE_NAME}=`, "Path=/", "HttpOnly", "SameSite=Lax", "Max-Age=0"]
|
|
154
|
+
: [
|
|
155
|
+
`${SESSION_COOKIE_NAME}=${sessionCookieValue(entries)}`,
|
|
156
|
+
"Path=/",
|
|
157
|
+
"HttpOnly",
|
|
158
|
+
"SameSite=Lax",
|
|
159
|
+
`Max-Age=${Math.max(1, Math.max(...[...entries.values()].map((e) => e.expiresAt)) - opts.nowSeconds)}`,
|
|
160
|
+
];
|
|
161
|
+
if (opts.secure) flags.push("Secure");
|
|
162
|
+
return flags.join("; ");
|
|
163
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
|
|
3
|
+
import { describe, expect, it } from "bun:test";
|
|
4
|
+
import {
|
|
5
|
+
isPluginCallPath,
|
|
6
|
+
STAGING_PLUGIN_STUB_BODY,
|
|
7
|
+
STAGING_PREVIEW_ERROR_KIND,
|
|
8
|
+
} from "./staging-plugin-stub.js";
|
|
9
|
+
|
|
10
|
+
describe("staging plugin stub (#1326)", () => {
|
|
11
|
+
it("is an ok:false answer of its own kind, with nothing else", () => {
|
|
12
|
+
expect(JSON.parse(STAGING_PLUGIN_STUB_BODY)).toEqual({
|
|
13
|
+
ok: false,
|
|
14
|
+
error: { kind: STAGING_PREVIEW_ERROR_KIND },
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("recognises plugin calls only", () => {
|
|
19
|
+
expect(isPluginCallPath("/api/plugin/forms/submit")).toBe(true);
|
|
20
|
+
expect(isPluginCallPath("/api/captcha/challenge")).toBe(false);
|
|
21
|
+
expect(isPluginCallPath("/api/variant.js")).toBe(false);
|
|
22
|
+
});
|
|
23
|
+
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* What a staged page's plugin call gets until staging has its own gateway
|
|
5
|
+
* (#1326 interim, #1329/#1330; epic #763 N4).
|
|
6
|
+
*
|
|
7
|
+
* The live gateway takes plugin posts only from a live-site Origin, and
|
|
8
|
+
* staging is never one: unreviewed module JS on staging must not write
|
|
9
|
+
* into live data. A plain 403 would make every staged page with a plugin
|
|
10
|
+
* component log "Failed to load resource" in the browser console, which the
|
|
11
|
+
* Stage quality audit counts as a page error and which would then block
|
|
12
|
+
* Publish live. So the two places a staged page's `/api/plugin/*` call can
|
|
13
|
+
* reach answer it with this instead, without dispatching anything:
|
|
14
|
+
*
|
|
15
|
+
* - the API gateway, for a POST whose Origin is the staging host
|
|
16
|
+
* (gcp-firebase, self-hosted: staging's `/api` reaches the live gateway);
|
|
17
|
+
* - the audit's loopback origin (admin-core `quality/staged-origin.ts`,
|
|
18
|
+
* gcp / aws / azure), which no longer forwards plugin calls to live.
|
|
19
|
+
*
|
|
20
|
+
* HTTP 200 with `ok: false`: the browser logs nothing, and the component
|
|
21
|
+
* takes its ordinary "no data" / "failed" path (plugin-component-kit
|
|
22
|
+
* `postPluginJson` callers all read `ok`).
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/** The error kind a staged page's plugin call answers with. */
|
|
26
|
+
export const STAGING_PREVIEW_ERROR_KIND = "StagingPreview";
|
|
27
|
+
|
|
28
|
+
/** The JSON body of that answer. */
|
|
29
|
+
export const STAGING_PLUGIN_STUB_BODY = JSON.stringify({
|
|
30
|
+
ok: false,
|
|
31
|
+
error: { kind: STAGING_PREVIEW_ERROR_KIND },
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
/** Whether `pathname` is a plugin operation call (`/api/plugin/…`). */
|
|
35
|
+
export function isPluginCallPath(pathname: string): boolean {
|
|
36
|
+
return pathname.startsWith("/api/plugin/");
|
|
37
|
+
}
|