Skip to content

Commit

Permalink
Merge pull request #337 from MatthiasValvekens/bugfix/sign-large-files
Browse files Browse the repository at this point in the history
Change ByteRange placeholder to allow bigger input
  • Loading branch information
MatthiasValvekens authored Nov 2, 2023
2 parents e13f7c1 + 0a66252 commit 30d272c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
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

0 comments on commit 30d272c

Please sign in to comment.