Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ All notable changes to this project are documented here. Format follows

### Fixed

- `parseTranscriptExport` now validates the room name even when the export has
no rows. The check previously lived only in `transcriptRecord`, which an
empty export never calls, so an invalid room silently parsed as an empty
transcript. A non-string room no longer slips through regex coercion
(`123` testing as `"123"`, `null` as `"null"`) in either entry point.
- Transcript JSON readers on runtimes without reviver source context now preserve
escaped nonce keys and complete numeric tokens without accepting invalid JSON or
changing signed text. Noncanonical nonce tokens remain rejected by verification.
Expand Down
16 changes: 15 additions & 1 deletion src/transcript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,19 @@ function object(value: unknown, where: string): Record<string, unknown> {
return value as Record<string, unknown>;
}

function requireRoomName(room: string): string {
// The typeof arm matters: ROOM_NAME.test coerces, so 123 would test "123"
// and null would test "null" — both inside the grammar, neither a room the
// caller named. verifyTranscriptRecord already checks typeof the same way.
if (typeof room !== "string" || !ROOM_NAME.test(room)) {
throw new Error(`tclk: invalid transcript room ${JSON.stringify(room)}`);
}
return room;
}

/** Normalize one `?format=json` or `/export` message without discarding its exact line. */
export function transcriptRecord(room: string, value: unknown): TranscriptRecord {
if (!ROOM_NAME.test(room)) throw new Error(`tclk: invalid transcript room ${JSON.stringify(room)}`);
requireRoomName(room);
const message = object(value, "transcript message");
if (!Number.isSafeInteger(message.seq) || (message.seq as number) < 0) {
throw new Error("tclk: transcript message seq must be a non-negative safe integer");
Expand Down Expand Up @@ -201,6 +211,10 @@ export function transcriptRecord(room: string, value: unknown): TranscriptRecord

/** Parse a byte-exact technocore `/export` JSONL response. One malformed row fails all. */
export function parseTranscriptExport(room: string, jsonl: string): TranscriptRecord[] {
// Validate the room before looking at any row: with no rows, transcriptRecord
// never runs, and an invalid room would otherwise parse as a clean empty
// export, hiding the caller's bug behind an empty result.
requireRoomName(room);
const records: TranscriptRecord[] = [];
jsonl.split("\n").forEach((line, index) => {
if (line.trim() === "") return;
Expand Down
27 changes: 27 additions & 0 deletions tests/transcript.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
makeOffer,
parseTranscriptExport,
parseRecordJson,
transcriptRecord,
verifyTranscriptRecord,
type TranscriptRecord,
} from "../src/index.js";
Expand Down Expand Up @@ -219,6 +220,32 @@ describe("trusted transcript records", () => {
expect(() => parseTranscriptExport(BOARD, localeTimestamp)).toThrow(/timezone-qualified/);
});

it("validates the room name even when the export has no rows", () => {
// The room check used to live only inside transcriptRecord, which an empty
// export never calls — so a mistyped room silently "parsed" as an empty
// transcript and hid the caller's bug behind a clean empty result.
expect(parseTranscriptExport(BOARD, "")).toEqual([]);
expect(parseTranscriptExport(BOARD, "\n\n")).toEqual([]);
for (const badRoom of ["BAD ROOM", "", "has space", "-leading-dash", "x".repeat(49)]) {
expect(() => parseTranscriptExport(badRoom, ""), badRoom).toThrow(/invalid transcript room/);
expect(() => parseTranscriptExport(badRoom, "\n"), badRoom).toThrow(/invalid transcript room/);
}

// A non-string room must not slip through regex coercion either:
// ROOM_NAME.test(123) tests "123", and .test(null) tests "null".
const message = {
seq: 1,
ts: "2026-09-03T23:50:00Z",
from: payer.did,
text: "hello",
};
for (const badRoom of [123, null, undefined, true, {}]) {
expect(() => parseTranscriptExport(badRoom as never, ""), String(badRoom)).toThrow(/invalid transcript room/);
expect(() => transcriptRecord(badRoom as never, message), String(badRoom)).toThrow(/invalid transcript room/);
}
expect(transcriptRecord(BOARD, message).room).toBe(BOARD);
});

it("preserves exact nonces above 2^53 in export rows (#78)", () => {
const { offer } = deal();
const line = encodeFrame(offer);
Expand Down