From 0e04ae8b888e7276ddd3428b4912d9ad98275d8f Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 4 Nov 2026 05:57:20 -0800 Subject: [PATCH 1/5] chore(quest): claim quest/m1/proto-safe-names.md Quest-Claim: 5a97252b906eea51 From c97afcaeafb706955594f84f308368c06f77b10f Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 4 Nov 2026 06:05:16 -0800 Subject: [PATCH 2/5] fix(js): preserve prototype-named catalog and rendition keys Co-Authored-By: GPT-6.1 Sol --- quest/m1/README.md | 1 - quest/m1/proto-safe-names.md | 46 ------------------------------------ 2 files changed, 47 deletions(-) delete mode 100644 quest/m1/proto-safe-names.md diff --git a/quest/m1/README.md b/quest/m1/README.md index 9136e7589c9..5eea8b83eb5 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -181,7 +181,6 @@ their end report, so an embedder's drain alert pages on real cuts. - [The relay reports native sessions it force-closes at the drain deadline](/quest/m1/relay-drain-cuts.md) - the drain's public outcome and live tally separate sessions cut while open from sessions only finishing their end report, so an embedder's drain alert pages on real cuts - [A Rust relay never replays from group 0 to a lite-03/04 subscriber](/quest/m1/relay-inflight-replay.md) - A Rust relay or publisher serving a lite-03 or lite-04 SUBSCRIBE from a track copy that is still resolving starts at the newest group once the copy resolves - [The JS resume model passes seed 12345](/quest/m1/js-resume-model-12345.md) - a reader updated to no ranges keeps reading instead of ending cleanly -- [Prototype-safe rendition names in JS](/quest/m1/proto-safe-names.md) - `js/msf`'s `ext` and `js/publish`'s rendition maps keep `__proto__` and `constructor` names, like Rust - [The resume model ends #7739's clean-end seed](/quest/m1/resume-seed-17893651.md) - replay seed 17893651 after #7705's wake fix, and fix it if a long-budget reader still never gets the end - [Every moq-mux consumer gets the catalog warmup from one entry point](/quest/m1/mux-warmup.md) - one moq-mux call widens the subscription and configures the consumer, adopted by moq-audio, moq-c, moq-ffi's raw consumer, and moqsrc - [Codex shells never load another checkout's dev environment](/quest/m1/codex-env-snapshot.md) - the Codex dev-shell snapshot is skipped when stale or inside `nix develop` diff --git a/quest/m1/proto-safe-names.md b/quest/m1/proto-safe-names.md deleted file mode 100644 index 6ee1feeff33..00000000000 --- a/quest/m1/proto-safe-names.md +++ /dev/null @@ -1,46 +0,0 @@ -# [S] Prototype-safe rendition names in JS - -## Goal - -JS keeps names that collide with `Object.prototype` (`__proto__`, -`constructor`, `toString`) wherever it keys renditions or catalog members -by name, the way Rust does: - -- `js/msf/src/catalog.ts`'s exported `CatalogSchema` keeps a `__proto__` - `ext` key as an own property, as its `decode` already does, like - `moq-msf`'s `Catalog::ext`. -- `js/publish` registers a rendition named `__proto__` or `constructor` - instead of refusing it as "rendition already registered", and its catalog - and stats builders publish it. - -The shared parity corpus covers each. - -## Plan - -Decided 2026-11-03 (maintainer, follow-up planning after the 2026-11-03 /quest-afk and /quest-complete runs), milestone: - -- m0 -- ✅ m1 -- m2 - -Both gaps are the same bug as hang's catalog maps, fixed by -[moq-dev/moq#7498](https://github.com/moq-dev/moq/pull/7498) (merged as -[7b4f53165](https://github.com/moq-dev/moq/commit/7b4f53165056d665378a09ec84465b6b033f5bf7)): -a plain object keyed by an untrusted name. Planned as one quest (maintainer, from #7498's -iterate, 2026-11-03). - -- **MSF `ext`.** `decode` and `encode` already keep a `__proto__` member - (null-prototype maps; `catalog.test.ts`'s "round-trips a __proto__ - extension member as data"). Only the exported `CatalogSchema` (around line - 69) still declares `ext` as a `z.record`, which drops it for a caller that - parses with the schema directly. Give it the same null-prototype parse, or - stop exporting it as a parser if nothing outside `js/msf` should use it. -- **js/publish.** `#renditions` and `#records` in - `js/publish/src/broadcast.ts` (around lines 147 and 163) are plain - objects, so `this.#renditions.peek()[name]` finds `Object.prototype`'s - member and throws "already registered". Key them with a `Map` (or - null-prototype objects) and carry that through the catalog and stats - builders that read them, so the published catalog keeps the name. -- Test `__proto__`, `constructor`, and `toString` in each place that changes. - -Public API: none. Wire: none. From 734907604e5b0780ceadbad6c4e12d1623900711 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 4 Nov 2026 06:05:28 -0800 Subject: [PATCH 3/5] fix(js): keep own prototype-named data through catalog builders Co-Authored-By: GPT-6.1 Sol --- js/msf/MIGRATE.md | 5 ++ js/msf/api/index.api.md | 7 ++- js/msf/src/catalog.test.ts | 25 +++----- js/msf/src/catalog.ts | 10 ++- js/publish/src/broadcast.test.ts | 86 ++++++++++++++++++++++++++ js/publish/src/broadcast.ts | 35 +++++------ rs/hang/fixtures/catalog-parity.json | 40 +++++++++++- rs/hang/fixtures/stats-snapshot.json | 14 ++++- rs/moq-msf/fixtures/catalog-names.json | 7 +++ rs/moq-msf/src/lib.rs | 13 ++++ 10 files changed, 202 insertions(+), 40 deletions(-) create mode 100644 rs/moq-msf/fixtures/catalog-names.json diff --git a/js/msf/MIGRATE.md b/js/msf/MIGRATE.md index 8d2cbe631b4..89585e391a6 100644 --- a/js/msf/MIGRATE.md +++ b/js/msf/MIGRATE.md @@ -4,6 +4,11 @@ How to update your code for each breaking change, newest first. ## Unreleased +### `CatalogSchema` keeps prototype-named extension members + +`CatalogSchema.parse` preserves own `__proto__`, `constructor`, and `toString` members in `ext`, matching `decode`. +The parsed extension map now has a null prototype; inspect it with `Object.hasOwn` and `Object.entries`. + ### `Track.stalled` is removed (#4915) The non-standard `stalled` track field is gone and no longer read or written. diff --git a/js/msf/api/index.api.md b/js/msf/api/index.api.md index 21db3a9c199..9072a2e3920 100644 --- a/js/msf/api/index.api.md +++ b/js/msf/api/index.api.md @@ -4,20 +4,21 @@ import { $strip } from "zod/v4/core"; import { ZodMiniArray } from "zod/mini"; import { ZodMiniBoolean } from "zod/mini"; +import { ZodMiniCustom } from "zod/mini"; import { ZodMiniEnum } from "zod/mini"; import { ZodMiniNumber } from "zod/mini"; import { ZodMiniObject } from "zod/mini"; import { ZodMiniOptional } from "zod/mini"; -import { ZodMiniRecord } from "zod/mini"; +import { ZodMiniPipe } from "zod/mini"; import { ZodMiniString } from "zod/mini"; +import { ZodMiniTransform } from "zod/mini"; import { ZodMiniUnion } from "zod/mini"; -import { ZodMiniUnknown } from "zod/mini"; import { infer } from "zod/mini"; export type Catalog = infer; export const CatalogSchema: ZodMiniObject<{ - ext: ZodMiniOptional, ZodMiniUnknown>>; + ext: ZodMiniOptional, Record>, ZodMiniTransform, Record>>>; tracks: ZodMiniArray>; bitrate: ZodMiniOptional>; diff --git a/js/msf/src/catalog.test.ts b/js/msf/src/catalog.test.ts index b5d4b91e2ec..b22203f86e5 100644 --- a/js/msf/src/catalog.test.ts +++ b/js/msf/src/catalog.test.ts @@ -1,5 +1,5 @@ import { expect, test } from "bun:test"; -import { decode, encode } from "./catalog.ts"; +import { CatalogSchema, decode, encode } from "./catalog.ts"; function encodeJson(value: unknown): Uint8Array { return new TextEncoder().encode(JSON.stringify(value)); @@ -264,20 +264,15 @@ test("refuses an extension section named after a reserved root field", () => { expect(() => encode({ tracks: [], ext: { tracks: "nope" } })).toThrow(/reserved root field/); }); -test("round-trips a __proto__ extension member as data", () => { - // Assigning `__proto__` to an ordinary object invokes the prototype setter: the member - // vanishes from the catalog and its value becomes the map's prototype. Built as raw JSON, - // since `__proto__:` in an object literal is that same setter syntax and never lands as a key. - const raw = '{"version":"draft-01","tracks":[],"__proto__":{"polluted":1},"ok":2}'; - const catalog = decode(new TextEncoder().encode(raw)); - - const ext = catalog.ext ?? {}; - expect(Object.keys(ext).sort()).toEqual(["__proto__", "ok"]); - expect(Object.getPrototypeOf(ext)).toBeNull(); - - const wire = decodeJson(encode(catalog)); - expect(Object.keys(wire)).toContain("__proto__"); - expect(wire.ok).toBe(2); +test("shared Rust extension names survive decoding and the public schema", async () => { + const raw = await Bun.file(new URL("../../../rs/moq-msf/fixtures/catalog-names.json", import.meta.url)).bytes(); + const { version: _, tracks, ...ext } = decodeJson(raw); + const catalog = decode(raw); + for (const parsed of [catalog, CatalogSchema.parse({ tracks, ext }), CatalogSchema.parse(catalog)]) { + expect(Object.keys(parsed.ext ?? {}).sort()).toEqual(Object.keys(ext).sort()); + expect(Object.getPrototypeOf(parsed.ext)).toBeNull(); + expect(decodeJson(encode(parsed))).toEqual(decodeJson(raw)); + } }); test("caption role, label, language and codec survive the wire", () => { diff --git a/js/msf/src/catalog.ts b/js/msf/src/catalog.ts index 78438c20a79..99e9d3e6670 100644 --- a/js/msf/src/catalog.ts +++ b/js/msf/src/catalog.ts @@ -73,7 +73,15 @@ export const CatalogSchema = z.object({ // draft-lcurley-moq-mpegts. Mirrors `Catalog::ext` in `moq-msf`, which is typed; // here it stays an untyped map, so members this build has never seen survive a // decode/encode round trip unchanged. - ext: z.optional(z.record(z.string(), z.unknown())), + ext: z.optional( + z.pipe( + // A record parser drops `__proto__`; every own extension member is application data. + z.custom>( + (value) => typeof value === "object" && value !== null && !Array.isArray(value), + ), + z.transform((value) => Object.assign(Object.create(null), value) as Record), + ), + ), }); /** The MSF catalog: a snapshot of the available tracks. */ diff --git a/js/publish/src/broadcast.test.ts b/js/publish/src/broadcast.test.ts index 440098b4f99..e3c8157fed5 100644 --- a/js/publish/src/broadcast.test.ts +++ b/js/publish/src/broadcast.test.ts @@ -33,6 +33,92 @@ const audioConfig: Catalog.AudioConfig = { container: { kind: "legacy" }, }; +const parity = (await Bun.file(new URL("../../../rs/hang/fixtures/catalog-parity.json", import.meta.url)).json()) as { + catalog: unknown; + refused?: boolean; +}[]; + +for (const kind of ["video", "audio", "text"] as const) { + const fixture = parity.find((c) => { + if (c.refused) return false; + const entries = Catalog.RootSchema.parse(c.catalog)[kind]?.renditions; + return entries && ["__proto__", "constructor", "toString"].every((name) => Object.hasOwn(entries, name)); + }); + if (!fixture) throw new Error(`missing shared ${kind} name fixture`); + const configs = Catalog.RootSchema.parse(fixture.catalog)[kind]?.renditions ?? {}; + + test(`shared Rust ${kind} names survive registration and the published catalog`, async () => { + const origin = new Origin.Producer(); + const broadcast = new Broadcast({ origin, name: Path.from("names.hang") }); + try { + const register = () => + Object.entries(configs).map(([name, config]) => { + const rendition = broadcast[kind](name); + rendition.config.set(config); + expect(() => broadcast[kind](name)).toThrow(); + return rendition; + }); + const registered = register(); + await settle(); + const entries = (await readCatalog(broadcast))?.[kind]?.renditions ?? {}; + expect(Object.keys(entries).sort()).toEqual(Object.keys(configs).sort()); + expect(entries).toEqual(configs); + for (const rendition of registered) { + expect(rendition.track.peek()?.name).toBe(rendition.name); + rendition.close(); + } + await settle(); + expect((await readCatalog(broadcast))?.[kind]).toBeUndefined(); + register(); + await settle(); + const restored = (await readCatalog(broadcast))?.[kind]?.renditions ?? {}; + expect(Object.keys(restored).sort()).toEqual(Object.keys(configs).sort()); + expect(restored).toEqual(configs); + } finally { + broadcast.close(); + origin.close(); + } + }); +} + +test("shared Rust stats names survive the publisher's report builder", async () => { + const fixture = Stats.SnapshotSchema.parse( + await Bun.file(new URL("../../../rs/hang/fixtures/stats-snapshot.json", import.meta.url)).json(), + ); + const clock = new Manual(); + const installed = clock.install(); + const origin = new Origin.Producer(); + const broadcast = new Broadcast({ origin, name: Path.from("stats-names.hang"), stats: true }); + try { + for (const [name, stats] of Object.entries(fixture.renditions ?? {})) { + const rendition = broadcast.video(name); + rendition.config.set(videoConfig); + rendition.stats.set(stats); + } + await settle(); + const net = broadcast.net.peek(); + if (!net) throw new Error("expected a network producer"); + const subscriber = net.track(Broadcast.STATS_TRACK).subscribe(); + try { + const consumer = new Json.Snapshot.Consumer({ + track: subscriber, + schema: Stats.SnapshotSchema, + }); + await settle(); + await clock.advance(Nano.fromMilli(Milli(1000))); + const reported = (await consumer.latest())?.value.renditions ?? {}; + expect(Object.keys(reported).sort()).toEqual(Object.keys(fixture.renditions ?? {}).sort()); + expect(reported).toEqual(fixture.renditions ?? {}); + } finally { + subscriber.close(); + } + } finally { + broadcast.close(); + origin.close(); + installed.close(); + } +}); + test("sibling broadcasts on one page advertise the same capture clock", async () => { const origin = new Origin.Producer(); const camera = new Broadcast({ enabled: true, origin, name: Path.from("participant/camera.hang") }); diff --git a/js/publish/src/broadcast.ts b/js/publish/src/broadcast.ts index 75007db23e1..3ab3c9cc9d1 100644 --- a/js/publish/src/broadcast.ts +++ b/js/publish/src/broadcast.ts @@ -94,9 +94,9 @@ export type BroadcastInput = { // The longest group among the video and audio renditions, the catalog's `media.join.interval`: the // promise that every one starts a group at least that often. Undefined while none declares one, so a // later listing tries again; it reads every hint until then, since none is fixed yet. -function longestGroup(effect: Effect, renditions: Record>): Catalog.U53 | undefined { +function longestGroup(effect: Effect, renditions: ReadonlyMap>): Catalog.U53 | undefined { let longest: number | undefined; - for (const rendition of Object.values(renditions)) { + for (const rendition of renditions.values()) { if (rendition.kind === "text") continue; const hint = effect.get(rendition.joinInterval); if (hint === undefined || !Number.isFinite(hint) || hint <= 0) continue; @@ -149,9 +149,8 @@ export class Broadcast { */ readonly order = new Order(); - // The registered renditions keyed by full track name. A plain object so deep-equality detects a - // key add/remove; the Rendition values compare by identity, which is stable. - readonly #renditions = new Signal>>({}); + // Replaced on add/remove so signal subscribers see every registry change. + readonly #renditions = new Signal>>(new Map()); // The writable signals backing each Rendition's read-only `track` and `slot`, keyed by name. // A static network track is exposed here while it is open. On-demand gates non-audio tracks @@ -227,7 +226,7 @@ export class Broadcast { } #register(name: string, kind: Kind): Rendition { - if (this.#renditions.peek()[name]) { + if (this.#renditions.peek().has(name)) { throw new Error(`rendition already registered: ${name}`); } this.#publication.peek()?.check(name, kind); @@ -241,7 +240,7 @@ export class Broadcast { // The slot is the one its producer fills, so it holds this rendition's config. const slot = handle.slot as Getter | undefined>; const rendition = makeRendition(name, kind, handle.track, slot, () => this.#unregister(name)); - this.#renditions.update((renditions) => ({ ...renditions, [name]: rendition as Rendition })); + this.#renditions.update((renditions) => new Map(renditions).set(name, rendition as Rendition)); return rendition; } @@ -256,9 +255,9 @@ export class Broadcast { } this.#renditions.update((renditions) => { - if (!(name in renditions)) return renditions; - const next = { ...renditions }; - delete next[name]; + if (!renditions.has(name)) return renditions; + const next = new Map(renditions); + next.delete(name); return next; }); } @@ -271,13 +270,13 @@ export class Broadcast { const publication = effect.get(this.#publication); if (publication) effect.get(publication.renames); - const video: Record = {}; - const audio: Record = {}; - const text: Record = {}; + const video: Record = Object.create(null); + const audio: Record = Object.create(null); + const text: Record = Object.create(null); const listed = new Map(); const served: [Rendition, string, Identity][] = []; - for (const rendition of Object.values(renditions)) { + for (const rendition of renditions.values()) { const source = enabled ? effect.get(rendition.config) : undefined; if (source === undefined) continue; // The rendition's media is this broadcast's own track, so its config can't point elsewhere. @@ -402,8 +401,8 @@ export class Broadcast { // What each listed rendition has sent, keyed by its catalog ID, and the connection's round-trip time. #stats(): { transport?: { rtt: number }; renditions: Record } { - const renditions: Record = {}; - for (const rendition of Object.values(this.#renditions.peek())) { + const renditions: Record = Object.create(null); + for (const rendition of this.#renditions.peek().values()) { const stats = rendition.stats.peek(); if (stats && rendition.config.peek() !== undefined) renditions[rendition.name] = stats; } @@ -414,7 +413,7 @@ export class Broadcast { // The names outside a publication's own that a replacement must avoid: every registered ID, and // every ID and track name in the catalog, including an application's own sections. #reserved(): Set { - const taken = new Set(Object.keys(this.#renditions.peek())); + const taken = new Set(this.#renditions.peek().keys()); // A no-op edit is the catalog's only read; it publishes nothing. this.catalog.mutate((catalog) => { for (const entries of [ @@ -520,7 +519,7 @@ export class Broadcast { const renditions = inner.get(this.#renditions); inner.get(publication.renames); - for (const rendition of Object.values(renditions)) { + for (const rendition of renditions.values()) { const handle = this.#handles.get(rendition.name); if (!handle) continue; diff --git a/rs/hang/fixtures/catalog-parity.json b/rs/hang/fixtures/catalog-parity.json index f39ae0ae8dc..92cf1a0f078 100644 --- a/rs/hang/fixtures/catalog-parity.json +++ b/rs/hang/fixtures/catalog-parity.json @@ -3375,7 +3375,7 @@ "refused": true }, { - "name": "rendition IDs are any string", + "name": "video rendition IDs are any string", "catalog": { "video": { "renditions": { @@ -3384,11 +3384,47 @@ }, "__proto__": { "codec": "avc1.64001f" + }, + "constructor": { + "codec": "avc1.64001f" + }, + "toString": { + "codec": "avc1.64001f" + } + } + } + }, + "video": ["", "__proto__", "constructor", "toString"] + }, + { + "name": "audio rendition IDs are any string", + "catalog": { + "audio": { + "renditions": { + "": { + "codec": "opus", + "sampleRate": 48000, + "numberOfChannels": 2 + }, + "__proto__": { + "codec": "opus", + "sampleRate": 48000, + "numberOfChannels": 2 + }, + "constructor": { + "codec": "opus", + "sampleRate": 48000, + "numberOfChannels": 2 + }, + "toString": { + "codec": "opus", + "sampleRate": 48000, + "numberOfChannels": 2 } } } }, - "video": ["", "__proto__"] + "audio": ["", "__proto__", "constructor", "toString"] }, { "name": "empty catalog", diff --git a/rs/hang/fixtures/stats-snapshot.json b/rs/hang/fixtures/stats-snapshot.json index 37e4c287cf8..841a5e82e8a 100644 --- a/rs/hang/fixtures/stats-snapshot.json +++ b/rs/hang/fixtures/stats-snapshot.json @@ -14,6 +14,18 @@ "skipped": 4, "bitrate": 6000000 }, - "reset": {} + "reset": {}, + "__proto__": { + "frames": 1, + "bytes": 100 + }, + "constructor": { + "frames": 2, + "bytes": 200 + }, + "toString": { + "frames": 3, + "bytes": 300 + } } } diff --git a/rs/moq-msf/fixtures/catalog-names.json b/rs/moq-msf/fixtures/catalog-names.json new file mode 100644 index 00000000000..a14b6358ee2 --- /dev/null +++ b/rs/moq-msf/fixtures/catalog-names.json @@ -0,0 +1,7 @@ +{ + "version": "draft-01", + "tracks": [], + "__proto__": { "member": "prototype" }, + "constructor": [1, 2, 3], + "toString": "extension" +} diff --git a/rs/moq-msf/src/lib.rs b/rs/moq-msf/src/lib.rs index 1d577a6d7f6..0da55733d83 100644 --- a/rs/moq-msf/src/lib.rs +++ b/rs/moq-msf/src/lib.rs @@ -1297,4 +1297,17 @@ mod test { assert_eq!(value["mpegts"]["program"]["pmtPid"], serde_json::json!(100)); assert_eq!(value["somethingElse"], serde_json::json!([1, 2, 3])); } + + #[test] + fn shared_extension_names_roundtrip() { + type Extra = serde_json::Map; + let raw = include_str!("../fixtures/catalog-names.json"); + let mut expected: Extra = serde_json::from_str(raw).unwrap(); + expected.remove("version"); + expected.remove("tracks"); + let catalog = Catalog::::from_str(raw).unwrap(); + assert_eq!(catalog.ext, expected); + let wire: serde_json::Value = serde_json::from_str(&catalog.to_json().unwrap()).unwrap(); + assert_eq!(wire, serde_json::from_str::(raw).unwrap()); + } } From 771f04e873ce962bf9316ea55dd74f4419330798 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 4 Nov 2026 08:05:44 -0800 Subject: [PATCH 4/5] fix(msf): accept only plain objects as the catalog extension The `ext` predicate accepted any non-array object, so a `Map` or class instance passed and the transform silently copied no members. Restrict it to objects whose prototype is `Object.prototype` or null, as the former record schema did. Co-Authored-By: Claude Opus 5.5 --- js/msf/src/catalog.test.ts | 4 ++++ js/msf/src/catalog.ts | 10 +++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/js/msf/src/catalog.test.ts b/js/msf/src/catalog.test.ts index b22203f86e5..55ae3771f7e 100644 --- a/js/msf/src/catalog.test.ts +++ b/js/msf/src/catalog.test.ts @@ -273,6 +273,10 @@ test("shared Rust extension names survive decoding and the public schema", async expect(Object.getPrototypeOf(parsed.ext)).toBeNull(); expect(decodeJson(encode(parsed))).toEqual(decodeJson(raw)); } + // Like a record, the extension accepts only plain objects: these have no own members to keep. + for (const bad of [new Map([["mpegts", 1]]), new Set(["mpegts"]), [1, 2], "mpegts"]) { + expect(() => CatalogSchema.parse({ tracks: [], ext: bad })).toThrow(); + } }); test("caption role, label, language and codec survive the wire", () => { diff --git a/js/msf/src/catalog.ts b/js/msf/src/catalog.ts index 99e9d3e6670..d21579d1635 100644 --- a/js/msf/src/catalog.ts +++ b/js/msf/src/catalog.ts @@ -76,9 +76,13 @@ export const CatalogSchema = z.object({ ext: z.optional( z.pipe( // A record parser drops `__proto__`; every own extension member is application data. - z.custom>( - (value) => typeof value === "object" && value !== null && !Array.isArray(value), - ), + // Only plain objects qualify, as with a record: a `Map` or class instance has no + // own members to copy, so accepting one would silently empty the extension. + z.custom>((value) => { + if (typeof value !== "object" || value === null) return false; + const proto = Object.getPrototypeOf(value); + return proto === Object.prototype || proto === null; + }), z.transform((value) => Object.assign(Object.create(null), value) as Record), ), ), From c34eb22c8b0fcd1099cdf4d131107fc596d1ee04 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 4 Nov 2026 08:36:26 -0800 Subject: [PATCH 5/5] fix(msf): recognize plain catalog extensions from any realm Comparing against this realm's `Object.prototype` refused a plain object parsed in another realm, which the former record schema accepted. Match a plain object's prototype by shape instead: null, or a prototype whose own prototype is null and that owns `isPrototypeOf`. Also cover prototype-named renditions after re-registration: their tracks reattach under their own names, and a codec change replaces each under `/1` in both the handle and the catalog. Co-Authored-By: Claude Opus 5.5 --- js/msf/src/catalog.test.ts | 9 +++++++++ js/msf/src/catalog.ts | 8 ++++++-- js/publish/src/broadcast.test.ts | 21 ++++++++++++++++++++- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/js/msf/src/catalog.test.ts b/js/msf/src/catalog.test.ts index 55ae3771f7e..426192a9f52 100644 --- a/js/msf/src/catalog.test.ts +++ b/js/msf/src/catalog.test.ts @@ -1,4 +1,5 @@ import { expect, test } from "bun:test"; +import { runInNewContext } from "node:vm"; import { CatalogSchema, decode, encode } from "./catalog.ts"; function encodeJson(value: unknown): Uint8Array { @@ -277,6 +278,14 @@ test("shared Rust extension names survive decoding and the public schema", async for (const bad of [new Map([["mpegts", 1]]), new Set(["mpegts"]), [1, 2], "mpegts"]) { expect(() => CatalogSchema.parse({ tracks: [], ext: bad })).toThrow(); } + // A plain object from another realm has a different `Object.prototype`, yet is still plain. + const foreign = runInNewContext(`JSON.parse('{"mpegts":1,"constructor":2,"__proto__":3}')`); + expect(Object.getPrototypeOf(foreign)).not.toBe(Object.prototype); + expect(Object.keys(CatalogSchema.parse({ tracks: [], ext: foreign }).ext ?? {}).sort()).toEqual([ + "__proto__", + "constructor", + "mpegts", + ]); }); test("caption role, label, language and codec survive the wire", () => { diff --git a/js/msf/src/catalog.ts b/js/msf/src/catalog.ts index d21579d1635..bb9eb2ff6cb 100644 --- a/js/msf/src/catalog.ts +++ b/js/msf/src/catalog.ts @@ -77,11 +77,15 @@ export const CatalogSchema = z.object({ z.pipe( // A record parser drops `__proto__`; every own extension member is application data. // Only plain objects qualify, as with a record: a `Map` or class instance has no - // own members to copy, so accepting one would silently empty the extension. + // own members to copy, so accepting one would silently empty the extension. A plain + // object's prototype is null or some realm's `Object.prototype`, recognized by shape + // rather than identity so an object parsed in another realm (an iframe, `vm`) passes. z.custom>((value) => { if (typeof value !== "object" || value === null) return false; const proto = Object.getPrototypeOf(value); - return proto === Object.prototype || proto === null; + return ( + proto === null || (Object.getPrototypeOf(proto) === null && Object.hasOwn(proto, "isPrototypeOf")) + ); }), z.transform((value) => Object.assign(Object.create(null), value) as Record), ), diff --git a/js/publish/src/broadcast.test.ts b/js/publish/src/broadcast.test.ts index e3c8157fed5..9b77f2a5388 100644 --- a/js/publish/src/broadcast.test.ts +++ b/js/publish/src/broadcast.test.ts @@ -69,11 +69,30 @@ for (const kind of ["video", "audio", "text"] as const) { } await settle(); expect((await readCatalog(broadcast))?.[kind]).toBeUndefined(); - register(); + const again = register(); await settle(); const restored = (await readCatalog(broadcast))?.[kind]?.renditions ?? {}; expect(Object.keys(restored).sort()).toEqual(Object.keys(configs).sort()); expect(restored).toEqual(configs); + for (const rendition of again) expect(rendition.track.peek()?.name).toBe(rendition.name); + + // A codec change replaces each prototype-named track under a fresh name. Text has no codec identity. + if (kind === "text") return; + const codec = kind === "video" ? "vp8" : "mp4a.40.2"; + const byName = new Map(again.map((rendition) => [rendition.name, rendition])); + for (const [name, config] of Object.entries(configs)) { + expect(config.codec).not.toBe(codec); + byName.get(name)?.config.set({ ...config, codec }); + } + await settle(); + const replaced = (await readCatalog(broadcast))?.[kind]?.renditions ?? {}; + expect(Object.keys(replaced).sort()).toEqual(Object.keys(configs).sort()); + for (const rendition of again) { + const track = `${rendition.name}/1`; + expect(rendition.track.peek()?.name).toBe(track); + expect(replaced[rendition.name]?.codec).toBe(codec); + expect(replaced[rendition.name]?.track).toBe(track); + } } finally { broadcast.close(); origin.close();