Skip to content

Show a PDF's attached files in a Files tab and save them - #92

Open
pablopda wants to merge 6 commits into
Forward-Future:mainfrom
pablopda:feat/attachments-list-save
Open

pablopda wants to merge 6 commits into
Forward-Future:mainfrom
pablopda:feat/attachments-list-save

Conversation

@pablopda

@pablopda pablopda commented Nov 11, 2026 •

Copy link
Copy Markdown

Problem

A PDF can carry files embedded in the document itself (a spreadsheet, a source file, a signed copy), but Folio gave no sign they were there and no way to get them out.

Change

When a PDF has files attached at the document level, the sidebar gets a Files tab next to Pages, Outline and Notes. It lists each file with its name and description, and the button on a row saves that file exactly as it was embedded. PDFs without attachments show no extra tab.

The names, descriptions and contents come from the PDF, so they are treated as untrusted:

  • A name is reduced to a plain file name before it is shown or offered for saving: no folders, no leading dot, no control or direction characters, no invisible characters except the two joiners that Persian and Indic names need, no run of blanks that hides the ending, at most 255 characters.
  • At most 500 files are listed, and the list says so only when the PDF has more.
  • Size limit, enforced while reading. PDF.js can only unpack a whole attachment, so the contents are not read through it. The browser's DecompressionStream unpacks the embedded stream in 16 KB input pieces, and the read is cancelled once it passes 300 MB (the existing PDF size limit).
  • Finding the file, within a memory bound. A PDF library has to read every object in the file to find one, and a file of a few hundred bytes can make that cost gigabytes. So saving no longer opens the PDF with pdf-lib. A small reader (src/attachmentReader.ts) follows only the objects that lead to the attachment: the object table, the catalog, the name tree, the file specification and the stream. It reads them the way PDF.js does, since PDF.js's list is where the attachment's id comes from. It refuses a file that is damaged or built in a way it doesn't know; it never repairs one.
  • Off the main thread, with a time limit. The save runs in a worker, is given up 30 seconds after the click, and is cancelled when the document closes.
  • Files that could run. Saving a file whose ending can run, install or mount something first asks, showing the whole file name, with Cancel focused; the reader's shortcuts don't act on the page behind the question. The check also covers endings hidden behind trailing dots, spaces, a colon, or letters that Windows folds to ASCII.
  • The file is saved as plain binary, so the offered name doesn't gain an extension guessed from the contents.

Files attached to a page as annotations, and reading attached PDFs in their own tabs, are left out, as agreed in the issue. The README's "Read and navigate" list has one new line.

The memory bound, in numbers

Beyond the PDF's own bytes (at most 300 MB, which pdf.getData() hands over as it does for every export) and the attachment itself (at most 300 MB), a save may use:

What the PDF controls Limit Past it
Object tables and object streams it unpacks, counted twice because they are briefly held twice, plus strings it keeps 64 MB in total refused
Numbers, names, strings and other tokens it reads 1,000,000 refused
Strings longer than 32 KB not kept read past
Revisions of the object table it follows 1,024 refused
Lines of classic object tables it checks 20,000,000, none kept refused
Nesting of lists and dictionaries 64 refused

Two things sit on top of the 64 MB. Chromium unpacks each 16 KB piece whole before it can be counted, which can be about 17 MB. The parsed values themselves took up to about 20 MB at the token limit when I measured them in Node. Time is bounded only by the 30 seconds: a PDF can keep the worker busy until then.

What this does not save, and what it does not bound

These are refused with a message, never saved wrong:

  • Attachments in a protected PDF, including one with only an owner password that opens without a prompt.
  • Streams stored with anything other than a single Flate filter, or with a predictor, or whose stated length doesn't end at endstream.
  • PDFs whose object table is damaged. PDF.js rebuilds such a table by searching the file; this reader refuses instead, as you suggested a first version could.

Two things are for your judgement:

  • A hand-written reader. src/attachmentReader.ts is about 525 lines: a port of the parts of PDF.js's lexer, parser and object-table reading that the path to an attachment needs. In exchange, pdf-lib is gone from this path, and so are the wrapper on its private ensureBuffer and the second, different decoding of names. If you would rather not carry this code, the alternative I see is running pdf-lib in a separate process with a memory cap.
  • Which endings ask. The list is now Microsoft's published "Blocked attachments in Outlook" list, whole, plus newer Windows installers, Office files and add-ins made to carry macros (docm, xlam, xla, ppa and so on), and what macOS and Linux run. It includes js, py and sh, so code attached to a paper will ask. It leaves out doc, xls, html and svg, so the question keeps its meaning.

Not bounded by this PR: PDF.js reads the whole attachment list in its worker when a document opens, so a PDF made to have very many or very large entries costs memory and time before any of these limits apply. One of my test PDFs took the app to 1.7 GB on opening, before any save.

Issue or approval

Refs #53. Scope approved in #53 (comment) ("lists attachments embedded at the document level and lets the reader save one. A small PDF fixture can verify that the saved bytes match the attachment."), and the changes requested in the two reviews.

How I checked it

  • Same bytes as PDF.js. On 74 PDFs written by poppler, Ghostscript, MuPDF, pypdf, PyMuPDF and qpdf or built by hand (1,149 listed attachments), 984 saved byte for byte the same as getAttachmentContent, 165 were refused with a message (160 in four protected PDFs, 5 hand-built: ASCII85 with Flate, a predictor, LZW, truncated Flate, a table with wrong offsets), and none saved different bytes. This set is not in the repository.
  • Unit tests (src/attachmentReader.test.ts, 9 tests, and src/attachments.test.ts, 15 tests). The reader's fixtures are written byte by byte, not with a PDF library. They cover: the old and new file layouts, a predictor, saved revisions, a deleted file; 276 kinds of name, each found by the id PDF.js lists it with; every limit in the table above with a lowered limit, asserting the refusal and the counted amount; two object streams that each fit the limit and together don't; an object table with uneven lines or a line hidden in a comment, where PDF.js and a fixed-position read would pick different objects; the 30 second limit, including while waiting for the PDF's bytes, and closing the document mid-save.
  • Each test can fail. I removed each of 33 limits and checks in turn; a test failed every time.
  • The sequence you asked for, in the hidden Electron app (scripts/smoke-attachments.mjs): highlight a line, try to save an attachment Folio must refuse and see the message, highlight a second line, save another attachment and compare its bytes, save the PDF and check both highlights are in it. The same script checks that Cancel has focus in the question, that R and Ctrl+S do nothing behind it, and that exactly the four files asked for are saved. It passed 10 of 10 runs locally before the last change and 3 of 3 after it.
  • Memory in the hidden Electron app on Linux (whole app, sampled every 50 ms, so short peaks can be missed): the 732-byte PDF that took dc8ad0e from about 670 MB to 2.8 GB now saves its attachment with 679 → 686 MB. An object stream on the path that unpacks to 1 GB is refused in under 200 ms with 1,707 → 1,715 MB (the 1.7 GB is PDF.js opening that file). An object table stream that unpacks to 1 GB is refused in under 200 ms with 679 → 683 MB. A 400 MB attachment is refused in about 0.4 s with 676 → 939 MB, which is the attachment's own 300 MB limit.
  • An independent security review of the reader found two defects, both fixed with tests that failed first: an object table with uneven lines could save a different object than the one listed, and long strings cost far more memory than was counted.
  • Windows, macOS, and Linux CI passed in my fork on commit 1563b7d: type check, unit tests, build and the full smoke suite.
  • Locally on Linux (Ubuntu): npx tsc --noEmit, the two attachment test files, and the attachments smoke check with a hidden window.

Not checked: Windows and macOS by hand (CI only); the native save dialog, which the smoke check bypasses by setting the save path; whether Windows or macOS mark the saved file as downloaded; PDFs written by Acrobat, Word, LibreOffice or pdfTeX; the Files tab next to the new Organize pages mode, by hand.

Before and after, the same PDF with three attached files:

Before:

before

After:

after

Model and harness: Claude Fable 5.2 in Claude Code.

🤖 Generated with Claude Code

When a PDF has files embedded at the document level, the sidebar gets a
Files tab that lists them and saves each one exactly as it was attached.
PDFs without attachments show no extra tab. Names come from the PDF, so
they are reduced to a plain file name before being shown or saved.

Files attached to a page as annotations and reading attached PDFs in
their own tabs are left out, as agreed in Forward-Future#53.
@pablopda
pablopda marked this pull request as ready for review November 11, 2026 20:20

@mberman84 mberman84 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Files tab covers the agreed first part of #53. On macOS the type check, production build, all 266 unit tests, and hidden smoke suite passed, including saving attachments byte for byte.

Please address the 300 MB limit before merging. Folio currently asks PDF.js for the complete attachment and checks the byte length after it returns. That keeps an oversized file off disk, but the reader has already had to allocate it. Please enforce a bound while reading the contents, or use an extraction path that can stop at the limit and keep the document usable. A small fixture with a reduced test limit is sufficient.

For the executable-file question in the description, I recommend an explicit confirmation that shows the full filename before handing an executable attachment to the save dialog. The automated desktop test verifies the suggested filename and downloaded bytes. It bypasses the native save dialog.

… one that could run

An attachment was unpacked whole by pdf.js and only then checked against the
300 MB limit. It is now read from the PDF's own bytes and unpacked a small
piece at a time, stopping once it passes the limit. Attachments stored in a
form that can't be read that way (a protected PDF, a filter other than a
single Flate, a predictor, a length that doesn't fit) are refused with a
message instead of being saved wrong.

Saving a file whose name could run when opened first shows its full name
and asks.
…ke Folio unpack or wait for

Finding an attached file means reading the whole PDF with pdf-lib, which
unpacks every packed list of objects in it. That now happens in a worker,
stops once 300 MB has been unpacked, and is given up after 30 seconds, so
a PDF built to be expensive no longer freezes the window on Save.

Also: an empty file, a single filter with empty settings, a name starting
with the UTF-8 mark and data followed by a counted line break now save;
the question before saving a file that could run always shows the whole
name and covers more endings; one file is saved at a time.
@pablopda

Copy link
Copy Markdown
Author

Both points are addressed in 7dd0c65 and dc8ad0e, and the description above is updated to match. CI passed on Windows, macOS and Linux in my fork for dc8ad0e.

Size limit. PDF.js has no way to stop partway through an attachment, so the contents are no longer read through it. pdf-lib locates the embedded stream and the browser's DecompressionStream unpacks it in 16 KB input pieces; the read is cancelled once it passes the limit. A test packs 64 MB into under 100 KB and checks, with a 4 KB limit, that no more than four pieces are handed to the unpacker. In the Electron app, a 400 MB attachment in a 408 KB PDF is refused in about half a second.

Executable attachments. Saving a file whose ending can run, install or mount something first asks, showing the whole file name, with Cancel focused. The smoke test attaches Quarterly report.pdf.bat, checks the question shows that name, that Cancel saves nothing, and that Save writes the right bytes.

Three things I'd like your call on:

  1. Parsing the PDF to find the attachment is only partly bounded. It runs in a worker, with a 300 MB cap on what pdf-lib may unpack and a 30 second limit, and the window stayed responsive in every case I measured. But a hostile PDF can still make pdf-lib allocate a lot in that time: a 732-byte file took the app from about 670 MB to 2.8 GB before pdf-lib gave up. Page editing already runs the same PDFDocument.load in its worker, so the exposure isn't new, but Save now reaches it. Closing it fully would take a separate process with a memory cap or a purpose-built reader for the name tree. I can do that here or as a follow-up, whichever you prefer.
  2. Protected PDFs. Attachments in an encrypted PDF are refused, including one with only an owner password that opens without a prompt, because pdf-lib can't decrypt them and PDF.js can't read them within a limit. If you'd rather those save through PDF.js without the bound, that is a small change.
  3. Which endings ask. The list includes js, py and sh, and leaves out doc, xls, html and svg. Happy to move either way.

This PR is larger than the first version (a worker file and more tests). If you'd prefer it split, the confirmation dialog separates cleanly from the read path.

@mberman84 mberman84 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for addressing both earlier review points. The save now stops decoding the attachment when it reaches the limit and asks before saving a runnable file. I verified that all three platforms passed in your fork at dc8ad0e.

I’d address the remaining memory issue in this PR. The save worker still parses the whole PDF before it can extract the attachment. The 300 MB check covers decompression buffers, and the timeout covers elapsed time. Neither bounds the parser’s total memory. Your measurement of roughly 2.8 GB makes that a blocker for an everyday Save action: a reader could lose unrelated unsaved work if the renderer runs out of memory.

A small first version can refuse a file it cannot read within a real memory bound. The protected-PDF refusal is reasonable. Please keep the current document-level scope and verify that the parent PDF and its edits stay usable when extraction is refused.

…its, instead of opening the PDF with pdf-lib

Saving an attachment no longer parses the whole document: a small reader follows the table of objects, the name
tree and the file's stream the way pdf.js does, counts what it unpacks and reads against limits, and refuses a
file that is damaged or built in a way it doesn't know. The question before saving a file that could run now owns
the keyboard, the list of endings follows Outlook's published list, the 30 seconds count from the click, and a
PDF with exactly 500 files is no longer said to have more.
…o be a name

A table whose lines were of uneven length could lead to a different object than pdf.js reads, so a file could be
saved with other contents than the one listed: every line is now checked to be written the regular way before any
is read from its place. A long string in an object on the way to the file was kept in a form many times its size,
past what the limit counted: strings over 32 KB are read past and not kept.
@pablopda

Copy link
Copy Markdown
Author

The memory issue is addressed in 87681c6 and 1563b7d, and the branch is merged up to main. Windows, macOS, and Linux CI passed in my fork on commit 1563b7d: type check, unit tests, build and the full smoke suite.

What changed. Saving an attachment no longer opens the PDF with pdf-lib. A small reader follows only the objects that lead to the attachment, reads them the way PDF.js does, and counts what it unpacks and reads against fixed limits (64 MB of working data, a million tokens; the table is in the description). A file that is damaged or built in a way it doesn't know is refused, not repaired. Protected PDFs are still refused.

The numbers. The 732-byte PDF that reached 2.8 GB now saves its attachment with about 7 MB of growth. PDFs built against the new reader, with structures that unpack to 1 GB, are refused in under 200 ms with under 10 MB of growth.

Parent PDF and edits after a refusal. The smoke test now does this in the app: highlight, refused attachment, second highlight, save another attachment, save the PDF, and check both highlights are in the saved file.

Also fixed on the way: reader shortcuts acted on the page behind the question about a runnable file; .xla and .ppa didn't ask (the list now follows Outlook's published one); the 30 seconds didn't cover waiting for the PDF's bytes; a PDF with exactly 500 files was said to have more.

Two things for your call, both in the description: the reader is about 525 lines of hand-written PDF reading, and PDF.js's own cost of opening and listing a hostile PDF is outside this change.

@PedroHenriquePdR

Copy link
Copy Markdown

I tested commit 1563b7d on Ubuntu 26.04.1 x86_64 with Electron 45.2.3. I opened the PDFs from disk and used Files → Save. The automated test entered the destination in the native GTK file chooser on a virtual X11 display.

  • LibreOffice Writer 26.2.6.3, hybrid PDF and hybrid PDF/A-3b exports. Both listed Original.odt. Each saved ODT matched its own embedded file byte for byte in independent extractions with Poppler and qpdf.
  • pdfTeX 1.40.28 and a Writer PDF with a Poppler-added attachment. Both saved the original 76-byte UTF-8 text file exactly.
  • In the pdfTeX case, I entered a filename and cancelled. The destination directory stayed empty. Saving again in the same reader succeeded.
  • A plain Writer PDF with no attachments showed no Files tab after the page thumbnail rendered.

The production build and the project's full smoke command also passed on Linux. I ran these checks with Codex.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants