From a63f5bc4ea1081dfbb8c504103dbb735ab2d1994 Mon Sep 17 00:00:00 2001 From: Thomas Leitner Date: Sat, 14 Sep 2024 22:27:59 +0200 Subject: [PATCH] Fix Signature#signed_data to work for invalid offsets --- CHANGELOG.md | 4 ++++ lib/hexapdf/digital_signature/signature.rb | 2 +- test/hexapdf/digital_signature/test_signature.rb | 7 +++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c3963272..dbef86fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,10 @@ * [HexaPDF::Type::AcroForm::Form] text-like field creation methods to always set a default appearance string +### Fixed + +* [HexaPDF::DigitalSignature::Signature#signed_data] to work for invalid offsets + ## 0.47.0 - 2024-09-07 diff --git a/lib/hexapdf/digital_signature/signature.rb b/lib/hexapdf/digital_signature/signature.rb index 346a6ce4..8bbad086 100644 --- a/lib/hexapdf/digital_signature/signature.rb +++ b/lib/hexapdf/digital_signature/signature.rb @@ -211,7 +211,7 @@ def signed_data data = ''.b self[:ByteRange]&.each_slice(2) do |offset, length| io.pos = offset - data << io.read(length) + data << io.read(length).to_s end data end diff --git a/test/hexapdf/digital_signature/test_signature.rb b/test/hexapdf/digital_signature/test_signature.rb index 869cfd96..dbe4b043 100644 --- a/test/hexapdf/digital_signature/test_signature.rb +++ b/test/hexapdf/digital_signature/test_signature.rb @@ -111,6 +111,13 @@ assert_equal((MINIMAL_PDF[0, 400] << MINIMAL_PDF[500, 333]).b, @sig.signed_data) end + it "works for invalid offsets" do + doc = HexaPDF::Document.new(io: StringIO.new(MINIMAL_PDF)) + @sig.document = doc + @sig[:ByteRange] = [0, 400, 9000, 333] + assert_equal(MINIMAL_PDF[0, 400], @sig.signed_data) + end + it "fails if the document isn't associated with an existing PDF file" do assert_raises(HexaPDF::Error) { @sig.signed_data } end