mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-10-25 02:29:31 -06:00
Fuzzed inputs drove one check-one process to 2-6 GB (the 16 GB CI runner running two of them was killed every night). Three causes, each with a test: - lopdf (bootstrap inspector) sized PNG predictor rows from /Columns before reading data: /Columns 4294967295 allocated two 4 GiB rows. Vendored lopdf 0.45.0 refuses rows longer than the data (and uses saturating row widths); its load-time stream decoding also gets a 256 MiB limit (it had none). - hayro sized a tiling pattern's cell pixmap from /XStep x scale, bounded only by u16: a step far beyond /BBox allocated 2 GB. The vendored patch keeps the cell within 3000 px a side, lowering the resolution instead. - cos decoded cross-reference streams up to the general 1 GiB limit; they now stop at what 8,388,607 objects' rows can hold (~200 MB at most), and the section is reconstructed (logged) instead. On the nightly's seed (41) the worst captured input fell from 6.2 GB to 6 MB, and a 300 s fuzz run had no process above 300 MB (before: many at 2-5 GB). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
169 lines
5.0 KiB
Rust
169 lines
5.0 KiB
Rust
#[cfg(not(feature = "async"))]
|
|
use lopdf::{Document, Object};
|
|
|
|
#[test]
|
|
#[cfg(all(test, not(feature = "async")))]
|
|
fn test_get_object() {
|
|
use self::Object;
|
|
use lopdf::Dictionary as LoDictionary;
|
|
use lopdf::Stream as LoStream;
|
|
|
|
let mut doc = Document::new();
|
|
let id = doc.add_object(Object::string_literal("test"));
|
|
let id2 = doc.add_object(Object::Stream(LoStream::new(
|
|
LoDictionary::new(),
|
|
"stream".as_bytes().to_vec(),
|
|
)));
|
|
|
|
println!("{:?}", id);
|
|
println!("{:?}", id2);
|
|
|
|
let obj1_exists = doc.get_object(id).is_ok();
|
|
let obj2_exists = doc.get_object(id2).is_ok();
|
|
|
|
assert!(obj1_exists);
|
|
assert!(obj2_exists);
|
|
}
|
|
|
|
#[cfg(all(test, not(feature = "async")))]
|
|
mod tests_with_parsing {
|
|
use super::*;
|
|
use lopdf::Result;
|
|
|
|
fn modify_text() -> Result<bool> {
|
|
let mut doc = Document::load("assets/example.pdf")?;
|
|
doc.version = "1.4".to_string();
|
|
if let Some(Object::Stream(stream)) = doc.objects.get_mut(&(4, 0)) {
|
|
let mut content = stream.decode_content().unwrap();
|
|
content.operations[3].operands[0] = Object::string_literal("Modified text!");
|
|
stream.set_content(content.encode().unwrap());
|
|
}
|
|
|
|
// Create temporary folder to store file.
|
|
let temp_dir = tempfile::tempdir()?;
|
|
let file_path = temp_dir.path().join("test_3_modify.pdf");
|
|
doc.save(file_path)?;
|
|
Ok(true)
|
|
}
|
|
|
|
#[test]
|
|
fn test_modify() {
|
|
assert!(modify_text().unwrap());
|
|
}
|
|
|
|
fn replace_text() -> Result<Document> {
|
|
let mut doc = Document::load("assets/example.pdf")?;
|
|
doc.replace_text(1, "Hello World!", "Modified text!", None)?;
|
|
|
|
// Create temporary folder to store file.
|
|
let temp_dir = tempfile::tempdir()?;
|
|
let file_path = temp_dir.path().join("test_4_unicode_replace.pdf");
|
|
doc.save(&file_path)?;
|
|
|
|
let doc = Document::load(file_path)?;
|
|
Ok(doc)
|
|
}
|
|
|
|
#[test]
|
|
fn test_replace() {
|
|
assert_eq!(replace_text().unwrap().extract_text(&[1]).unwrap(), "Modified text!\n");
|
|
}
|
|
|
|
fn replace_unicode_text() -> Result<Document> {
|
|
let mut doc = Document::load("assets/unicode.pdf")?;
|
|
doc.replace_text(1, "😀", "🔧2", Some("🔨"))?;
|
|
|
|
let temp_dir = tempfile::tempdir()?;
|
|
let file_path = temp_dir.path().join("test_4_unicode_replace.pdf");
|
|
doc.save(&file_path)?;
|
|
|
|
let doc = Document::load(file_path)?;
|
|
Ok(doc)
|
|
}
|
|
|
|
#[test]
|
|
fn test_unicode_replace() {
|
|
let text = replace_unicode_text().unwrap().extract_text(&[1]).unwrap();
|
|
assert_eq!(text, "🔧🔨\n🔧\n🔨\n");
|
|
}
|
|
|
|
fn build_doc_with_tj_array(content_bytes: Vec<u8>) -> Document {
|
|
use lopdf::dictionary;
|
|
|
|
let mut doc = Document::with_version("1.5");
|
|
|
|
let pages_id = doc.new_object_id();
|
|
|
|
let font_id = doc.add_object(dictionary! {
|
|
"Type" => "Font",
|
|
"Subtype" => "Type1",
|
|
"BaseFont" => "Helvetica",
|
|
});
|
|
|
|
let resources_id = doc.add_object(dictionary! {
|
|
"Font" => dictionary! {
|
|
"F1" => font_id,
|
|
},
|
|
});
|
|
|
|
let content_id = doc.add_object(lopdf::Stream::new(dictionary! {}, content_bytes));
|
|
|
|
let single_page_id = doc.add_object(dictionary! {
|
|
"Type" => "Page",
|
|
"Parent" => pages_id,
|
|
"Resources" => resources_id,
|
|
"Contents" => content_id,
|
|
"MediaBox" => vec![0.into(), 0.into(), 612.into(), 792.into()],
|
|
});
|
|
|
|
doc.objects.insert(
|
|
pages_id,
|
|
Object::Dictionary(dictionary! {
|
|
"Type" => "Pages",
|
|
"Kids" => vec![single_page_id.into()],
|
|
"Count" => 1,
|
|
}),
|
|
);
|
|
|
|
let catalog_id = doc.add_object(dictionary! {
|
|
"Type" => "Catalog",
|
|
"Pages" => pages_id,
|
|
});
|
|
doc.trailer.set("Root", catalog_id);
|
|
|
|
doc
|
|
}
|
|
|
|
fn replace_text_in_tj_array_doc() -> Result<Document> {
|
|
// Content uses the TJ operator with a single-string array containing
|
|
// all 12 characters of "Hello World!".
|
|
let content = b"BT\n/F1 12 Tf\n100 700 Td\n[(Hello World!)] TJ\nET\n".to_vec();
|
|
let mut doc = build_doc_with_tj_array(content);
|
|
doc.replace_text(1, "Hello World!", "Modified text!", None)?;
|
|
Ok(doc)
|
|
}
|
|
|
|
#[test]
|
|
fn test_replace_text_in_tj_array_does_not_truncate() {
|
|
let doc = replace_text_in_tj_array_doc().unwrap();
|
|
let text = doc.extract_text(&[1]).unwrap();
|
|
assert_eq!(text, "Modified text! \n");
|
|
}
|
|
|
|
fn get_mut() -> Result<bool> {
|
|
let mut doc = Document::load("assets/example.pdf")?;
|
|
let arr = doc
|
|
.get_object_mut((5, 0))?
|
|
.as_dict_mut()?
|
|
.get_mut(b"Contents")?
|
|
.as_array_mut()?;
|
|
arr[0] = arr[0].clone();
|
|
Ok(true)
|
|
}
|
|
|
|
#[test]
|
|
fn test_get_mut() {
|
|
assert!(get_mut().unwrap());
|
|
}
|
|
}
|