vbuch / node-signpdf

Simple signing of PDFs in node.
MIT License
679 stars 176 forks source link

Not able to sign some complex documents, with a unacceptable xref root #168

Closed sameerdutta9703 closed 1 year ago

sameerdutta9703 commented 2 years ago

@vbuch Describe the bug and the expected behavior --> Currently, I am using this library to signup a document and then verify it, over in the code I initially create a placeholder via plainPlaceholder and then sign it using the signer, which works for some pdf but throws an error as " SignPdfError: Failed to find the pages descriptor. This is probably a problem in node-signpdf." in some of the complex PDF's

Is it a bug in signing or in the helpers? If it's an issue with helpers, within the /helpers/plainAddPlaceholder/getPagesDictionaryRef.js:20:11

To Reproduce To reproduce try signing the file "pdf_scripting.pdf", by adding a planeAddPlaceHolder to it initially and then signing it via the signer, as in the code below --->>>

// Generate p12
const pkcsAsn1 = forge.pkcs12.toPkcs12Asn1(
    privateKey,
    [certificate],
    passphrase,
);

// Convert to Buffer
const pkcsAsn1Bytes = forge.asn1.toDer(pkcsAsn1).getBytes();
const pkcsb64 = forge.util.encode64(pkcsAsn1Bytes);
const pk12Buffer = Buffer.from(pkcsb64, 'base64');

pdfBuffer = plainAddPlaceholder({
    pdfBuffer,
    reason: 'No reason provided',
    location: 'Random Location',
    signatureLength: pk12Buffer.length,
});

return signer.sign(pdfBuffer, pk12Buffer);

The Findings I found that this error might have been at the regex check in the helper file getPagesDictionary, The above file giving error had a info.xref.root as "'\n/S /JavaScript\n/JS (event.value = "www.pdfscripting.com"\r\n\r\n)\n'", while the one which worked properly had a info.xref.root as "'\n/Pages 2 0 R\n/Type /Catalog\n'".

Can you modify the regex to accept the below-attached files, or please provide us with a different solution.

Error Throwing pdf -> pdf_scripting.pdf Working pdf -> sample.pdf

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had activity in the past 90 days. It will be closed if no further activity occurs. Thank you for your contributions.