From 68d0e770f265a36ef5f6e2a083a6bcb3a9646641 Mon Sep 17 00:00:00 2001 From: Thomas Leitner Date: Fri, 18 Feb 2022 10:35:02 +0100 Subject: [PATCH] Really fix encryption issue The previous fix incorrectly escaped "\r" as "\\\r". However, it needs to be escaped as "\\r" (so backslash followed by letter r). --- lib/prawn/security.rb | 4 ++-- spec/prawn/document/security_spec.rb | 6 ++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/prawn/security.rb b/lib/prawn/security.rb index dcc03884c..a13392c42 100644 --- a/lib/prawn/security.rb +++ b/lib/prawn/security.rb @@ -225,14 +225,14 @@ def encrypted_pdf_object(obj, key, id, gen, in_content_stream = false) obj = ByteString.new( Prawn::Document::Security.encrypt_string(obj, key, id, gen), - ).gsub(/[\\\r()]/) { |m| "\\#{m}" } + ).gsub(/[\\\r()]/, STRING_ESCAPE_MAP) "(#{obj})" when Time obj = "#{obj.strftime('D:%Y%m%d%H%M%S%z').chop.chop}'00'" obj = ByteString.new( Prawn::Document::Security.encrypt_string(obj, key, id, gen), - ).gsub(/[\\\r()]/) { |m| "\\#{m}" } + ).gsub(/[\\\r()]/, STRING_ESCAPE_MAP) "(#{obj})" when String pdf_object( diff --git a/spec/prawn/document/security_spec.rb b/spec/prawn/document/security_spec.rb index 0f030440c..f12b808bd 100644 --- a/spec/prawn/document/security_spec.rb +++ b/spec/prawn/document/security_spec.rb @@ -131,7 +131,7 @@ class << pdf PDF::Core::LiteralString.new("\xAF\xC5fh\x9A\x14\x97,\xD3,\x06\x87\xCDSS"), nil, 123, 0, ), ).to eq( - bin_string("(2&\\(\x02P\x92\x9C\e\xAF\\)\\\r\x83\x94\x11\x0F)"), + bin_string("(2&\\(\x02P\x92\x9C\e\xAF\\)\\r\x83\x94\x11\x0F)"), ) end @@ -140,9 +140,7 @@ class << pdf PDF::Core.encrypted_pdf_object( Time.utc(10_002, 0o4, 26, 10, 17, 10), '12345', 123, 0, ), - ).to eq( - bin_string("(h\x83\xBD\xDC\xE9\x99\\\r\xD3/!\x14\xD5%\xBE\xF6\x17\xA3\x9B\xC5\xFE&+\xD8\x93)"), - ) + ).to eq bin_string("(h\x83\xBD\xDC\xE9\x99\\r\xD3/!\x14\xD5%\xBE\xF6\x17\xA3\x9B\xC5\xFE&+\xD8\x93)") end it 'properlies handle compound types' do