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 834202ecc7f..3a0cf210fb1 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,33 @@ 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("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 9948233532e..9efb5d66169 100644 --- a/js/json/src/snapshot/decoder.ts +++ b/js/json/src/snapshot/decoder.ts @@ -1,7 +1,7 @@ -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"; +import { isObject } from "../diff.ts"; import { MissingSnapshot } from "../error.ts"; import type { Config } from "./encoder.ts"; @@ -26,6 +26,11 @@ export class Decoder { #flate?: Flate; // The reconstructed value, `undefined` until the first snapshot. #current?: unknown; + // 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"> = {}) { this.#schema = config.schema; @@ -41,17 +46,27 @@ 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 [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`); + } } /** @@ -76,9 +91,58 @@ 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)); } } + +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 7457b8b947b..e0132323f1f 100644 --- a/rs/moq-json/src/snapshot/consumer.rs +++ b/rs/moq-json/src/snapshot/consumer.rs @@ -19,7 +19,7 @@ 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. + /// 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 31ece91018f..d4890d8d661 100644 --- a/rs/moq-json/src/snapshot/decoder.rs +++ b/rs/moq-json/src/snapshot/decoder.rs @@ -31,7 +31,16 @@ 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, + + /// 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. flate: Option, @@ -53,7 +62,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 +82,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 +108,17 @@ 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)); } - crate::merge::apply_bytes( - self.current.as_mut().expect("a snapshot precedes any delta"), - plain, - &self.check, - )?; - self.check_size() + let current = self.current.as_mut().expect("a snapshot precedes any delta"); + 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)); + } + Ok(()) })(); if result.is_err() { self.current = None; @@ -117,40 +126,12 @@ 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() } } -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. /// @@ -287,6 +268,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 {