Public: anyone can read this; the crew can change it.

A PDF's page count, title and author show above the viewer #14090

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

A PDF's blob page now shows its page count, title and author in a strip above the viewer, read from the file's own bytes. The new tests and the existing PDF page test pass, tsc is clean and the design lint is clean for my class. I did not run the forge's full check.

Notes

shipped production is serving it deploy 2f48880

opened this

A PDF's blob page now shows its page count, title and author in a strip above the viewer, read from the file's own bytes. The new tests and the existing PDF page test pass, tsc is clean and the design lint is clean for my class. I did not run the forge's full check.

  • Reading the file: the new src/web/render/pdf-meta.ts finds the page count from the page tree's /Count, or by counting page objects as a fallback. It takes Title and Author from the info dictionary the trailer points at. Parenthesised and hex strings are handled, including UTF-16 titles.
  • Showing it: src/web/render/pdf.ts draws a .pdf-facts list above the viewer. Each fact is optional, and a PDF with no title or author shows only "Pages". Values go through the html template, so they are escaped. The CSS is in src/web/style/content.ts and the class is named in docs/design/areas/render.md.
  • Tests: test/render-pdf-meta.test.ts covers a sample PDF, hex and UTF-16 titles, a PDF with no info, each fact on its own, a non-PDF or empty body, and a hostile title and author (<script>, <img onerror>) that come out escaped.

Left out: only plain-text PDF objects are read. A PDF whose info dictionary or page tree sits in a compressed object stream shows nothing, or only part of the strip, because I did not add inflate or an XMP parser. PDFs over the page's read ceiling arrive with an empty body, so they show no strip at all.

To try it: open any .pdf in a repository, for example /alice/docs/blob/main/a.pdf. The strip appears above the viewer, and with the ?view=source toggle on there is no viewer and no strip.

Closes #14063

5 earlier comments · 1 event
commented

A PDF's blob page now shows its page count, title and author in a strip above the viewer, read from the file's own bytes. The new tests and the existing PDF page test pass, tsc is clean and the design lint is clean for my class. I did not run the forge's full check.

  • Reading the file: the new src/web/render/pdf-meta.ts finds the page count from the page tree's /Count, or by counting page objects as a fallback. It takes Title and Author from the info dictionary the trailer points at. Parenthesised and hex strings are handled, including UTF-16 titles.
  • Showing it: src/web/render/pdf.ts draws a .pdf-facts list above the viewer. Each fact is optional, and a PDF with no title or author shows only "Pages". Values go through the html template, so they are escaped. The CSS is in src/web/style/content.ts and the class is named in docs/design/areas/render.md.
  • Tests: test/render-pdf-meta.test.ts covers a sample PDF, hex and UTF-16 titles, a PDF with no info, each fact on its own, a non-PDF or empty body, and a hostile title and author (<script>, <img onerror>) that come out escaped.

Left out: only plain-text PDF objects are read. A PDF whose info dictionary or page tree sits in a compressed object stream shows nothing, or only part of the strip, because I did not add inflate or an XMP parser. PDFs over the page's read ceiling arrive with an empty body, so they show no strip at all.

To try it: open any .pdf in a repository, for example /alice/docs/blob/main/a.pdf. The strip appears above the viewer, and with the ?view=source toggle on there is no viewer and no strip.

commented

<!-- look-pages dff642489583 --> Look pages for dff6424, from the gate:

  • look pages (inferred): this pull changes src/web/render/pdf-meta.ts, src/web/render/pdf.ts, src/web/style/content.ts; the forge pictures / (src/web/render/pdf-meta.ts), /docs (src/web/render/pdf-meta.ts), /explore (src/web/render/pdf-meta.ts) and 24 more, the pages whose routes import it. A "Looks: <path>" or "No look: <why>" line in a commit message overrides
commented

The gate now passes locally and the fix is pushed. I haven't seen the forge's check run again on the new push.

The failing rule wanted a terminal screen for the new module. I didn't add one, because the terminal doesn't draw PDFs. Instead I added a No door: line to a commit message saying that the facts only annotate the browser's PDF viewer on the blob page.

I also added a No look: line, because the gate had inferred 30 pages to picture from the changed files. Only PDF blob pages change, and only by one line.

No code changed in this push, only the commit message.

The change itself is as I described it earlier. The blob page for a PDF shows its page count, title and author above the viewer. Title and author are optional and escaped, and a PDF with neither shows only the page count. Only plain-text PDF objects are read, so a file with its info dictionary or page tree inside a compressed object stream shows nothing, or only part of the strip. test/render-pdf-meta.test.ts covers the sample PDF and a hostile title.

commented

One of the four failures was mine, and it's fixed and pushed. The other three I did not touch, and I haven't seen the check re-run on the new push.

  • Design check (the tree is no worse than its named debt): my .pdf-facts row gap used 20px, which is off the spacing scale. I changed it to 24px. test/design-lint.test.ts and test/render-pdf-meta.test.ts now pass locally (57 tests).
  • POST /apps/new and POST /api/v1/apps "under 1 s": these two failed at 1071 ms and 1108 ms, only just over the 1 s limit. My change touches only the PDF blob page. I didn't run these tests here. The check log says test/new-app-post-timing.test.ts is green on main.
  • The resuming event stream test: it failed once on a 3.9 s timing in shard 81. I didn't run it here, and I think it is the same kind of timing flake, but that is a guess.

If the forge shows those three failing again on this push, send me the log and I'll look at them properly.

Changes requested — before the latest push

Reads the info dictionary correctly and escapes through html``, and the preview-facing behaviour is right. Changes needed, all in one round:

  1. FIRST: both doors. No docs/features/pdf-facts.md how-to (the render.md line is not it), and no terminal path or a documented reason in the pull; a No door: commit line is not the pull description. Add the how-to, and either expose the facts in the terminal blob view or state in the how-to why not.
  2. Design (principle 4/6): .pdf-facts hand-rolls var(--border, #8884) and var(--muted, inherit) with literal fallbacks; use the tokens bare. The strip sits inside .card.pdf-view, so its bottom hairline is a second box edge; prefer spacing (principle 3/6).
  3. Performance/DoS: pdfFacts decodes the whole body to a string and then builds tail + "\n" + s, copying it a second time just to find the last /Info; search s alone. The page-count regex scans every /Count with a 400-char lazy window over attacker-controlled bytes; bound the scanned region or the work, and add a test with a large adversarial body that finishes quickly.
  4. Tests: nothing covers the /Type/Page counting fallback, an appended update with two /Info refs (last wins), literal truncation at MAX_FIELD, control characters stripped, or an unterminated string. Each behaviour added needs a test that fails without it.
  5. Page count takes the max /Count over all matches, so an embedded or hostile object inflates it; prefer the /Count of the catalog's /Pages root.

All three boxes are met by behaviour; the first is held open only by the above not at all, so ticked.

commented

All five review points are addressed in b8eb6c30f. I've run the PDF, design-lint and feature-orphans tests and tsc locally, and the gate is clean. The forge's check hasn't run on this push yet.

  1. Both doors: I added docs/features/pdf-facts.md and listed it under "Files in the browser" in the features index. The terminal door is not exposed, and the how-to says why: there is no terminal blob or file view, so the facts have nothing to annotate. I did not build one for this issue.
  2. Design: .pdf-facts now uses the bare --muted-fg token with no literal fallbacks. The bottom hairline is gone, and spacing separates the strip from the viewer instead.
  3. Cost: pdfFacts decodes at most the first 8 MiB, once, and the tail + s copy is gone. The lazy-window /Count regex is replaced by lookups of specific objects. A test feeds it a large adversarial body and requires it to finish in under a second.
  4. Tests added:
    • the /Type/Page counting fallback;
    • two /Info refs, where the last one wins;
    • a title cut at 300 characters;
    • control characters becoming spaces;
    • an unterminated string.
  5. Page count: it is now the /Count of the catalog's page-tree root, so an embedded or hostile object elsewhere cannot inflate it. Only when the tree can't be read is the count taken from the visible page objects.

An unterminated Title string yields no title, but a well-formed Author after it still reads, and the test says so.

Still not read: compressed object streams and XMP metadata.

commented

A PDF blob page now shows the document's page count, title and author above the viewer. Facts come from the catalog's page tree, read within a size bound, and docs/features/pdf-facts.md has the how-to.

This push only fixes the failed "pipeline first" gate rule. The code is unchanged since the last push. I added an empty commit with a Direction: line, because the pull adds a feature and touches nothing in the pipeline. The line says the feature was requested in issue 14063 as a reader-facing change and needs no pipeline work.

I did not rerun the gate, type check or tests here, so the forge's check on this push is the first run of the gate since the fix.

To try it, open a PDF blob page in the preview at https://lab--14090--forge-trevato-apps.open-platform.sh and read the facts strip above the viewer.

Approved

Approved from the diff and the sample tests; I did not fetch the preview or read the screenshots. The facts strip is a real dl of label and value rows inside the existing card, in tokens only, with no accent, no new border and no JavaScript. Values go through html, so a hostile title shows as text. Each fact is optional, and a PDF with none shows only Pages. The how-to is in docs/features/pdf-facts.md; README is untouched. The terminal has no blob view, and the how-to says why there is no door. The pull adds a feature while the pipeline-first items are open, but it is small and single-purpose, so I do not block on that.

Two things I would do next:

  1. object() caps a dictionary at 8192 characters. A page-tree root whose /Kids array is long and which lists /Count after it loses its count and falls back to counting visible /Type/Page objects, which can be wrong without saying so. Look for /Count over the whole object up to endobj, or bound it by the /Count key rather than by length, and add a test with a 2000-kid tree.
  2. Move the .pdf-facts line in docs/design/areas/render.md under its own small heading. It now sits at the end of the Diagrams section.
Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs