From fa6968a2b96288ec3731b242471ef5ff4b0fe70e Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 22 Oct 2026 10:27:40 -0700 Subject: [PATCH 1/4] fix(catalog): bound catalog decoding from a peer Backport of the main fix. A compressed catalog frame is bounded only by the 64 MiB DEFLATE frame cap, and the reconstructed JSON value allocates several times the inflated size. Default snapshot consumers to 4 MiB, and refuse an MSF catalog frame over the same limit before parsing it. Co-Authored-By: DeepSeek V4.1 Flash --- rs/moq-json/src/snapshot/consumer.rs | 33 ++++++++++++++++-- rs/moq-mux/src/catalog/msf/consumer.rs | 47 ++++++++++++++++++++------ rs/moq-mux/src/catalog/msf/mod.rs | 3 ++ 3 files changed, 71 insertions(+), 12 deletions(-) diff --git a/rs/moq-json/src/snapshot/consumer.rs b/rs/moq-json/src/snapshot/consumer.rs index 7457b8b947b..b49b0ce1ae6 100644 --- a/rs/moq-json/src/snapshot/consumer.rs +++ b/rs/moq-json/src/snapshot/consumer.rs @@ -7,11 +7,18 @@ use serde::de::DeserializeOwned; use super::Decoder; use crate::{Compression, Result}; +/// The default [`Config::max_size`]. +/// +/// A peer supplies the compressed frame, so an unset limit lets a small DEFLATE frame +/// reconstruct an unbounded value. Every catalog and metadata track this crate reads +/// fits far below this, and a consumer that needs more sets its own. +pub const DEFAULT_MAX_SIZE: usize = 4 * 1024 * 1024; + /// Track-owning options for a [`Consumer`]. /// /// Build from [`Default`] and override fields (the struct is `#[non_exhaustive]`, so new options /// stay additive). -#[derive(Debug, Clone, Default)] +#[derive(Debug, Clone)] #[non_exhaustive] pub struct Config { /// How the frames are compressed. Must match the encoder's @@ -19,10 +26,20 @@ pub struct Config { pub compression: Compression, /// Maximum bytes in an inflated frame or the compact JSON of the reconstructed value. - /// Unset preserves the DEFLATE decoder's default frame cap and leaves state size unlimited. + /// Defaults to [`DEFAULT_MAX_SIZE`]; `None` keeps only the DEFLATE decoder's frame + /// cap and leaves state size unlimited. pub max_size: Option, } +impl Default for Config { + fn default() -> Self { + Self { + compression: Compression::None, + max_size: Some(DEFAULT_MAX_SIZE), + } + } +} + /// Consumes a JSON value from a track, reconstructing it from snapshots and deltas. /// /// A [`Decoder`] that owns its track: it reads groups, routes each frame by its position, and @@ -153,3 +170,15 @@ impl Consumer { Ok(()) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn default_config_bounds_the_reconstructed_value() { + // A peer picks the compressed frame, so the default must not leave the + // reconstructed value unlimited. + assert_eq!(Config::default().max_size, Some(DEFAULT_MAX_SIZE)); + } +} diff --git a/rs/moq-mux/src/catalog/msf/consumer.rs b/rs/moq-mux/src/catalog/msf/consumer.rs index 58374e78ecf..e65c6d32c43 100644 --- a/rs/moq-mux/src/catalog/msf/consumer.rs +++ b/rs/moq-mux/src/catalog/msf/consumer.rs @@ -51,16 +51,7 @@ impl Consumer { match ready!(group.poll_read_frame(waiter)?) { Some(frame) => { self.group = None; - let json = std::str::from_utf8(&frame.payload).map_err(|_| Error::InvalidUtf8)?; - let msf = match moq_msf::Catalog::::from_str(json) { - Ok(msf) => msf, - Err(err) => { - tracing::warn!(error = %err, "failed to parse MSF catalog frame"); - return Poll::Ready(Err(Error::ParseFrame.into())); - } - }; - let catalog = from_msf(&msf)?; - catalog.check_renditions()?; + let catalog = parse::(&frame.payload)?; return Poll::Ready(Ok(Some(catalog))); } None => self.group = None, @@ -84,6 +75,31 @@ impl Consumer { } } +/// Decode one MSF catalog frame, bounding it before parsing. +/// +/// The frame comes from a peer, and JSON parsing allocates several times its bytes, +/// so a catalog must fit [`moq_json::snapshot::consumer::DEFAULT_MAX_SIZE`], the same +/// limit every compressed catalog track uses. +fn parse(payload: &[u8]) -> Result> { + const MAX: usize = moq_json::snapshot::consumer::DEFAULT_MAX_SIZE; + if payload.len() > MAX { + return Err(Error::FrameTooLarge { + size: payload.len(), + max: MAX, + } + .into()); + } + + let json = std::str::from_utf8(payload).map_err(|_| Error::InvalidUtf8)?; + let msf = moq_msf::Catalog::::from_str(json).map_err(|err| { + tracing::warn!(error = %err, "failed to parse MSF catalog frame"); + Error::ParseFrame + })?; + let catalog = from_msf(&msf)?; + catalog.check_renditions()?; + Ok(catalog) +} + impl From for Consumer { fn from(inner: moq_net::track::Subscriber) -> Self { Self::new(inner) @@ -793,6 +809,17 @@ mod test { ); } + #[test] + fn oversized_catalog_frame_is_refused_before_parsing() { + let payload = vec![b'{'; moq_json::snapshot::consumer::DEFAULT_MAX_SIZE + 1]; + let err = parse::<()>(&payload).expect_err("oversized frame must be refused"); + assert!(matches!( + err, + crate::Error::Msf(Error::FrameTooLarge { size, max }) + if size == payload.len() && max == moq_json::snapshot::consumer::DEFAULT_MAX_SIZE + )); + } + #[test] fn invalid_video_codec_includes_codec_in_error() { // avc1 with a too-short profile string is a malformed structured codec. diff --git a/rs/moq-mux/src/catalog/msf/mod.rs b/rs/moq-mux/src/catalog/msf/mod.rs index 7e3c29892f0..66fb6e34458 100644 --- a/rs/moq-mux/src/catalog/msf/mod.rs +++ b/rs/moq-mux/src/catalog/msf/mod.rs @@ -17,6 +17,9 @@ pub enum Error { #[error("MSF catalog frame is not valid UTF-8")] InvalidUtf8, + #[error("MSF catalog frame is {size} bytes, over the {max}-byte limit")] + FrameTooLarge { size: usize, max: usize }, + #[error("failed to parse MSF catalog frame")] ParseFrame, From ae3f93c27dff879f34714c1f0a70f376b5d4f98c Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 22 Oct 2026 11:33:26 -0700 Subject: [PATCH 2/4] fix(json): bound compressed snapshot state from a peer Replace the 4 MiB consumer default and the MSF FrameTooLarge check with a state bound on the snapshot decoder. An unset max_size now falls back to moq-flate's 64 MiB frame cap, the largest snapshot a producer publishes, and the bound is tracked incrementally: a merge patch grows the value by at most its own bytes, so the value is only measured once the running total passes the limit. The JS decoder mirrors it. Co-Authored-By: Claude Opus 5.5 --- js/json/src/snapshot/codec.test.ts | 17 ++++++ js/json/src/snapshot/decoder.ts | 36 ++++++++--- rs/moq-json/src/snapshot/consumer.rs | 33 +--------- rs/moq-json/src/snapshot/decoder.rs | 83 ++++++++++++++++---------- rs/moq-mux/src/catalog/msf/consumer.rs | 47 ++++----------- rs/moq-mux/src/catalog/msf/mod.rs | 3 - 6 files changed, 109 insertions(+), 110 deletions(-) diff --git a/js/json/src/snapshot/codec.test.ts b/js/json/src/snapshot/codec.test.ts index 834202ecc7f..42fa08502c7 100644 --- a/js/json/src/snapshot/codec.test.ts +++ b/js/json/src/snapshot/codec.test.ts @@ -1,4 +1,5 @@ import { expect, test } from "bun:test"; +import { DEFAULT_MAX_FRAME_SIZE, Encoder as Flate } from "@moq/flate"; import { MissingSnapshot } from "../error.ts"; import { Decoder } from "./decoder.ts"; import { type Config, Encoder } from "./encoder.ts"; @@ -151,6 +152,22 @@ test("a delta before a snapshot throws", () => { }); // A backlog is applied in full but materialized once: the intermediate reconstructions are stale. +test("compressed patches cannot grow the value past the frame cap", () => { + // Each frame inflates under the per-frame cap, but the patches together would not. + const decoder = new Decoder({ compression: "deflate" }); + const flate = new Flate(); + const frame = (value: Doc) => flate.frame(new TextEncoder().encode(JSON.stringify(value))); + const big = "x".repeat(DEFAULT_MAX_FRAME_SIZE / 2); + + decoder.snapshot(frame({ a: big })); + // Past the running bound, but the deletion leaves the value itself small enough. + decoder.delta(frame({ a: null, b: big })); + expect(Object.keys(decoder.decode() ?? {})).toEqual(["b"]); + + expect(() => decoder.delta(frame({ c: big }))).toThrow(`${DEFAULT_MAX_FRAME_SIZE}`); + expect(decoder.decode()).toBeUndefined(); +}); + test("frames apply without materializing", () => { const encoder = new Encoder({ deltaRatio: 100 }); const decoder = new Decoder({}); diff --git a/js/json/src/snapshot/decoder.ts b/js/json/src/snapshot/decoder.ts index 9948233532e..0889b17cf83 100644 --- a/js/json/src/snapshot/decoder.ts +++ b/js/json/src/snapshot/decoder.ts @@ -1,4 +1,4 @@ -import { Decoder as Flate } from "@moq/flate"; +import { DEFAULT_MAX_FRAME_SIZE, Decoder as Flate } from "@moq/flate"; import { isDeflate } from "../compression.ts"; import { merge } from "../diff.ts"; @@ -26,6 +26,9 @@ export class Decoder { #flate?: Flate; // The reconstructed value, `undefined` until the first snapshot. #current?: unknown; + // Bounds the reconstructed value's size in JSON bytes: the snapshot plus every patch applied + // since, as a merge patch adds no more to a value than its own bytes. + #size = 0; constructor(config: Pick, "schema" | "compression"> = {}) { this.#schema = config.schema; @@ -41,17 +44,33 @@ export class Decoder { snapshot(payload: Uint8Array): void { // Each group is its own compressed stream, so the window starts cold here. this.#flate = this.#decompress ? new Flate() : undefined; - this.#current = this.#parse(payload); + const plain = this.#inflate(payload); + this.#current = this.#parse(plain); + this.#size = plain.byteLength; } /** * Apply one of a group's later frames: an RFC 7396 merge patch against the current value. * - * Throws when no snapshot has been applied yet, since a patch has nothing to apply to. + * Throws when no snapshot has been applied yet, since a patch has nothing to apply to, or when + * the result would outgrow the largest snapshot a producer publishes. A refused patch drops the + * value until the next snapshot. */ delta(payload: Uint8Array): void { if (this.#current === undefined) throw new MissingSnapshot(); - this.#current = merge(this.#current, this.#parse(payload)); + const plain = this.#inflate(payload); + this.#current = merge(this.#current, this.#parse(plain)); + this.#size += plain.byteLength; + if (this.#size <= DEFAULT_MAX_FRAME_SIZE) return; + + // The bound never shrinks on its own, so once it passes the limit measure the value itself: + // deletions may have freed room. That resets the bound to the real size, so the measurement + // repeats only after another `limit - size` bytes of patches, not per delta. + this.#size = new TextEncoder().encode(JSON.stringify(this.#current)).byteLength; + if (this.#size > DEFAULT_MAX_FRAME_SIZE) { + this.#current = undefined; + throw new Error(`reconstructed value exceeded ${DEFAULT_MAX_FRAME_SIZE} bytes`); + } } /** @@ -76,9 +95,12 @@ export class Decoder { return this.#schema ? this.#schema.parse(this.#current) : (structuredClone(this.#current) as T); } - // Decompress a frame against the group's window when compressing, then parse it as JSON. - #parse(payload: Uint8Array): unknown { - const plain = this.#flate ? this.#flate.frame(payload) : payload; + // Decompress a frame against the group's window when compressing. + #inflate(payload: Uint8Array): Uint8Array { + return this.#flate ? this.#flate.frame(payload) : payload; + } + + #parse(plain: Uint8Array): unknown { return JSON.parse(new TextDecoder().decode(plain)); } } diff --git a/rs/moq-json/src/snapshot/consumer.rs b/rs/moq-json/src/snapshot/consumer.rs index b49b0ce1ae6..e0132323f1f 100644 --- a/rs/moq-json/src/snapshot/consumer.rs +++ b/rs/moq-json/src/snapshot/consumer.rs @@ -7,18 +7,11 @@ use serde::de::DeserializeOwned; use super::Decoder; use crate::{Compression, Result}; -/// The default [`Config::max_size`]. -/// -/// A peer supplies the compressed frame, so an unset limit lets a small DEFLATE frame -/// reconstruct an unbounded value. Every catalog and metadata track this crate reads -/// fits far below this, and a consumer that needs more sets its own. -pub const DEFAULT_MAX_SIZE: usize = 4 * 1024 * 1024; - /// Track-owning options for a [`Consumer`]. /// /// Build from [`Default`] and override fields (the struct is `#[non_exhaustive]`, so new options /// stay additive). -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Default)] #[non_exhaustive] pub struct Config { /// How the frames are compressed. Must match the encoder's @@ -26,20 +19,10 @@ pub struct Config { pub compression: Compression, /// Maximum bytes in an inflated frame or the compact JSON of the reconstructed value. - /// Defaults to [`DEFAULT_MAX_SIZE`]; `None` keeps only the DEFLATE decoder's frame - /// cap and leaves state size unlimited. + /// Unset uses [`moq_flate::DEFAULT_MAX_FRAME_SIZE`], the largest snapshot a producer publishes. pub max_size: Option, } -impl Default for Config { - fn default() -> Self { - Self { - compression: Compression::None, - max_size: Some(DEFAULT_MAX_SIZE), - } - } -} - /// Consumes a JSON value from a track, reconstructing it from snapshots and deltas. /// /// A [`Decoder`] that owns its track: it reads groups, routes each frame by its position, and @@ -170,15 +153,3 @@ impl Consumer { Ok(()) } } - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn default_config_bounds_the_reconstructed_value() { - // A peer picks the compressed frame, so the default must not leave the - // reconstructed value unlimited. - assert_eq!(Config::default().max_size, Some(DEFAULT_MAX_SIZE)); - } -} diff --git a/rs/moq-json/src/snapshot/decoder.rs b/rs/moq-json/src/snapshot/decoder.rs index 31ece91018f..713d37ae9a5 100644 --- a/rs/moq-json/src/snapshot/decoder.rs +++ b/rs/moq-json/src/snapshot/decoder.rs @@ -31,7 +31,13 @@ use crate::{Error, Result}; pub struct Decoder { /// Whether frames are DEFLATE-compressed, matching the encoder's config. compression: bool, - max_size: Option, + + /// Maximum bytes in an inflated frame or the reconstructed value's JSON. + limit: usize, + + /// Bounds the reconstructed value's size in JSON bytes: the snapshot plus every patch applied + /// since, as a merge patch adds no more to a value than its own bytes. + size: usize, /// The current group's DEFLATE decoder (one window per group), rebuilt at each snapshot. flate: Option, @@ -53,7 +59,10 @@ impl Decoder { pub fn new(config: Config) -> Self { Self { compression: config.compression.is_deflate(), - max_size: config.max_size, + // Unset falls back to the largest frame a producer emits, which also caps its snapshots, + // so any value it can publish fits while a peer cannot grow the state without bound. + limit: config.max_size.unwrap_or(moq_flate::DEFAULT_MAX_FRAME_SIZE as usize), + size: 0, flate: None, plain: Vec::new(), check: RefCell::new(crate::merge::CheckScratch::default()), @@ -70,19 +79,16 @@ impl Decoder { self.current = None; // Every group starts a new DEFLATE window; enforce the budget while inflating. self.flate = self.compression.then(|| { - moq_flate::Decoder::with_max_frame_size(self.max_size.map_or(moq_flate::DEFAULT_MAX_FRAME_SIZE, |size| { - (size as u64).min(moq_flate::DEFAULT_MAX_FRAME_SIZE) - })) + moq_flate::Decoder::with_max_frame_size((self.limit as u64).min(moq_flate::DEFAULT_MAX_FRAME_SIZE)) }); let inflated = self.flate.as_mut().map(|flate| flate.frame(payload)).transpose()?; let plain = inflated.as_deref().unwrap_or(payload); - if let Some(limit) = self.max_size - && plain.len() > limit - { - return Err(Error::TooLarge(limit)); + if plain.len() > self.limit { + return Err(Error::TooLarge(self.limit)); } self.current = Some(serde_json::from_slice(plain)?); - self.check_size() + self.size = plain.len(); + Ok(()) } /// Apply a merge patch, refusing frames or reconstructed state beyond the configured budget. @@ -99,17 +105,26 @@ impl Decoder { } None => payload, }; - if let Some(limit) = self.max_size - && plain.len() > limit - { - return Err(Error::TooLarge(limit)); + if plain.len() > self.limit { + return Err(Error::TooLarge(self.limit)); + } + let current = self.current.as_mut().expect("a snapshot precedes any delta"); + crate::merge::apply_bytes(current, plain, &self.check)?; + self.size = self.size.saturating_add(plain.len()); + if self.size <= self.limit { + return Ok(()); } - crate::merge::apply_bytes( - self.current.as_mut().expect("a snapshot precedes any delta"), - plain, - &self.check, - )?; - self.check_size() + + // The bound never shrinks on its own, so once it passes the limit measure the value itself: + // deletions may have freed room. Counting the compact JSON resets the bound to the real + // size, so the walk repeats only after another `limit - size` bytes of patches, not per + // delta. An oversized value is dropped before anything can materialize it. + let mut budget = Budget(self.limit); + if serde_json::to_writer(&mut budget, current).is_err() { + return Err(Error::TooLarge(self.limit)); + } + self.size = self.limit - budget.0; + Ok(()) })(); if result.is_err() { self.current = None; @@ -117,18 +132,6 @@ impl Decoder { result } - fn check_size(&mut self) -> Result<()> { - let Some(limit) = self.max_size else { return Ok(()) }; - // Count the compact representation without allocating another full snapshot. - // A patch and the prior state are each bounded, and an oversized result is - // released before it can be materialized or used by another patch. - if serde_json::to_writer(Budget(limit), self.current.as_ref().unwrap()).is_err() { - self.current = None; - return Err(Error::TooLarge(limit)); - } - Ok(()) - } - /// The reconstructed value as raw JSON, or `None` before the first snapshot. pub fn value(&self) -> Option<&Value> { self.current.as_ref() @@ -287,6 +290,22 @@ mod test { } } + #[test] + fn unset_budget_still_bounds_compressed_state() { + // Each frame inflates under the per-frame cap, but together they would grow the state past + // it, which no producer could publish as a snapshot. + let half = moq_flate::DEFAULT_MAX_FRAME_SIZE as usize / 2; + let mut decoder = Decoder::::new(consume(Compression::Deflate)); + let mut encoder = moq_flate::Encoder::new(); + let mut frame = |key: &str| encoder.frame(format!(r#"{{"{key}":"{}"}}"#, "x".repeat(half)).as_bytes()); + decoder.snapshot(&frame("a")).unwrap(); + assert!(matches!( + decoder.delta(&frame("b")), + Err(Error::TooLarge(limit)) if limit as u64 == moq_flate::DEFAULT_MAX_FRAME_SIZE + )); + assert!(decoder.value().is_none()); + } + #[test] fn plain_frames_obey_the_budget_before_parsing() { let config = ConsumerConfig { diff --git a/rs/moq-mux/src/catalog/msf/consumer.rs b/rs/moq-mux/src/catalog/msf/consumer.rs index e65c6d32c43..58374e78ecf 100644 --- a/rs/moq-mux/src/catalog/msf/consumer.rs +++ b/rs/moq-mux/src/catalog/msf/consumer.rs @@ -51,7 +51,16 @@ impl Consumer { match ready!(group.poll_read_frame(waiter)?) { Some(frame) => { self.group = None; - let catalog = parse::(&frame.payload)?; + let json = std::str::from_utf8(&frame.payload).map_err(|_| Error::InvalidUtf8)?; + let msf = match moq_msf::Catalog::::from_str(json) { + Ok(msf) => msf, + Err(err) => { + tracing::warn!(error = %err, "failed to parse MSF catalog frame"); + return Poll::Ready(Err(Error::ParseFrame.into())); + } + }; + let catalog = from_msf(&msf)?; + catalog.check_renditions()?; return Poll::Ready(Ok(Some(catalog))); } None => self.group = None, @@ -75,31 +84,6 @@ impl Consumer { } } -/// Decode one MSF catalog frame, bounding it before parsing. -/// -/// The frame comes from a peer, and JSON parsing allocates several times its bytes, -/// so a catalog must fit [`moq_json::snapshot::consumer::DEFAULT_MAX_SIZE`], the same -/// limit every compressed catalog track uses. -fn parse(payload: &[u8]) -> Result> { - const MAX: usize = moq_json::snapshot::consumer::DEFAULT_MAX_SIZE; - if payload.len() > MAX { - return Err(Error::FrameTooLarge { - size: payload.len(), - max: MAX, - } - .into()); - } - - let json = std::str::from_utf8(payload).map_err(|_| Error::InvalidUtf8)?; - let msf = moq_msf::Catalog::::from_str(json).map_err(|err| { - tracing::warn!(error = %err, "failed to parse MSF catalog frame"); - Error::ParseFrame - })?; - let catalog = from_msf(&msf)?; - catalog.check_renditions()?; - Ok(catalog) -} - impl From for Consumer { fn from(inner: moq_net::track::Subscriber) -> Self { Self::new(inner) @@ -809,17 +793,6 @@ mod test { ); } - #[test] - fn oversized_catalog_frame_is_refused_before_parsing() { - let payload = vec![b'{'; moq_json::snapshot::consumer::DEFAULT_MAX_SIZE + 1]; - let err = parse::<()>(&payload).expect_err("oversized frame must be refused"); - assert!(matches!( - err, - crate::Error::Msf(Error::FrameTooLarge { size, max }) - if size == payload.len() && max == moq_json::snapshot::consumer::DEFAULT_MAX_SIZE - )); - } - #[test] fn invalid_video_codec_includes_codec_in_error() { // avc1 with a too-short profile string is a malformed structured codec. diff --git a/rs/moq-mux/src/catalog/msf/mod.rs b/rs/moq-mux/src/catalog/msf/mod.rs index 66fb6e34458..7e3c29892f0 100644 --- a/rs/moq-mux/src/catalog/msf/mod.rs +++ b/rs/moq-mux/src/catalog/msf/mod.rs @@ -17,9 +17,6 @@ pub enum Error { #[error("MSF catalog frame is not valid UTF-8")] InvalidUtf8, - #[error("MSF catalog frame is {size} bytes, over the {max}-byte limit")] - FrameTooLarge { size: usize, max: usize }, - #[error("failed to parse MSF catalog frame")] ParseFrame, From f966ce33fce137bc3189f67dc0b8583d9d6de018 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 22 Oct 2026 12:00:51 -0700 Subject: [PATCH 3/4] docs(json): say what the snapshot state bound counts Co-Authored-By: Claude Opus 5.5 --- js/json/src/snapshot/decoder.ts | 6 ++++-- rs/moq-json/src/snapshot/consumer.rs | 3 ++- rs/moq-json/src/snapshot/decoder.rs | 6 ++++-- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/js/json/src/snapshot/decoder.ts b/js/json/src/snapshot/decoder.ts index 0889b17cf83..17d113b85ac 100644 --- a/js/json/src/snapshot/decoder.ts +++ b/js/json/src/snapshot/decoder.ts @@ -26,8 +26,10 @@ export class Decoder { #flate?: Flate; // The reconstructed value, `undefined` until the first snapshot. #current?: unknown; - // Bounds the reconstructed value's size in JSON bytes: the snapshot plus every patch applied - // since, as a merge patch adds no more to a value than its own bytes. + // The reconstructed value's size in JSON bytes, counted as the snapshot plus every patch applied + // since: a merge patch adds no more nodes to a value than its own bytes can hold. Re-serializing + // can still print a number longer than its source (`9e9`), but the value's memory follows its + // nodes, so the source bytes bound it as well as the compact JSON would. #size = 0; constructor(config: Pick, "schema" | "compression"> = {}) { diff --git a/rs/moq-json/src/snapshot/consumer.rs b/rs/moq-json/src/snapshot/consumer.rs index e0132323f1f..7d0dabb0a44 100644 --- a/rs/moq-json/src/snapshot/consumer.rs +++ b/rs/moq-json/src/snapshot/consumer.rs @@ -18,7 +18,8 @@ pub struct Config { /// [`Config::compression`](super::Config::compression). Defaults to [`Compression::None`]. pub compression: Compression, - /// Maximum bytes in an inflated frame or the compact JSON of the reconstructed value. + /// Maximum bytes in an inflated frame or the reconstructed value, counted as the JSON of the + /// frames that built it until that passes the limit, then as the value's compact JSON. /// Unset uses [`moq_flate::DEFAULT_MAX_FRAME_SIZE`], the largest snapshot a producer publishes. pub max_size: Option, } diff --git a/rs/moq-json/src/snapshot/decoder.rs b/rs/moq-json/src/snapshot/decoder.rs index 713d37ae9a5..ad6cc7b427c 100644 --- a/rs/moq-json/src/snapshot/decoder.rs +++ b/rs/moq-json/src/snapshot/decoder.rs @@ -35,8 +35,10 @@ pub struct Decoder { /// Maximum bytes in an inflated frame or the reconstructed value's JSON. limit: usize, - /// Bounds the reconstructed value's size in JSON bytes: the snapshot plus every patch applied - /// since, as a merge patch adds no more to a value than its own bytes. + /// The reconstructed value's size in JSON bytes, counted as the snapshot plus every patch + /// applied since: a merge patch adds no more nodes to a value than its own bytes can hold. + /// Re-serializing can still print a number longer than its source (`9e9`), but the value's + /// memory follows its nodes, so the source bytes bound it as well as the compact JSON would. size: usize, /// The current group's DEFLATE decoder (one window per group), rebuilt at each snapshot. From b9b3e317203164c2b620ddf1bf6c21ff4d600802 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 22 Oct 2026 12:47:35 -0700 Subject: [PATCH 4/4] fix(json): count each patch's exact growth instead of re-measuring the value A value held at its limit made every tiny patch re-serialize the whole value, so a small compressed group could force a full walk per frame. Applying a patch now reports its exact effect on the compact JSON, measuring only the patch and what it replaces or removes, so a frame's cost follows the frame. The JS decoder applies patches in place with the same accounting. Co-Authored-By: Claude Opus 5.5 --- js/json/src/diff.ts | 2 +- js/json/src/snapshot/codec.test.ts | 11 +++ js/json/src/snapshot/decoder.ts | 68 +++++++++++++---- rs/moq-json/benches/codec.rs | 8 +- rs/moq-json/src/merge.rs | 107 +++++++++++++++++++++------ rs/moq-json/src/snapshot/consumer.rs | 3 +- rs/moq-json/src/snapshot/decoder.rs | 42 +++-------- 7 files changed, 166 insertions(+), 75 deletions(-) diff --git a/js/json/src/diff.ts b/js/json/src/diff.ts index db57f649f97..269a7fb5b52 100644 --- a/js/json/src/diff.ts +++ b/js/json/src/diff.ts @@ -13,7 +13,7 @@ export interface Diff { type Obj = Record; -function isObject(value: unknown): value is Obj { +export function isObject(value: unknown): value is Obj { return typeof value === "object" && value !== null && !Array.isArray(value); } diff --git a/js/json/src/snapshot/codec.test.ts b/js/json/src/snapshot/codec.test.ts index 42fa08502c7..3a0cf210fb1 100644 --- a/js/json/src/snapshot/codec.test.ts +++ b/js/json/src/snapshot/codec.test.ts @@ -168,6 +168,17 @@ test("compressed patches cannot grow the value past the frame cap", () => { expect(decoder.decode()).toBeUndefined(); }); +test("a __proto__ patch key stays a member", () => { + const decoder = new Decoder(); + const json = (text: string) => new TextEncoder().encode(text); + decoder.snapshot(json(`{"a":{}}`)); + decoder.delta(json(`{"__proto__":{"polluted":true},"a":{"__proto__":{"polluted":true}}}`)); + expect(({} as Doc).polluted).toBeUndefined(); + expect(JSON.stringify(decoder.decode())).toBe( + `{"a":{"__proto__":{"polluted":true}},"__proto__":{"polluted":true}}`, + ); +}); + test("frames apply without materializing", () => { const encoder = new Encoder({ deltaRatio: 100 }); const decoder = new Decoder({}); diff --git a/js/json/src/snapshot/decoder.ts b/js/json/src/snapshot/decoder.ts index 17d113b85ac..9efb5d66169 100644 --- a/js/json/src/snapshot/decoder.ts +++ b/js/json/src/snapshot/decoder.ts @@ -1,7 +1,7 @@ import { DEFAULT_MAX_FRAME_SIZE, Decoder as Flate } from "@moq/flate"; import { isDeflate } from "../compression.ts"; -import { merge } from "../diff.ts"; +import { isObject } from "../diff.ts"; import { MissingSnapshot } from "../error.ts"; import type { Config } from "./encoder.ts"; @@ -26,10 +26,10 @@ export class Decoder { #flate?: Flate; // The reconstructed value, `undefined` until the first snapshot. #current?: unknown; - // The reconstructed value's size in JSON bytes, counted as the snapshot plus every patch applied - // since: a merge patch adds no more nodes to a value than its own bytes can hold. Re-serializing - // can still print a number longer than its source (`9e9`), but the value's memory follows its - // nodes, so the source bytes bound it as well as the compact JSON would. + // The reconstructed value's size in JSON bytes: the snapshot's own bytes, then moved by what each + // patch adds, replaces, or removes (see {@link apply}). The snapshot is not re-serialized, so a + // number printed longer than its source (`9e9`) goes uncounted, but the value's memory follows + // its nodes, which its bytes bound all the same. #size = 0; constructor(config: Pick, "schema" | "compression"> = {}) { @@ -60,15 +60,9 @@ export class Decoder { */ delta(payload: Uint8Array): void { if (this.#current === undefined) throw new MissingSnapshot(); - const plain = this.#inflate(payload); - this.#current = merge(this.#current, this.#parse(plain)); - this.#size += plain.byteLength; - if (this.#size <= DEFAULT_MAX_FRAME_SIZE) return; - - // The bound never shrinks on its own, so once it passes the limit measure the value itself: - // deletions may have freed room. That resets the bound to the real size, so the measurement - // repeats only after another `limit - size` bytes of patches, not per delta. - this.#size = new TextEncoder().encode(JSON.stringify(this.#current)).byteLength; + const [value, growth] = apply(this.#current, this.#parse(this.#inflate(payload))); + this.#current = value; + this.#size += growth; if (this.#size > DEFAULT_MAX_FRAME_SIZE) { this.#current = undefined; throw new Error(`reconstructed value exceeded ${DEFAULT_MAX_FRAME_SIZE} bytes`); @@ -106,3 +100,49 @@ export class Decoder { return JSON.parse(new TextDecoder().decode(plain)); } } + +const utf8 = new TextEncoder(); + +// Bytes in a value's JSON, or none for an absent one. +function bytes(value: unknown): number { + return value === undefined ? 0 : utf8.encode(JSON.stringify(value)).byteLength; +} + +// Apply an RFC 7396 merge patch, mutating `target` where it can, and return the result with how many +// JSON bytes it grew. Only the patch and what it replaces or removes are measured, never the +// untouched rest, so the cost follows the frame rather than the value it lands on. A member counts its +// key, value, colon, and comma, without the O(n) scan for whether an object has other members, so +// the count can be off from the compact JSON by a byte per object but never drifts as members come +// and go. +function apply(target: unknown, patch: unknown): [unknown, number] { + if (!isObject(patch)) return [patch, bytes(patch) - bytes(target)]; + + let growth = 0; + let object: Record; + if (isObject(target)) { + object = target; + } else { + object = {}; + growth += 2 - bytes(target); + } + + for (const [key, value] of Object.entries(patch)) { + const present = Object.hasOwn(object, key); + if (value === null) { + if (!present) continue; + growth -= bytes(key) + bytes(object[key]) + 2; + delete object[key]; + } else if (present) { + const [next, grown] = apply(object[key], value); + object[key] = next; + growth += grown; + } else { + const [next, grown] = apply(undefined, value); + // Defined rather than assigned, so a `__proto__` key becomes a member instead of the + // object's prototype. + Object.defineProperty(object, key, { value: next, enumerable: true, writable: true, configurable: true }); + growth += bytes(key) + 2 + grown; + } + } + return [object, growth]; +} diff --git a/rs/moq-json/benches/codec.rs b/rs/moq-json/benches/codec.rs index fd2d2f58c62..d8e41e0c14e 100644 --- a/rs/moq-json/benches/codec.rs +++ b/rs/moq-json/benches/codec.rs @@ -382,13 +382,15 @@ fn snapshot_budget(c: &mut Criterion) { .map(|i| (format!("path-{i}"), json!({"bytes": 123}))) .collect(); let bytes = serde_json::to_vec(&value).unwrap(); - for bounded in [false, true] { + // A value right at its limit is the worst case: every patch lands on the boundary, so a + // decoder that measured the whole value there would slope with `entries`. + for at_limit in [false, true] { group.bench_with_input( - BenchmarkId::new(if bounded { "bounded" } else { "unbounded" }, entries), + BenchmarkId::new(if at_limit { "at_limit" } else { "default" }, entries), &bytes, |b, bytes| { let mut config = moq_json::snapshot::consumer::Config::default(); - config.max_size = bounded.then_some(8 * 1024 * 1024); + config.max_size = at_limit.then_some(bytes.len()); let mut decoder = moq_json::snapshot::Decoder::::new(config); decoder.snapshot(bytes).unwrap(); b.iter(|| decoder.delta(black_box(br#"{"path-0":{"bytes":124}}"#)).unwrap()); diff --git a/rs/moq-json/src/merge.rs b/rs/moq-json/src/merge.rs index c5afa9eea49..4a1d0305b5b 100644 --- a/rs/moq-json/src/merge.rs +++ b/rs/moq-json/src/merge.rs @@ -4,9 +4,19 @@ use serde_json::{Map, Value}; /// Validate the complete frame before changing the retained value, then apply its patch in place. /// Existing object keys and numeric values are reused instead of building a patch tree. -pub(crate) fn apply_bytes(target: &mut Value, bytes: &[u8], scratch: &RefCell) -> serde_json::Result<()> { +/// +/// Returns how much the target's compact JSON grew (negative when it shrank). Only the patch and +/// whatever it replaces or removes are measured, never the untouched rest of the value, so a peer +/// pays for every byte it makes the caller count. +pub(crate) fn apply_bytes( + target: &mut Value, + bytes: &[u8], + scratch: &RefCell, +) -> serde_json::Result { check(bytes, scratch)?; - apply_generated_bytes(target, bytes) + let growth = Cell::new(0); + apply(target, bytes, Some(&growth))?; + Ok(growth.get()) } /// Validate one JSON value's syntax, rejecting any object that repeats a key. @@ -19,17 +29,40 @@ pub(crate) fn check(bytes: &[u8], scratch: &RefCell) -> serde_json /// Apply a patch emitted by our serializer, whose keys and syntax were already checked. pub(crate) fn apply_generated_bytes(target: &mut Value, bytes: &[u8]) -> serde_json::Result<()> { + apply(target, bytes, None) +} + +fn apply(target: &mut Value, bytes: &[u8], growth: Option<&Cell>) -> serde_json::Result<()> { use serde::de::DeserializeSeed; let mut decoder = serde_json::Deserializer::from_slice(bytes); Patch { target, deletion: false, + growth, } .deserialize(&mut decoder)?; Ok(()) } -use std::cell::RefCell; +/// Bytes in `value`'s compact JSON. +fn len(value: &(impl serde::Serialize + ?Sized)) -> i64 { + struct Count(i64); + impl std::io::Write for Count { + fn write(&mut self, bytes: &[u8]) -> std::io::Result { + self.0 += bytes.len() as i64; + Ok(bytes.len()) + } + fn flush(&mut self) -> std::io::Result<()> { + Ok(()) + } + } + let mut count = Count(0); + // Neither a `Value` nor a `str` fails to serialize, and counting never fails to write. + let _ = serde_json::to_writer(&mut count, value); + count.0 +} + +use std::cell::{Cell, RefCell}; #[derive(Default)] pub(crate) struct CheckScratch { @@ -163,6 +196,24 @@ impl<'de> serde::Deserialize<'de> for Key<'de> { struct Patch<'a> { target: &'a mut Value, deletion: bool, + /// Accumulates the change in the target's compact JSON size, when the caller tracks it. + growth: Option<&'a Cell>, +} + +impl Patch<'_> { + /// Replace the target wholesale, measuring only the old and new values. + fn replace(self, value: Value) -> bool { + grow(self.growth, || len(&value) - len(self.target)); + *self.target = value; + false + } +} + +/// Add to the running growth, measuring nothing when no caller tracks it. +fn grow(growth: Option<&Cell>, by: impl FnOnce() -> i64) { + if let Some(growth) = growth { + growth.set(growth.get() + by()); + } } impl<'de> serde::de::DeserializeSeed<'de> for Patch<'_> { @@ -184,8 +235,7 @@ impl<'de> serde::de::Visitor<'de> for Patch<'_> { if self.deletion { Ok(true) } else { - *self.target = Value::Null; - Ok(false) + Ok(self.replace(Value::Null)) } } @@ -194,33 +244,27 @@ impl<'de> serde::de::Visitor<'de> for Patch<'_> { } fn visit_bool(self, value: bool) -> Result { - *self.target = Value::Bool(value); - Ok(false) + Ok(self.replace(Value::Bool(value))) } fn visit_i64(self, value: i64) -> Result { - *self.target = Value::from(value); - Ok(false) + Ok(self.replace(Value::from(value))) } fn visit_u64(self, value: u64) -> Result { - *self.target = Value::from(value); - Ok(false) + Ok(self.replace(Value::from(value))) } fn visit_f64(self, value: f64) -> Result { - *self.target = Value::from(value); - Ok(false) + Ok(self.replace(Value::from(value))) } fn visit_str(self, value: &str) -> Result { - *self.target = Value::from(value); - Ok(false) + Ok(self.replace(Value::from(value))) } fn visit_string(self, value: String) -> Result { - *self.target = Value::String(value); - Ok(false) + Ok(self.replace(Value::String(value))) } fn visit_seq>(self, mut seq: A) -> Result { @@ -228,12 +272,13 @@ impl<'de> serde::de::Visitor<'de> for Patch<'_> { while let Some(value) = seq.next_element()? { values.push(value); } - *self.target = Value::Array(values); - Ok(false) + Ok(self.replace(Value::Array(values))) } fn visit_map>(self, mut map: A) -> Result { + let growth = self.growth; if !self.target.is_object() { + grow(growth, || 2 - len(self.target)); *self.target = Value::Object(Map::new()); } let object = self.target.as_object_mut().expect("target is an object"); @@ -242,16 +287,25 @@ impl<'de> serde::de::Visitor<'de> for Patch<'_> { let remove = map.next_value_seed(Patch { target: value, deletion: true, + growth, })?; - if remove { - object.remove(key.as_ref()); + if remove && let Some(value) = object.remove(key.as_ref()) { + // The member, its colon, and the comma that separated it from a remaining one. + grow(growth, || { + -(len(key.as_ref()) + 1 + len(&value) + i64::from(!object.is_empty())) + }); } } else { + // Measured whole once inserted, so the placeholder it starts from is never counted. let mut value = Value::Null; if !map.next_value_seed(Patch { target: &mut value, deletion: true, + growth: None, })? { + grow(growth, || { + len(key.as_ref()) + 1 + len(&value) + i64::from(!object.is_empty()) + }); object.insert(key.into_owned(), value); } } @@ -275,16 +329,25 @@ mod test { (json!({"old": 1}), json!({"new": {"absent": null, "kept": 1}})), (json!(1), json!({"nested": {"absent": null, "kept": 1}})), (json!({"old": 1}), json!([1, 2, 3])), + (json!({"only": [1, 2]}), json!({"only": null, "absent": null})), + ( + json!({"a": "x", "b": 1e15}), + json!({"b": null, "a": {"esc\"aped": 9e9}}), + ), + (json!({"a": {"b": 1}}), json!({"a": "flat"})), ] { let mut expected = target.clone(); json_patch::merge(&mut expected, &patch); - apply_bytes( + let before = len(&target); + let growth = apply_bytes( &mut target, serde_json::to_string(&patch).unwrap().as_bytes(), &RefCell::new(CheckScratch::default()), ) .unwrap(); assert_eq!(target, expected); + // Counted from the touched members alone, yet exact for the whole value. + assert_eq!(before + growth, len(&target), "{patch}"); } } diff --git a/rs/moq-json/src/snapshot/consumer.rs b/rs/moq-json/src/snapshot/consumer.rs index 7d0dabb0a44..e0132323f1f 100644 --- a/rs/moq-json/src/snapshot/consumer.rs +++ b/rs/moq-json/src/snapshot/consumer.rs @@ -18,8 +18,7 @@ pub struct Config { /// [`Config::compression`](super::Config::compression). Defaults to [`Compression::None`]. pub compression: Compression, - /// Maximum bytes in an inflated frame or the reconstructed value, counted as the JSON of the - /// frames that built it until that passes the limit, then as the value's compact JSON. + /// Maximum bytes in an inflated frame or the compact JSON of the reconstructed value. /// Unset uses [`moq_flate::DEFAULT_MAX_FRAME_SIZE`], the largest snapshot a producer publishes. pub max_size: Option, } diff --git a/rs/moq-json/src/snapshot/decoder.rs b/rs/moq-json/src/snapshot/decoder.rs index ad6cc7b427c..d4890d8d661 100644 --- a/rs/moq-json/src/snapshot/decoder.rs +++ b/rs/moq-json/src/snapshot/decoder.rs @@ -35,10 +35,11 @@ pub struct Decoder { /// Maximum bytes in an inflated frame or the reconstructed value's JSON. limit: usize, - /// The reconstructed value's size in JSON bytes, counted as the snapshot plus every patch - /// applied since: a merge patch adds no more nodes to a value than its own bytes can hold. - /// Re-serializing can still print a number longer than its source (`9e9`), but the value's - /// memory follows its nodes, so the source bytes bound it as well as the compact JSON would. + /// The reconstructed value's size in JSON bytes: the snapshot's own bytes, then moved by each + /// patch's exact effect on the compact JSON. Measuring only what a patch touches keeps the + /// cost per frame proportional to that frame, never to the whole value. The snapshot is not + /// re-serialized, so a number printed longer than its source (`9e9`) goes uncounted, but the + /// value's memory follows its nodes, which its bytes bound all the same. size: usize, /// The current group's DEFLATE decoder (one window per group), rebuilt at each snapshot. @@ -111,21 +112,12 @@ impl Decoder { return Err(Error::TooLarge(self.limit)); } let current = self.current.as_mut().expect("a snapshot precedes any delta"); - crate::merge::apply_bytes(current, plain, &self.check)?; - self.size = self.size.saturating_add(plain.len()); - if self.size <= self.limit { - return Ok(()); - } - - // The bound never shrinks on its own, so once it passes the limit measure the value itself: - // deletions may have freed room. Counting the compact JSON resets the bound to the real - // size, so the walk repeats only after another `limit - size` bytes of patches, not per - // delta. An oversized value is dropped before anything can materialize it. - let mut budget = Budget(self.limit); - if serde_json::to_writer(&mut budget, current).is_err() { + let growth = crate::merge::apply_bytes(current, plain, &self.check)?; + self.size = (self.size as i64).saturating_add(growth).max(0) as usize; + // An oversized value is dropped before anything can materialize it. + if self.size > self.limit { return Err(Error::TooLarge(self.limit)); } - self.size = self.limit - budget.0; Ok(()) })(); if result.is_err() { @@ -140,22 +132,6 @@ impl Decoder { } } -struct Budget(usize); - -impl std::io::Write for Budget { - fn write(&mut self, bytes: &[u8]) -> std::io::Result { - if bytes.len() > self.0 { - return Err(std::io::Error::other("JSON size budget exceeded")); - } - self.0 -= bytes.len(); - Ok(bytes.len()) - } - - fn flush(&mut self) -> std::io::Result<()> { - Ok(()) - } -} - impl Decoder { /// Materialize the reconstructed value as `T`, or `None` before the first snapshot. ///