gettalong / hexapdf

Versatile PDF creation and manipulation for Ruby
https://hexapdf.gettalong.org
Other
1.24k stars 70 forks source link

Invalid /R value 5 for standard security handler #236

Open casperbrike opened 1 year ago

casperbrike commented 1 year ago

Greetings!

When I try to read a PDF document using the following code:

HexaPDF::Document.open 'sample.pdf'

I get the error:

Invalid /R value 5 for standard security handler (HexaPDF::UnsupportedEncryptionError)

According to the stacktrace, the error happens here:

lib/hexapdf/encryption/standard_security_handler.rb:329

My HexaPDF version is 0.30.0.

However, this error is specific to this document, I can read other documents without problems. Could you point out how to read the document correctly, please?

Thanks in advance!

gettalong commented 1 year ago

The encryption that is specified via /R value of 5 is a proprietary Adobe algorithm that is not specified in the ISO PDF 1.7 or 2.0 standards. Therefore it is not supported at the moment. I may have a look into that at a later time.

vladislav-zubov commented 2 months ago

Do we have any updates on that?

gettalong commented 2 months ago

@vladislav-zubov As stated I may look into this at a later time. I have found a reference for implementation, so it is just a matter when there is time for it.

In the meantime it would be great if an actual PDF that is encrypted with /R 5 is attached to this issue for testing the implementation.

gettalong commented 2 months ago

@vladislav-zubov @casperbrike So... "it's just a matter of time"... maybe the time is now - could you try the current version of HexaPDF in the devel branch on a PDF document that fails with the "Invalid /R value 5..." error?

A quick look through the spec of Adobe for that feature that I found suggests that the code in the devel branch might work.

vladislav-zubov commented 1 month ago

@gettalong Hello, thanks for your answers.

I have tried with the devel branch and it says that incorrect password but a pdf file does not have any passwords installed

HexaPDF::EncryptionError: Invalid password specified
from /usr/local/bundle/bundler/gems/hexapdf-789a377cb6ac/lib/hexapdf/encryption/standard_security_handler.rb:369:in `prepare_decryption'
gettalong commented 1 month ago

@vladislav-zubov Could you please provide the PDF to debug this?

gettalong commented 1 month ago

@vladislav-zubov I found another small difference between /R 5 and /R 6 and updated the devel branch - could you try again?

vladislav-zubov commented 1 month ago

@gettalong it works. thanks. Could you bump up a version of the gem?

gettalong commented 1 month ago

I'm sorry but before I can release this, I need to properly test it. The changes were just shots in the dark by having quick looks at the spec 😉

I can probably do a release in a week.

vladislav-zubov commented 1 month ago

@gettalong I'll test with you. I have around 30k documents failed with that error

gettalong commented 1 month ago

@vladislav-zubov I found a way to generate the necessary test files for /R 5 and added tests. The changes are live on the devel branch for you to test on your collection of real-world files.