mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-18 12:27:37 +00:00
MCP: a confined package refuses a PDF whose editing file can't be resolved
A linked PDF or .ai file that lists the editing file among its attachments, but whose file specification, EF dictionary or F or UF stream can't be resolved, was taken for a file without editing data and copied without its stored paths being checked. Under read roots, file.package now refuses it with the error for editing data that can't be read. Opening such a PDF is unchanged. The plug-in export test removes its folders when it ends.
This commit is contained in:
@@ -121,8 +121,9 @@ enum Carried {
|
||||
/// The native document, read.
|
||||
Readable(Box<Document>),
|
||||
/// The file is a native document, or carries one, that can't be read: a damaged native file,
|
||||
/// a PDF or `.ai` file that can't be opened without a password or can't be read at all, or
|
||||
/// editing data that doesn't read as a native document.
|
||||
/// a PDF or `.ai` file that can't be opened without a password or can't be read at all, a PDF
|
||||
/// or `.ai` file listing an editing file whose data can't be resolved, or editing data that
|
||||
/// doesn't read as a native document.
|
||||
Unreadable,
|
||||
}
|
||||
|
||||
@@ -136,7 +137,12 @@ fn carried_document(name: &str, bytes: &[u8]) -> Carried {
|
||||
if super::place::document::is_pdf(name, bytes) {
|
||||
// A PDF that can't be opened may carry editing data all the same.
|
||||
return match vectorcraft_pdf::open_pdf(bytes, None) {
|
||||
Ok(opened) => opened.editing().map_or(Carried::Absent, |e| read(&e.data)),
|
||||
// An editing file listed whose data can't be resolved can't be checked either.
|
||||
Ok(opened) => match opened.editing() {
|
||||
Some(e) => read(&e.data),
|
||||
None if opened.has_editing_file() => Carried::Unreadable,
|
||||
None => Carried::Absent,
|
||||
},
|
||||
Err(_) => Carried::Unreadable,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -509,6 +509,15 @@ fn a_confined_package_refuses_a_linked_file_whose_editing_data_cannot_be_read()
|
||||
refused(&mut s, json!({"folder": out}));
|
||||
refused(&mut s, json!({"relink": false}));
|
||||
assert_eq!(run(&mut s, "file.package", json!({}))["links"], 1, "the person's package");
|
||||
// A PDF listing the editing file without its data (`EF` renamed, the same length).
|
||||
let at = editable.windows(4).position(|w| w.starts_with(b"/EF") && !w[3].is_ascii_alphanumeric()).expect("the editing file's EF");
|
||||
let mut detached = editable.clone();
|
||||
detached[at..at + 3].copy_from_slice(b"/XF");
|
||||
assert!(vectorcraft_pdf::editing(&detached).is_none(), "the editing data can't be resolved");
|
||||
write(&pdf, &detached);
|
||||
refused(&mut s, json!({"folder": out}));
|
||||
refused(&mut s, json!({"relink": false}));
|
||||
assert_eq!(run(&mut s, "file.package", json!({}))["links"], 1, "the person's package");
|
||||
// A PDF without editing data.
|
||||
write(&pdf, &plain);
|
||||
assert_eq!(package(&mut s, json!({"folder": out})).unwrap()["links"], 1);
|
||||
|
||||
@@ -388,4 +388,6 @@ fn a_confined_exporter_is_refused_stored_paths_outside_the_read_roots() {
|
||||
let mut found = vec![];
|
||||
strings_in(&serde_json::from_slice(&std::fs::read(r["files"][0].as_str().unwrap()).unwrap()).unwrap(), &mut found);
|
||||
assert!(!found.contains(&placed), "{found:?}");
|
||||
let _ = std::fs::remove_dir_all(&away);
|
||||
let _ = std::fs::remove_dir_all(&home);
|
||||
}
|
||||
|
||||
+47
-10
@@ -77,12 +77,33 @@ pub fn editing_with(bytes: &[u8], password: Option<&str>) -> Option<Editing> {
|
||||
editing_in(&crate::pages::open(bytes, password).ok()?)
|
||||
}
|
||||
|
||||
/// The file specification of the editing file among a parsed PDF's attachments, if any, and whether
|
||||
/// a name tree key names the editing file (set also when its file specification can't be read).
|
||||
fn editing_file_spec(pdf: &Pdf) -> (Option<Dict<'_>>, bool) {
|
||||
let xref = pdf.xref();
|
||||
let Some(tree) = xref
|
||||
.get::<Dict<'_>>(xref.root_id())
|
||||
.and_then(|catalog| catalog.get::<Dict<'_>>(b"Names"))
|
||||
.and_then(|names| names.get::<Dict<'_>>(b"EmbeddedFiles"))
|
||||
else {
|
||||
return (None, false);
|
||||
};
|
||||
let mut keyed = false;
|
||||
let spec = editing_spec(&tree, 0, &mut HashSet::new(), &mut keyed, xref);
|
||||
(spec, keyed)
|
||||
}
|
||||
|
||||
/// Does a parsed PDF list the editing file among its attachments, also when [`editing_in`] can't
|
||||
/// read it (its file specification, its `EF` dictionary or its `F` or `UF` stream can't be
|
||||
/// resolved)?
|
||||
pub(crate) fn has_editing_file_in(pdf: &Pdf) -> bool {
|
||||
let (spec, keyed) = editing_file_spec(pdf);
|
||||
spec.is_some() || keyed
|
||||
}
|
||||
|
||||
/// [`editing`] of a parsed PDF.
|
||||
pub(crate) fn editing_in(pdf: &Pdf) -> Option<Editing> {
|
||||
let xref = pdf.xref();
|
||||
let catalog = xref.get::<Dict<'_>>(xref.root_id())?;
|
||||
let tree = catalog.get::<Dict<'_>>(b"Names")?.get::<Dict<'_>>(b"EmbeddedFiles")?;
|
||||
let spec = editing_spec(&tree, 0, &mut HashSet::new(), xref)?;
|
||||
let spec = editing_file_spec(pdf).0?;
|
||||
let files = spec.get::<Dict<'_>>(b"EF")?;
|
||||
let stream = files.get::<Stream<'_>>(b"F").or_else(|| files.get::<Stream<'_>>(b"UF"))?;
|
||||
let data = stream.decoded().map(|d| d.into_owned()).unwrap_or_default();
|
||||
@@ -98,8 +119,21 @@ pub(crate) fn editing_in(pdf: &Pdf) -> Option<Editing> {
|
||||
|
||||
/// The file specification of the editing file in the name tree node `node`. `seen` holds the indirect
|
||||
/// nodes already searched, so a node shared by many `Kids` arrays is searched once, not once per path.
|
||||
fn editing_spec<'a>(node: &Dict<'a>, depth: usize, seen: &mut HashSet<ObjectIdentifier>, xref: &'a XRef) -> Option<Dict<'a>> {
|
||||
/// `keyed` is set when a key searched names the editing file.
|
||||
fn editing_spec<'a>(node: &Dict<'a>, depth: usize, seen: &mut HashSet<ObjectIdentifier>, keyed: &mut bool, xref: &'a XRef) -> Option<Dict<'a>> {
|
||||
if let Some(names) = node.get::<Array<'a>>(b"Names") {
|
||||
// The keys read as written, so a value that can't be resolved doesn't shift the pairs.
|
||||
let mut raw = items(&names);
|
||||
while let (Some(key), Some(_)) = (raw.next(), raw.next()) {
|
||||
let key = match key {
|
||||
MaybeRef::NotRef(Object::String(key)) => Some(key),
|
||||
MaybeRef::Ref(r) => xref.get::<PdfString<'a>>(r.into()),
|
||||
MaybeRef::NotRef(_) => None,
|
||||
};
|
||||
if key.is_some_and(|key| is_editing_name(&key)) {
|
||||
*keyed = true;
|
||||
}
|
||||
}
|
||||
let mut items = names.iter::<Object<'a>>();
|
||||
while let (Some(_), Some(spec)) = (items.next(), items.next()) {
|
||||
if let Object::Dict(spec) = spec
|
||||
@@ -121,17 +155,20 @@ fn editing_spec<'a>(node: &Dict<'a>, depth: usize, seen: &mut HashSet<ObjectIden
|
||||
MaybeRef::NotRef(Object::Dict(kid)) => kid,
|
||||
_ => return None,
|
||||
};
|
||||
editing_spec(&kid, depth + 1, seen, xref)
|
||||
editing_spec(&kid, depth + 1, seen, keyed, xref)
|
||||
})
|
||||
}
|
||||
|
||||
/// A file specification naming the editing file (current or legacy name), in any text string
|
||||
/// encoding.
|
||||
fn is_editing_file(spec: &Dict<'_>) -> bool {
|
||||
[&b"UF"[..], b"F"].into_iter().filter_map(|k| spec.get::<PdfString<'_>>(k)).any(|name| {
|
||||
let name = text_string(name.as_bytes());
|
||||
name == EDITING_FILE || name == LEGACY_EDITING_FILE
|
||||
})
|
||||
[&b"UF"[..], b"F"].into_iter().filter_map(|k| spec.get::<PdfString<'_>>(k)).any(|name| is_editing_name(&name))
|
||||
}
|
||||
|
||||
/// A text string that is the editing file's name (current or legacy), in any encoding.
|
||||
fn is_editing_name(name: &PdfString<'_>) -> bool {
|
||||
let name = text_string(name.as_bytes());
|
||||
name == EDITING_FILE || name == LEGACY_EDITING_FILE
|
||||
}
|
||||
|
||||
/// The most decoded page content [`page_hash`] reads in all (bytes).
|
||||
|
||||
@@ -261,6 +261,12 @@ impl Opened {
|
||||
crate::editing::editing_in(&self.pdf)
|
||||
}
|
||||
|
||||
/// Does the PDF list the editing file among its attachments, also when
|
||||
/// [`editing`](Self::editing) can't read it?
|
||||
pub fn has_editing_file(&self) -> bool {
|
||||
crate::editing::has_editing_file_in(&self.pdf)
|
||||
}
|
||||
|
||||
/// The private data of an Illustrator `.ai`, as [`illustrator_data`] reads it.
|
||||
pub fn illustrator_data(&self) -> Option<Vec<u8>> {
|
||||
private_data(&self.pdf)
|
||||
|
||||
@@ -237,3 +237,45 @@ fn a_file_name_written_as_utf16_is_found() {
|
||||
assert_eq!(e.data, b"hello", "/{key}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_editing_file_whose_data_cannot_be_resolved_is_still_listed() {
|
||||
// No `EF` dictionary, an `EF` without `F` or `UF`, and an `F` naming no object.
|
||||
for ef in ["", "/EF << >>", "/EF << /F 9 0 R >>"] {
|
||||
let objects = [
|
||||
"<< /Type /Catalog /Pages 2 0 R /Names << /EmbeddedFiles 3 0 R >> >>".to_string(),
|
||||
"<< /Type /Pages /Kids [5 0 R] /Count 1 >>".to_string(),
|
||||
"<< /Names [(a) 4 0 R] >>".to_string(),
|
||||
format!("<< /Type /Filespec /UF ({EDITING_FILE}) {ef} >>"),
|
||||
"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 10 10] >>".to_string(),
|
||||
];
|
||||
let opened = open_pdf(&raw_pdf(&objects), None).unwrap();
|
||||
assert_eq!(opened.editing(), None, "{ef:?}");
|
||||
assert!(opened.has_editing_file(), "{ef:?}");
|
||||
}
|
||||
// A name tree key naming it, with a file specification that names no object or isn't one.
|
||||
for spec in ["9 0 R", "(not a file specification)"] {
|
||||
let objects = [
|
||||
"<< /Type /Catalog /Pages 2 0 R /Names << /EmbeddedFiles 3 0 R >> >>".to_string(),
|
||||
"<< /Type /Pages /Kids [4 0 R] /Count 1 >>".to_string(),
|
||||
format!("<< /Names [(a) 9 0 R ({EDITING_FILE}) {spec}] >>"),
|
||||
"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 10 10] >>".to_string(),
|
||||
];
|
||||
let opened = open_pdf(&raw_pdf(&objects), None).unwrap();
|
||||
assert_eq!(opened.editing(), None, "{spec}");
|
||||
assert!(opened.has_editing_file(), "{spec}");
|
||||
}
|
||||
// The key written as an indirect string.
|
||||
let objects = [
|
||||
"<< /Type /Catalog /Pages 2 0 R /Names << /EmbeddedFiles 3 0 R >> >>".to_string(),
|
||||
"<< /Type /Pages /Kids [4 0 R] /Count 1 >>".to_string(),
|
||||
"<< /Names [5 0 R 9 0 R] >>".to_string(),
|
||||
"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 10 10] >>".to_string(),
|
||||
format!("({EDITING_FILE})"),
|
||||
];
|
||||
assert!(open_pdf(&raw_pdf(&objects), None).unwrap().has_editing_file());
|
||||
// A PDF without the editing file, and one with it.
|
||||
assert!(!open_pdf(&export(&doc(), &PdfOptions::default()).unwrap(), None).unwrap().has_editing_file());
|
||||
let opened = open_pdf(&pdf_with(Some(b"native"), false).bytes, None).unwrap();
|
||||
assert!(opened.editing().is_some() && opened.has_editing_file());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user