PhotoCraft: An open-source, photo editor written by scratch in pure Rust
security(io): index XCF parasite names for duplicate replacement
Dylan M. Taylore75790b8780f
diff --git a/crates/io/src/xcf.rs b/crates/io/src/xcf.rsindex 4bb62cacb..d5cda4aa4 100644--- a/crates/io/src/xcf.rs+++ b/crates/io/src/xcf.rs@@ -17,6 +17,7 @@#[cfg(test)]mod tests;+use std::collections::HashMap;use std::io::Read;use std::sync::Arc;@@ -294,13 +295,21 @@ fn be_f32(b: &[u8]) -> Option<f32> {fn parasites(data: &[u8]) -> Vec<(String, Vec<u8>)> {let mut c = Cursor { b: data, pos: 0, wide: false };let mut out: Vec<(String, Vec<u8>)> = Vec::new();+ // Map each name to its output slot so duplicate replacement is O(1). The parasite+ // records are file-controlled, so scanning `out` with iter_mut().find() per record is+ // O(n^2) and lets a crafted payload with many distinct names stall import. The map+ // preserves insertion order and last-value-wins behavior.+ let mut index: HashMap<String, usize> = HashMap::new();while c.pos < data.len() {let Ok(name) = c.string() else { break };let (Ok(_flags), Ok(len)) = (c.u32(), c.u32()) else { break };let Ok(payload) = c.take(len as usize) else { break };- match out.iter_mut().find(|(n, _)| *n == name) {- Some(slot) => slot.1 = payload.to_vec(),- None => out.push((name, payload.to_vec())),+ match index.get(&name) {+ Some(&i) => out[i].1 = payload.to_vec(),+ None => {+ index.insert(name.clone(), out.len());+ out.push((name, payload.to_vec()));+ }}}outdiff --git a/crates/io/src/xcf/tests.rs b/crates/io/src/xcf/tests.rsindex ee96f1650..400887f92 100644--- a/crates/io/src/xcf/tests.rs+++ b/crates/io/src/xcf/tests.rs@@ -702,3 +702,33 @@ fn a_path_failing_below_the_top_level_lands_at_the_top_level() {assert_eq!(children, ["A"], "the valid sibling placed after the stray one still lands in the group");assert!(r.warnings.iter().any(|w| w.contains("Stray") && w.contains("item path")), "{:?}", r.warnings);}++/// A parasite name field: u32 `len+1`, then the bytes and a trailing NUL.+fn parasite_name(out: &mut Vec<u8>, name: &str) {+ out.extend_from_slice(&((name.len() as u32) + 1).to_be_bytes());+ out.extend_from_slice(name.as_bytes());+ out.push(0);+}++/// One PROP_PARASITES record: name, u32 flags, u32 len, then the payload.+fn parasite_record(out: &mut Vec<u8>, name: &str, payload: &[u8]) {+ parasite_name(out, name);+ out.extend_from_slice(&0u32.to_be_bytes());+ out.extend_from_slice(&(payload.len() as u32).to_be_bytes());+ out.extend_from_slice(payload);+}++#[test]+fn duplicate_parasites_keep_order_and_last_value() {+ // Duplicate names must resolve to the last payload while keeping first-seen order.+ // This is the behavior the name->index map must preserve versus the old linear scan.+ let mut data = Vec::new();+ parasite_record(&mut data, "gimp-comment", b"first");+ parasite_record(&mut data, "gimp-image-grid", b"grid");+ parasite_record(&mut data, "gimp-comment", b"second");+ let out = parasites(&data);+ assert_eq!(out.len(), 2, "duplicate collapses to one slot");+ assert_eq!(out[0].0, "gimp-comment");+ assert_eq!(out[0].1, b"second", "last value wins");+ assert_eq!(out[1].0, "gimp-image-grid", "insertion order preserved");+}