PDF encryption RuntimeError: integer out of range converting 4294967295 from a 8-byte signed type to a 4-byte signed type

Any one has encountered error like below for a pdf encryption:-

(exit) an exception was raised:
2020-11-02T23:16:20.453666662Z     ** (RuntimeError) integer out of range converting 4294967295 from a 8-byte signed type to a 4-byte signed type
2020-11-02T23:16:20.453669462Z 
2020-11-02T23:16:20.453671862Z         (my_future_now 4.0.0) lib/my_future_now/services/encrypt_pdf_service.ex:61: MyFutureNow.EncryptPdfService.encrypt_pdf!/3
2020-11-02T23:16:20.453674663Z         (my_future_now 4.0.0) lib/my_future_now/services/encrypt_pdf_service.ex:20: MyFutureNow.EncryptPdfService.encrypt_and_write_file!/2
2020-11-02T23:16:20.453677463Z         (my_future_now 4.0.0) lib/my_future_now_web/email/loa_email.ex:90: MyFutureNowWeb.LOAEmail.employer_encrypted_loa_parts/1
2020-11-02T23:16:20.453680163Z         (my_future_now 4.0.0) lib/my_future_now_web/email/loa_email.ex:353: MyFutureNowWeb.LOAEmail.loa_email_to_employer/2

You should describe what You are doing, with which tools… like this it’s hard to have any idea.

1 Like

We are using
QPDF (PDF document processing)

We use qpdf for encrypting (password protecting) PDFs.

Below is the code for encrypting pdf:-

defp encrypt_pdf!(orig_path, password, opts) do
    {ext, rest} = orig_path |> String.split(".") |> List.pop_at(-1)
    new_path = Enum.join(rest ++ ["encrypted", ext], ".")

    page_range = if range = opts[:page_range], do: to_string(range), else: "1-z"

    opts =
      ["--empty", "--pages", orig_path, page_range, "--"] ++
        ["--encrypt", password, password, "128", "--use-aes=y", "--", new_path]

    case System.cmd("qpdf", opts, stderr_to_stdout: true) do
      {_, 0} -> new_path
      {err, _} -> raise(err)
    end
  end
def encrypt_and_upload!(%Document{} = orig_document, opts \\ []) do
    password = SecureRandom.urlsafe_base64(8)

    orig_document
    |> download_and_write_file!
    |> encrypt_pdf!(password, opts)
    |> upload_new!(password, orig_document)
  end

  def encrypt_and_write_file!(%Document{} = document, opts \\ []) do
    path = download_and_write_file!(document)
    password = SecureRandom.urlsafe_base64(8)

    enc_path = encrypt_pdf!(path, password, opts)

    {enc_path, password}
  end

1 Like

The error logs says that the encryption is breaking, not sure where and why

(RuntimeError) integer out of range converting 4294967295 from a 8-byte signed type to a 4-byte signed type

RuntimeError is what raise produces; based on the stacktrace I’d guess {err, _} -> ... is on line 61.

qpdf is returning that message from deep in a utility function, unfortunately:

1 Like

QPDF_VERSION=9.1.1 is used by us.
Will a upgrade of the version help. How to fix the issue any idea?

This post is the first time I’ve ever seen qpdf, so I can’t really say what the problem is.

Skimming the changelog on that repo doesn’t show any obvious “fix this exact bug” entries since 9.1.1, but it likely wouldn’t hurt to upgrade.

The next lead I’d recommend you follow is that value 4294967295 - where does it appear in the input? Does qpdf give a different error for a different input file?

Edit to add: 4294967295 is 0xFFFF_FFFF, which is what you’d get if code mistakenly interpreted a 4-byte signed -1 as a 4-byte unsigned value - not sure if that’s significant.

1 Like

integer out of range converting 4294967295 from a 8-byte signed type to a 4-byte signed type is a bug in qpdf <= 1.0.4. See here https://www.gitmemory.com/issue/qpdf/qpdf/482/721737907

I had the same, upgraded qpdf to 1.0.4 and I confirm it works (also tested qpdf 1.1.0, works too).