cursedbelt-server 4.73.0 → 4.73.1
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/dist/server/master-lock/guard.js +5 -7
- package/dist/server/metrics/normalizeRoute.js +11 -10
- package/package.json +2 -2
- package/src/server/events/actions.spec.ts +6 -6
- package/src/server/master-lock/ctgrLockTwin.spec.ts +13 -0
- package/src/server/master-lock/guard.ts +5 -8
- package/src/server/metrics/normalizeRoute.spec.ts +22 -6
- package/src/server/metrics/normalizeRoute.ts +11 -11
|
@@ -14,16 +14,11 @@
|
|
|
14
14
|
import { MASTER_LOCK_PATHS, MASTER_LOCK_PREFIX, MASTER_LOCK_STATE_HEADER, masterLockCookieName, } from "cursedbelt-core/master-lock";
|
|
15
15
|
import { MASTER_LOCK_ACCOUNTS_PAGE_PATHS, MASTER_LOCK_ACCOUNTS_SCRIPT, MASTER_LOCK_ACCOUNTS_STYLE, accountsPageHtml, } from "./accountsPage.js";
|
|
16
16
|
import { ChunkStore } from "cursedbelt-core/ctgr/chunk-store";
|
|
17
|
+
import { isCtgrTwinOf } from "cursedbelt-core/ctgr/types";
|
|
17
18
|
import { V0ChunkStore } from "cursedbelt-core/ctgr/v0compat";
|
|
18
19
|
import { decodeCtgrTwin } from "../ctgrTwin.js";
|
|
19
20
|
import { LOCK_SCRIPT, LOCK_STYLE, lockPageHtml } from "./lockPage.js";
|
|
20
21
|
const noStore = { "cache-control": "no-store, no-cache, must-revalidate", pragma: "no-cache" };
|
|
21
|
-
/**
|
|
22
|
-
* A GET-only device's twin of a lock write: `/ctgr<xx>/__lock/…` (forge task 5158). Its writes —
|
|
23
|
-
* unlock, enroll, ping, lock — would otherwise reach the wall as a GET to a path that is not the
|
|
24
|
-
* lock's, and be refused, so such a device could never get in.
|
|
25
|
-
*/
|
|
26
|
-
const LOCK_TWIN = new RegExp(`^/ctgr[a-z]{2}${MASTER_LOCK_PREFIX}(?:/|$)`, "i");
|
|
27
22
|
/**
|
|
28
23
|
* The lock page's own CSP. Strict enough to stand in front of `apps/vault`'s wall without
|
|
29
24
|
* loosening it: nothing loads from anywhere but this origin, the form posts nowhere (the
|
|
@@ -249,7 +244,10 @@ export function createMasterLockGuard(options) {
|
|
|
249
244
|
// 🔴 The twin is DECODED here and the real request judged by the very rule below, so the
|
|
250
245
|
// wall decides an unlock's twin exactly as it decides the POST, whichever of this guard and
|
|
251
246
|
// `mountCtgrTunnel` an app mounts first — and in a bare `Bun.serve` app that has no tunnel.
|
|
252
|
-
|
|
247
|
+
// A GET-only device's twin of a lock write: `/ctgr<xx>/__lock/…` (forge task 5158). Its writes —
|
|
248
|
+
// unlock, enroll, ping, lock — would otherwise reach the wall as a GET to a path that is not the
|
|
249
|
+
// lock's, and be refused, so such a device could never get in. Core says what a twin is (task 5162).
|
|
250
|
+
if (isCtgrTwinOf(url.pathname, MASTER_LOCK_PREFIX)) {
|
|
253
251
|
const decoded = await decodeCtgrTwin(req, url.pathname, twinStore, twinLegacy);
|
|
254
252
|
return decoded instanceof Response ? decoded : handle(decoded);
|
|
255
253
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ABBREV_TO_METHOD, ctgrTwinTarget } from 'cursedbelt-core/ctgr/types';
|
|
1
2
|
/**
|
|
2
3
|
* Turn a request into a stable, low-cardinality route label for metric aggregation
|
|
3
4
|
* (e.g. `/api/v1/notes/:id`). The method is tracked separately.
|
|
@@ -11,9 +12,6 @@
|
|
|
11
12
|
*/
|
|
12
13
|
const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
13
14
|
const LONG_HEX_RE = /^[0-9a-f]{16,}$/i;
|
|
14
|
-
// The ctgr twin URL is `/ctgr<last-2-of-method><url>`, so the two-letter suffix
|
|
15
|
-
// recovers the original verb (POST→st, PUT→ut, PATCH→ch, DELETE→te).
|
|
16
|
-
const CTGR_METHOD = { st: 'POST', ut: 'PUT', ch: 'PATCH', te: 'DELETE' };
|
|
17
15
|
function collapseSegment(seg) {
|
|
18
16
|
if (!seg)
|
|
19
17
|
return seg;
|
|
@@ -50,13 +48,16 @@ export function normalizeRoute(c) {
|
|
|
50
48
|
const path = c.req.path.split('?')[0];
|
|
51
49
|
let route = pattern ?? (collapsePath(path) || '/');
|
|
52
50
|
let unmatchedPath = pattern ? undefined : path;
|
|
53
|
-
// Map a ctgr GET-twin back to its real verb + url:
|
|
54
|
-
//
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
51
|
+
// Map a ctgr GET-twin back to its real verb + url: `/ctgrpa/api/notes/n1` → PATCH /api/notes/:id.
|
|
52
|
+
// Which paths are twins, and which verb each stands for, is core's one answer (forge task 5162) —
|
|
53
|
+
// this file's own table once held abbreviations no client sends, so every twin counted as a GET.
|
|
54
|
+
const twin = ctgrTwinTarget(path);
|
|
55
|
+
if (twin !== null) {
|
|
56
|
+
method = ABBREV_TO_METHOD[path.slice(5, 7)];
|
|
57
|
+
// A route registered at the twin itself is still a real pattern; the tunnel's catch-all is not.
|
|
58
|
+
const realPattern = pattern === undefined ? null : ctgrTwinTarget(pattern);
|
|
59
|
+
route = realPattern ?? (collapsePath(twin) || '/');
|
|
60
|
+
unmatchedPath = realPattern === null ? twin : undefined;
|
|
60
61
|
}
|
|
61
62
|
return { method, route, ...(unmatchedPath === undefined ? {} : { unmatchedPath }) };
|
|
62
63
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cursedbelt-server",
|
|
3
|
-
"version": "4.73.
|
|
3
|
+
"version": "4.73.1",
|
|
4
4
|
"license": "ISC",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "The app-facing Bun/Hono server tier of the cursedbelt split — storage, sharing, activity, guard, sync. React-free; cursedbelt-core below it.",
|
|
@@ -400,7 +400,7 @@
|
|
|
400
400
|
},
|
|
401
401
|
"dependencies": {
|
|
402
402
|
"argon2id": "1.0.1",
|
|
403
|
-
"cursedbelt-core": "^2.
|
|
403
|
+
"cursedbelt-core": "^2.20.0",
|
|
404
404
|
"cursedops": ">=0.8.1 <1.0.0",
|
|
405
405
|
"cwip": "^4.6.0",
|
|
406
406
|
"jose": "^6.2.3"
|
|
@@ -100,9 +100,9 @@ describe("serverActions — the API seam that names a state-changing act", () =>
|
|
|
100
100
|
const dataset = fakeDataset();
|
|
101
101
|
const events = createEventSink(dataset, { app: "music" });
|
|
102
102
|
const app = new Hono();
|
|
103
|
-
app.use("
|
|
104
|
-
app.get("/api/
|
|
105
|
-
await app.request("https://music.cursedalchemy.com/api/
|
|
103
|
+
app.use("*", serverActions(events, RULES, { actorOf: () => ({ id: "curt@example.com" }) }));
|
|
104
|
+
app.get("/ctgrde/api/playlists/:id", (c) => c.json({ ok: true }));
|
|
105
|
+
await app.request("https://music.cursedalchemy.com/ctgrde/api/playlists/7", { headers: { "user-agent": BROWSER } });
|
|
106
106
|
expect(dataset.points[0]?.indexes).toEqual(["action:playlist.delete"]);
|
|
107
107
|
});
|
|
108
108
|
|
|
@@ -138,8 +138,8 @@ describe("serverActions — the API seam that names a state-changing act", () =>
|
|
|
138
138
|
const events = createEventSink(dataset, { app: "lists" });
|
|
139
139
|
const app = new Hono();
|
|
140
140
|
// A known actor writes at once; the anonymous hash would make every assertion wait on Web Crypto.
|
|
141
|
-
app.use("
|
|
142
|
-
app.use("
|
|
141
|
+
app.use("*", serverActions(events, GATED, { actorOf: () => ({ id: "curt@example.com" }) }));
|
|
142
|
+
app.use("*", async (c) => c.json({ error: "sign in" }, status as 401));
|
|
143
143
|
return {
|
|
144
144
|
names: () => dataset.points.map((p) => [p.blobs?.[3], p.blobs?.[13]]),
|
|
145
145
|
blobs: () => JSON.stringify(dataset.points),
|
|
@@ -178,7 +178,7 @@ describe("serverActions — the API seam that names a state-changing act", () =>
|
|
|
178
178
|
|
|
179
179
|
test("a ctgr GET-twin refused by the gate counts as its real verb", async () => {
|
|
180
180
|
const { names, send } = refused();
|
|
181
|
-
await send("GET", "/api/
|
|
181
|
+
await send("GET", "/ctgrde/api/playlists/my-mix");
|
|
182
182
|
expect(names()).toEqual([["playlist.delete", "refused"]]);
|
|
183
183
|
});
|
|
184
184
|
|
|
@@ -142,6 +142,19 @@ describe("the guard decodes a lock twin and judges the POST it carries", () => {
|
|
|
142
142
|
expect(res?.headers.get("set-cookie")).toBeNull();
|
|
143
143
|
});
|
|
144
144
|
|
|
145
|
+
test.each(["/ctgrPO", "/ctgrzz"])(
|
|
146
|
+
"only core's verbs make a lock twin: the right password at %s/__lock/… is not decoded, so it unlocks nothing",
|
|
147
|
+
async (head) => {
|
|
148
|
+
const guard = build();
|
|
149
|
+
const real = await twin("POST", MASTER_LOCK_PATHS.unlock, { verifier: VERIFIER });
|
|
150
|
+
const url = new URL(real.url);
|
|
151
|
+
url.pathname = head + url.pathname.slice(7);
|
|
152
|
+
const res = await guard.handle(new Request(url, { headers: real.headers }));
|
|
153
|
+
expect(res?.status).not.toBe(200);
|
|
154
|
+
expect(res?.headers.get("set-cookie") ?? null).toBeNull();
|
|
155
|
+
},
|
|
156
|
+
);
|
|
157
|
+
|
|
145
158
|
test("a twin of an app path is not the guard's to decode — it is walled like any request", async () => {
|
|
146
159
|
const guard = build();
|
|
147
160
|
const res = await guard.handle(await twin("POST", "/api/thing", { a: 1 }));
|
|
@@ -24,6 +24,7 @@ import {
|
|
|
24
24
|
accountsPageHtml,
|
|
25
25
|
} from "./accountsPage.js";
|
|
26
26
|
import { ChunkStore } from "cursedbelt-core/ctgr/chunk-store";
|
|
27
|
+
import { isCtgrTwinOf } from "cursedbelt-core/ctgr/types";
|
|
27
28
|
import { V0ChunkStore } from "cursedbelt-core/ctgr/v0compat";
|
|
28
29
|
import { decodeCtgrTwin } from "../ctgrTwin.js";
|
|
29
30
|
import { LOCK_SCRIPT, LOCK_STYLE, type LockPageOptions, lockPageHtml } from "./lockPage.js";
|
|
@@ -78,13 +79,6 @@ export interface MasterLockGuardOptions extends LockPageOptions {
|
|
|
78
79
|
|
|
79
80
|
const noStore = { "cache-control": "no-store, no-cache, must-revalidate", pragma: "no-cache" } as const;
|
|
80
81
|
|
|
81
|
-
/**
|
|
82
|
-
* A GET-only device's twin of a lock write: `/ctgr<xx>/__lock/…` (forge task 5158). Its writes —
|
|
83
|
-
* unlock, enroll, ping, lock — would otherwise reach the wall as a GET to a path that is not the
|
|
84
|
-
* lock's, and be refused, so such a device could never get in.
|
|
85
|
-
*/
|
|
86
|
-
const LOCK_TWIN = new RegExp(`^/ctgr[a-z]{2}${MASTER_LOCK_PREFIX}(?:/|$)`, "i");
|
|
87
|
-
|
|
88
82
|
/**
|
|
89
83
|
* The lock page's own CSP. Strict enough to stand in front of `apps/vault`'s wall without
|
|
90
84
|
* loosening it: nothing loads from anywhere but this origin, the form posts nowhere (the
|
|
@@ -353,7 +347,10 @@ export function createMasterLockGuard(options: MasterLockGuardOptions): MasterLo
|
|
|
353
347
|
// 🔴 The twin is DECODED here and the real request judged by the very rule below, so the
|
|
354
348
|
// wall decides an unlock's twin exactly as it decides the POST, whichever of this guard and
|
|
355
349
|
// `mountCtgrTunnel` an app mounts first — and in a bare `Bun.serve` app that has no tunnel.
|
|
356
|
-
|
|
350
|
+
// A GET-only device's twin of a lock write: `/ctgr<xx>/__lock/…` (forge task 5158). Its writes —
|
|
351
|
+
// unlock, enroll, ping, lock — would otherwise reach the wall as a GET to a path that is not the
|
|
352
|
+
// lock's, and be refused, so such a device could never get in. Core says what a twin is (task 5162).
|
|
353
|
+
if (isCtgrTwinOf(url.pathname, MASTER_LOCK_PREFIX)) {
|
|
357
354
|
const decoded = await decodeCtgrTwin(req, url.pathname, twinStore, twinLegacy);
|
|
358
355
|
return decoded instanceof Response ? decoded : handle(decoded);
|
|
359
356
|
}
|
|
@@ -48,23 +48,39 @@ describe('normalizeRoute', () => {
|
|
|
48
48
|
return c.text('');
|
|
49
49
|
};
|
|
50
50
|
app.get('/api/notes/:id', gate);
|
|
51
|
-
app.use('
|
|
51
|
+
app.use('*', async (c, next) => {
|
|
52
52
|
await next();
|
|
53
53
|
if (c.req.path.includes('gated')) seen.push(normalizeRoute(c).unmatchedPath);
|
|
54
54
|
});
|
|
55
|
-
app.use('
|
|
55
|
+
app.use('*', async (c, next) => (c.req.path.includes('gated') ? c.text('no', 401) : next()));
|
|
56
56
|
await app.request('http://x/api/notes/n1');
|
|
57
57
|
await app.request('http://x/api/gated/my-slug');
|
|
58
|
-
await app.request('http://x/api/
|
|
58
|
+
await app.request('http://x/ctgrde/api/gated/my-slug');
|
|
59
59
|
expect(seen).toEqual([undefined, '/api/gated/my-slug', '/api/gated/my-slug']);
|
|
60
60
|
});
|
|
61
61
|
|
|
62
62
|
test('maps a ctgr GET-twin back to its real verb + path', async () => {
|
|
63
63
|
const label = await labelFor(
|
|
64
|
-
(app) => app.get('/api/
|
|
65
|
-
new Request('http://x/api/
|
|
64
|
+
(app) => app.get('/ctgrpa/api/notes/:id', probe),
|
|
65
|
+
new Request('http://x/ctgrpa/api/notes/n1'),
|
|
66
66
|
);
|
|
67
|
-
// /
|
|
67
|
+
// /ctgrpa = PATCH twin → aggregates with the real PATCH /api/notes/:id
|
|
68
68
|
expect(label).toBe('PATCH /api/notes/:id');
|
|
69
69
|
});
|
|
70
|
+
|
|
71
|
+
test.each([
|
|
72
|
+
['/ctgrpo/api/notes', 'POST /api/notes'],
|
|
73
|
+
['/ctgrpu/api/notes/42', 'PUT /api/notes/:id'],
|
|
74
|
+
['/ctgrde/api/notes/42', 'DELETE /api/notes/:id'],
|
|
75
|
+
])('a twin the tunnel catches (%s) counts as the verb the client sent, with its own path collapsed', async (twin, want) => {
|
|
76
|
+
const label = await labelFor((app) => app.get('/:ctgrTwin{ctgr.+}', probe), new Request(`http://x${twin}`));
|
|
77
|
+
expect(label).toBe(want);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test('only core\'s verbs make a twin: /ctgrzz/ and a twin buried mid-path stay the GET they are', async () => {
|
|
81
|
+
const zz = await labelFor((app) => app.get('*', probe), new Request('http://x/ctgrzz/api/notes'));
|
|
82
|
+
expect(zz).toBe('GET /ctgrzz/api/notes');
|
|
83
|
+
const buried = await labelFor((app) => app.get('*', probe), new Request('http://x/api/ctgrpo/notes'));
|
|
84
|
+
expect(buried).toBe('GET /api/ctgrpo/notes');
|
|
85
|
+
});
|
|
70
86
|
});
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ABBREV_TO_METHOD, type CtgrAbbrev, ctgrTwinTarget } from 'cursedbelt-core/ctgr/types';
|
|
1
2
|
import type { Context } from 'hono';
|
|
2
3
|
|
|
3
4
|
/**
|
|
@@ -15,10 +16,6 @@ import type { Context } from 'hono';
|
|
|
15
16
|
const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
16
17
|
const LONG_HEX_RE = /^[0-9a-f]{16,}$/i;
|
|
17
18
|
|
|
18
|
-
// The ctgr twin URL is `/ctgr<last-2-of-method><url>`, so the two-letter suffix
|
|
19
|
-
// recovers the original verb (POST→st, PUT→ut, PATCH→ch, DELETE→te).
|
|
20
|
-
const CTGR_METHOD: Record<string, string> = { st: 'POST', ut: 'PUT', ch: 'PATCH', te: 'DELETE' };
|
|
21
|
-
|
|
22
19
|
function collapseSegment(seg: string): string {
|
|
23
20
|
if (!seg) return seg;
|
|
24
21
|
if (/^\d+$/.test(seg)) return ':id';
|
|
@@ -64,13 +61,16 @@ export function normalizeRoute(c: Context): NormalizedRoute {
|
|
|
64
61
|
let route = pattern ?? (collapsePath(path) || '/');
|
|
65
62
|
let unmatchedPath = pattern ? undefined : path;
|
|
66
63
|
|
|
67
|
-
// Map a ctgr GET-twin back to its real verb + url:
|
|
68
|
-
//
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
64
|
+
// Map a ctgr GET-twin back to its real verb + url: `/ctgrpa/api/notes/n1` → PATCH /api/notes/:id.
|
|
65
|
+
// Which paths are twins, and which verb each stands for, is core's one answer (forge task 5162) —
|
|
66
|
+
// this file's own table once held abbreviations no client sends, so every twin counted as a GET.
|
|
67
|
+
const twin = ctgrTwinTarget(path);
|
|
68
|
+
if (twin !== null) {
|
|
69
|
+
method = ABBREV_TO_METHOD[path.slice(5, 7) as CtgrAbbrev];
|
|
70
|
+
// A route registered at the twin itself is still a real pattern; the tunnel's catch-all is not.
|
|
71
|
+
const realPattern = pattern === undefined ? null : ctgrTwinTarget(pattern);
|
|
72
|
+
route = realPattern ?? (collapsePath(twin) || '/');
|
|
73
|
+
unmatchedPath = realPattern === null ? twin : undefined;
|
|
74
74
|
}
|
|
75
75
|
|
|
76
76
|
return { method, route, ...(unmatchedPath === undefined ? {} : { unmatchedPath }) };
|