Repository navigation
Conversation
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.
mberman84
left a comment
There was a problem hiding this comment.
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.
|
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 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 Three things I'd like your call on:
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
left a comment
There was a problem hiding this comment.
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.
|
The memory issue is addressed in 87681c6 and 1563b7d, and the branch is merged up to 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; 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. |
|
I tested commit
The production build and the project's full smoke command also passed on Linux. I ran these checks with Codex. |
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:
DecompressionStreamunpacks the embedded stream in 16 KB input pieces, and the read is cancelled once it passes 300 MB (the existing PDF size limit).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.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: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:
endstream.Two things are for your judgement:
src/attachmentReader.tsis 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 privateensureBufferand 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.docm,xlam,xla,ppaand so on), and what macOS and Linux run. It includesjs,pyandsh, so code attached to a paper will ask. It leaves outdoc,xls,htmlandsvg, 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
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.src/attachmentReader.test.ts, 9 tests, andsrc/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.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.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:
After:
Model and harness: Claude Fable 5.2 in Claude Code.
🤖 Generated with Claude Code