cursedbelt-server 4.1.0 → 4.3.0
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/bench/assert.d.ts +16 -3
- package/dist/server/bench/assert.js +54 -0
- package/dist/server/bench/budget.d.ts +35 -1
- package/dist/server/bench/budget.js +35 -1
- package/dist/server/bench/cpuClock.js +21 -1
- package/dist/server/bench/index.d.ts +1 -1
- package/dist/server/bench/index.js +1 -1
- package/dist/server/d1/fakeD1.d.ts +5 -0
- package/dist/server/d1/fakeD1.js +51 -18
- package/dist/server/d1/index.d.ts +1 -0
- package/dist/server/d1/index.js +1 -0
- package/dist/server/d1/invocation.d.ts +72 -0
- package/dist/server/d1/invocation.js +166 -0
- package/dist/server/d1/local.js +48 -3
- package/dist/server/d1/values.d.ts +19 -2
- package/dist/server/d1/values.js +21 -2
- package/dist/server/middleware/bodyLimit.d.ts +152 -0
- package/dist/server/middleware/bodyLimit.js +161 -0
- package/package.json +8 -2
- package/src/leafSubpathsImportNothing.spec.ts +13 -0
- package/src/server/bench/assert.ts +78 -3
- package/src/server/bench/budget.spec.ts +27 -0
- package/src/server/bench/budget.ts +36 -1
- package/src/server/bench/cpuBudget.spec.ts +101 -1
- package/src/server/bench/cpuClock.ts +22 -1
- package/src/server/bench/index.ts +1 -0
- package/src/server/d1/fakeD1.ts +56 -18
- package/src/server/d1/index.ts +1 -0
- package/src/server/d1/invocation.spec.ts +174 -0
- package/src/server/d1/invocation.ts +207 -0
- package/src/server/d1/local.ts +56 -3
- package/src/server/d1/sameShape.spec.ts +73 -0
- package/src/server/d1/values.ts +23 -2
- package/src/server/middleware/bodyLimit.spec.ts +238 -0
- package/src/server/middleware/bodyLimit.ts +210 -0
package/dist/server/d1/local.js
CHANGED
|
@@ -15,9 +15,16 @@
|
|
|
15
15
|
*/
|
|
16
16
|
import { assertBatchSize, assertWithinLimits } from './limits';
|
|
17
17
|
import { D1UnsupportedError, } from './types';
|
|
18
|
-
import { makeMeta, normalizeBinds, normalizeRows, normalizeValue } from './values';
|
|
18
|
+
import { isPureReadSql, makeMeta, normalizeBinds, normalizeRows, normalizeValue } from './values';
|
|
19
19
|
/** Shared clock, so `meta.duration` means the same thing on both sides of the seam. */
|
|
20
20
|
const now = () => performance.now();
|
|
21
|
+
/**
|
|
22
|
+
* Rows changed on this CONNECTION since it was opened — monotonic, so a difference across
|
|
23
|
+
* one statement is that statement's own count. Used only where `.run()` cannot report it.
|
|
24
|
+
*/
|
|
25
|
+
const totalChanges = (db) => db.query('SELECT total_changes() AS n').get().n;
|
|
26
|
+
/** The connection's last inserted rowid, which is what D1 reports in `meta.last_row_id`. */
|
|
27
|
+
const lastInsertRowid = (db) => db.query('SELECT last_insert_rowid() AS r').get().r;
|
|
21
28
|
class LocalStatement {
|
|
22
29
|
db;
|
|
23
30
|
sql;
|
|
@@ -37,11 +44,49 @@ class LocalStatement {
|
|
|
37
44
|
}
|
|
38
45
|
allSync() {
|
|
39
46
|
const started = now();
|
|
40
|
-
const
|
|
47
|
+
const stmt = this.stmt();
|
|
48
|
+
// 🔴 A statement with NO result columns cannot return rows, so `.run()` is both the
|
|
49
|
+
// cheaper path and the only one that reports `changes` at all — `.all()` does not.
|
|
50
|
+
// `columnNames` is `bun:sqlite`'s own answer and is exact: `[]` for INSERT/UPDATE/
|
|
51
|
+
// DELETE, populated for SELECT and for anything carrying RETURNING (probed 2026-09-17).
|
|
52
|
+
if (stmt.columnNames.length === 0) {
|
|
53
|
+
const res = stmt.run(...this.params);
|
|
54
|
+
return {
|
|
55
|
+
results: [],
|
|
56
|
+
success: true,
|
|
57
|
+
meta: makeMeta({
|
|
58
|
+
changes: res.changes,
|
|
59
|
+
last_row_id: res.lastInsertRowid,
|
|
60
|
+
duration: now() - started,
|
|
61
|
+
}),
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
const rowsOf = () => this.stmt().all(...this.params);
|
|
65
|
+
// A pure read changed nothing, and its counters are the PREVIOUS statement's — see
|
|
66
|
+
// `isPureReadSql`. Reporting zero is the measurement, not a default.
|
|
67
|
+
if (isPureReadSql(this.sql)) {
|
|
68
|
+
const rows = rowsOf();
|
|
69
|
+
return {
|
|
70
|
+
results: normalizeRows(rows),
|
|
71
|
+
success: true,
|
|
72
|
+
meta: makeMeta({ duration: now() - started }),
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
// Returns rows AND may write: `INSERT … RETURNING`, `WITH … UPDATE … RETURNING`.
|
|
76
|
+
// `total_changes()` is cumulative for the connection, so the DIFFERENCE across the
|
|
77
|
+
// statement is this statement's own count — the one reading that stays correct when
|
|
78
|
+
// the statement is neither a plain read nor a plain write.
|
|
79
|
+
const before = totalChanges(this.db);
|
|
80
|
+
const rows = rowsOf();
|
|
81
|
+
const changes = totalChanges(this.db) - before;
|
|
41
82
|
return {
|
|
42
83
|
results: normalizeRows(rows),
|
|
43
84
|
success: true,
|
|
44
|
-
meta: makeMeta({
|
|
85
|
+
meta: makeMeta({
|
|
86
|
+
changes,
|
|
87
|
+
last_row_id: changes > 0 ? lastInsertRowid(this.db) : null,
|
|
88
|
+
duration: now() - started,
|
|
89
|
+
}),
|
|
45
90
|
};
|
|
46
91
|
}
|
|
47
92
|
async first(column) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The one place that decides what a bound parameter
|
|
3
|
-
* both drivers.
|
|
2
|
+
* The one place that decides what a bound parameter, a returned column and a statement's
|
|
3
|
+
* own `meta` MEAN, shared by both drivers.
|
|
4
4
|
*
|
|
5
5
|
* 🔴 **Both drivers call these, and that is why the identical-shapes test can pass.** If
|
|
6
6
|
* the local driver normalized its own way and the remote driver normalized its own way,
|
|
@@ -23,6 +23,23 @@ import { type D1LikeBindable, type D1LikeMeta, type D1LikeValue } from './types'
|
|
|
23
23
|
export declare function normalizeBind(value: unknown, index: number): D1LikeBindable;
|
|
24
24
|
/** Normalize a whole parameter list, reporting the offending position on failure. */
|
|
25
25
|
export declare const normalizeBinds: (values: readonly unknown[]) => D1LikeBindable[];
|
|
26
|
+
/**
|
|
27
|
+
* Is this statement a PURE READ — one that cannot possibly have changed a row?
|
|
28
|
+
*
|
|
29
|
+
* 🔴 Measured 2026-09-17, and this is why the question has to be asked at all:
|
|
30
|
+
* SQLite's `changes()` and `last_insert_rowid()` are **connection-wide and sticky**.
|
|
31
|
+
* After a plain `SELECT` they still read the counters left by the last write — probed
|
|
32
|
+
* here, a `SELECT` following a 1-row insert reports `changes = 1`. So those counters may
|
|
33
|
+
* only ever be attributed to a statement that actually wrote something, and a driver
|
|
34
|
+
* that reads them after a read invents a `changes` out of the previous statement's.
|
|
35
|
+
*
|
|
36
|
+
* Only a leading `SELECT` is claimed, because that is the one keyword that is
|
|
37
|
+
* unambiguous: `WITH … INSERT … RETURNING` and `INSERT … RETURNING` both return rows AND
|
|
38
|
+
* write. Anything this returns `false` for is measured exactly instead (see
|
|
39
|
+
* `local.ts`), so a statement shape this does not recognise costs two extra queries —
|
|
40
|
+
* never a wrong number.
|
|
41
|
+
*/
|
|
42
|
+
export declare const isPureReadSql: (sql: string) => boolean;
|
|
26
43
|
/**
|
|
27
44
|
* Normalize one column value coming BACK from a driver.
|
|
28
45
|
*
|
package/dist/server/d1/values.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The one place that decides what a bound parameter
|
|
3
|
-
* both drivers.
|
|
2
|
+
* The one place that decides what a bound parameter, a returned column and a statement's
|
|
3
|
+
* own `meta` MEAN, shared by both drivers.
|
|
4
4
|
*
|
|
5
5
|
* 🔴 **Both drivers call these, and that is why the identical-shapes test can pass.** If
|
|
6
6
|
* the local driver normalized its own way and the remote driver normalized its own way,
|
|
@@ -69,6 +69,25 @@ export function normalizeBind(value, index) {
|
|
|
69
69
|
}
|
|
70
70
|
/** Normalize a whole parameter list, reporting the offending position on failure. */
|
|
71
71
|
export const normalizeBinds = (values) => values.map((v, i) => normalizeBind(v, i));
|
|
72
|
+
/** Leading whitespace and SQL comments, which sit in front of the real first keyword. */
|
|
73
|
+
const LEADING_NOISE = /^(?:\s|--[^\n]*\n?|\/\*[\s\S]*?\*\/)+/;
|
|
74
|
+
/**
|
|
75
|
+
* Is this statement a PURE READ — one that cannot possibly have changed a row?
|
|
76
|
+
*
|
|
77
|
+
* 🔴 Measured 2026-09-17, and this is why the question has to be asked at all:
|
|
78
|
+
* SQLite's `changes()` and `last_insert_rowid()` are **connection-wide and sticky**.
|
|
79
|
+
* After a plain `SELECT` they still read the counters left by the last write — probed
|
|
80
|
+
* here, a `SELECT` following a 1-row insert reports `changes = 1`. So those counters may
|
|
81
|
+
* only ever be attributed to a statement that actually wrote something, and a driver
|
|
82
|
+
* that reads them after a read invents a `changes` out of the previous statement's.
|
|
83
|
+
*
|
|
84
|
+
* Only a leading `SELECT` is claimed, because that is the one keyword that is
|
|
85
|
+
* unambiguous: `WITH … INSERT … RETURNING` and `INSERT … RETURNING` both return rows AND
|
|
86
|
+
* write. Anything this returns `false` for is measured exactly instead (see
|
|
87
|
+
* `local.ts`), so a statement shape this does not recognise costs two extra queries —
|
|
88
|
+
* never a wrong number.
|
|
89
|
+
*/
|
|
90
|
+
export const isPureReadSql = (sql) => /^select\b/i.test(sql.replace(LEADING_NOISE, ''));
|
|
72
91
|
/**
|
|
73
92
|
* Normalize one column value coming BACK from a driver.
|
|
74
93
|
*
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reading a request body without first agreeing how big it may be.
|
|
3
|
+
*
|
|
4
|
+
* ── Why this is one function rather than one per app (2026-07-27) ────────────
|
|
5
|
+
* The two apps behind the shared owner-auth were inconsistently hardened, in a
|
|
6
|
+
* way an adversarial review found and neither test suite could:
|
|
7
|
+
*
|
|
8
|
+
* - the companion app (since folded into `apps/orch`) read `c.req.text()`,
|
|
9
|
+
* checked the length, and returned 413 BEFORE parsing — correct.
|
|
10
|
+
* - `apps/roms` called `await c.req.json()` first and checked the size deep
|
|
11
|
+
* inside the save-state store — i.e. it buffered and parsed an arbitrary body
|
|
12
|
+
* into a process whose unit caps at 300 MB of memory before forming an
|
|
13
|
+
* opinion about its size.
|
|
14
|
+
*
|
|
15
|
+
* Both went through {@link readBoundedJson} after that. The guard is one
|
|
16
|
+
* function so the posture cannot diverge again, and it is deliberately the
|
|
17
|
+
* *last* line of defense: a proxy's `client_max_body_size` refuses an oversized
|
|
18
|
+
* body at the edge, and this refuses it if the proxy is absent, misconfigured,
|
|
19
|
+
* or bypassed — which is exactly the case in dev, in `preview:prod`, and for any
|
|
20
|
+
* request that reaches the loopback port directly.
|
|
21
|
+
*
|
|
22
|
+
* ── Where it came from (2026-09-17) ──────────────────────────────────────────
|
|
23
|
+
* Five apps — `roms`, `family`, `music`, `vault`, `collections` — carried this
|
|
24
|
+
* file byte-for-byte identical (`b36d0eedee7a`), which is the posture diverging
|
|
25
|
+
* again, only more slowly and with nothing watching: a repo's gate proves that
|
|
26
|
+
* repo, so five green gates is what five copies of a security guard look like
|
|
27
|
+
* from the outside. Confirmed identical with `shasum -a256` before the move, so
|
|
28
|
+
* there was no fork to reconcile and nothing was dropped.
|
|
29
|
+
*
|
|
30
|
+
* 🔴 It is published as the LEAF subpath `cursedbelt-server/body-limit`, never
|
|
31
|
+
* through the `./middleware` barrel — that barrel drags `hono` and the error
|
|
32
|
+
* envelope, and this module's whole promise is that it imports NOTHING. The
|
|
33
|
+
* source is a `{ req: { text() } }` / `{ req: { arrayBuffer() } }` /
|
|
34
|
+
* `{ req: { header() } }` structural type rather than a Hono context, so a
|
|
35
|
+
* caller needs no framework to use it and a test needs none to exercise it.
|
|
36
|
+
* `src/leafSubpathsImportNothing.spec.ts` is what holds that promise.
|
|
37
|
+
*/
|
|
38
|
+
/** What {@link readBoundedBytes} gives back. */
|
|
39
|
+
export type BoundedBytes = {
|
|
40
|
+
ok: true;
|
|
41
|
+
value: Uint8Array;
|
|
42
|
+
} | {
|
|
43
|
+
ok: false;
|
|
44
|
+
status: 413 | 400;
|
|
45
|
+
error: string;
|
|
46
|
+
};
|
|
47
|
+
/** The minimum a route needs to hand over a raw body. */
|
|
48
|
+
export interface BodyBytesSource {
|
|
49
|
+
req: {
|
|
50
|
+
arrayBuffer(): Promise<ArrayBuffer>;
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
/** What {@link readBoundedJson} gives back — a discriminated result, never a throw. */
|
|
54
|
+
export type BoundedJson<T> = {
|
|
55
|
+
ok: true;
|
|
56
|
+
value: T;
|
|
57
|
+
} | {
|
|
58
|
+
ok: false;
|
|
59
|
+
status: 413;
|
|
60
|
+
error: string;
|
|
61
|
+
} | {
|
|
62
|
+
ok: false;
|
|
63
|
+
status: 400;
|
|
64
|
+
error: string;
|
|
65
|
+
};
|
|
66
|
+
export interface BoundedJsonOptions {
|
|
67
|
+
/** Hard ceiling on the raw body, in bytes. */
|
|
68
|
+
maxBytes: number;
|
|
69
|
+
/** What to call the payload in the error message (e.g. "save state"). */
|
|
70
|
+
label?: string;
|
|
71
|
+
}
|
|
72
|
+
/** The minimum a route needs from Hono's context — kept structural so tests need no Hono. */
|
|
73
|
+
export interface BodyTextSource {
|
|
74
|
+
req: {
|
|
75
|
+
text(): Promise<string>;
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
/** What {@link readBoundedText} gives back. Same shape, minus the parse failure. */
|
|
79
|
+
export type BoundedText = {
|
|
80
|
+
ok: true;
|
|
81
|
+
value: string;
|
|
82
|
+
} | {
|
|
83
|
+
ok: false;
|
|
84
|
+
status: 413 | 400;
|
|
85
|
+
error: string;
|
|
86
|
+
};
|
|
87
|
+
/**
|
|
88
|
+
* Read a body as TEXT, refusing anything over `maxBytes`.
|
|
89
|
+
*
|
|
90
|
+
* The sibling of {@link readBoundedJson}, for a payload that IS the text rather
|
|
91
|
+
* than a document containing it — `apps/patterns`' chunked sends put their
|
|
92
|
+
* metadata in the query string and the chunk in the raw body, which spares a
|
|
93
|
+
* megabyte-scale caller from JSON-escaping every piece and spares this process
|
|
94
|
+
* from parsing what it is only going to concatenate.
|
|
95
|
+
*
|
|
96
|
+
* Same byte-not-character measurement, and for the same reason: it is the number
|
|
97
|
+
* the proxy in front of this is enforcing.
|
|
98
|
+
*/
|
|
99
|
+
export declare function readBoundedText(c: BodyTextSource, options: BoundedJsonOptions): Promise<BoundedText>;
|
|
100
|
+
/**
|
|
101
|
+
* Read a body as RAW BYTES, refusing anything over `maxBytes`.
|
|
102
|
+
*
|
|
103
|
+
* The third member of the family, for a payload that is neither JSON nor text:
|
|
104
|
+
* roms' ROM-upload chunks are cartridge bytes, and routing them through
|
|
105
|
+
* {@link readBoundedText} would decode arbitrary binary as UTF-8 — which is
|
|
106
|
+
* lossy (every invalid sequence becomes U+FFFD) and measures a size that is not
|
|
107
|
+
* the size the proxy in front is enforcing.
|
|
108
|
+
*
|
|
109
|
+
* A `Content-Length` claim is deliberately NOT trusted here: it is a header, and
|
|
110
|
+
* the guard has to hold against a body that disagrees with it. The measurement is
|
|
111
|
+
* the buffer that actually arrived.
|
|
112
|
+
*/
|
|
113
|
+
export declare function readBoundedBytes(c: BodyBytesSource, options: BoundedJsonOptions): Promise<BoundedBytes>;
|
|
114
|
+
/** The minimum {@link refuseOversizedBody} needs — a header lookup, nothing else. */
|
|
115
|
+
export interface BodyHeaderSource {
|
|
116
|
+
req: {
|
|
117
|
+
header(name: string): string | undefined;
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Refuse a body by its DECLARED `Content-Length`, without reading it.
|
|
122
|
+
*
|
|
123
|
+
* The fourth member of the family, and the only one for a body that cannot be
|
|
124
|
+
* buffered first: a `multipart/form-data` upload. The other three measure the
|
|
125
|
+
* bytes that actually arrived, which is strictly better — but they get to,
|
|
126
|
+
* because they buffer. `c.req.formData()` gives no such opportunity: by the time
|
|
127
|
+
* it resolves, the whole upload is already in a process whose unit caps at
|
|
128
|
+
* 300 MB of memory, which is the thing the limit exists to prevent.
|
|
129
|
+
*
|
|
130
|
+
* So this trusts the header, and the docblock says so plainly. A liar gets
|
|
131
|
+
* through to the buffering call — but a liar was always going to, and this still
|
|
132
|
+
* turns the honest 3 GB folder-drop (the actual failure shape) into a refusal
|
|
133
|
+
* the app can explain instead of an OOM or an nginx HTML 413 the app never sees.
|
|
134
|
+
* It is a companion to the proxy's `client_max_body_size`, never a replacement.
|
|
135
|
+
*
|
|
136
|
+
* A missing or unparseable header is NOT a refusal: chunked requests omit it
|
|
137
|
+
* legitimately, and refusing them would break a correct client to catch a
|
|
138
|
+
* dishonest one.
|
|
139
|
+
*/
|
|
140
|
+
export declare function refuseOversizedBody(c: BodyHeaderSource, options: BoundedJsonOptions): {
|
|
141
|
+
status: 413;
|
|
142
|
+
error: string;
|
|
143
|
+
} | null;
|
|
144
|
+
/**
|
|
145
|
+
* Read a JSON body, refusing anything over `maxBytes` before parsing it.
|
|
146
|
+
*
|
|
147
|
+
* The size is measured in BYTES, not characters. `String.length` counts UTF-16
|
|
148
|
+
* code units, so a body of 4-byte emoji measures half its true size that way —
|
|
149
|
+
* which would let a caller past a byte limit the proxy is enforcing in real
|
|
150
|
+
* bytes, and produce the mismatch this module exists to close.
|
|
151
|
+
*/
|
|
152
|
+
export declare function readBoundedJson<T = unknown>(c: BodyTextSource, options: BoundedJsonOptions): Promise<BoundedJson<T>>;
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reading a request body without first agreeing how big it may be.
|
|
3
|
+
*
|
|
4
|
+
* ── Why this is one function rather than one per app (2026-07-27) ────────────
|
|
5
|
+
* The two apps behind the shared owner-auth were inconsistently hardened, in a
|
|
6
|
+
* way an adversarial review found and neither test suite could:
|
|
7
|
+
*
|
|
8
|
+
* - the companion app (since folded into `apps/orch`) read `c.req.text()`,
|
|
9
|
+
* checked the length, and returned 413 BEFORE parsing — correct.
|
|
10
|
+
* - `apps/roms` called `await c.req.json()` first and checked the size deep
|
|
11
|
+
* inside the save-state store — i.e. it buffered and parsed an arbitrary body
|
|
12
|
+
* into a process whose unit caps at 300 MB of memory before forming an
|
|
13
|
+
* opinion about its size.
|
|
14
|
+
*
|
|
15
|
+
* Both went through {@link readBoundedJson} after that. The guard is one
|
|
16
|
+
* function so the posture cannot diverge again, and it is deliberately the
|
|
17
|
+
* *last* line of defense: a proxy's `client_max_body_size` refuses an oversized
|
|
18
|
+
* body at the edge, and this refuses it if the proxy is absent, misconfigured,
|
|
19
|
+
* or bypassed — which is exactly the case in dev, in `preview:prod`, and for any
|
|
20
|
+
* request that reaches the loopback port directly.
|
|
21
|
+
*
|
|
22
|
+
* ── Where it came from (2026-09-17) ──────────────────────────────────────────
|
|
23
|
+
* Five apps — `roms`, `family`, `music`, `vault`, `collections` — carried this
|
|
24
|
+
* file byte-for-byte identical (`b36d0eedee7a`), which is the posture diverging
|
|
25
|
+
* again, only more slowly and with nothing watching: a repo's gate proves that
|
|
26
|
+
* repo, so five green gates is what five copies of a security guard look like
|
|
27
|
+
* from the outside. Confirmed identical with `shasum -a256` before the move, so
|
|
28
|
+
* there was no fork to reconcile and nothing was dropped.
|
|
29
|
+
*
|
|
30
|
+
* 🔴 It is published as the LEAF subpath `cursedbelt-server/body-limit`, never
|
|
31
|
+
* through the `./middleware` barrel — that barrel drags `hono` and the error
|
|
32
|
+
* envelope, and this module's whole promise is that it imports NOTHING. The
|
|
33
|
+
* source is a `{ req: { text() } }` / `{ req: { arrayBuffer() } }` /
|
|
34
|
+
* `{ req: { header() } }` structural type rather than a Hono context, so a
|
|
35
|
+
* caller needs no framework to use it and a test needs none to exercise it.
|
|
36
|
+
* `src/leafSubpathsImportNothing.spec.ts` is what holds that promise.
|
|
37
|
+
*/
|
|
38
|
+
/**
|
|
39
|
+
* Read a body as TEXT, refusing anything over `maxBytes`.
|
|
40
|
+
*
|
|
41
|
+
* The sibling of {@link readBoundedJson}, for a payload that IS the text rather
|
|
42
|
+
* than a document containing it — `apps/patterns`' chunked sends put their
|
|
43
|
+
* metadata in the query string and the chunk in the raw body, which spares a
|
|
44
|
+
* megabyte-scale caller from JSON-escaping every piece and spares this process
|
|
45
|
+
* from parsing what it is only going to concatenate.
|
|
46
|
+
*
|
|
47
|
+
* Same byte-not-character measurement, and for the same reason: it is the number
|
|
48
|
+
* the proxy in front of this is enforcing.
|
|
49
|
+
*/
|
|
50
|
+
export async function readBoundedText(c, options) {
|
|
51
|
+
const { maxBytes, label = 'body' } = options;
|
|
52
|
+
let raw;
|
|
53
|
+
try {
|
|
54
|
+
raw = await c.req.text();
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
return { ok: false, status: 400, error: `${label} could not be read` };
|
|
58
|
+
}
|
|
59
|
+
const bytes = new TextEncoder().encode(raw).length;
|
|
60
|
+
if (bytes > maxBytes) {
|
|
61
|
+
return {
|
|
62
|
+
ok: false,
|
|
63
|
+
status: 413,
|
|
64
|
+
error: `${label} too large (${bytes} bytes; the limit is ${maxBytes})`,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
return { ok: true, value: raw };
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Read a body as RAW BYTES, refusing anything over `maxBytes`.
|
|
71
|
+
*
|
|
72
|
+
* The third member of the family, for a payload that is neither JSON nor text:
|
|
73
|
+
* roms' ROM-upload chunks are cartridge bytes, and routing them through
|
|
74
|
+
* {@link readBoundedText} would decode arbitrary binary as UTF-8 — which is
|
|
75
|
+
* lossy (every invalid sequence becomes U+FFFD) and measures a size that is not
|
|
76
|
+
* the size the proxy in front is enforcing.
|
|
77
|
+
*
|
|
78
|
+
* A `Content-Length` claim is deliberately NOT trusted here: it is a header, and
|
|
79
|
+
* the guard has to hold against a body that disagrees with it. The measurement is
|
|
80
|
+
* the buffer that actually arrived.
|
|
81
|
+
*/
|
|
82
|
+
export async function readBoundedBytes(c, options) {
|
|
83
|
+
const { maxBytes, label = 'body' } = options;
|
|
84
|
+
let buffer;
|
|
85
|
+
try {
|
|
86
|
+
buffer = await c.req.arrayBuffer();
|
|
87
|
+
}
|
|
88
|
+
catch {
|
|
89
|
+
return { ok: false, status: 400, error: `${label} could not be read` };
|
|
90
|
+
}
|
|
91
|
+
if (buffer.byteLength > maxBytes) {
|
|
92
|
+
return {
|
|
93
|
+
ok: false,
|
|
94
|
+
status: 413,
|
|
95
|
+
error: `${label} too large (${buffer.byteLength} bytes; the limit is ${maxBytes})`,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
return { ok: true, value: new Uint8Array(buffer) };
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Refuse a body by its DECLARED `Content-Length`, without reading it.
|
|
102
|
+
*
|
|
103
|
+
* The fourth member of the family, and the only one for a body that cannot be
|
|
104
|
+
* buffered first: a `multipart/form-data` upload. The other three measure the
|
|
105
|
+
* bytes that actually arrived, which is strictly better — but they get to,
|
|
106
|
+
* because they buffer. `c.req.formData()` gives no such opportunity: by the time
|
|
107
|
+
* it resolves, the whole upload is already in a process whose unit caps at
|
|
108
|
+
* 300 MB of memory, which is the thing the limit exists to prevent.
|
|
109
|
+
*
|
|
110
|
+
* So this trusts the header, and the docblock says so plainly. A liar gets
|
|
111
|
+
* through to the buffering call — but a liar was always going to, and this still
|
|
112
|
+
* turns the honest 3 GB folder-drop (the actual failure shape) into a refusal
|
|
113
|
+
* the app can explain instead of an OOM or an nginx HTML 413 the app never sees.
|
|
114
|
+
* It is a companion to the proxy's `client_max_body_size`, never a replacement.
|
|
115
|
+
*
|
|
116
|
+
* A missing or unparseable header is NOT a refusal: chunked requests omit it
|
|
117
|
+
* legitimately, and refusing them would break a correct client to catch a
|
|
118
|
+
* dishonest one.
|
|
119
|
+
*/
|
|
120
|
+
export function refuseOversizedBody(c, options) {
|
|
121
|
+
const { maxBytes, label = 'body' } = options;
|
|
122
|
+
const declared = Number(c.req.header('content-length'));
|
|
123
|
+
if (!Number.isFinite(declared) || declared <= maxBytes)
|
|
124
|
+
return null;
|
|
125
|
+
return {
|
|
126
|
+
status: 413,
|
|
127
|
+
error: `${label} too large (${declared} bytes; the limit is ${maxBytes})`,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Read a JSON body, refusing anything over `maxBytes` before parsing it.
|
|
132
|
+
*
|
|
133
|
+
* The size is measured in BYTES, not characters. `String.length` counts UTF-16
|
|
134
|
+
* code units, so a body of 4-byte emoji measures half its true size that way —
|
|
135
|
+
* which would let a caller past a byte limit the proxy is enforcing in real
|
|
136
|
+
* bytes, and produce the mismatch this module exists to close.
|
|
137
|
+
*/
|
|
138
|
+
export async function readBoundedJson(c, options) {
|
|
139
|
+
const { maxBytes, label = 'body' } = options;
|
|
140
|
+
let raw;
|
|
141
|
+
try {
|
|
142
|
+
raw = await c.req.text();
|
|
143
|
+
}
|
|
144
|
+
catch {
|
|
145
|
+
return { ok: false, status: 400, error: `${label} could not be read` };
|
|
146
|
+
}
|
|
147
|
+
const bytes = new TextEncoder().encode(raw).length;
|
|
148
|
+
if (bytes > maxBytes) {
|
|
149
|
+
return {
|
|
150
|
+
ok: false,
|
|
151
|
+
status: 413,
|
|
152
|
+
error: `${label} too large (${bytes} bytes; the limit is ${maxBytes})`,
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
try {
|
|
156
|
+
return { ok: true, value: JSON.parse(raw) };
|
|
157
|
+
}
|
|
158
|
+
catch {
|
|
159
|
+
return { ok: false, status: 400, error: `${label} must be JSON` };
|
|
160
|
+
}
|
|
161
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cursedbelt-server",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.3.0",
|
|
4
4
|
"license": "ISC",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "The app-facing Bun/Hono server tier of the cursedbelt split \u2014 storage, sharing, activity, guard, sync. React-free; cursedbelt-core below it.",
|
|
@@ -66,6 +66,12 @@
|
|
|
66
66
|
"source": "./src/server/storage/binaryStoreFake.ts",
|
|
67
67
|
"import": "./dist/server/storage/binaryStoreFake.js"
|
|
68
68
|
},
|
|
69
|
+
"./body-limit": {
|
|
70
|
+
"types": "./dist/server/middleware/bodyLimit.d.ts",
|
|
71
|
+
"bun": "./src/server/middleware/bodyLimit.ts",
|
|
72
|
+
"source": "./src/server/middleware/bodyLimit.ts",
|
|
73
|
+
"import": "./dist/server/middleware/bodyLimit.js"
|
|
74
|
+
},
|
|
69
75
|
"./d1": {
|
|
70
76
|
"types": "./dist/server/d1/index.d.ts",
|
|
71
77
|
"bun": "./src/server/d1/index.ts",
|
|
@@ -252,7 +258,7 @@
|
|
|
252
258
|
"@node-rs/argon2": "^2.0.2",
|
|
253
259
|
"@types/bun": "^1.3.14",
|
|
254
260
|
"@types/node": "^24",
|
|
255
|
-
"cursedbelt": "^
|
|
261
|
+
"cursedbelt": "^4.1.1",
|
|
256
262
|
"hono": "4.12.28",
|
|
257
263
|
"kysely": "^0.28.17",
|
|
258
264
|
"kysely-bun-sqlite": "^0.4.0",
|
|
@@ -138,6 +138,19 @@ const LEAVES = [
|
|
|
138
138
|
*/
|
|
139
139
|
evaluates: 'binaryStoreFakeDefaults',
|
|
140
140
|
},
|
|
141
|
+
{
|
|
142
|
+
subpath: './body-limit',
|
|
143
|
+
/**
|
|
144
|
+
* 🔴 The last line of defense on an oversized request body, and the leaf with the
|
|
145
|
+
* strictest promise of the lot: it imports NOTHING — not `hono`, not the error
|
|
146
|
+
* envelope, not a sibling in `src/server/middleware/`. It takes its request as a
|
|
147
|
+
* structural `{ req: { text() } }` source precisely so that neither a consumer nor a
|
|
148
|
+
* test needs a framework, which is what made it an ADDITION rather than a port when it
|
|
149
|
+
* arrived in 4.2.0 from five byte-identical app-side copies. Reaching it through the
|
|
150
|
+
* `./middleware` barrel would undo all of that in one import.
|
|
151
|
+
*/
|
|
152
|
+
evaluates: 'readBoundedJson',
|
|
153
|
+
},
|
|
141
154
|
] as const;
|
|
142
155
|
|
|
143
156
|
/**
|
|
@@ -19,7 +19,11 @@ export type CpuViolationKind =
|
|
|
19
19
|
/** An exempt route crossed its own `noticeAboveMs`. Never fails a build. */
|
|
20
20
|
| 'exempt-notice'
|
|
21
21
|
/** The clock could not measure at all. */
|
|
22
|
-
| 'unmeasurable'
|
|
22
|
+
| 'unmeasurable'
|
|
23
|
+
/** The clock claimed to work and the report holds no route at all. */
|
|
24
|
+
| 'no-samples'
|
|
25
|
+
/** Every reading on every route was exactly zero — a reader that is not reading. */
|
|
26
|
+
| 'degenerate';
|
|
23
27
|
|
|
24
28
|
export interface CpuViolation {
|
|
25
29
|
kind: CpuViolationKind;
|
|
@@ -48,10 +52,19 @@ export interface AssertCpuBudgetsOpts {
|
|
|
48
52
|
*/
|
|
49
53
|
requireDeclared?: boolean;
|
|
50
54
|
/**
|
|
51
|
-
* Treat an unavailable clock
|
|
52
|
-
* because it measured nothing is worse than no gate.
|
|
55
|
+
* Treat an unavailable clock — or a report holding no route at all — as a pass. Off by
|
|
56
|
+
* default: a gate that goes green because it measured nothing is worse than no gate.
|
|
53
57
|
*/
|
|
54
58
|
allowUnmeasurable?: boolean;
|
|
59
|
+
/**
|
|
60
|
+
* Accept a report in which every reading on every route was exactly zero. Off by
|
|
61
|
+
* default.
|
|
62
|
+
*
|
|
63
|
+
* 🔴 The only reason to turn this on is a runtime whose CPU reader has whole-millisecond
|
|
64
|
+
* resolution and an app genuinely below it — and then the budget is not measuring
|
|
65
|
+
* anything either. Prefer a finer reader.
|
|
66
|
+
*/
|
|
67
|
+
allowZeroCpu?: boolean;
|
|
55
68
|
}
|
|
56
69
|
|
|
57
70
|
const fmt = (n: number): string => (Number.isFinite(n) ? n.toFixed(2) : 'n/a');
|
|
@@ -82,6 +95,68 @@ export function checkCpuBudgets(
|
|
|
82
95
|
];
|
|
83
96
|
}
|
|
84
97
|
|
|
98
|
+
// 🔴 A report with no route in it is a wiring failure wearing a pass. The loop below
|
|
99
|
+
// runs zero times and returns zero violations, so `cpuBudget()` left unmounted, a bench
|
|
100
|
+
// whose cases never reached the middleware, or a reader returning a non-number (every
|
|
101
|
+
// sample dropped as unmeasurable by `createCpuRecorder`) all assert GREEN. Measured
|
|
102
|
+
// 2026-09-17 against this file before this guard existed: all three did.
|
|
103
|
+
if (report.available && report.routes.length === 0 && !opts.allowUnmeasurable) {
|
|
104
|
+
return [
|
|
105
|
+
{
|
|
106
|
+
kind: 'no-samples',
|
|
107
|
+
key: '*',
|
|
108
|
+
method: '*',
|
|
109
|
+
route: '*',
|
|
110
|
+
p99: Number.NaN,
|
|
111
|
+
budgetMs: null,
|
|
112
|
+
samples: 0,
|
|
113
|
+
fails: true,
|
|
114
|
+
message:
|
|
115
|
+
`CPU budget: clock source '${report.source}' reported itself available but not ` +
|
|
116
|
+
'one route was recorded. Nothing was measured, so nothing was proven: check ' +
|
|
117
|
+
'that cpuBudget() is mounted on the app under test, that the bench cases reach ' +
|
|
118
|
+
'it, and that the CPU reader returns a finite number.',
|
|
119
|
+
},
|
|
120
|
+
];
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// 🔴 A reader that answers the same number every time is the failure `cpuClock.ts`'s
|
|
124
|
+
// header names: "a clock that reads zero for ever and a gate that can never go red".
|
|
125
|
+
// It is the more dangerous shape of the two above, because `workerCpuClock` stamps it
|
|
126
|
+
// `proxy: false` — so the report claims to be the real quantity Cloudflare bills while
|
|
127
|
+
// every route sits at 0.00 and every budget passes. Requiring EVERY route to be totally
|
|
128
|
+
// flat keeps this off a real measurement: one non-zero reading anywhere clears it.
|
|
129
|
+
const totalSamples = report.routes.reduce((n, r) => n + r.samples, 0);
|
|
130
|
+
const allFlatZero = report.routes.every(
|
|
131
|
+
(r) => r.retained > 0 && r.max === 0 && r.totalCpuMs === 0,
|
|
132
|
+
);
|
|
133
|
+
if (
|
|
134
|
+
report.available &&
|
|
135
|
+
report.routes.length > 0 &&
|
|
136
|
+
totalSamples >= minSamples &&
|
|
137
|
+
allFlatZero &&
|
|
138
|
+
!opts.allowZeroCpu
|
|
139
|
+
) {
|
|
140
|
+
return [
|
|
141
|
+
{
|
|
142
|
+
kind: 'degenerate',
|
|
143
|
+
key: '*',
|
|
144
|
+
method: '*',
|
|
145
|
+
route: '*',
|
|
146
|
+
p99: 0,
|
|
147
|
+
budgetMs: null,
|
|
148
|
+
samples: totalSamples,
|
|
149
|
+
fails: true,
|
|
150
|
+
message:
|
|
151
|
+
`CPU budget: every one of ${totalSamples} sample(s) across ` +
|
|
152
|
+
`${report.routes.length} route(s) read exactly 0 CPU-ms from source ` +
|
|
153
|
+
`'${report.source}'${report.proxy ? '' : ' (reported as NON-proxy)'}. A reader ` +
|
|
154
|
+
'that never moves cannot ever redden this gate. Wire a real CPU reader, or set ' +
|
|
155
|
+
'allowZeroCpu deliberately.',
|
|
156
|
+
},
|
|
157
|
+
];
|
|
158
|
+
}
|
|
159
|
+
|
|
85
160
|
for (const r of report.routes) {
|
|
86
161
|
const base: Omit<CpuViolation, 'kind' | 'message' | 'fails'> = {
|
|
87
162
|
key: r.key,
|
|
@@ -3,6 +3,7 @@ import {
|
|
|
3
3
|
DAYS_PER_MONTH,
|
|
4
4
|
DEFAULT_ROUTE_CPU_BUDGET_MS,
|
|
5
5
|
deriveCpuBudgetMs,
|
|
6
|
+
MEASURED_FLEET_REQUESTS_PER_DAY,
|
|
6
7
|
monthlyOverageUsd,
|
|
7
8
|
OWNER_PROJECTED_REQUESTS_PER_DAY,
|
|
8
9
|
resolveBudget,
|
|
@@ -35,6 +36,32 @@ describe('the derivation', () => {
|
|
|
35
36
|
it('refuses a zero/negative request volume rather than returning Infinity', () => {
|
|
36
37
|
expect(() => deriveCpuBudgetMs({ requestsPerMonth: 0 })).toThrow(/must be > 0/);
|
|
37
38
|
});
|
|
39
|
+
|
|
40
|
+
it('re-derives ~22.9 CPU-ms from the MEASURED fleet volume, not the projection', () => {
|
|
41
|
+
// The outcome asked for the 9.9 to be re-derived from real traffic. It was, on
|
|
42
|
+
// 2026-09-16: six apps' `request_metrics` tables sum to 42,971/day, which is 43 %
|
|
43
|
+
// of the owner's 100,000/day guess — so the allowance is 2.3× wider than shipped.
|
|
44
|
+
expect(MEASURED_FLEET_REQUESTS_PER_DAY / OWNER_PROJECTED_REQUESTS_PER_DAY).toBeCloseTo(
|
|
45
|
+
0.43,
|
|
46
|
+
2,
|
|
47
|
+
);
|
|
48
|
+
const measured = deriveCpuBudgetMs({ requestsPerDay: MEASURED_FLEET_REQUESTS_PER_DAY });
|
|
49
|
+
expect(measured).toBeCloseTo(22.9, 1);
|
|
50
|
+
// And the measured volume is well inside the included REQUEST allowance, which is
|
|
51
|
+
// the whole reason CPU — not requests — is the budget worth gating on.
|
|
52
|
+
expect((MEASURED_FLEET_REQUESTS_PER_DAY * DAYS_PER_MONTH) / 10_000_000).toBeLessThan(0.2);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('🔴 keeps the shipped default STRICTER than the measured allowance', () => {
|
|
56
|
+
// This is the guard, not the paragraph. The measured corpus is the RETIRED
|
|
57
|
+
// generation's — every table stops within hours of its app's graduation and no app
|
|
58
|
+
// is on a Worker yet — so raising the default to 22.9 would loosen every budget in
|
|
59
|
+
// the fleet on evidence that cannot carry it. Being 2.3× conservative costs nothing;
|
|
60
|
+
// being 2.3× loose costs the thing this module exists to prevent. If someone later
|
|
61
|
+
// drifts the default above what the traffic can justify, this goes red.
|
|
62
|
+
const measured = deriveCpuBudgetMs({ requestsPerDay: MEASURED_FLEET_REQUESTS_PER_DAY });
|
|
63
|
+
expect(DEFAULT_ROUTE_CPU_BUDGET_MS).toBeLessThanOrEqual(measured);
|
|
64
|
+
});
|
|
38
65
|
});
|
|
39
66
|
|
|
40
67
|
describe('what going over actually costs', () => {
|