From d550d3d476e83b0b1987522d0cfee99128c138c4 Mon Sep 17 00:00:00 2001 From: arnav-hiwarkar Date: Tue, 17 Nov 2026 22:09:04 +0530 Subject: [PATCH 1/7] #3830: regression tests for hostile .3dl meshes and row-less .spi3d lattices --- crates/effects/src/ocio/tests.rs | 34 ++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/crates/effects/src/ocio/tests.rs b/crates/effects/src/ocio/tests.rs index d40d3b046..3952a8ab3 100644 --- a/crates/effects/src/ocio/tests.rs +++ b/crates/effects/src/ocio/tests.rs @@ -547,3 +547,37 @@ fn an_ocio_transform_file_is_read_again_once_it_changes() { assert!(Arc::ptr_eq(&inline, &load_file(BLACK).unwrap())); std::fs::remove_dir_all(&dir).unwrap(); } + +#[test] +fn a_3dl_with_a_hostile_mesh_is_refused_without_overflow() { + // #3830: 2 642 246³ is past usize::MAX, so cubing the mesh length overflowed. + let huge = "0 ".repeat(2_642_246) + "\n0 0 0\n"; + assert!(parse_file(&huge, "3dl").is_none()); + // A mesh longer than any lattice the readers accept, and a mesh whose rows run out. + let long = (0..130).map(|i| i.to_string()).collect::>().join(" ") + "\n0 0 0\n"; + assert!(parse_file(&long, "3dl").is_none()); + let short = (0..17).map(|i| (i * 64).to_string()).collect::>().join(" ") + "\n0 0 0\n1 1 1\n"; + assert!(parse_file(&short, "3dl").is_none()); + // A normal small table still loads. + let mut ok = String::from("0 511 1023\n"); + for k in 0..27 { + ok += &format!("{} {} {}\n", k * 30, k * 30, k * 30); + } + assert!(matches!(parse_file(&ok, "3dl"), Some(FileXform::Lut { lut, .. }) if lut.n3 == 3 && lut.d3.len() == 27)); +} + +#[test] +fn a_spi3d_without_its_rows_is_refused_before_the_lattice_is_reserved() { + // #3831: a header-only 129³ claim must not reach the reader that reserves the whole lattice. + let empty = "SPILUT 1.0\n3 3\n129 129 129\n"; + assert!(!lut::spi3d_rows_present(empty)); + assert!(!lut::spi3d_rows_present("SPILUT 1.0\n3 3\n2 2 2\n0 0 0 1 1 1\n")); + assert!(!lut::spi3d_rows_present("SPILUT 1.0\n3 3\n99999999999 2 2\n")); + assert!(parse_spi3d(empty).is_none()); + let mut ok = String::from("SPILUT 1.0\n3 3\n2 2 2\n"); + for i in 0..8 { + ok += &format!("{} {} {} 0 0 0\n", i & 1, (i >> 1) & 1, i >> 2); + } + assert!(lut::spi3d_rows_present(&ok)); + assert!(parse_spi3d(&ok).is_some_and(|l| l.n3 == 2 && l.d3.len() == 8)); +} From a65a1f746ff41305f395801d6d4ae130b36797c0 Mon Sep 17 00:00:00 2001 From: arnav-hiwarkar Date: Tue, 17 Nov 2026 22:11:34 +0530 Subject: [PATCH 2/7] #3830: cap the .3dl mesh and rows and cube with checked math; #3831: refuse a .spi3d short of its rows before the lattice is reserved --- crates/effects/src/ocio/lut.rs | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/crates/effects/src/ocio/lut.rs b/crates/effects/src/ocio/lut.rs index 4618a4da0..fc16da0ea 100644 --- a/crates/effects/src/ocio/lut.rs +++ b/crates/effects/src/ocio/lut.rs @@ -32,10 +32,24 @@ pub fn parse_spi1d(text: &str) -> Option { /// Parse a Sony Imageworks `.spi3d` lattice: `SPILUT 1.0`, `3 3`, the sizes, then one /// `r g b R G B` line per lattice point (indices, then the colour). pub fn parse_spi3d(text: &str) -> Option { + if !spi3d_rows_present(text) { + return None; + } let value = craft_cms::sony_lut::parse_spi3d(text)?; Some(Lut { n1: 0, d1: vec![], n3: value.size, d3: value.values, dmin: [0.0; 3], dmax: [1.0; 3] }) } +/// Whether a `.spi3d` file has at least as many rows as its header's lattice claims. The shared +/// reader reserves the whole lattice before it reads a row, so a short file is turned away +/// first (#3831). Counting stops at the claimed volume; nothing is allocated. +pub(super) fn spi3d_rows_present(text: &str) -> bool { + let mut lines = text.lines().map(str::trim).filter(|l| !l.is_empty()).skip(2); + let Some(n) = lines.next().and_then(|l| l.split_whitespace().next()?.parse::().ok()) else { + return false; + }; + n.checked_pow(3).is_some_and(|count| lines.take(count).count() == count) +} + /// Parse an Autodesk `.3dl` lattice (integers, blue fastest; an optional input-mesh line). pub fn parse_3dl(text: &str) -> Option { let mut mesh: Option> = None; @@ -56,8 +70,15 @@ pub fn parse_3dl(text: &str) -> Option { } let v: Vec = l.split_whitespace().filter_map(|t| t.parse().ok()).collect(); if v.len() > 3 && mesh.is_none() && rows.is_empty() { + // The mesh length is the lattice side: refuse one no lattice reader accepts (#3830). + if v.len() > MAX_LATTICE { + return None; + } mesh = Some(v); } else if v.len() == 3 { + if rows.len() >= MAX_LATTICE.pow(3) { + return None; + } rows.push([v[0], v[1], v[2]]); } else { return None; @@ -75,7 +96,7 @@ pub fn parse_3dl(text: &str) -> Option { Some(m) => m.len(), None => (rows.len() as f64).cbrt().round() as usize, }; - if n < 2 || rows.len() != n * n * n { + if !(2..=MAX_LATTICE).contains(&n) || n.checked_pow(3) != Some(rows.len()) { return None; } let maxv = rows.iter().flat_map(|r| r.iter()).fold(0.0f64, |m, &v| m.max(v)); @@ -92,7 +113,7 @@ pub fn parse_3dl(text: &str) -> Option { Some(Lut { n1: 0, d1: vec![], n3: n, d3, dmin: [0.0; 3], dmax: [1.0; 3] }) } -/// Largest lattice side the lattice readers accept (`.spi3d`, `.csp`). +/// Largest lattice side the lattice readers accept (`.spi3d`, `.csp`, `.3dl`). const MAX_LATTICE: usize = 129; /// Most entries of a 1D table (interpolated by index, so capped like the lattices: 2²⁴). const MAX_1D: usize = 1 << 24; From 6232bed5562da9676cf223cba9ae66fafccc8703 Mon Sep 17 00:00:00 2001 From: arnav-hiwarkar Date: Tue, 17 Nov 2026 22:20:14 +0530 Subject: [PATCH 3/7] #3830: stop collecting a .3dl line's numbers past the mesh limit --- crates/effects/src/ocio/lut.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/effects/src/ocio/lut.rs b/crates/effects/src/ocio/lut.rs index fc16da0ea..f3622e990 100644 --- a/crates/effects/src/ocio/lut.rs +++ b/crates/effects/src/ocio/lut.rs @@ -68,7 +68,7 @@ pub fn parse_3dl(text: &str) -> Option { } continue; } - let v: Vec = l.split_whitespace().filter_map(|t| t.parse().ok()).collect(); + let v: Vec = l.split_whitespace().filter_map(|t| t.parse().ok()).take(MAX_LATTICE + 1).collect(); if v.len() > 3 && mesh.is_none() && rows.is_empty() { // The mesh length is the lattice side: refuse one no lattice reader accepts (#3830). if v.len() > MAX_LATTICE { From 104875a89755d973bea5468cb351344bcd3ceb9b Mon Sep 17 00:00:00 2001 From: arnav-hiwarkar Date: Tue, 17 Nov 2026 22:28:16 +0530 Subject: [PATCH 4/7] #3830: read only the three .3dl Mesh header tokens it uses --- crates/effects/src/ocio/lut.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/effects/src/ocio/lut.rs b/crates/effects/src/ocio/lut.rs index f3622e990..f7ae08986 100644 --- a/crates/effects/src/ocio/lut.rs +++ b/crates/effects/src/ocio/lut.rs @@ -62,7 +62,7 @@ pub fn parse_3dl(text: &str) -> Option { } if l.starts_with(|c: char| c.is_ascii_alphabetic()) { // "Mesh " (Lustre header); other keywords are ignored. - let t: Vec<&str> = l.split_whitespace().collect(); + let t: Vec<&str> = l.split_whitespace().take(3).collect(); if t[0].eq_ignore_ascii_case("mesh") && t.len() >= 3 { out_bits = t[2].parse().ok(); } From 648ae785915de614063f1afbfeb340a743185bf8 Mon Sep 17 00:00:00 2001 From: arnav-hiwarkar Date: Wed, 18 Nov 2026 15:27:36 +0530 Subject: [PATCH 5/7] #3830: test the .3dl cap through an injectable limit; drop the local .spi3d precheck (#3831 belongs in craft-cms) --- crates/effects/src/ocio/lut.rs | 27 +++++++++----------------- crates/effects/src/ocio/tests.rs | 33 +++++++++++++++++--------------- 2 files changed, 27 insertions(+), 33 deletions(-) diff --git a/crates/effects/src/ocio/lut.rs b/crates/effects/src/ocio/lut.rs index f7ae08986..6868dff46 100644 --- a/crates/effects/src/ocio/lut.rs +++ b/crates/effects/src/ocio/lut.rs @@ -32,26 +32,17 @@ pub fn parse_spi1d(text: &str) -> Option { /// Parse a Sony Imageworks `.spi3d` lattice: `SPILUT 1.0`, `3 3`, the sizes, then one /// `r g b R G B` line per lattice point (indices, then the colour). pub fn parse_spi3d(text: &str) -> Option { - if !spi3d_rows_present(text) { - return None; - } let value = craft_cms::sony_lut::parse_spi3d(text)?; Some(Lut { n1: 0, d1: vec![], n3: value.size, d3: value.values, dmin: [0.0; 3], dmax: [1.0; 3] }) } -/// Whether a `.spi3d` file has at least as many rows as its header's lattice claims. The shared -/// reader reserves the whole lattice before it reads a row, so a short file is turned away -/// first (#3831). Counting stops at the claimed volume; nothing is allocated. -pub(super) fn spi3d_rows_present(text: &str) -> bool { - let mut lines = text.lines().map(str::trim).filter(|l| !l.is_empty()).skip(2); - let Some(n) = lines.next().and_then(|l| l.split_whitespace().next()?.parse::().ok()) else { - return false; - }; - n.checked_pow(3).is_some_and(|count| lines.take(count).count() == count) -} - /// Parse an Autodesk `.3dl` lattice (integers, blue fastest; an optional input-mesh line). pub fn parse_3dl(text: &str) -> Option { + parse_3dl_limited(text, MAX_LATTICE) +} + +/// [`parse_3dl`] with the largest accepted lattice side as a parameter, so tests can reach it. +pub(super) fn parse_3dl_limited(text: &str, max_side: usize) -> Option { let mut mesh: Option> = None; let mut rows: Vec<[f64; 3]> = vec![]; let mut out_bits: Option = None; @@ -68,15 +59,15 @@ pub fn parse_3dl(text: &str) -> Option { } continue; } - let v: Vec = l.split_whitespace().filter_map(|t| t.parse().ok()).take(MAX_LATTICE + 1).collect(); + let v: Vec = l.split_whitespace().filter_map(|t| t.parse().ok()).take(max_side.saturating_add(1)).collect(); if v.len() > 3 && mesh.is_none() && rows.is_empty() { // The mesh length is the lattice side: refuse one no lattice reader accepts (#3830). - if v.len() > MAX_LATTICE { + if v.len() > max_side { return None; } mesh = Some(v); } else if v.len() == 3 { - if rows.len() >= MAX_LATTICE.pow(3) { + if rows.len() >= max_side.saturating_pow(3) { return None; } rows.push([v[0], v[1], v[2]]); @@ -96,7 +87,7 @@ pub fn parse_3dl(text: &str) -> Option { Some(m) => m.len(), None => (rows.len() as f64).cbrt().round() as usize, }; - if !(2..=MAX_LATTICE).contains(&n) || n.checked_pow(3) != Some(rows.len()) { + if !(2..=max_side).contains(&n) || n.checked_pow(3) != Some(rows.len()) { return None; } let maxv = rows.iter().flat_map(|r| r.iter()).fold(0.0f64, |m, &v| m.max(v)); diff --git a/crates/effects/src/ocio/tests.rs b/crates/effects/src/ocio/tests.rs index 3952a8ab3..a11fe32f5 100644 --- a/crates/effects/src/ocio/tests.rs +++ b/crates/effects/src/ocio/tests.rs @@ -553,9 +553,7 @@ fn a_3dl_with_a_hostile_mesh_is_refused_without_overflow() { // #3830: 2 642 246³ is past usize::MAX, so cubing the mesh length overflowed. let huge = "0 ".repeat(2_642_246) + "\n0 0 0\n"; assert!(parse_file(&huge, "3dl").is_none()); - // A mesh longer than any lattice the readers accept, and a mesh whose rows run out. - let long = (0..130).map(|i| i.to_string()).collect::>().join(" ") + "\n0 0 0\n"; - assert!(parse_file(&long, "3dl").is_none()); + // A mesh whose rows run out. let short = (0..17).map(|i| (i * 64).to_string()).collect::>().join(" ") + "\n0 0 0\n1 1 1\n"; assert!(parse_file(&short, "3dl").is_none()); // A normal small table still loads. @@ -566,18 +564,23 @@ fn a_3dl_with_a_hostile_mesh_is_refused_without_overflow() { assert!(matches!(parse_file(&ok, "3dl"), Some(FileXform::Lut { lut, .. }) if lut.n3 == 3 && lut.d3.len() == 27)); } +/// A complete `.3dl` table of side `n`, with its input mesh line or without one. +fn table_3dl(n: usize, mesh: bool) -> String { + let mut text = if mesh { (0..n).map(|i| (i * 1023 / (n - 1)).to_string()).collect::>().join(" ") + "\n" } else { String::new() }; + for k in 0..n * n * n { + text += &format!("{} {} {}\n", k % 1024, k % 1024, k % 1024); + } + text +} + #[test] -fn a_spi3d_without_its_rows_is_refused_before_the_lattice_is_reserved() { - // #3831: a header-only 129³ claim must not reach the reader that reserves the whole lattice. - let empty = "SPILUT 1.0\n3 3\n129 129 129\n"; - assert!(!lut::spi3d_rows_present(empty)); - assert!(!lut::spi3d_rows_present("SPILUT 1.0\n3 3\n2 2 2\n0 0 0 1 1 1\n")); - assert!(!lut::spi3d_rows_present("SPILUT 1.0\n3 3\n99999999999 2 2\n")); - assert!(parse_spi3d(empty).is_none()); - let mut ok = String::from("SPILUT 1.0\n3 3\n2 2 2\n"); - for i in 0..8 { - ok += &format!("{} {} {} 0 0 0\n", i & 1, (i >> 1) & 1, i >> 2); +fn a_3dl_lattice_is_capped_at_the_largest_side() { + // #3830: a complete table at the cap loads, and one a side larger is refused, with or without + // its mesh line (without one, row collection stops at the cap's volume). + for mesh in [true, false] { + let at = lut::parse_3dl_limited(&table_3dl(4, mesh), 4); + assert!(at.is_some_and(|l| l.n3 == 4 && l.d3.len() == 64), "mesh {mesh}"); + assert!(lut::parse_3dl_limited(&table_3dl(5, mesh), 4).is_none(), "mesh {mesh}"); + assert!(lut::parse_3dl_limited(&table_3dl(5, mesh), 5).is_some(), "mesh {mesh}"); } - assert!(lut::spi3d_rows_present(&ok)); - assert!(parse_spi3d(&ok).is_some_and(|l| l.n3 == 2 && l.d3.len() == 8)); } From bf31b6527123afbcd3fc53be041ba51e3afe213d Mon Sep 17 00:00:00 2001 From: arnav-hiwarkar Date: Wed, 18 Nov 2026 15:38:08 +0530 Subject: [PATCH 6/7] #3830: keep four .3dl tokens and a spare mesh row at small limits --- crates/effects/src/ocio/lut.rs | 6 ++++-- crates/effects/src/ocio/tests.rs | 5 +++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/crates/effects/src/ocio/lut.rs b/crates/effects/src/ocio/lut.rs index 6868dff46..8424e33cd 100644 --- a/crates/effects/src/ocio/lut.rs +++ b/crates/effects/src/ocio/lut.rs @@ -59,7 +59,7 @@ pub(super) fn parse_3dl_limited(text: &str, max_side: usize) -> Option { } continue; } - let v: Vec = l.split_whitespace().filter_map(|t| t.parse().ok()).take(max_side.saturating_add(1)).collect(); + let v: Vec = l.split_whitespace().filter_map(|t| t.parse().ok()).take(max_side.max(3).saturating_add(1)).collect(); if v.len() > 3 && mesh.is_none() && rows.is_empty() { // The mesh length is the lattice side: refuse one no lattice reader accepts (#3830). if v.len() > max_side { @@ -67,7 +67,9 @@ pub(super) fn parse_3dl_limited(text: &str, max_side: usize) -> Option { } mesh = Some(v); } else if v.len() == 3 { - if rows.len() >= max_side.saturating_pow(3) { + // One spare row while no mesh is known: a 3-entry mesh reads as a data row until the + // count tells them apart below. + if rows.len() >= max_side.saturating_pow(3).saturating_add(usize::from(mesh.is_none())) { return None; } rows.push([v[0], v[1], v[2]]); diff --git a/crates/effects/src/ocio/tests.rs b/crates/effects/src/ocio/tests.rs index a11fe32f5..afeabea22 100644 --- a/crates/effects/src/ocio/tests.rs +++ b/crates/effects/src/ocio/tests.rs @@ -583,4 +583,9 @@ fn a_3dl_lattice_is_capped_at_the_largest_side() { assert!(lut::parse_3dl_limited(&table_3dl(5, mesh), 4).is_none(), "mesh {mesh}"); assert!(lut::parse_3dl_limited(&table_3dl(5, mesh), 5).is_some(), "mesh {mesh}"); } + // A 3-entry mesh reads as a 28th row until the count separates it, and a four-value row is + // still refused at the smallest limit. + assert!(lut::parse_3dl_limited(&table_3dl(3, true), 3).is_some_and(|l| l.n3 == 3)); + let four = table_3dl(2, false).replacen("7 7 7", "7 7 7 7", 1); + assert!(lut::parse_3dl_limited(&four, 2).is_none()); } From c6612da7986ada222eeff03b8b9798b14811c00f Mon Sep 17 00:00:00 2001 From: arnav-hiwarkar Date: Wed, 18 Nov 2026 15:46:04 +0530 Subject: [PATCH 7/7] #3830: tell a 3-entry .3dl mesh apart by the row count alone --- crates/effects/src/ocio/lut.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/crates/effects/src/ocio/lut.rs b/crates/effects/src/ocio/lut.rs index 8424e33cd..8674d9bd9 100644 --- a/crates/effects/src/ocio/lut.rs +++ b/crates/effects/src/ocio/lut.rs @@ -77,12 +77,8 @@ pub(super) fn parse_3dl_limited(text: &str, max_side: usize) -> Option { return None; } } - let is_cube = |k: usize| { - let n = (k as f64).cbrt().round() as usize; - n * n * n == k - }; - // A 3-entry input mesh (3³ lattice) looks like a data row: tell them apart by the count. - if mesh.is_none() && !is_cube(rows.len()) && rows.len() == 28 { + // A 3-entry input mesh (3³ lattice) looks like a data row: 28 rows are a mesh and 27. + if mesh.is_none() && rows.len() == 28 { mesh = Some(rows.remove(0).to_vec()); } let n = match &mesh {