cultmesh-ts: gate before shard refusal, bound the refusal, harden tests
Some checks failed
CultMesh QUIC native / Build win32-x64 (pull_request) Has been skipped
CultMesh QUIC native / Build linux-x64 (pull_request) Has been skipped
CultMesh QUIC native / Release native binaries (pull_request) Has been skipped
CultMesh portability and performance / ubuntu-latest reactive semantics and leases (pull_request) Failing after 5m2s
CultMesh QUIC native / Verify committed binaries match SHA256SUMS (pull_request) Successful in 36s

A put with wrong-typed shard fields is judged with null shard fields first, then
refused as malformed once granted. The refusal's record key and reason are
clipped so it fits maxPayloadBytes. The allocation test watches transient
allocations; catch-path and delete tests assert ids, operation, reply count,
onError and handlers. README/CHANGELOG no longer claim the schema is resolved
after the gate.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Libby 2026-11-07 20:13:49 +00:00
commit ada30be84e
4 changed files with 121 additions and 48 deletions

View file

@ -10,7 +10,7 @@ All notable changes to `@gamecult/cultmesh-ts` are recorded here, following
- The package is renamed `cultmesh-ts` to `@gamecult/cultmesh-ts` (the repo already scopes `@gamecult/cultcache-ts`), so the unscoped name cannot be claimed by someone else. Import from `@gamecult/cultmesh-ts` and its subpaths; the API is unchanged. Depends on `@gamecult/cultnet-ts`.
- `asSchemaAlias` is removed from `CultMeshDocumentHandle`, `CultMeshCollectionHandle` and their bound forms. Read the handle by the schema you mean.
- `CultMeshDocumentCatalog` (`document`, `tryDocument`, `latest`, `replace`, `submitPrediction`, `authoritativeWriter`, `predictionWriter`, `watch`) and `CultMeshCollectionCatalog` (`collection`, `tryCollection`, `latest`, `watchChanges`) take `<TDocument = never, TSchema>` and yield `CultMeshCatalogValue<TDocument, TSchema>`: an explicit type argument wins, a CultCache document definition names its own value type, and a bare schema descriptor yields `unknown`. A value type is no longer inferred from a value or an expected result. The `parse` option is removed from these calls, and a lookup by type answers with that type's handle or none, not an alias of a compatible schema.
- `createRudpDocumentServer` refuses every peer put and delete unless it is given a `writeAuthority`. A refused write is answered with the typed `write_not_authorized` naming the record key (the bytes pinned in `error-vectors.json`), and `onDocumentPutRaw` is never called; pass `writeAuthority: new CultMeshGrantAllWrites()` to keep the old accept-everything behaviour. The authority is asked before the put is sized, resolved or decoded, with the canonical schema id and the peer's address; one that throws, rejects or answers anything but `granted: true` refuses. A frame the server cannot read is judged as the write it looks like (a put when it does not decode at all), so an ungranted peer gets `write_not_authorized` for it too; a granted peer gets `onError` and a `malformed_document_put` or `malformed_document_delete` refusal. A put carrying a wrong-typed `shardId` or `shardEpoch` is refused with `malformed_document_put` before the authority is asked. Residual: ungranted peers can still distinguish registered from unregistered schemas by timing. A peer `document_delete` now reaches the new `onDocumentDeleteRaw` once granted (without it, a text refusal) instead of the unsupported-request error.
- `createRudpDocumentServer` refuses every peer put and delete unless it is given a `writeAuthority`. A refused write is answered with the typed `write_not_authorized` naming the record key (the bytes pinned in `error-vectors.json`), and `onDocumentPutRaw` is never called; pass `writeAuthority: new CultMeshGrantAllWrites()` to keep the old accept-everything behaviour. The authority is asked before the put is sized or decoded and before any handler runs, with the canonical schema id and the peer's address (the schema id is resolved to its canonical form first, which is the timing residual below); one that throws, rejects or answers anything but `granted: true` refuses. A frame the server cannot read is judged as the write it looks like (a put when it does not decode at all), so an ungranted peer gets `write_not_authorized` for it too; a granted peer gets `onError` and a `malformed_document_put` or `malformed_document_delete` refusal. A put carrying a wrong-typed `shardId` or `shardEpoch` is judged with null shard fields and, once granted, refused with `malformed_document_put`. A refusal's record key and reason are clipped (64 and 96 UTF-8 bytes) so it fits `maxPayloadBytes`. Residual: ungranted peers can still distinguish registered from unregistered schemas by timing. A peer `document_delete` now reaches the new `onDocumentDeleteRaw` once granted (without it, a text refusal) instead of the unsupported-request error.
- Adding a document or collection to a catalog under a schema id or schema name another type already holds is refused with `SchemaConflictError`.
### Added

View file

@ -345,7 +345,7 @@ evidence for the requested provider identity.
## RUDP Helpers
`createRudpDocumentServer` refuses every peer put and delete unless given a `writeAuthority` (`new CultMeshGrantAllWrites()` accepts all). The authority is asked before a write's payload is read or its schema resolved, and a refusal is the typed `write_not_authorized`. Residual: ungranted peers can still distinguish registered from unregistered schemas by timing.
`createRudpDocumentServer` refuses every peer put and delete unless given a `writeAuthority` (`new CultMeshGrantAllWrites()` accepts all). The authority is asked before a write's payload is sized or decoded and before any handler runs, and a refusal is the typed `write_not_authorized`. Residual: ungranted peers can still distinguish registered from unregistered schemas by timing.
Node runtimes can build the shared CultNet reliable-UDP transport from the
branded CultMesh entrypoint while the transport semantics stay owned by

View file

@ -5648,7 +5648,7 @@ export class CultMesh {
message = parseCultNetMessage(decoded, wireContract);
} catch (error) {
// A frame the server cannot read is judged as the write it looks like (a put when it does not even decode), so
// an ungranted peer hears the same refusal for it as for any other write, never silence or a parse error.
// an ungranted peer hears the same refusal for it as for any other write, not a parse error.
const write = readableWrite(decoded);
if (decoded !== undefined && !write) throw error;
if (await writeRefused(record, write?.operation ?? "put", write?.schemaId ?? "", write?.recordKey ?? "", null, null)) return;
@ -5689,16 +5689,22 @@ export class CultMesh {
return;
case "cultnet.document_put_raw.v0":
{
// The parser passes shard fields of any type through a put; a wrong-typed one is refused before the authority.
// The parser passes shard fields of any type through a put. The authority is asked first, with the shard
// fields it can trust (null when wrong-typed), so an ungranted peer hears only the refusal; a granted peer
// whose shard fields are wrong-typed is then refused as malformed.
const { shardId, shardEpoch } = message as { shardId?: unknown; shardEpoch?: unknown };
if (
const malformedShard =
(shardId !== undefined && typeof shardId !== "string") ||
(shardEpoch !== undefined && !Number.isSafeInteger(shardEpoch))
) {
(shardEpoch !== undefined && !Number.isSafeInteger(shardEpoch));
if (await writeRefused(
record, "put", message.document.schemaId, message.document.recordKey,
malformedShard ? null : (shardId as string | undefined) ?? null,
malformedShard ? null : (shardEpoch as number | undefined) ?? null,
)) return;
if (malformedShard) {
sendSchemaMessage(record, malformedWriteError("put", "Raw put messages must carry a string shardId and an integer shardEpoch."));
return;
}
if (await writeRefused(record, "put", message.document.schemaId, message.document.recordKey, (shardId as string | undefined) ?? null, (shardEpoch as number | undefined) ?? null)) return;
}
if (options.onDocumentPutRaw) {
const unservable = unservablePut(message);
@ -5745,6 +5751,11 @@ export class CultMesh {
}
}
function clipUtf8(text: string, maxBytes: number): string {
const bytes = Buffer.from(text, "utf8");
return bytes.byteLength <= maxBytes ? text : new TextDecoder().decode(bytes.subarray(0, maxBytes)).replace(/\uFFFD+$/, "");
}
/** The put or delete a decoded frame claims to be, with the ids it carries when they are strings. */
function readableWrite(value: unknown): { operation: CultMeshRemoteWriteOperation; schemaId: string; recordKey: string } | undefined {
if (typeof value !== "object" || value === null) return undefined;
@ -5802,7 +5813,9 @@ export class CultMesh {
}
}
try {
sendSchemaMessage(record, cultNetWriteNotAuthorizedError(recordKey, reason));
// The key is the peer's and the reason the authority's: both are clipped so the refusal fits `maxPayloadBytes`
// whatever they are (the peer is told the clipped key; a `maxPayloadBytes` under about 512 can still not carry it).
sendSchemaMessage(record, cultNetWriteNotAuthorizedError(clipUtf8(recordKey, 64), reason === undefined ? undefined : clipUtf8(reason, 96)));
} catch (error) {
reportError(error);
}

View file

@ -264,7 +264,7 @@ test("the refusal on the wire is the pinned error-vectors bytes", async () => {
});
});
test("wrong-typed shard fields on a put are refused before the authority and reach no handler", async () => {
test("wrong-typed shard fields on a put are judged with null shard fields, then refused as malformed once granted", async () => {
const authority = new Recording(grant);
await using(await rig(authority), async (r) => {
const bad: Record<string, unknown>[] = [
@ -274,9 +274,31 @@ test("wrong-typed shard fields on a put are refused before the authority and rea
await r.settle();
assert.equal(r.replies.length, bad.length);
assert.ok(r.replies.every((reply) => reply.schemaVersion === "cultnet.error.v0" && reply.code === null && reply.error.startsWith("malformed_document_put:")));
assert.deepEqual(authority.requests, [], "the authority was never asked");
assert.equal(authority.requests.length, bad.length, "the authority is asked first");
assert.ok(authority.requests.every((q) => q.shardId === null && q.shardEpoch === null), "with null shard fields");
assert.equal(r.puts.length, 0);
});
await using(await rig(undefined), async (r) => {
r.peer.send(put(KEY_A, { shardId: 5 }));
await r.settle();
assertRefused(r.replies[0], KEY_A);
});
});
test("a refusal fits maxPayloadBytes however long the record key and reason are", async () => {
const longKey = "k".repeat(3_000) + "é";
await using(await rig(new Recording(() => ({ granted: false, reason: "r".repeat(100_000) })), { maxPayloadBytes: 1024 }), async (r) => {
r.peer.send(put(longKey));
r.peer.send(del(longKey));
await r.settle();
assert.equal(r.replies.length, 2);
for (const reply of r.replies) {
assert.equal((reply as { code?: string }).code, "write_not_authorized");
assert.ok(encodeWireValue(reply).byteLength <= 1024);
assert.ok(longKey.startsWith((reply as { details: { value: string } }).details.value));
}
assert.deepEqual(r.errors, []);
});
});
test("well-typed shard fields reach the authority as sent", async () => {
@ -332,11 +354,12 @@ function rawPut(payloadBytes: number[], key = KEY_A): Uint8Array {
async function rawRig(writeAuthority?: CultMeshRemoteWriteAuthority) {
const connectionId = nextConnection++;
const puts: CultMeshRudpDocumentPut[] = [];
const deletes: CultMeshRudpDocumentDelete[] = [];
const errors: Error[] = [];
const server = CultMesh.createRudpDocumentServer("authority-raw", connectionId, {
documents: new CultNetDocumentRegistry([defineCultNetDocumentBinding({ definition: note })]),
writeAuthority, bindHost: "127.0.0.1", bindPort: 0, resendDelayMs: 25, resendPollMs: 5,
onDocumentPutRaw: (d) => { puts.push(d); }, onError: (e) => errors.push(e),
onDocumentPutRaw: (d) => { puts.push(d); }, onDocumentDeleteRaw: (d) => { deletes.push(d); }, onError: (e) => errors.push(e),
});
await server.start();
const client = await CultMesh.createRudpClient("raw", connectionId, `rudp://127.0.0.1:${server.bind.port}`, { resendDelayMs: 25, resendPollMs: 5 });
@ -348,54 +371,91 @@ async function rawRig(writeAuthority?: CultMeshRemoteWriteAuthority) {
}
const replies: CultNetMessage[] = [];
new CultNetPeer(client, { wireContract: "cultnet.schema.v0" }).on("message", (m) => replies.push(m));
return { puts, errors, replies, send: (b: Uint8Array) => client.send("schema", b), close: () => { client.close(); server.close(); } };
return { puts, deletes, errors, replies, send: (b: Uint8Array) => client.send("schema", b), close: () => { client.close(); server.close(); } };
}
const hostile: [string, Uint8Array][] = [
["an int payload", rawPut([0x2a])],
["a 2**70-sized integer payload", rawPut([0xcf, 255, 255, 255, 255, 255, 255, 255, 255])],
["a bin whose claimed length exceeds the frame", rawPut(bin(1_000_000_000, 1, 2, 3))],
["a frame that does not decode at all", Uint8Array.from([0xc1])],
["a put whose document is not a map", Uint8Array.from([0x82, ...str("schemaVersion"), ...str("cultnet.document_put_raw.v0"), ...str("document"), 0x05])],
const float64 = (v: number) => { const b = Buffer.alloc(9); b[0] = 0xcb; b.writeDoubleBE(v, 1); return [...b]; };
// Each hostile frame, the operation it is judged as, and the ids the authority is told (empty when the frame does not carry them).
const hostile: { name: string; frame: Uint8Array; operation: "put" | "delete"; schemaId: string; recordKey: string }[] = [
{ name: "an int payload", frame: rawPut([0x2a]), operation: "put", schemaId: SCHEMA_ID, recordKey: KEY_A },
{ name: "a uint64-max integer payload", frame: rawPut([0xcf, 255, 255, 255, 255, 255, 255, 255, 255]), operation: "put", schemaId: SCHEMA_ID, recordKey: KEY_A },
{ name: "a float64 2**70 payload", frame: rawPut(float64(2 ** 70)), operation: "put", schemaId: SCHEMA_ID, recordKey: KEY_A },
{ name: "a bin whose claimed length exceeds the frame", frame: rawPut(bin(1_000_000_000, 1, 2, 3)), operation: "put", schemaId: "", recordKey: "" },
{ name: "a frame that does not decode at all", frame: Uint8Array.from([0xc1]), operation: "put", schemaId: "", recordKey: "" },
{ name: "a put whose document is not a map", frame: Uint8Array.from([0x82, ...str("schemaVersion"), ...str("cultnet.document_put_raw.v0"), ...str("document"), 0x05]), operation: "put", schemaId: "", recordKey: "" },
{ name: "a delete whose recordKey is not a string", frame: Uint8Array.from([0x84, ...str("schemaVersion"), ...str("cultnet.document_delete.v0"), ...str("messageId"), ...str("d"), ...str("schemaId"), ...str(SCHEMA_ID), ...str("recordKey"), 0x07]), operation: "delete", schemaId: SCHEMA_ID, recordKey: "" },
];
test("an ungranted peer's unreadable put is refused with write_not_authorized, never silence, and nothing is allocated for it", async () => {
for (const [name, frame] of hostile) {
const r = await rawRig(undefined);
try {
const before = process.memoryUsage().arrayBuffers;
r.send(frame);
await sleep(250);
assert.equal(r.replies.length, 1, name);
assert.equal((r.replies[0] as { code?: string }).code, "write_not_authorized", name);
assert.ok(process.memoryUsage().arrayBuffers - before < 50_000_000, `${name}: the claimed length was not allocated`);
assert.deepEqual([r.puts.length, r.errors.length], [0, 0], name);
} finally { r.close(); }
/** Records the size of every buffer a frame makes the process allocate, so a transient allocation is seen after it is freed. */
function watchAllocations(): { largest: () => number; stop: () => void } {
let largest = 0;
const note = (n: unknown) => { if (typeof n === "number" && n > largest) largest = n; };
const originals = { alloc: Buffer.alloc, unsafe: Buffer.allocUnsafe, slow: Buffer.allocUnsafeSlow, u8: globalThis.Uint8Array, ab: globalThis.ArrayBuffer };
Buffer.alloc = ((n: number, ...rest: never[]) => { note(n); return originals.alloc.call(Buffer, n, ...rest); }) as typeof Buffer.alloc;
Buffer.allocUnsafe = ((n: number) => { note(n); return originals.unsafe.call(Buffer, n); }) as typeof Buffer.allocUnsafe;
Buffer.allocUnsafeSlow = ((n: number) => { note(n); return originals.slow.call(Buffer, n); }) as typeof Buffer.allocUnsafeSlow;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const watched = (ctor: any): any => new Proxy(ctor, { construct: (target, args, newTarget) => { note(args[0]); return Reflect.construct(target, args, newTarget); } });
(globalThis as { Uint8Array: unknown }).Uint8Array = watched(originals.u8);
(globalThis as { ArrayBuffer: unknown }).ArrayBuffer = watched(originals.ab);
return {
largest: () => largest,
stop: () => {
Buffer.alloc = originals.alloc; Buffer.allocUnsafe = originals.unsafe; Buffer.allocUnsafeSlow = originals.slow;
(globalThis as { Uint8Array: unknown }).Uint8Array = originals.u8;
(globalThis as { ArrayBuffer: unknown }).ArrayBuffer = originals.ab;
},
};
}
test("an ungranted peer's unreadable write is refused with write_not_authorized naming what it carried, and nothing is allocated for it", async () => {
for (const h of hostile) {
const authority = new Recording(() => ({ granted: false }));
for (const withAuthority of [false, true]) {
const r = await rawRig(withAuthority ? authority : undefined);
const watch = watchAllocations();
try {
r.send(h.frame);
await sleep(250);
} finally { watch.stop(); }
try {
assert.equal(r.replies.length, 1, h.name);
assertRefused(r.replies[0], h.recordKey);
assert.ok(watch.largest() < 10_000_000, `${h.name}: allocated ${watch.largest()} bytes`);
assert.deepEqual([r.puts.length, r.deletes.length, r.errors.length], [0, 0, 0], h.name);
} finally { r.close(); }
}
assert.equal(authority.requests.length, 1, h.name);
assert.deepEqual(
[authority.requests[0]!.operation, authority.requests[0]!.schemaId, authority.requests[0]!.recordKey],
[h.operation, h.schemaId, h.recordKey], h.name);
}
});
test("a granted peer's unreadable put reaches onError and gets a malformed_document_put refusal", async () => {
for (const [name, frame] of hostile) {
const r = await rawRig(new CultMeshGrantAllWrites());
try {
r.send(frame);
await sleep(250);
assert.equal(r.replies.length, 1, name);
assert.ok((r.replies[0] as { error: string }).error.startsWith("malformed_document_put:"), name);
assert.equal(r.errors.length, 1, name);
assert.equal(r.puts.length, 0, name);
} finally { r.close(); }
}
test("the allocation watch sees a transient allocation", () => {
const watch = watchAllocations();
try { void Buffer.alloc(20_000_000); } finally { watch.stop(); }
assert.ok(watch.largest() >= 20_000_000);
const typed = watchAllocations();
try { void new Uint8Array(20_000_000); } finally { typed.stop(); }
assert.ok(typed.largest() >= 20_000_000);
});
test("an ungranted peer's malformed delete is refused with write_not_authorized, a granted one gets malformed_document_delete", async () => {
const frame = Uint8Array.from([0x84, ...str("schemaVersion"), ...str("cultnet.document_delete.v0"), ...str("messageId"), ...str("d"), ...str("schemaId"), ...str(SCHEMA_ID), ...str("recordKey"), 0x07]);
for (const [authority, code] of [[undefined, "write_not_authorized"], [new CultMeshGrantAllWrites(), "malformed_document_delete:"]] as const) {
test("a granted peer's unreadable write reaches onError and gets a malformed refusal, and no handler runs", async () => {
for (const h of hostile) {
const authority = new Recording(grant);
const r = await rawRig(authority);
try {
r.send(frame);
r.send(h.frame);
await sleep(250);
assert.ok((r.replies[0] as { error: string })?.error.startsWith(code), code);
assert.equal(r.replies.length, 1, h.name);
const reply = r.replies[0] as { code: unknown; error: string };
assert.equal(reply.code, null, h.name);
assert.ok(reply.error.startsWith(`malformed_document_${h.operation}:`), h.name);
assert.equal(r.errors.length, 1, h.name);
assert.deepEqual([r.puts.length, r.deletes.length], [0, 0], h.name);
assert.deepEqual([authority.requests.length, authority.requests[0]!.operation, authority.requests[0]!.recordKey], [1, h.operation, h.recordKey], h.name);
} finally { r.close(); }
}
});