mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 13:29:19 +08:00
fix(tokenizers): start a stream's data after a lone CR, as readers do
The walk only recognised `stream` followed by LF or CRLF. After a lone CR (or trailing spaces) the stream data was read as object syntax, so bare tokens in a content stream such as `ET endobj 2 0 obj << /Type /Pages /Count 9999 >> endobj` redefined the page tree: pypdf strict mode opens that file at 1 page, Headroom priced 9999. The keyword now takes any spaces and an optional CR, CRLF or LF, the same data start pypdf uses, so the /Length check covers these streams too. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -54,8 +54,10 @@ _PDF_MAX_XREF_SECTIONS = 1_000 # bounds the /Prev chain an indirect /Length is
|
||||
_PDF_TOP_LEVEL = re.compile(
|
||||
rb"(?=[%\dt])(?:%[^\r\n]*|(?<!\d)(\d{1,10})\s+\d{1,5}\s+obj\b|(?<![A-Za-z/])trailer\b)"
|
||||
)
|
||||
# A stream's data starts after its keyword, any spaces and an EOL if present (a
|
||||
# lone CR too): the spec requires CRLF or LF, but readers accept the others.
|
||||
_PDF_SYNTAX_STOP = re.compile(
|
||||
rb"(?=[%(se])(?:%[^\r\n]*|\(|(?<![A-Za-z/])(?:stream\r?\n|endobj|startxref))"
|
||||
rb"(?=[%(se])(?:%[^\r\n]*|\(|(?<![A-Za-z/])(?:stream *(?:\r\n?|\n)?|endobj|startxref))"
|
||||
)
|
||||
_PDF_STRING_TOKEN = re.compile(rb"\\.|[()]", re.S)
|
||||
_PDF_STREAM_LENGTH = re.compile(rb"/Length\s+(\d{1,10})(?!\d|\s+\d+\s+R)")
|
||||
|
||||
@@ -346,6 +346,7 @@ def _packed(objects: dict[int, bytes], number: int) -> dict[int, bytes | dict[in
|
||||
|
||||
|
||||
_INDIRECT = _one_page_objects(_FAKE_TREE, length=b"/Length 6 0 R")
|
||||
_LONE_CR = b"BT /F1 12 Tf 72 720 Td (hi) Tj ET endobj 2 0 obj << /Type /Pages /Count 9999 >> endobj"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
@@ -363,6 +364,18 @@ _INDIRECT = _one_page_objects(_FAKE_TREE, length=b"/Length 6 0 R")
|
||||
# Through an xref stream, and through /Prev to the section that lists object 6.
|
||||
(_pdf_file(_packed(_INDIRECT, 5)), 1),
|
||||
(_pdf_file({5: _INDIRECT[5]}, prior=_pdf_file(_INDIRECT)), 1),
|
||||
# A lone CR after the stream keyword still starts a stream, so bare tokens after
|
||||
# the text operators stay data.
|
||||
(
|
||||
_pdf_file(
|
||||
{
|
||||
**_INDIRECT,
|
||||
4: b"<< /Length 6 0 R >>\nstream\r%s\nendstream" % _LONE_CR,
|
||||
6: b"%d" % len(_LONE_CR),
|
||||
}
|
||||
),
|
||||
1,
|
||||
),
|
||||
# A stream end that cannot be verified stops the walk, and 0 means the size
|
||||
# estimate: the length packed in an object stream, a wrong one, a reference to
|
||||
# no object, no /Length at all.
|
||||
@@ -376,6 +389,7 @@ _INDIRECT = _one_page_objects(_FAKE_TREE, length=b"/Length 6 0 R")
|
||||
"fake-length-object",
|
||||
"xref-stream",
|
||||
"prev-section",
|
||||
"lone-cr",
|
||||
"packed-length",
|
||||
"wrong-length",
|
||||
"dangling-length",
|
||||
|
||||
Reference in New Issue
Block a user