cursedbelt-server 4.71.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/ctgrTunnel.d.ts +1 -1
- package/dist/server/ctgrTunnel.js +12 -59
- package/dist/server/ctgrTwin.d.ts +8 -0
- package/dist/server/ctgrTwin.js +79 -0
- package/dist/server/master-lock/accountsPage.js +3 -7
- package/dist/server/master-lock/guard.js +42 -24
- package/dist/server/master-lock/lockPage.d.ts +12 -0
- package/dist/server/master-lock/lockPage.js +66 -15
- package/dist/server/metrics/normalizeRoute.js +11 -10
- package/package.json +3 -3
- package/src/server/ctgrTunnel.spec.ts +12 -0
- package/src/server/ctgrTunnel.ts +12 -71
- package/src/server/ctgrTwin.ts +98 -0
- package/src/server/events/actions.spec.ts +6 -6
- package/src/server/master-lock/accountsPage.ts +3 -7
- package/src/server/master-lock/ctgrLockTwin.spec.ts +164 -0
- package/src/server/master-lock/guard.ts +42 -22
- package/src/server/master-lock/lockPage.ts +67 -15
- package/src/server/metrics/normalizeRoute.spec.ts +22 -6
- package/src/server/metrics/normalizeRoute.ts +11 -11
- package/src/server/middleware/requestLogger.spec.ts +95 -0
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { Database } from 'bun:sqlite';
|
|
2
|
+
import { describe, expect, test } from 'bun:test';
|
|
3
|
+
import { encodeRequest } from 'cursedbelt-core/ctgr';
|
|
4
|
+
import { Hono } from 'hono';
|
|
5
|
+
import type { CursedbeltEnv } from '../context.js';
|
|
6
|
+
import { mountCtgrTunnel } from '../ctgrTunnel.js';
|
|
7
|
+
import { ApiError } from '../errors.js';
|
|
8
|
+
import type { TelemetryEvent, TelemetrySink } from '../metrics/telemetrySink.js';
|
|
9
|
+
import { correlationId } from './correlationId.js';
|
|
10
|
+
import { errorEnvelope } from './errorEnvelope.js';
|
|
11
|
+
import { requestLogger } from './requestLogger.js';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 🔴 A GET-only write carries its whole body in its URL (forge task 5157): a `/ctgr<xx>` twin's
|
|
15
|
+
* `_d=` and `_c*` params are the payload — vault's are its ciphertext and unlock verifier. The
|
|
16
|
+
* request log is backed up, so what it keeps must be the route and never the query. Every row this
|
|
17
|
+
* logger can write — the telemetry event and both kinds of `event_logs` row — is checked here for
|
|
18
|
+
* the twin's own query text.
|
|
19
|
+
*/
|
|
20
|
+
const ORIGIN = 'http://localhost';
|
|
21
|
+
|
|
22
|
+
function setup() {
|
|
23
|
+
const db = new Database(':memory:');
|
|
24
|
+
db.run(
|
|
25
|
+
`CREATE TABLE event_logs (id TEXT PRIMARY KEY, correlation_id TEXT, event_name TEXT NOT NULL,
|
|
26
|
+
type TEXT NOT NULL DEFAULT 'info', message TEXT, details TEXT, duration_ms REAL, user_id TEXT,
|
|
27
|
+
created_at TEXT NOT NULL)`,
|
|
28
|
+
);
|
|
29
|
+
const recorded: TelemetryEvent[] = [];
|
|
30
|
+
const sink: TelemetrySink = {
|
|
31
|
+
record: (event) => {
|
|
32
|
+
recorded.push(event);
|
|
33
|
+
},
|
|
34
|
+
flush: () => {},
|
|
35
|
+
stop: () => {},
|
|
36
|
+
size: 0,
|
|
37
|
+
};
|
|
38
|
+
const app = new Hono<CursedbeltEnv>().use('*', correlationId());
|
|
39
|
+
// slowMs 0: every request also writes an `event_logs` row, so the slow path is checked too.
|
|
40
|
+
app.use('*', requestLogger(db, { sink, slowMs: 0 }));
|
|
41
|
+
mountCtgrTunnel(app);
|
|
42
|
+
app.post('/api/vault/items', async (c) => c.json({ ok: true, got: await c.req.json() }));
|
|
43
|
+
app.post('/api/vault/refuse', () => {
|
|
44
|
+
throw new ApiError(400, 'refused');
|
|
45
|
+
});
|
|
46
|
+
app.onError(errorEnvelope);
|
|
47
|
+
const stored = () =>
|
|
48
|
+
[
|
|
49
|
+
...recorded.map((e) => JSON.stringify(e)),
|
|
50
|
+
...(db.query('SELECT * FROM event_logs').all() as Record<string, unknown>[]).map((r) => JSON.stringify(r)),
|
|
51
|
+
].join('\n');
|
|
52
|
+
return { app, recorded, stored };
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** A real twin, as `installCtgrFetch` would send it, and the query text that must never be kept. */
|
|
56
|
+
async function twin(path: string, body: unknown): Promise<{ url: string; secrets: string[] }> {
|
|
57
|
+
const { chunks } = await encodeRequest('POST', path, body);
|
|
58
|
+
const url = chunks[0].url;
|
|
59
|
+
const params = new URL(ORIGIN + url).searchParams;
|
|
60
|
+
const d = params.get('_d');
|
|
61
|
+
expect(d && d.length > 16).toBe(true);
|
|
62
|
+
return { url, secrets: [d as string, '_d=', '?_', url.slice(url.indexOf('?'))] };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
describe('requestLogger: a ctgr twin\'s body never reaches a stored row (forge task 5157)', () => {
|
|
66
|
+
test('a delivered twin is recorded by its route, without its query', async () => {
|
|
67
|
+
const { app, recorded, stored } = setup();
|
|
68
|
+
const { url, secrets } = await twin('/api/vault/items', { ciphertext: 'c'.repeat(64), verifier: 'v'.repeat(64) });
|
|
69
|
+
const res = await app.fetch(new Request(ORIGIN + url, { headers: { 'sec-fetch-site': 'same-origin' } }));
|
|
70
|
+
expect(res.status).toBe(200);
|
|
71
|
+
expect(recorded.some((e) => e.method === 'POST' && e.route === '/api/vault/items')).toBe(true);
|
|
72
|
+
const kept = stored();
|
|
73
|
+
for (const s of secrets) expect(kept).not.toContain(s);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test('a refused twin\'s error row carries no query either', async () => {
|
|
77
|
+
const { app, recorded, stored } = setup();
|
|
78
|
+
const { url, secrets } = await twin('/api/vault/refuse', { verifier: 'v'.repeat(64) });
|
|
79
|
+
const res = await app.fetch(new Request(ORIGIN + url));
|
|
80
|
+
expect(res.status).toBe(400);
|
|
81
|
+
expect(recorded.some((e) => e.status === 400)).toBe(true);
|
|
82
|
+
const kept = stored();
|
|
83
|
+
expect(kept).toContain('http_error');
|
|
84
|
+
for (const s of secrets) expect(kept).not.toContain(s);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test('a twin no route matches (a 404) is recorded unplaced, without its query', async () => {
|
|
88
|
+
const { app, stored } = setup();
|
|
89
|
+
const { url, secrets } = await twin('/api/vault/no-such-route', { verifier: 'v'.repeat(64) });
|
|
90
|
+
const res = await app.fetch(new Request(ORIGIN + url));
|
|
91
|
+
expect(res.status).toBe(404);
|
|
92
|
+
const kept = stored();
|
|
93
|
+
for (const s of secrets) expect(kept).not.toContain(s);
|
|
94
|
+
});
|
|
95
|
+
});
|