Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions crates/engine/tests/import_fuzz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2415,6 +2415,64 @@ proptest! {
let text = type42_composite_eps(&font, gid);
survive("damaged Type 42 glyphs", || vectorcraft_eps::import(text.as_bytes()).ok().map(|r| r.document))?;
}

/// CID-keyed fonts with hostile CMaps (codespace ranges of any length, mappings past them)
/// and Type 0 CIDFonts with damaged binary data (offset table, charstrings, subroutine table,
/// any FDBytes and GDBytes), shown with any bytes: drawn, measured and outlined, or refused.
#[test]
fn eps_cid_fonts_never_panic(
space in prop::collection::vec((prop::collection::vec(any::<u8>(), 0..6), prop::collection::vec(any::<u8>(), 0..6)), 0..4),
cids in prop::collection::vec((prop::collection::vec(any::<u8>(), 1..5), any::<u16>()), 0..6),
data in prop::collection::vec(any::<u8>(), 0..200),
fd_bytes in 0u8..6,
gd_bytes in 0u8..6,
len_iv in -2i8..8,
shown in prop::collection::vec(any::<u8>(), 0..24),
) {
let hex = |b: &[u8]| b.iter().map(|x| format!("{x:02x}")).collect::<String>();
let ranges: String = space.iter().map(|(lo, hi)| format!("<{}> <{}>\n", hex(lo), hex(hi))).collect();
let maps: String = cids.iter().map(|(c, cid)| format!("<{}> <{}> {cid}\n", hex(c), hex(c))).collect();
let text = format!(
"%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: 0 0 100 100\n%%EndComments\n\
/CIDInit /ProcSet findresource begin 20 dict begin /CIDFontName /F def /CIDFontType 0 def\n\
/FDArray [4 dict begin /FontMatrix [0.001 0 0 0.001 0 0] def /Private 4 dict dup begin /lenIV {len_iv} def\n\
/SubrMapOffset 3 def /SDBytes 2 def /SubrCount 4 def end def currentdict end] def\n\
/CIDMapOffset 0 def /FDBytes {fd_bytes} def /GDBytes {gd_bytes} def /CIDCount 40 def\n\
(Hex) {} StartData\n{}\n\
/CIDInit /ProcSet findresource begin 12 dict begin begincmap /CMapName /M def\n\
{} begincodespacerange\n{ranges}endcodespacerange\n{} begincidrange\n{maps}endcidrange\n\
endcmap CMapName currentdict /CMap defineresource pop end end\n\
/C /M [/F] composefont 40 scalefont setfont 10 20 moveto <{}> show <{}> stringwidth pop pop\n\
10 60 moveto <{}> true charpath fill\nshowpage\n%%EOF\n",
data.len(), hex(&data), space.len(), cids.len(), hex(&shown), hex(&shown), hex(&shown)
);
survive("hostile CID-keyed fonts", || vectorcraft_eps::import(text.as_bytes()).ok().map(|r| r.document))?;
}

/// Embedded Type 1 fonts whose encrypted part is damaged or hostile (any bytes, binary or
/// hexadecimal), shown, measured and outlined: drawn, or left out.
#[test]
fn eps_type1_programs_never_panic(
part in prop::collection::vec(any::<u8>(), 0..400),
hex in any::<bool>(),
shown in prop::collection::vec(any::<u8>(), 0..12),
) {
let hexed = |b: &[u8]| b.iter().map(|x| format!("{x:02x}")).collect::<String>();
// Bytes that would end the part early are left out, as `cleartomark` ends it.
let part: Vec<u8> = part.into_iter().filter(|b| *b != b'c').collect();
let mut bytes = b"%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: 0 0 100 100\n%%EndComments\n\
12 dict begin /FontName /T1 def /FontType 1 def /FontMatrix [0.001 0 0 0.001 0 0] def /FontBBox [0 0 1000 1000] def\n\
/Encoding StandardEncoding def currentdict end currentfile eexec\n"
.to_vec();
if hex {
bytes.extend(hexed(&part).into_bytes());
} else {
bytes.extend(part);
}
bytes.extend(format!("\n{}\ncleartomark\n/T1 findfont 40 scalefont setfont 10 20 moveto <{}> show <{}> stringwidth pop pop\n\
10 60 moveto <{}> true charpath fill\nshowpage\n%%EOF\n", "0".repeat(512), hexed(&shown), hexed(&shown), hexed(&shown)).into_bytes());
survive("hostile Type 1 programs", || vectorcraft_eps::import(&bytes).ok().map(|r| r.document))?;
}
}

/// The TrueType font [`type42_composite_eps`] carries.
Expand Down
8 changes: 8 additions & 0 deletions crates/eps/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

mod ai;
mod ate;
mod cid;
mod data;
mod graphics;
mod interp;
Expand All @@ -44,11 +45,14 @@ mod obj;
mod preview;
mod shading;
mod text;
mod type1;
mod type42;

#[cfg(test)]
mod tests;
#[cfg(test)]
mod tests_cid;
#[cfg(test)]
mod tests_composite;
#[cfg(test)]
mod tests_fontnames;
Expand All @@ -60,6 +64,10 @@ mod tests_illustrator;
mod tests_images;
#[cfg(test)]
mod tests_native;
#[cfg(test)]
mod tests_type1;
#[cfg(test)]
mod tests_type42;

use std::collections::HashSet;
use std::sync::Arc;
Expand Down
Loading
Loading