Skip to content

Commit

Permalink
Really fix encryption issue
Browse files Browse the repository at this point in the history
The previous fix incorrectly escaped "\r" as "\\\r". However, it needs
to be escaped as "\\r" (so backslash followed by letter r).
  • Loading branch information
gettalong authored and pointlessone committed Mar 4, 2024
1 parent 9af8c3c commit 68d0e77
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/prawn/security.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
6 changes: 2 additions & 4 deletions spec/prawn/document/security_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down

0 comments on commit 68d0e77

Please sign in to comment.