Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change ByteRange placeholder to allow bigger input #337

Merged
merged 1 commit into from
Nov 2, 2023
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
21 changes: 14 additions & 7 deletions pyhanko/sign/signers/pdf_byterange.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
]


BYTE_RANGE_ARR_PLACE_HOLDER_LENGTH = 60


class SigByteRangeObject(generic.PdfObject):
"""
Internal class to handle the ``/ByteRange`` arrays themselves.
Expand Down Expand Up @@ -70,13 +73,17 @@ def fill_offsets(self, stream, sig_start, sig_end, eof):
def write_to_stream(self, stream, handler=None, container_ref=None):
if self._range_object_offset is None:
self._range_object_offset = stream.tell()
string_repr = "[ %08d %08d %08d %08d ]" % (
0,
self.first_region_len,
self.second_region_offset,
self.second_region_len,
)
stream.write(string_repr.encode('ascii'))
stream.write(b"[]")
stream.write(b" " * BYTE_RANGE_ARR_PLACE_HOLDER_LENGTH)
else:
string_repr = b"[%d %d %d %d]" % (
0,
self.first_region_len,
self.second_region_offset,
self.second_region_len,
)
assert len(string_repr) <= BYTE_RANGE_ARR_PLACE_HOLDER_LENGTH + 2
stream.write(string_repr)


class DERPlaceholder(generic.PdfObject):
Expand Down
19 changes: 19 additions & 0 deletions pyhanko_tests/test_signing.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,25 @@ def test_simple_sign_fresh_doc():
val_untrusted(emb)


def test_simple_sign_120mb_file():
# put this in a function to avoid putting multiple copies
# of a huge buffer in locals
def _gen_signed():
w = IncrementalPdfFileWriter(BytesIO(MINIMAL))
w.root['/YugeObject'] = w.add_object(
generic.StreamObject(stream_data=bytes(120 * 1024 * 1024))
)
w.update_root()

meta = signers.PdfSignatureMetadata(field_name='Sig1')
return signers.sign_pdf(w, meta, signer=SELF_SIGN)

r = PdfFileReader(_gen_signed())
emb = r.embedded_signatures[0]
assert emb.field_name == 'Sig1'
val_untrusted(emb)


def test_append_sigfield_tu_on_signing():
buf = BytesIO(MINIMAL)
w = IncrementalPdfFileWriter(buf)
Expand Down
Loading