From 0d88706ec55147cb7a7e481bc706ba31e29003ca Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Thu, 12 Nov 2026 06:28:53 -0500 Subject: [PATCH] security(meta): bound namespace declarations in scope while parsing XMP * security(meta): bound namespace declarations in scope while parsing XMP * address review: a readable sidecar that fails the merge read-back is kept and the save fails * address review: a sidecar over the XMP complexity limit is kept and the save fails * merge main into fix/sec-xmp-namespace-bound * address review: text with no rdf:RDF element is backed up and replaced, not kept as readable * xmp merge: open a self-closing rdf:RDF element instead of replacing the sidecar --- crates/engine/src/sidecar.rs | 89 +++++++++++++++++++++++++++++++----- crates/meta/src/xmp.rs | 13 ++++++ crates/meta/src/xmp/tests.rs | 9 ++++ crates/meta/src/xmp_merge.rs | 34 ++++++++++++-- 4 files changed, 130 insertions(+), 15 deletions(-) diff --git a/crates/engine/src/sidecar.rs b/crates/engine/src/sidecar.rs index 9836ca4f07..fdc72522f7 100644 --- a/crates/engine/src/sidecar.rs +++ b/crates/engine/src/sidecar.rs @@ -784,17 +784,28 @@ fn sidecar_contents(path: &Path, fresh: String, stamp: &str) -> std::io::Result< return Ok((fresh, false, None)); } let rules = lightcraft_meta::MergeRules { owned: OWNED, owned_if_present: OWNED_IF_STATED }; - match std::str::from_utf8(&bytes) - .map_err(|e| e.to_string()) - .and_then(|old| lightcraft_meta::merge_xmp(old, &fresh, rules).map_err(|e| e.to_string())) - { - Ok(merged) => Ok((merged, true, None)), - Err(why) => { - let b = backup(path, stamp)?; - log::warn!("{}: not readable as XMP ({why}); kept a copy as {} before replacing it", path.display(), b.display()); - Ok((fresh, false, Some(b))) - } - } + let why = match std::str::from_utf8(&bytes) { + Ok(old) => match lightcraft_meta::merge_xmp(old, &fresh, rules) { + Ok(merged) => return Ok((merged, true, None)), + // a readable sidecar whose merge fails (e.g. the merged packet exceeds the XMP limits), or one + // that is valid but over the parser's complexity limits: fail the save and keep the file + // as it is, rather than replace metadata that could be read + // (text with no `rdf:RDF` element parses as empty metadata but holds nothing to keep: it is replaced + // after a backup, as any sidecar that is not XMP) + Err(why) + if why == lightcraft_meta::XmpError::TooComplex + || (lightcraft_meta::parse_xmp(old.strip_prefix('\u{feff}').unwrap_or(old)).is_ok() + && !matches!(&why, lightcraft_meta::XmpError::Xml(m) if m.contains("no rdf:RDF element"))) => + { + return Err(std::io::Error::other(format!("the sidecar is readable but could not be merged ({why}); it was left unchanged"))); + } + Err(why) => why.to_string(), + }, + Err(why) => why.to_string(), + }; + let b = backup(path, stamp)?; + log::warn!("{}: not readable as XMP ({why}); kept a copy as {} before replacing it", path.display(), b.display()); + Ok((fresh, false, Some(b))) } /// Key of the stem sidecar a file maps to (case-insensitive). @@ -1595,6 +1606,62 @@ mod tests { std::fs::remove_dir_all(dir).unwrap(); } + /// A readable sidecar whose merged packet fails read-back (too many namespace declarations in scope) is kept: the + /// save fails instead of moving it to a backup and writing fresh content only. + #[test] + fn a_readable_sidecar_that_cannot_be_merged_is_kept() { + let dir = std::env::temp_dir().join(format!("lightcraft-sidecar-keep-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + let decls: String = (0..500).map(|i| format!(" xmlns:n{i}=\"urn:n:{i}\"")).collect(); + let old = format!( + "" + ); + assert!(lightcraft_meta::parse_xmp(&old).is_ok(), "the existing sidecar is readable"); + let path = dir.join("a.xmp"); + std::fs::write(&path, &old).unwrap(); + let fresh = lightcraft_meta::write_xmp_lc(&lightcraft_meta::Metadata { rating: Some(3), ..Default::default() }, &[("settings", "{}")]); + assert!(super::sidecar_contents(&path, fresh, "t").is_err()); + assert_eq!(std::fs::read_to_string(&path).unwrap(), old); + assert_eq!(std::fs::read_dir(&dir).unwrap().count(), 1, "no backup was made"); + std::fs::remove_dir_all(dir).unwrap(); + } + + /// Text that is not XMP at all (it parses as empty metadata, but has no `rdf:RDF` element) is backed up and replaced. + #[test] + fn a_sidecar_with_no_rdf_element_is_backed_up_and_replaced() { + let dir = std::env::temp_dir().join(format!("lightcraft-sidecar-plain-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join("a.xmp"); + std::fs::write(&path, "not xml at all").unwrap(); + let fresh = lightcraft_meta::write_xmp_lc(&lightcraft_meta::Metadata { rating: Some(3), ..Default::default() }, &[("settings", "{}")]); + let (data, merged, backup) = super::sidecar_contents(&path, fresh.clone(), "t").unwrap(); + assert!(!merged && data == fresh); + assert!(backup.is_some_and(|b| std::fs::read_to_string(b).unwrap() == "not xml at all")); + std::fs::remove_dir_all(dir).unwrap(); + } + + /// A sidecar over the parser's namespace-declaration limit cannot be read or merged: the save fails and the file stays. + #[test] + fn a_sidecar_over_the_complexity_limit_is_kept() { + let dir = std::env::temp_dir().join(format!("lightcraft-sidecar-complex-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + let decls: String = (0..600).map(|i| format!(" xmlns:n{i}=\"urn:n:{i}\"")).collect(); + let old = format!( + "" + ); + assert_eq!(lightcraft_meta::parse_xmp(&old).unwrap_err(), lightcraft_meta::XmpError::TooComplex); + let path = dir.join("a.xmp"); + std::fs::write(&path, &old).unwrap(); + let fresh = lightcraft_meta::write_xmp_lc(&lightcraft_meta::Metadata { rating: Some(3), ..Default::default() }, &[("settings", "{}")]); + assert!(super::sidecar_contents(&path, fresh, "t").is_err()); + assert_eq!(std::fs::read_to_string(&path).unwrap(), old); + assert_eq!(std::fs::read_dir(&dir).unwrap().count(), 1, "no backup was made"); + std::fs::remove_dir_all(dir).unwrap(); + } + /// A sidecar written by Lightroom Classic (names flat, paths in `lr:hierarchicalSubject`) /// gives the photo its keywords as paths. #[test] diff --git a/crates/meta/src/xmp.rs b/crates/meta/src/xmp.rs index 600ca0fbb5..2326b0f722 100644 --- a/crates/meta/src/xmp.rs +++ b/crates/meta/src/xmp.rs @@ -170,6 +170,18 @@ pub(crate) const MAX_OPAQUE_NAME_BYTES: usize = 8 * 1024 * 1024; /// retains its prefix once per property: node limits alone cannot bound that duplication. const MAX_KEY_BYTES: usize = 1 << 20; +/// Most namespace declarations in scope at once. Prefix lookup scans them, so declarations times the names of a +/// start tag is the work; real packets declare a few dozen. +const MAX_NS_DECLS: usize = 512; + +/// Refuses a scope stack holding more than [`MAX_NS_DECLS`] declarations (one count per start tag over a stack at most [`MAX_DEPTH`] deep). +pub(crate) fn check_scopes(scopes: &[Vec<(String, String)>]) -> Result<(), XmpError> { + if scopes.iter().map(Vec::len).sum::() > MAX_NS_DECLS { + return Err(XmpError::TooComplex); + } + Ok(()) +} + /// Names canonicalised by namespace URI (see [`NAMESPACES`]); conflicting foreign prefixes are opaque. pub(crate) fn canonical_name(qname: &str, scopes: &[Vec<(String, String)>], opaque_budget: &mut usize) -> Result { canonical(qname, scopes, opaque_budget) @@ -298,6 +310,7 @@ fn parse_tree(s: &str) -> Result { } } scopes.push(scope); + check_scopes(&scopes)?; let name = canonical(&String::from_utf8_lossy(e.name().as_ref()), &scopes, &mut opaque_budget)?; let attrs = raw_attrs.into_iter().map(|(k, v)| Ok((canonical(&k, &scopes, &mut opaque_budget)?, v))).collect::>()?; diff --git a/crates/meta/src/xmp/tests.rs b/crates/meta/src/xmp/tests.rs index 8e0bdae938..66d807a7d6 100644 --- a/crates/meta/src/xmp/tests.rs +++ b/crates/meta/src/xmp/tests.rs @@ -563,6 +563,15 @@ fn malformed_capture_time_without_fallback_is_not_projected() { assert_eq!(parsed.properties["exif:DateTimeOriginal"], vec!["2026-01-01T".to_string()]); } +#[test] +fn a_start_tag_with_thousands_of_namespace_declarations_is_refused() { + let decls: String = (0..2000).map(|i| format!(" xmlns:p{i}=\"urn:x{i}\"")).collect(); + let xmp = format!( + "" + ); + assert!(matches!(parse_xmp(&xmp), Err(XmpError::TooComplex))); +} + #[test] fn many_ampersands_before_one_semicolon_are_read_once() { // 200 000 `&` then a `;`: each `&` once searched the whole rest of the attribute for the `;` diff --git a/crates/meta/src/xmp_merge.rs b/crates/meta/src/xmp_merge.rs index e1e4bca5e5..f9c0efcf4c 100644 --- a/crates/meta/src/xmp_merge.rs +++ b/crates/meta/src/xmp_merge.rs @@ -23,7 +23,7 @@ use std::collections::{BTreeSet, HashSet}; use quick_xml::events::Event; -use crate::xmp::{MAX_OPAQUE_NAME_BYTES, XmpError, canonical_name, parse_xmp, unescape_attr}; +use crate::xmp::{MAX_OPAQUE_NAME_BYTES, XmpError, canonical_name, check_scopes, parse_xmp, unescape_attr}; /// Which properties [`merge_xmp`] takes from the fresh packet. Names are canonical /// `prefix:name` (prefixes as [`crate::parse_xmp`] reports them); `prefix:*` is a whole namespace. @@ -75,6 +75,8 @@ struct Scan { descs: Vec, /// Where the first `` starts. rdf_close: Option, + /// The span of a self-closing `` (used only when there is no ``). + rdf_empty: Option<(usize, usize)>, } enum Kind { @@ -132,6 +134,7 @@ fn scan(s: &str) -> Result { raw_attrs.push((k, v)); } scopes.push(scope); + check_scopes(&scopes)?; let raw_name = String::from_utf8_lossy(e.name().as_ref()).into_owned(); let name = canonical_name(&raw_name, &scopes, &mut opaque_budget)?; let kind = match stack.last() { @@ -148,6 +151,9 @@ fn scan(s: &str) -> Result { _ => Kind::Other, }; if empty { + if matches!(kind, Kind::Rdf) { + out.rdf_empty = out.rdf_empty.or(Some((before, after))); + } scopes.pop(); if let Kind::Prop { desc, start, name } = kind && let Some(d) = out.descs.get_mut(desc) @@ -213,7 +219,9 @@ pub fn merge_xmp(existing: &str, fresh: &str, rules: MergeRules) -> Result = fresh_desc.props.iter().map(|p| p.name.as_str()).collect(); @@ -261,8 +269,16 @@ pub fn merge_xmp(existing: &str, fresh: &str, rules: MergeRules) -> Result"); if written > 0 { - let at = old.get(..rdf_close).map_or(rdf_close, |h| h.trim_end().len()); - edits.push((at, at, block)); + if let Some(rdf_close) = ex.rdf_close { + let at = old.get(..rdf_close).map_or(rdf_close, |h| h.trim_end().len()); + edits.push((at, at, block)); + } else if let Some((s, e)) = ex.rdf_empty { + // a self-closing `` is opened up around the new description + let tag = old.get(s..e).ok_or_else(|| XmpError::Xml("bad span".into()))?; + let open = tag.trim_end().strip_suffix("/>").ok_or_else(|| XmpError::Xml("bad rdf:RDF tag".into()))?; + let name = tag.get(1..).and_then(|t| t.split(|c: char| c.is_whitespace() || c == '/' || c == '>').next()).unwrap_or("rdf:RDF"); + edits.push((s, e, format!("{open}>{block}\n"))); + } } edits.sort_by_key(|e| (e.0, e.1)); // overlapping edits can't happen (properties sit inside their description, which is either @@ -386,6 +402,16 @@ mod tests { assert_eq!(merge_xmp(&again, &fresh(1, &[]), RULES).unwrap(), again, "idempotent"); } + #[test] + fn a_self_closing_rdf_element_is_opened_not_replaced() { + let ex = ""; + let out = merge_xmp(ex, &fresh(3, &[]), RULES).unwrap(); + assert!(out.starts_with(""), "{out}"); + assert_eq!(parse_xmp(&out).unwrap().properties.get("xmp:Rating"), Some(&vec!["3".to_string()])); + assert!(merge_xmp("", &fresh(3, &[]), RULES).is_err(), "no RDF element at all"); + } + #[test] fn owned_if_present_and_prefix_independence() { // the existing packet binds the namespaces to other prefixes, and has a capture time