cultnet-ts: one UTF-8 byte bound for presence and daemon health detail
signed-daemon-health sliced detail at 512 UTF-16 units while the Rust owner counts bytes, so 300 x "é" failed shape validation. truncateUtf8Bytes now serves runtime presence and both daemon-health sites, replacing truncateRuntimePresenceDetail. It makes the string well formed first, so a lone surrogate becomes U+FFFD instead of invalid UTF-8 in msgpack. The unsigned payload is extracted as unsignedDaemonHealthPayload so it can be tested without a socket. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
parent
8e578ea2ab
commit
a957f07539
6 changed files with 114 additions and 35 deletions
|
|
@ -6,14 +6,15 @@ import { decode, encode } from "@msgpack/msgpack";
|
|||
import {
|
||||
GAMECULT_RUNTIME_PRESENCE_HEALTH_SCHEMA,
|
||||
GAMECULT_RUNTIME_PRESENCE_IDENTITY_NAME,
|
||||
RUNTIME_PRESENCE_DETAIL_MAX_BYTES,
|
||||
IDUNN_RUNTIME_ACTIVATION_CREDENTIAL_NAME,
|
||||
runtimePresenceActivationSigningMessage,
|
||||
runtimePresenceProofPayload,
|
||||
truncateRuntimePresenceDetail,
|
||||
runtimePresenceProviderSigningMessage,
|
||||
encodeRuntimePresenceHealth,
|
||||
type RuntimePresenceHealth,
|
||||
} from "./runtime-presence-health";
|
||||
import { truncateUtf8Bytes } from "./utf8-bound";
|
||||
import { encodeCultNetMessageForWire, parseCultNetMessage, type CultNetRawDocumentRecord, type CultNetSnapshotRequestMessage } from "./contracts";
|
||||
import { CultNetRudpSession, decodeRudpPacket, encodeRudpPacket } from "./rudp";
|
||||
import type { CultNetRudpPacket } from "./rudp";
|
||||
|
|
@ -242,7 +243,7 @@ export function createIdunnRuntimePresencePublisher(
|
|||
const signed = signIdunnRuntimePresence(options.authority, {
|
||||
capabilities: options.capabilities,
|
||||
state,
|
||||
detail: truncateRuntimePresenceDetail(detail),
|
||||
detail: truncateUtf8Bytes(detail, RUNTIME_PRESENCE_DETAIL_MAX_BYTES),
|
||||
writeLeaseSha256,
|
||||
publisherSequence: sequence,
|
||||
observedAtUnixMillis: Date.now(),
|
||||
|
|
|
|||
|
|
@ -20,6 +20,8 @@
|
|||
|
||||
import { encode } from "@msgpack/msgpack";
|
||||
|
||||
import { truncateUtf8Bytes } from "./utf8-bound";
|
||||
|
||||
export const GAMECULT_RUNTIME_PRESENCE_HEALTH_SCHEMA = "gamecult.runtime_presence_health.v2";
|
||||
|
||||
/** Purpose for the stable provider-health signature over the proof payload. */
|
||||
|
|
@ -174,22 +176,6 @@ function presenceTuple(
|
|||
/** The Rust owner (`cultnet-rs`) bounds presence detail by UTF-8 bytes, not UTF-16 units. */
|
||||
export const RUNTIME_PRESENCE_DETAIL_MAX_BYTES = 512;
|
||||
|
||||
/**
|
||||
* Longest prefix of `detail` within the UTF-8 byte limit, cut on a code point
|
||||
* boundary. The single rule for both signing and validation: a detail is valid
|
||||
* exactly when this returns it unchanged.
|
||||
*/
|
||||
export function truncateRuntimePresenceDetail(detail: string): string {
|
||||
let bytes = 0;
|
||||
let end = 0;
|
||||
for (const char of detail) {
|
||||
bytes += Buffer.byteLength(char, "utf8");
|
||||
if (bytes > RUNTIME_PRESENCE_DETAIL_MAX_BYTES) break;
|
||||
end += char.length;
|
||||
}
|
||||
return detail.slice(0, end);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reject statements the contract forbids, before they are signed.
|
||||
*
|
||||
|
|
@ -204,7 +190,7 @@ export function validateRuntimePresence(presence: RuntimePresenceHealth): void {
|
|||
if (presence.observedAtUnixMillis <= 0 || !Number.isSafeInteger(presence.observedAtUnixMillis)) {
|
||||
throw new Error("runtime presence observation time must be a positive integer");
|
||||
}
|
||||
if (truncateRuntimePresenceDetail(presence.detail) !== presence.detail || /\p{Cc}/u.test(presence.detail)) {
|
||||
if (truncateUtf8Bytes(presence.detail, RUNTIME_PRESENCE_DETAIL_MAX_BYTES) !== presence.detail || /\p{Cc}/u.test(presence.detail)) {
|
||||
throw new Error("runtime presence detail is too long or carries control characters");
|
||||
}
|
||||
if (presence.state === "warming" && presence.writeLeaseSha256 !== null) {
|
||||
|
|
|
|||
|
|
@ -19,6 +19,10 @@ import { encode } from "@msgpack/msgpack";
|
|||
import { CultNetRudpSession, decodeRudpPacket, encodeRudpPacket } from "./rudp";
|
||||
import type { CultNetRudpPacket } from "./rudp";
|
||||
import { encodeCultNetMessageForWire } from "./contracts";
|
||||
import { truncateUtf8Bytes } from "./utf8-bound";
|
||||
|
||||
/** The Rust owner (`cultnet-rs`) bounds daemon health detail by UTF-8 bytes. */
|
||||
export const DAEMON_HEALTH_DETAIL_MAX_BYTES = 512;
|
||||
|
||||
export const CULTNET_RUDP_PROTOCOL_ID = "cultnet.transport.rudp.v0";
|
||||
|
||||
|
|
@ -120,15 +124,7 @@ export async function publishSignedDaemonHealth(
|
|||
|
||||
const observedAt = health.observedAt || new Date().toISOString();
|
||||
const signed = publisher.privateKey ? signedDaemonHealthPayload(publisher, health, observedAt) : null;
|
||||
const payload = signed?.payload || encode([
|
||||
publisher.daemonId,
|
||||
health.state,
|
||||
String(health.detail || "").slice(0, 512),
|
||||
observedAt,
|
||||
publisher.healthContract,
|
||||
"daemon-published",
|
||||
CULTNET_RUDP_PROTOCOL_ID,
|
||||
]);
|
||||
const payload = signed?.payload || unsignedDaemonHealthPayload(publisher, health, observedAt);
|
||||
|
||||
const message = {
|
||||
schemaVersion: "cultnet.document_put_raw.v0",
|
||||
|
|
@ -165,6 +161,22 @@ export async function publishSignedDaemonHealth(
|
|||
}
|
||||
}
|
||||
|
||||
export function unsignedDaemonHealthPayload(
|
||||
publisher: SignedDaemonHealthPublisher,
|
||||
health: DaemonHealth,
|
||||
observedAt: string,
|
||||
): Uint8Array {
|
||||
return encode([
|
||||
publisher.daemonId,
|
||||
health.state,
|
||||
truncateUtf8Bytes(String(health.detail || ""), DAEMON_HEALTH_DETAIL_MAX_BYTES),
|
||||
observedAt,
|
||||
publisher.healthContract,
|
||||
"daemon-published",
|
||||
CULTNET_RUDP_PROTOCOL_ID,
|
||||
]);
|
||||
}
|
||||
|
||||
export function signedDaemonHealthPayload(
|
||||
publisher: SignedDaemonHealthPublisher,
|
||||
health: DaemonHealth,
|
||||
|
|
@ -185,7 +197,7 @@ export function signedDaemonHealthPayload(
|
|||
publisher.healthContract,
|
||||
publisher.sourceRuntimeId,
|
||||
health.state,
|
||||
String(health.detail || "").slice(0, 512),
|
||||
truncateUtf8Bytes(String(health.detail || ""), DAEMON_HEALTH_DETAIL_MAX_BYTES),
|
||||
publisher.signerIdentityId,
|
||||
publisher.publisherIncarnationId,
|
||||
publisher.publisherSequence,
|
||||
|
|
|
|||
20
packages/cultnet-ts/src/utf8-bound.ts
Normal file
20
packages/cultnet-ts/src/utf8-bound.ts
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
/**
|
||||
* Longest prefix of `value` whose UTF-8 encoding fits in `maxBytes`, cut on a
|
||||
* code point boundary. Rust owners (`cultnet-rs`) bound strings by UTF-8 bytes,
|
||||
* not UTF-16 units, so every TypeScript publisher bounds through this one path.
|
||||
*
|
||||
* Lone surrogates are replaced with U+FFFD first: they have no UTF-8 encoding,
|
||||
* and a msgpack writer would otherwise emit invalid bytes that Rust refuses.
|
||||
* A value is therefore in bounds exactly when this returns it unchanged.
|
||||
*/
|
||||
export function truncateUtf8Bytes(value: string, maxBytes: number): string {
|
||||
const wellFormed = (value as string & { toWellFormed(): string }).toWellFormed();
|
||||
let bytes = 0;
|
||||
let end = 0;
|
||||
for (const char of wellFormed) {
|
||||
bytes += Buffer.byteLength(char, "utf8");
|
||||
if (bytes > maxBytes) break;
|
||||
end += char.length;
|
||||
}
|
||||
return wellFormed.slice(0, end);
|
||||
}
|
||||
|
|
@ -18,10 +18,10 @@ import {
|
|||
RUNTIME_PRESENCE_FIELD_COUNT,
|
||||
encodeRuntimePresenceHealth,
|
||||
runtimePresenceProofPayload,
|
||||
truncateRuntimePresenceDetail,
|
||||
validateRuntimePresence,
|
||||
type RuntimePresenceHealth,
|
||||
} from "../src/runtime-presence-health";
|
||||
import { truncateUtf8Bytes } from "../src/utf8-bound";
|
||||
|
||||
// Read from the source tree, not a copy beside the compiled test: the vectors
|
||||
// are generated output and must have exactly one home.
|
||||
|
|
@ -127,27 +127,39 @@ const utf8Bytes = (text: string) => Buffer.byteLength(text, "utf8");
|
|||
const isValidUtf8 = (text: string) => !Buffer.from(text, "utf8").toString("utf8").includes("\uFFFD") && !text.includes("\uFFFD");
|
||||
|
||||
test("presence detail is truncated to 512 UTF-8 bytes, not UTF-16 units", () => {
|
||||
const truncated = truncateRuntimePresenceDetail("é".repeat(300));
|
||||
const truncated = truncateUtf8Bytes("é".repeat(300), 512);
|
||||
assert.ok(utf8Bytes(truncated) <= 512);
|
||||
assert.equal(truncated, "é".repeat(256));
|
||||
assert.ok(isValidUtf8(truncated));
|
||||
});
|
||||
|
||||
test("presence detail drops a 4-byte character straddling the boundary whole", () => {
|
||||
const truncated = truncateRuntimePresenceDetail("a".repeat(510) + "😀");
|
||||
const truncated = truncateUtf8Bytes("a".repeat(510) + "😀", 512);
|
||||
assert.equal(truncated, "a".repeat(510));
|
||||
assert.ok(isValidUtf8(truncated));
|
||||
assert.equal(truncateRuntimePresenceDetail("a".repeat(508) + "😀"), "a".repeat(508) + "😀");
|
||||
assert.equal(truncateUtf8Bytes("a".repeat(508) + "😀", 512), "a".repeat(508) + "😀");
|
||||
});
|
||||
|
||||
test("presence detail of exactly 512 ASCII bytes passes unchanged and 513 is cut", () => {
|
||||
const exact = "a".repeat(512);
|
||||
assert.equal(truncateRuntimePresenceDetail(exact), exact);
|
||||
assert.equal(truncateUtf8Bytes(exact, 512), exact);
|
||||
assert.doesNotThrow(() => validateRuntimePresence({ ...referencePresence(), detail: exact }));
|
||||
assert.equal(truncateRuntimePresenceDetail(exact + "a"), exact);
|
||||
assert.equal(truncateUtf8Bytes(exact + "a", 512), exact);
|
||||
assert.throws(() => validateRuntimePresence({ ...referencePresence(), detail: exact + "a" }), /too long/);
|
||||
});
|
||||
|
||||
test("validation refuses a detail that is short in UTF-16 units but over 512 UTF-8 bytes", () => {
|
||||
assert.throws(() => validateRuntimePresence({ ...referencePresence(), detail: "é".repeat(300) }), /too long/);
|
||||
});
|
||||
|
||||
test("a lone high surrogate at the end becomes U+FFFD and the result is valid UTF-8", () => {
|
||||
const truncated = truncateUtf8Bytes("abc\uD83D", 512);
|
||||
assert.equal(truncated, "abc\uFFFD");
|
||||
assert.equal(Buffer.from(truncated, "utf8").toString("utf8"), truncated);
|
||||
});
|
||||
|
||||
test("a lone low surrogate in the middle becomes U+FFFD and the result is valid UTF-8", () => {
|
||||
const truncated = truncateUtf8Bytes("ab\uDE00cd", 512);
|
||||
assert.equal(truncated, "ab\uFFFDcd");
|
||||
assert.equal(Buffer.from(truncated, "utf8").toString("utf8"), truncated);
|
||||
});
|
||||
|
|
|
|||
48
packages/cultnet-ts/test/signed-daemon-health.test.ts
Normal file
48
packages/cultnet-ts/test/signed-daemon-health.test.ts
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import crypto from "node:crypto";
|
||||
import { decode } from "@msgpack/msgpack";
|
||||
|
||||
import {
|
||||
signedDaemonHealthPayload,
|
||||
unsignedDaemonHealthPayload,
|
||||
type SignedDaemonHealthPublisher,
|
||||
} from "../src/signed-daemon-health";
|
||||
|
||||
function publisher(): SignedDaemonHealthPublisher {
|
||||
return {
|
||||
daemonId: "heimdall",
|
||||
endpoint: { host: "127.0.0.1", port: 1 },
|
||||
healthContract: "contract",
|
||||
publisherIncarnationId: "incarnation",
|
||||
publisherSequence: 0,
|
||||
sourceRuntimeId: "runtime",
|
||||
contract: {
|
||||
connectionId: 1,
|
||||
signedSchemaId: "signed.schema",
|
||||
unsignedSchemaId: "unsigned.schema",
|
||||
} as SignedDaemonHealthPublisher["contract"],
|
||||
privateKey: crypto.generateKeyPairSync("ed25519").privateKey,
|
||||
signerIdentityId: "signer",
|
||||
};
|
||||
}
|
||||
|
||||
const observedAt = "2026-01-01T00:00:00.000Z";
|
||||
const detail = "é".repeat(300);
|
||||
|
||||
function assertBounded(text: unknown) {
|
||||
assert.equal(typeof text, "string");
|
||||
const bytes = Buffer.from(text as string, "utf8");
|
||||
assert.ok(bytes.length <= 512, `detail is ${bytes.length} bytes`);
|
||||
assert.equal(bytes.toString("utf8"), text);
|
||||
}
|
||||
|
||||
test("unsigned daemon health detail is bounded by UTF-8 bytes", () => {
|
||||
const fields = decode(unsignedDaemonHealthPayload(publisher(), { state: "ok", detail }, observedAt)) as unknown[];
|
||||
assertBounded(fields[2]);
|
||||
});
|
||||
|
||||
test("signed daemon health detail is bounded by UTF-8 bytes", () => {
|
||||
const { statement } = signedDaemonHealthPayload(publisher(), { state: "ok", detail }, observedAt);
|
||||
assertBounded(statement[5]);
|
||||
});
|
||||
Loading…
Reference in a new issue