Skip to content

Commit 0526740

Browse files
rthJ-F-Liu
authored andcommitted
fix(parser): accept binary bytes on the PDF header line
In lenient mode (default), only capture version digits from the header line, skipping any trailing binary marker bytes that some generators (e.g. ImageMill) place before the newline. In strict mode, reject headers with trailing bytes after the version string.
1 parent e7eb667 commit 0526740

File tree

3 files changed

+97
-9
lines changed

3 files changed

+97
-9
lines changed

src/parser/mod.rs

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -409,15 +409,28 @@ fn _indirect_object<'a>(
409409
Ok((object_id, object))
410410
}
411411

412-
pub fn header(input: ParserInput) -> Option<String> {
413-
strip_nom(map_res(
414-
delimited(
415-
tag(&b"%PDF-"[..]),
412+
pub fn header(input: ParserInput, strict: bool) -> Option<String> {
413+
// Parse version digits (e.g. "1.7") separately from any trailing bytes
414+
// before the newline. Some PDF generators (e.g. ImageMill) place binary
415+
// marker bytes on the header line which would fail UTF-8 validation.
416+
// In strict mode we reject such trailing bytes; in lenient mode we skip them.
417+
let (_, (version_raw, trailing)) = delimited(
418+
tag(&b"%PDF-"[..]),
419+
pair(
420+
take_while(|c: u8| c.is_ascii_digit() || c == b'.'),
416421
take_while(|c: u8| !b"\r\n".contains(&c)),
417-
pair(eol, many0_count(comment)),
418422
),
419-
|v: ParserInput| str::from_utf8(&v).map(Into::into),
420-
).parse(input))
423+
pair(eol, many0_count(comment)),
424+
)
425+
.parse(input)
426+
.ok()?;
427+
428+
if strict && !trailing.is_empty() {
429+
return None;
430+
}
431+
432+
let version = str::from_utf8(&version_raw).ok()?.to_string();
433+
Some(version)
421434
}
422435

423436
pub fn binary_mark(input: ParserInput) -> Option<Vec<u8>> {
@@ -802,6 +815,49 @@ startxref
802815
}
803816
}
804817

818+
#[test]
819+
fn header_standard() {
820+
// Standard header with proper EOL
821+
let input = b"%PDF-1.7\n%\xe2\xe3\xcf\xd3\n";
822+
assert_eq!(header(test_span(input), false), Some("1.7".to_string()));
823+
}
824+
825+
#[test]
826+
fn header_with_binary_bytes_on_same_line() {
827+
// Some generators (e.g. ImageMill) place binary marker bytes on the
828+
// header line without a separating newline or '%' prefix.
829+
let input = b"%PDF-1.3 \xb0\x9f\x92\x9c\x9f\xd4\xe0\xce\xd0\xd0\xd0\r1 0 obj\r";
830+
assert_eq!(header(test_span(input), false), Some("1.3".to_string()));
831+
}
832+
833+
#[test]
834+
fn header_with_binary_bytes_strict_rejects() {
835+
// In strict mode, binary bytes on the header line should cause a
836+
// parse failure (the raw bytes are not valid UTF-8).
837+
let input = b"%PDF-1.3 \xb0\x9f\x92\x9c\x9f\xd4\xe0\xce\xd0\xd0\xd0\r1 0 obj\r";
838+
assert_eq!(header(test_span(input), true), None);
839+
}
840+
841+
#[test]
842+
fn header_cr_line_ending() {
843+
// CR-only line ending (common in older PDFs)
844+
let input = b"%PDF-1.3\r%\xe2\xe3\xcf\xd3\r";
845+
assert_eq!(header(test_span(input), false), Some("1.3".to_string()));
846+
}
847+
848+
#[test]
849+
fn header_crlf_line_ending() {
850+
// CRLF line ending (common on Windows-generated PDFs)
851+
let input = b"%PDF-1.7\r\n%\xe2\xe3\xcf\xd3\r\n";
852+
assert_eq!(header(test_span(input), false), Some("1.7".to_string()));
853+
}
854+
855+
#[test]
856+
fn header_pdf_2_0() {
857+
let input = b"%PDF-2.0\n%\xe2\xe3\xcf\xd3\n";
858+
assert_eq!(header(test_span(input), false), Some("2.0".to_string()));
859+
}
860+
805861
#[test]
806862
fn content_with_comments() {
807863
// It should be processed as usual but ignoring the comments

src/reader.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ impl Reader<'_> {
500500
self.buffer = &self.buffer[offset..];
501501

502502
let version =
503-
parser::header(ParserInput::new_extra(self.buffer, "header")).ok_or(ParseError::InvalidFileHeader)?;
503+
parser::header(ParserInput::new_extra(self.buffer, "header"), self.strict).ok_or(ParseError::InvalidFileHeader)?;
504504

505505
let xref_start = Self::get_xref_start(self.buffer)?;
506506
if xref_start > self.buffer.len() {
@@ -741,7 +741,7 @@ impl Reader<'_> {
741741
// The document structure can be expressed in PEG as:
742742
// document <- header indirect_object* xref trailer xref_start
743743
let version =
744-
parser::header(ParserInput::new_extra(self.buffer, "header")).ok_or(ParseError::InvalidFileHeader)?;
744+
parser::header(ParserInput::new_extra(self.buffer, "header"), self.strict).ok_or(ParseError::InvalidFileHeader)?;
745745

746746
//The binary_mark is in line 2 after the pdf version. If at other line number, then will be declared as invalid pdf.
747747
if let Some(pos) = self.buffer.iter().position(|&byte| byte == b'\n') {

tests/load_options_test.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,4 +187,36 @@ mod sync_tests {
187187
let result = Document::load_with_options("nonexistent.pdf", LoadOptions::default());
188188
assert!(result.is_err());
189189
}
190+
191+
#[test]
192+
fn strict_rejects_binary_bytes_on_header_line() {
193+
// Minimal PDF-like buffer with binary bytes on the header line.
194+
// Strict mode should reject this with InvalidFileHeader.
195+
let buf = b"%PDF-1.3 \xb0\x9f\x92\x9c\r%%EOF\r";
196+
let result = Document::load_mem_with_options(
197+
buf,
198+
LoadOptions { strict: true, ..Default::default() },
199+
);
200+
let err = result.unwrap_err().to_string();
201+
assert!(
202+
err.contains("invalid file header"),
203+
"expected InvalidFileHeader, got: {err}"
204+
);
205+
}
206+
207+
#[test]
208+
fn lenient_accepts_binary_bytes_on_header_line() {
209+
// Same buffer, but lenient (default) mode should parse the header
210+
// successfully (it will fail later because the rest isn't a real PDF,
211+
// but the header itself should be accepted).
212+
let buf = b"%PDF-1.3 \xb0\x9f\x92\x9c\r%%EOF\r";
213+
let result = Document::load_mem_with_options(buf, LoadOptions::default());
214+
// The error should NOT be InvalidFileHeader — the header parsed fine.
215+
if let Err(e) = &result {
216+
assert!(
217+
!e.to_string().contains("invalid file header"),
218+
"lenient mode should accept binary bytes on header line, got: {e}"
219+
);
220+
}
221+
}
190222
}

0 commit comments

Comments
 (0)