mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
security(io): index XCF parasite names for duplicate replacement
parasites() implemented last-duplicate-wins by scanning all previously collected names with iter_mut().find() per record. Parasite records are file-controlled, so many distinct names caused O(n^2) CPU work before unrecognized metadata was discarded. Maintain a name-to-index map so replacement is O(1) while preserving insertion order and last-value-wins behavior. Finding: 6107f41e-9b17-4632-a9df-f88a74a796a8 (CWE-407)
This commit is contained in:
1 parent
f42d7ef41a
commit
e75790b878
2 files changed
+42
-3
No files matched your search
+12
-3
@@ -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()));
|
||||
}
|
||||
}
|
||||
}
|
||||
out
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
Reference in new issue
Block a user