Skip to content

Open-source document signature platform. Not affiliated with DocuSign.

TypeScript

Refuse a PDF with more than 1,000 pages

Claudef3716928e256
diff --git a/docs/development.md b/docs/development.md
index 6e6e43a..dc952a9 100644
--- a/docs/development.md
+++ b/docs/development.md
@@ -115,7 +115,7 @@ To test a production build on your machine, set `APP_URL=http://localhost:3000`
Document import currently enforces these limits:
- PDF files only
-- 20 MB maximum upload size
+- 20 MB maximum upload size, and at most 1,000 pages
- password-protected or encrypted PDFs are rejected
- pure XFA or unsupported PDF variants are rejected
diff --git a/src/lib/services/document-import.test.ts b/src/lib/services/document-import.test.ts
new file mode 100644
index 0000000..01585a7
--- /dev/null
+++ b/src/lib/services/document-import.test.ts
@@ -0,0 +1,37 @@
+import assert from "node:assert/strict";
+import { test } from "node:test";
+
+import { PDFDocument } from "pdf-lib";
+
+import { MAX_PDF_PAGES } from "../types/domain.ts";
+import { DocumentImportError, importPdfDocument } from "./document-import.ts";
+
+async function pdfWithPages(count: number): Promise<Uint8Array> {
+ const document = await PDFDocument.create();
+
+ for (let page = 0; page < count; page += 1) {
+ document.addPage([612, 792]);
+ }
+
+ return document.save();
+}
+
+test("a PDF with more pages than the limit is refused, one at the limit is taken", async () => {
+ await assert.rejects(
+ importPdfDocument({
+ body: await pdfWithPages(MAX_PDF_PAGES + 1),
+ contentType: "application/pdf",
+ filename: "many.pdf",
+ }),
+ (error: unknown) =>
+ error instanceof DocumentImportError && error.code === "too_many_pages",
+ );
+
+ const imported = await importPdfDocument({
+ body: await pdfWithPages(MAX_PDF_PAGES),
+ contentType: "application/pdf",
+ filename: "long.pdf",
+ });
+
+ assert.equal(imported.pageCount, MAX_PDF_PAGES);
+});
diff --git a/src/lib/services/document-import.ts b/src/lib/services/document-import.ts
index 821953b..ec9b1ea 100644
--- a/src/lib/services/document-import.ts
+++ b/src/lib/services/document-import.ts
@@ -3,7 +3,7 @@ import { createHash } from "node:crypto";
import { PDFDocument } from "pdf-lib";
import type { EnvelopePageRecord } from "../schemas/envelope.ts";
-import { MAX_UPLOAD_SIZE_BYTES } from "../types/domain.ts";
+import { MAX_PDF_PAGES, MAX_UPLOAD_SIZE_BYTES } from "../types/domain.ts";
import { getDisplayedPageGeometry } from "./pdf-page-geometry.ts";
const PDF_MAGIC_HEADER = "%PDF-";
@@ -13,6 +13,7 @@ const PDF_IMPORT_ERROR_MESSAGES = {
"This PDF is password-protected or encrypted. Please upload an unlocked PDF.",
invalid_type: "Only PDF files are supported. Please upload a valid PDF.",
too_large: "This file exceeds the 20 MB limit.",
+ too_many_pages: `This PDF has more than ${String(MAX_PDF_PAGES)} pages. Split it into smaller documents.`,
unreadable_pdf: "We couldn't read this PDF. Please upload a valid file.",
unsupported_variant:
"This PDF uses an unsupported format. Please export it as a standard PDF.",
@@ -23,6 +24,7 @@ export const pdfImportFailureCodes = [
"encrypted_pdf",
"invalid_type",
"too_large",
+ "too_many_pages",
"unreadable_pdf",
"unsupported_variant",
"upload_failed",
@@ -151,6 +153,10 @@ async function parsePdfDocument(
ignoreEncryption: false,
});
+ if (document.getPageCount() > MAX_PDF_PAGES) {
+ throw new DocumentImportError("too_many_pages");
+ }
+
// The size of each page as a reader shows it: the CropBox, with width and
// height swapped on a page rotated a quarter turn. The field overlay is
// laid out to this size, so it has to match what the browser draws.
diff --git a/src/lib/types/domain.ts b/src/lib/types/domain.ts
index 1403641..670a859 100644
--- a/src/lib/types/domain.ts
+++ b/src/lib/types/domain.ts
@@ -10,6 +10,9 @@ export const DEFAULT_MAX_SIGNERS_PER_ENVELOPE = 25;
/** People who get a copy of the finished document without signing. */
export const MAX_COPY_RECIPIENTS_PER_ENVELOPE = 10;
export const MAX_UPLOAD_SIZE_BYTES = 20 * 1024 * 1024;
+// A PDF can pack a great many pages into a small file. Each page is a row
+// and is drawn again when the envelope is finished, so there is a limit.
+export const MAX_PDF_PAGES = 1000;
/**
* The largest request body the app reads in full: a PDF at the upload limit
* plus room for the rest of the multipart form (the title and the part