From a8ed8ca7749643887394363789b269ea47f78a07 Mon Sep 17 00:00:00 2001 From: jayzed80 Date: Sat, 31 Oct 2026 09:45:18 +0000 Subject: [PATCH 1/5] fix(effects): parse_cube rejects a hostile LUT_3D_SIZE before cubing it --- crates/effects/src/utility.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/crates/effects/src/utility.rs b/crates/effects/src/utility.rs index e41b42e99..17f0a243b 100644 --- a/crates/effects/src/utility.rs +++ b/crates/effects/src/utility.rs @@ -202,9 +202,12 @@ pub fn parse_cube(text: &str) -> Option { _ => rows.push(three(&mut it)?), } } + if lut.n3 > 256 { + return None; + } let need1 = lut.n1; let need3 = lut.n3 * lut.n3 * lut.n3; - if (lut.n1 == 0 && lut.n3 == 0) || lut.n1 == 1 || lut.n3 == 1 || rows.len() != need1 + need3 || lut.n3 > 256 { + if (lut.n1 == 0 && lut.n3 == 0) || lut.n1 == 1 || lut.n3 == 1 || Some(rows.len()) != need1.checked_add(need3) { return None; } lut.d1 = rows[..need1].to_vec(); @@ -464,6 +467,10 @@ mod tests { fn bad_cube_is_passthrough() { assert!(parse_cube("LUT_3D_SIZE 2\n0 0 0\n1 1 1\n").is_none()); assert!(parse_cube("LUT_3D_SIZE 2\n0 0 zz\n").is_none()); + // A hostile size must be rejected, not overflow `n3^3` or `n1 + n3^3`. + assert!(parse_cube("LUT_3D_SIZE 4294967296\n0 0 0\n").is_none()); + assert!(parse_cube(&format!("LUT_3D_SIZE {}\n0 0 0\n", usize::MAX)).is_none()); + assert!(parse_cube(&format!("LUT_1D_SIZE {}\nLUT_3D_SIZE 2\n0 0 0\n", usize::MAX)).is_none()); let img = test_img(); let out = run("ec.utility.applylut", &[("lut", Value::Str("LUT_3D_SIZE 3\n0 0 0\n".into()))], img.clone()); assert_eq!(out.img, img); From bad3e7c2320d5f8aa8608d3dc725ed301b3ed366 Mon Sep 17 00:00:00 2001 From: jayzed80 Date: Sat, 31 Oct 2026 09:45:18 +0000 Subject: [PATCH 2/5] fix(effects): parse_cube rejects a hostile LUT_3D_SIZE before cubing it Co-Authored-By: Claude Sonnet 5.5 --- crates/effects/src/utility.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/crates/effects/src/utility.rs b/crates/effects/src/utility.rs index e87c6d3ff..b38ded8db 100644 --- a/crates/effects/src/utility.rs +++ b/crates/effects/src/utility.rs @@ -215,9 +215,12 @@ pub fn parse_cube(text: &str) -> Option { _ => rows.push(three(&mut it)?), } } + if lut.n3 > 256 { + return None; + } let need1 = lut.n1; let need3 = lut.n3 * lut.n3 * lut.n3; - if (lut.n1 == 0 && lut.n3 == 0) || lut.n1 == 1 || lut.n3 == 1 || rows.len() != need1 + need3 || lut.n3 > 256 { + if (lut.n1 == 0 && lut.n3 == 0) || lut.n1 == 1 || lut.n3 == 1 || Some(rows.len()) != need1.checked_add(need3) { return None; } lut.d1 = rows[..need1].to_vec(); @@ -477,6 +480,10 @@ mod tests { fn bad_cube_is_passthrough() { assert!(parse_cube("LUT_3D_SIZE 2\n0 0 0\n1 1 1\n").is_none()); assert!(parse_cube("LUT_3D_SIZE 2\n0 0 zz\n").is_none()); + // A hostile size must be rejected, not overflow `n3^3` or `n1 + n3^3`. + assert!(parse_cube("LUT_3D_SIZE 4294967296\n0 0 0\n").is_none()); + assert!(parse_cube(&format!("LUT_3D_SIZE {}\n0 0 0\n", usize::MAX)).is_none()); + assert!(parse_cube(&format!("LUT_1D_SIZE {}\nLUT_3D_SIZE 2\n0 0 0\n", usize::MAX)).is_none()); let img = test_img(); let out = run("ec.utility.applylut", &[("lut", Value::Str("LUT_3D_SIZE 3\n0 0 0\n".into()))], img.clone()); assert_eq!(out.img, img); From ae2d595d9df799e8e8ed4545e38385fb809bbeca Mon Sep 17 00:00:00 2001 From: arnav-hiwarkar Date: Tue, 17 Nov 2026 17:25:47 +0530 Subject: [PATCH 3/5] fix(effects): exercise the wrapped combined cube row count --- crates/effects/src/utility.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/effects/src/utility.rs b/crates/effects/src/utility.rs index b38ded8db..171b9cc8e 100644 --- a/crates/effects/src/utility.rs +++ b/crates/effects/src/utility.rs @@ -483,7 +483,7 @@ mod tests { // A hostile size must be rejected, not overflow `n3^3` or `n1 + n3^3`. assert!(parse_cube("LUT_3D_SIZE 4294967296\n0 0 0\n").is_none()); assert!(parse_cube(&format!("LUT_3D_SIZE {}\n0 0 0\n", usize::MAX)).is_none()); - assert!(parse_cube(&format!("LUT_1D_SIZE {}\nLUT_3D_SIZE 2\n0 0 0\n", usize::MAX)).is_none()); + assert!(parse_cube(&format!("LUT_1D_SIZE {}\nLUT_3D_SIZE 2\n{}", usize::MAX, "0 0 0\n".repeat(7))).is_none()); let img = test_img(); let out = run("ec.utility.applylut", &[("lut", Value::Str("LUT_3D_SIZE 3\n0 0 0\n".into()))], img.clone()); assert_eq!(out.img, img); From 1eb4f39808e7f3e2855e787d69ffad528b576b3b Mon Sep 17 00:00:00 2001 From: arnav-hiwarkar Date: Tue, 17 Nov 2026 17:27:22 +0530 Subject: [PATCH 4/5] fix(effects): bound cube data row collection --- crates/effects/src/utility.rs | 38 ++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/crates/effects/src/utility.rs b/crates/effects/src/utility.rs index 171b9cc8e..ff8ff208d 100644 --- a/crates/effects/src/utility.rs +++ b/crates/effects/src/utility.rs @@ -169,6 +169,10 @@ pub struct Lut { pub dmax: [f32; 3], } +// Cube LUT Specification 1.0 §§6–7 permits at most 65,536 1D rows and a 256³ +// lattice. Allow their sum for combined shaper + 3D files, regardless of header order. +const MAX_CUBE_ROWS: usize = 65_536 + 256 * 256 * 256; + /// Parse `.cube` text (TITLE, LUT_1D_SIZE, LUT_3D_SIZE, DOMAIN_MIN/MAX, data red-fastest). pub fn parse_cube(text: &str) -> Option { let mut lut = Lut { n1: 0, d1: vec![], n3: 0, d3: vec![], dmin: [0.0; 3], dmax: [1.0; 3] }; @@ -212,7 +216,14 @@ pub fn parse_cube(text: &str) -> Option { lut.dmax = [hi; 3]; } k if k.starts_with(|c: char| c.is_ascii_alphabetic()) => continue, - _ => rows.push(three(&mut it)?), + _ => { + if rows.len() == MAX_CUBE_ROWS { + return None; + } + let row = three(&mut it)?; + rows.try_reserve(1).ok()?; + rows.push(row); + } } } if lut.n3 > 256 { @@ -223,8 +234,8 @@ pub fn parse_cube(text: &str) -> Option { if (lut.n1 == 0 && lut.n3 == 0) || lut.n1 == 1 || lut.n3 == 1 || Some(rows.len()) != need1.checked_add(need3) { return None; } - lut.d1 = rows[..need1].to_vec(); - lut.d3 = rows[need1..].to_vec(); + lut.d1 = rows.get(..need1)?.to_vec(); + lut.d3 = rows.get(need1..)?.to_vec(); Some(lut) } @@ -491,6 +502,27 @@ mod tests { assert_eq!(out.img, img); } + #[test] + fn cube_accepts_maximum_1d_shaper_with_3d_table() { + let text = format!("LUT_1D_SIZE 65536\nLUT_3D_SIZE 2\n{}", "0 0 0\n".repeat(65_536 + 8)); + let lut = parse_cube(&text).unwrap(); + assert_eq!(lut.d1.len(), 65_536); + assert_eq!(lut.d3.len(), 8); + // Keep the parser's existing tolerance for headers following data. + let text = format!("{}LUT_1D_SIZE 2\nLUT_3D_SIZE 2\n", "0 0 0\n".repeat(10)); + let lut = parse_cube(&text).unwrap(); + assert_eq!(lut.d1, vec![[0.0; 3]; 2]); + assert_eq!(lut.d3, vec![[0.0; 3]; 8]); + } + + #[test] + fn cube_row_collection_is_bounded() { + // Beyond the largest 3D table plus the largest 1D shaper in Cube LUT 1.0. + let count = 256 * 256 * 256 + 65_536 + 1; + let text = format!("LUT_1D_SIZE {count}\n{}", "0 0 0\n".repeat(count)); + assert!(parse_cube(&text).is_none()); + } + #[test] fn one_d_lut_with_domain() { let lut = parse_cube("LUT_1D_SIZE 2\nDOMAIN_MIN 0 0 0\nDOMAIN_MAX 2 2 2\n1 1 1\n0 0 0\n").unwrap(); From 1654633bcf1819a1db4cba14e6f11b25c979cb37 Mon Sep 17 00:00:00 2001 From: arnav-hiwarkar Date: Tue, 17 Nov 2026 17:33:35 +0530 Subject: [PATCH 5/5] fix(effects): test the cube row bound without a huge input --- crates/effects/src/utility.rs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/crates/effects/src/utility.rs b/crates/effects/src/utility.rs index 72f07cde9..b99de477b 100644 --- a/crates/effects/src/utility.rs +++ b/crates/effects/src/utility.rs @@ -222,6 +222,10 @@ const MAX_CUBE_ROWS: usize = 65_536 + 256 * 256 * 256; /// Parse `.cube` text (TITLE, LUT_1D_SIZE, LUT_3D_SIZE, DOMAIN_MIN/MAX, data red-fastest). pub fn parse_cube(text: &str) -> Option { + parse_cube_limited(text, MAX_CUBE_ROWS) +} + +fn parse_cube_limited(text: &str, max_rows: usize) -> Option { let mut lut = Lut { n1: 0, d1: vec![], n3: 0, d3: vec![], dmin: [0.0; 3], dmax: [1.0; 3] }; let mut rows: Vec<[f32; 3]> = Vec::new(); let three = |it: &mut std::str::SplitWhitespace| -> Option<[f32; 3]> { @@ -264,10 +268,11 @@ pub fn parse_cube(text: &str) -> Option { } k if k.starts_with(|c: char| c.is_ascii_alphabetic()) => continue, _ => { - if rows.len() == MAX_CUBE_ROWS { + if rows.len() == max_rows { return None; } let row = three(&mut it)?; + // Make buffer growth fallible before push can allocate. rows.try_reserve(1).ok()?; rows.push(row); } @@ -719,10 +724,11 @@ mod tests { #[test] fn cube_row_collection_is_bounded() { - // Beyond the largest 3D table plus the largest 1D shaper in Cube LUT 1.0. - let count = 256 * 256 * 256 + 65_536 + 1; - let text = format!("LUT_1D_SIZE {count}\n{}", "0 0 0\n".repeat(count)); - assert!(parse_cube(&text).is_none()); + assert_eq!(MAX_CUBE_ROWS, 65_536 + 256 * 256 * 256); + let text = "LUT_1D_SIZE 2\n0 0 0\n1 1 1\n"; + let lut = parse_cube_limited(text, 2).unwrap(); + assert_eq!(lut.d1, vec![[0.0; 3], [1.0; 3]]); + assert!(parse_cube_limited("LUT_1D_SIZE 3\n0 0 0\n0.5 0.5 0.5\n1 1 1\n", 2).is_none()); } #[test]