mirror of
https://github.com/storytold/photocraft.git
synced 2026-11-21 09:26:43 +00:00
fix(io): trust a Displace map path only when it is well formed and nothing is embedded
Review follow-ups on #9162: - a `txtu` path record must have a size of 12 + 2 * units matching its length and end in one NUL; otherwise the filter stays opaque. - the DspF path is used only when no DspD is embedded, so a damaged embedded map stays opaque instead of falling back to the path. - a path-only template writes to PSD only with its own mapPath, so the exported PSD never names a map the render did not read. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0f48dfacc8
commit
4bab657cd4
1 file changed
+57
-14
@@ -84,19 +84,33 @@ fn fltr_map(item: &Descriptor) -> Option<EmbeddedMap<'_>> {
|
||||
}
|
||||
}
|
||||
|
||||
/// The map file `DspF` names, when it is a `Pth `: `txtu`, u32 LE size, u32 LE count of UTF-16
|
||||
/// units, then the UTF-16LE path ending in NUL (ag-psd's layout). A Mac alias (`alis`) is not
|
||||
/// decoded.
|
||||
/// The map file `DspF` names, when it is a `Pth `: `txtu`, u32 LE total size (`12 + 2 * units`),
|
||||
/// u32 LE count of UTF-16 units, then the UTF-16LE path ending in one NUL (ag-psd's layout). A
|
||||
/// record whose size disagrees with its units, or that lacks the NUL, is not trusted as a path. A
|
||||
/// Mac alias (`alis`) is not decoded.
|
||||
fn fltr_path(item: &Descriptor) -> Option<String> {
|
||||
let Value::Path(b) = get_desc(item, "Fltr")?.get("DspF")? else { return None };
|
||||
let u32_at = |i: usize| usize::try_from(u32::from_le_bytes(b.get(i..i.checked_add(4)?)?.try_into().ok()?)).ok();
|
||||
if b.get(..4)? != b"txtu" {
|
||||
return None;
|
||||
}
|
||||
let units = usize::try_from(u32::from_le_bytes(b.get(8..12)?.try_into().ok()?)).ok()?;
|
||||
let text = b.get(12..12usize.checked_add(units.checked_mul(2)?)?)?;
|
||||
let path = String::from_utf16(&text.as_chunks::<2>().0.iter().map(|c| u16::from_le_bytes(*c)).collect::<Vec<_>>()).ok()?;
|
||||
let path = path.trim_end_matches('\0');
|
||||
(!path.is_empty()).then(|| path.to_string())
|
||||
let (size, units) = (u32_at(4)?, u32_at(8)?);
|
||||
if size != 12usize.checked_add(units.checked_mul(2)?)? || b.len() != size {
|
||||
return None;
|
||||
}
|
||||
let text: Vec<u16> = b.get(12..)?.as_chunks::<2>().0.iter().map(|c| u16::from_le_bytes(*c)).collect();
|
||||
let (&0, path) = text.split_last()? else { return None };
|
||||
let path = String::from_utf16(path).ok()?;
|
||||
(!path.is_empty() && !path.contains('\0')).then_some(path)
|
||||
}
|
||||
|
||||
/// The `DspF` path a Displace reads its map from, only when no `DspD` is embedded at all: a
|
||||
/// damaged embedded map keeps the filter opaque rather than falling back to the path.
|
||||
fn path_only(item: &Descriptor) -> Option<String> {
|
||||
if get_desc(item, "Fltr")?.get(MAP_KEY).is_some() {
|
||||
return None;
|
||||
}
|
||||
fltr_path(item)
|
||||
}
|
||||
|
||||
fn lookup<'a>(table: &[(&[u8], &'a str)], v: &[u8]) -> Option<&'a str> {
|
||||
@@ -123,7 +137,7 @@ pub(super) fn import_params(item: &Descriptor) -> Option<J> {
|
||||
}
|
||||
let mut params = settings(f)?;
|
||||
if fltr_map(item).is_none() {
|
||||
params.as_object_mut()?.insert("mapPath".into(), json!(fltr_path(item)?));
|
||||
params.as_object_mut()?.insert("mapPath".into(), json!(path_only(item)?));
|
||||
}
|
||||
// Neutral blending in the template: the model's own fields replace it on export.
|
||||
let mut template = item.clone();
|
||||
@@ -143,7 +157,7 @@ fn template(params: &J) -> Result<Descriptor, String> {
|
||||
}
|
||||
let bytes = from_hex(raw).ok_or("the Displace PSD data is not valid hex")?;
|
||||
let item = Descriptor::from_bytes(&bytes).map_err(|e| format!("the Displace PSD data is unreadable ({e})"))?;
|
||||
if !item.class_id.is("filterFX") || int(&item, "filterID") != Some(FILTER_ID) || (fltr_map(&item).is_none() && fltr_path(&item).is_none()) {
|
||||
if !item.class_id.is("filterFX") || int(&item, "filterID") != Some(FILTER_ID) || (fltr_map(&item).is_none() && path_only(&item).is_none()) {
|
||||
return Err("the Displace PSD data is not a Displace filter with an embedded map or map file".into());
|
||||
}
|
||||
Ok(item)
|
||||
@@ -154,7 +168,13 @@ pub(super) fn export_item(params: &J) -> Result<Descriptor, String> {
|
||||
let object = params.as_object().ok_or("Displace parameters must be an object")?;
|
||||
let mut item = template(params)?;
|
||||
// The `mapPath` imported from the template's own `DspF` is already in it; another one isn't.
|
||||
let own_path = |k: &str| k == "mapPath" && params.get(k).and_then(J::as_str) == fltr_path(&item).as_deref();
|
||||
// A path-only template also needs that `mapPath`: without it the filter renders no map while
|
||||
// the PSD would still name one.
|
||||
let template_path = path_only(&item);
|
||||
if template_path.is_some() && params.get("mapPath").and_then(J::as_str) != template_path.as_deref() {
|
||||
return Err("a Displace that reads its map from a file needs that file as `mapPath` to be written to PSD".into());
|
||||
}
|
||||
let own_path = |k: &str| k == "mapPath" && template_path.is_some();
|
||||
if let Some(k) = object.keys().find(|k| !super::is_meta(k) && !own_path(k) && !["horizontal", "vertical", "fit", "undefinedAreas"].contains(&k.as_str())) {
|
||||
return Err(format!("Displace setting `{k}` has no PSD equivalent next to its embedded map"));
|
||||
}
|
||||
@@ -440,13 +460,36 @@ mod tests {
|
||||
let mut moved = f.clone();
|
||||
moved.params["mapPath"] = json!("/elsewhere/map.png");
|
||||
assert!(item_for_filter(&moved).is_err(), "another map path has no place in the template");
|
||||
let mut no_path = f.clone();
|
||||
no_path.params.as_object_mut().unwrap().remove("mapPath");
|
||||
assert!(item_for_filter(&no_path).is_err(), "a path-only template needs its mapPath, or the PSD names a map the render never read");
|
||||
// A record whose size field disagrees with its units, or without the final NUL.
|
||||
let mut wrong_size = pth("map.psd");
|
||||
if let Value::Path(b) = &mut wrong_size {
|
||||
b[4] = b[4].wrapping_add(2);
|
||||
}
|
||||
let mut no_nul = b"txtu".to_vec();
|
||||
no_nul.extend(16u32.to_le_bytes());
|
||||
no_nul.extend(2u32.to_le_bytes());
|
||||
no_nul.extend([b'a', 0, b'b', 0]);
|
||||
// Not a path we can read: kept opaque, as before.
|
||||
for bad in
|
||||
[Value::Path(b"txtu".to_vec()), Value::Path(b"abcd\0\0\0\0\x02\0\0\0a\0\0\0".to_vec()), Value::Path(b"txtu\0\0\0\0\x09\0\0\0a\0".to_vec()), pth("")]
|
||||
{
|
||||
for bad in [
|
||||
Value::Path(b"txtu".to_vec()),
|
||||
Value::Path(b"abcd\0\0\0\0\x02\0\0\0a\0\0\0".to_vec()),
|
||||
Value::Path(b"txtu\0\0\0\0\x09\0\0\0a\0".to_vec()),
|
||||
pth(""),
|
||||
wrong_size,
|
||||
Value::Path(no_nul),
|
||||
] {
|
||||
let src = with_file(bad);
|
||||
assert_eq!(filter_from_item(&src).command, UNSUPPORTED_FILTER);
|
||||
}
|
||||
// A damaged embedded map is never replaced by a readable path.
|
||||
let mut damaged = item(vec![0; 4]);
|
||||
let mut fltr = get_desc(&damaged, "Fltr").unwrap().clone();
|
||||
set(&mut fltr, "DspF", pth("C:\\maps\\repro-map.psd"));
|
||||
set(&mut damaged, "Fltr", Value::Descriptor(fltr));
|
||||
assert_eq!(filter_from_item(&damaged).command, UNSUPPORTED_FILTER);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user