Skip to content

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

TypeScript

Name a password-locked PDF as locked on upload

Claude64942d6fc2aa
diff --git a/src/components/envelopes/upload-shell.tsx b/src/components/envelopes/upload-shell.tsx
index 0f726ee..44deee3 100644
--- a/src/components/envelopes/upload-shell.tsx
+++ b/src/components/envelopes/upload-shell.tsx
@@ -23,6 +23,10 @@ import {
renderPdfThumbnail,
uploadEnvelopeThumbnail,
} from "@/lib/pdf-thumbnail";
+import {
+ describePdfPreviewFailure,
+ PREVIEW_UNAVAILABLE_MESSAGE,
+} from "@/lib/pdf-preview-error";
import { assertPdfPreviewRenderable } from "@/lib/pdfjs-client";
import { type CreateEnvelopeResponse } from "@/lib/schemas/envelope";
@@ -39,8 +43,7 @@ type UploadSummary = {
const errorMessageMap: Record<UploadErrorCode, string> = {
"invalid-type": "Only PDF files are supported. Please upload a valid PDF.",
- "preview-unavailable":
- "We couldn't render this PDF in the editor. Please export it as a standard PDF and try again.",
+ "preview-unavailable": PREVIEW_UNAVAILABLE_MESSAGE,
"too-large": "This file exceeds the 20 MB limit.",
};
@@ -503,7 +506,7 @@ export function UploadShell({ directUploadPrefix = null }: UploadShellProps) {
"Failed to validate the PDF preview before draft creation.",
error,
);
- setErrorMessage(errorMessageMap["preview-unavailable"]);
+ setErrorMessage(describePdfPreviewFailure(error));
setIsCreatingDraft(false);
return;
}
diff --git a/src/lib/pdf-preview-error.test.ts b/src/lib/pdf-preview-error.test.ts
new file mode 100644
index 0000000..cc85ee0
--- /dev/null
+++ b/src/lib/pdf-preview-error.test.ts
@@ -0,0 +1,60 @@
+import assert from "node:assert/strict";
+import test from "node:test";
+
+import { PDFDocument } from "pdf-lib";
+
+import {
+ describePdfPreviewFailure,
+ LOCKED_PDF_MESSAGE,
+ PREVIEW_UNAVAILABLE_MESSAGE,
+} from "./pdf-preview-error.ts";
+
+// A one-page PDF that needs the password "demo-user" to open (AES-256).
+const PASSWORD_PDF_BASE64 =
+ "JVBERi0xLjMKJeLjz9MKM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OTNmMjA3MzgzM2RlN2M5NDJlMWRjOWM4NDBmPgovUGVybXMgPDYxNGVlYjdiZmFhNDIxNWUzZDFlMWM5ZWY5MTgxMjAzPgo+PgplbmRvYmoKeHJlZgowIDYKMDAwMDAwMDAwMCA2NTUzNSBmIAowMDAwMDAwMDE1IDAwMDAwIG4gCjAwMDAwMDAxMTMgMDAwMDAgbiAKMDAwMDAwMDE3MiAwMDAwMCBuIAowMDAwMDAwMjIxIDAwMDAwIG4gCjAwMDAwMDAzMTUgMDAwMDAgbiAKdHJhaWxlcgo8PAovU2l6ZSA2Ci9Sb290IDMgMCBSCi9JbmZvIDEgMCBSCi9JRCBbIDwzNTYxMzEzMjYyMzc2NDM3MzgzNTYxMzY2NDM1MzUzNzM1MzYzOTYyNjIzNzMwNjQzMjM0MzIzMjYxMzczMDM5PiA8MzU2MTMxMzI2MjM3NjQzNzM4MzU2MTM2NjQzNTM1MzczNTM2Mzk2MjYyMzczMDY0MzIzNDMyMzI2MTM3MzAzOT4gXQovRW5jcnlwdCA1IDAgUgo+PgpzdGFydHhyZWYKODcwCiUlRU9GCg==";
+
+async function openWithPdfJs(bytes: Uint8Array): Promise<unknown> {
+ const pdfjs = await import("pdfjs-dist/legacy/build/pdf.mjs");
+ const task = pdfjs.getDocument({ data: bytes, verbosity: 0 });
+
+ try {
+ const document = await task.promise;
+
+ await document.destroy();
+
+ return null;
+ } catch (error) {
+ return error;
+ } finally {
+ await task.destroy();
+ }
+}
+
+test("a PDF that needs a password to open is named as locked (issue #54)", async () => {
+ const error = await openWithPdfJs(
+ new Uint8Array(Buffer.from(PASSWORD_PDF_BASE64, "base64")),
+ );
+
+ assert.ok(error, "PDF.js opened a password-protected PDF");
+ assert.equal(describePdfPreviewFailure(error), LOCKED_PDF_MESSAGE);
+});
+
+test("a file that is not a PDF still gets the render-failure message", async () => {
+ const error = await openWithPdfJs(new TextEncoder().encode("not a PDF\n"));
+
+ assert.ok(error);
+ assert.equal(describePdfPreviewFailure(error), PREVIEW_UNAVAILABLE_MESSAGE);
+});
+
+test("an ordinary PDF opens, and other failures are not called locked", async () => {
+ const document = await PDFDocument.create();
+
+ document.addPage([612, 792]);
+
+ assert.equal(await openWithPdfJs(await document.save()), null);
+ assert.equal(
+ describePdfPreviewFailure(new Error("canvas failed")),
+ PREVIEW_UNAVAILABLE_MESSAGE,
+ );
+ assert.equal(describePdfPreviewFailure(null), PREVIEW_UNAVAILABLE_MESSAGE);
+});
diff --git a/src/lib/pdf-preview-error.ts b/src/lib/pdf-preview-error.ts
new file mode 100644
index 0000000..3a84a13
--- /dev/null
+++ b/src/lib/pdf-preview-error.ts
@@ -0,0 +1,26 @@
+// Why a PDF could not be shown in the editor, in words a sender can act on.
+// Kept apart from the upload page so it can be tested without a browser.
+
+export const LOCKED_PDF_MESSAGE =
+ "This PDF is password-protected or encrypted. Please upload an unlocked PDF.";
+
+export const PREVIEW_UNAVAILABLE_MESSAGE =
+ "We couldn't render this PDF in the editor. Please export it as a standard PDF and try again.";
+
+/**
+ * PDF.js refuses a file that needs a password to open with a
+ * PasswordException before anything is uploaded. That is the same refusal
+ * the server gives an encrypted file, so it gets the same message, not the
+ * render failure every other preview error gets (issue #54).
+ */
+export function describePdfPreviewFailure(error: unknown): string {
+ if (
+ typeof error === "object" &&
+ error !== null &&
+ (error as { name?: unknown }).name === "PasswordException"
+ ) {
+ return LOCKED_PDF_MESSAGE;
+ }
+
+ return PREVIEW_UNAVAILABLE_MESSAGE;
+}