Tests: cover broken hex strings in the stream scan and restricted file fonts

A hex string broken by a byte that is neither a hex digit nor white space
is no string: the hostile-input scan goes on from the next byte, so a
cross-reference stream after it is still read. A font a PDF carries with
restricted license flags (OS/2 fsType 0x0002) keeps drawing its type, and
a PDF saved from the document draws that type as outlines with the license
warning instead of embedding the font again.
This commit is contained in:
dotCooCoo committed 2026-11-30 23:03:07 -08:00
1 parent 84335645da
commit 3f21a06bed
3 files changed
+41 -2

No files matched your search

+13
View File
@@ -1034,6 +1034,19 @@ mod tests {
assert!(unread.is_empty(), "{unread:#?}");
}
#[test]
fn a_hex_string_broken_by_a_non_hex_byte_hides_no_stream_after_it() {
// A `<` followed by hex digits and then a byte that is neither a hex digit nor white space
// is no hex string: the scan goes on from the next byte instead of skipping to a `>`.
let bad = b"<< /Type /XRef /DecodeParms 9 0 R >> stream\nx\nendstream";
for lead in [&b"<4G "[..], b"<41 4z ", b"<\x00 "] {
let pdf = [lead, &bad[..]].concat();
assert!(unsafe_file_streams(&pdf), "{}", String::from_utf8_lossy(&pdf));
}
// A hex string that ends is skipped, and the stream after it is still read.
assert!(unsafe_file_streams(&[&b"<41 42> "[..], &bad[..]].concat()));
}
#[test]
fn a_run_of_lt_in_an_image_a_string_or_a_comment_opens() {
// #3924 review: every `<<` in a run of `<` (an image's flat area of grey 60) was read as a
+27 -1
View File
@@ -10,7 +10,7 @@ use kurbo::{Point, Rect, Shape};
use vectorcraft_doc::{Document, FILE_FONT_MIME, TextObject};
use vectorcraft_text::{FontDb, FontMatch};
use crate::tests::live_text::{SOURCE_SERIF, outlines, texts, uninstalled_serif};
use crate::tests::live_text::{SOURCE_SERIF, outlines, renamed, texts, uninstalled_serif};
use crate::*;
/// Lines of (x, y from the top, size, text): kerned pairs, a long line, two sizes.
@@ -274,3 +274,29 @@ fn two_glyphs_for_one_character_both_survive_import() {
};
close(&glyph_ink(db, &imported), "imported");
}
/// The license flags (OS/2 `fsType`) of the font a PDF carries go with the font the document keeps:
/// a restricted font (0x0002) still draws its type, and a PDF saved from the document draws that
/// type as outlines with the license warning instead of embedding the font again.
#[test]
fn a_restricted_font_the_file_carries_is_not_embedded_again() {
let mut font = renamed(SOURCE_SERIF, &[("SourceSerif4", "SourceSerif7"), ("Source Serif 4", "Source Serif 7")]);
let tables = usize::from(u16::from_be_bytes([font[4], font[5]]));
let os2 = (0..tables).map(|i| 12 + 16 * i).find(|r| &font[*r..*r + 4] == b"OS/2").unwrap();
let at = u32::from_be_bytes(font[os2 + 8..os2 + 12].try_into().unwrap()) as usize;
font[at + 8..at + 10].copy_from_slice(&0x0002u16.to_be_bytes());
let carried = import(&page_pdf(font));
let t = texts(&carried.document);
assert!(t.iter().any(|t| t.file_fonts.is_some()), "the file's font draws its type");
let saved = export_with_report(&carried.document, &PdfOptions::default()).unwrap();
assert!(saved.warnings.iter().any(|w| w.contains("Source Serif 7") && w.contains("doesn't allow embedding")), "{:?}", saved.warnings);
let back = import(&saved.bytes);
assert!(
texts(&back.document).is_empty(),
"no type in the saved file: {:?}",
texts(&back.document).iter().map(TextObject::plain_text).collect::<Vec<_>>()
);
assert!(!back.document.images.values().any(|b| b.mime == FILE_FONT_MIME), "no font in the saved file");
}
+1 -1
View File
@@ -271,7 +271,7 @@ fn invisible_text_is_type_whatever_its_glyphs_look_like() {
}
/// `font` with each of `names` (`from`, `to`, the same length) replaced, in ASCII and UTF-16.
fn renamed(font: &[u8], names: &[(&str, &str)]) -> Vec<u8> {
pub(crate) fn renamed(font: &[u8], names: &[(&str, &str)]) -> Vec<u8> {
let mut bytes = font.to_vec();
let utf16 = |s: &str| s.encode_utf16().flat_map(u16::to_be_bytes).collect::<Vec<u8>>();
for &(from, to) in names {