pdf-lib icon indicating copy to clipboard operation
pdf-lib copied to clipboard

Badly parsed JavaScript in AcroForm field

Open Strnadj opened this issue 6 months ago • 0 comments

What were you trying to do?

I am trying to catch an encrypted or PDF with JavaScript included.

How did you attempt to do it?

This is a debug piece of code, I was running:

import { PDFCatalog, PDFArray, PDFDict, PDFDocument, PDFName } from 'pdf-lib';

const file = fs.readFileSync("test/fixtures/script.pdf");

const doc = await PDFDocument.load(file);

const acroForm = doc.catalog.AcroForm();

function iterate(dict, level) {
  if (dict instanceof PDFDict) {
    for (const [key, value] of dict.entries()) {
      console.log(" ".repeat(level) + "-> " + key, value);
      iterate(value, level + 1);
    }
  }

  if (dict instanceof PDFArray) {
    for (const i = 0; i < dict.array.length; i++) {
      const item = dict.lookup(i);
      console.log(" ".repeat(level) + "-> " + item);
      iterate(item, level + 1);
    }
  }
}

console.log(acroForm);
iterate(acroForm, 1);

What actually happened?

The first item of that array was parsed wrongly as "text"

  -> <<
/A 103 0 R
/AP <<
/N 50 0 R
>>
/BS <<
/S /B
>>
/DA (/HeBo 10 Tf 0 g)
/F 4
/FT /Btn
/Ff 65536
/MK <<
/BC [ 0 0 1 ]
/BG [ 0.75293 ]
/CA (Make Text Lines -> List Entries)
>>
/P 49 0 R
/Rect [ 275.709 286.727 447.855 306.727 ]
/Subtype /Widget
/T (Button1)
/Type /Annot
>>
   -> /A PDFRef {objectNumber: 103, generationNumber: 0, tag: '103 0 R'}

What did you expect to happen?

This is

PDFS_CopyPastListEntries.pdf

the file opened in PDF debugger:

Image

How can we reproduce the issue?

The Example is above.

Version

1.17.1

What environment are you running pdf-lib in?

Node

Checklist

  • [x] My report includes a Short, Self Contained, Correct (Compilable) Example.
  • [x] I have attached all PDFs, images, and other files needed to run my SSCCE.

Additional Notes

No response

Strnadj avatar Aug 05 '25 11:08 Strnadj