From c5a6e95dc2ee9d4a8610ad471111a39a9c00a69d Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 7 Oct 2026 06:29:58 +0000 Subject: [PATCH] Fix PDF unlock failing for RC4 files that deny accessibility Removing only the open password re-encrypts with AES-256 and then verifies that every denied permission is still denied. R2/R3 (RC4) files can deny the accessibility permission; PDF 2.0 deprecates that bit and qpdf neither writes nor reports it for R >= 4. The verification therefore always failed and the user saw "file is corrupt or could not be read". The policy now ignores the accessibility permission when deciding whether a file has restrictions and when verifying the re-encrypted copy. All other restrictions are still preserved and verified. Adds R3 (accessibility denied) and R2 fixtures with regression tests. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01DBSwp8dMkpVxCSR4ArHAU5 --- README.md | 2 ++ docs/SECURITY.md | 2 +- scripts/gen-fixtures.py | 15 ++++++++ web/src/core/pdf/policy.ts | 33 +++++++++++------- web/tests/fixtures/README.md | 2 ++ web/tests/fixtures/pdf/restricted-rc4-40.pdf | Bin 0 -> 1004 bytes .../fixtures/pdf/restricted-rc4-noaccess.pdf | Bin 0 -> 1007 bytes web/tests/pdf-policy.test.ts | 12 +++++++ web/tests/pdf.test.ts | 32 ++++++++++++++--- 9 files changed, 79 insertions(+), 19 deletions(-) create mode 100644 web/tests/fixtures/pdf/restricted-rc4-40.pdf create mode 100644 web/tests/fixtures/pdf/restricted-rc4-noaccess.pdf diff --git a/README.md b/README.md index 44deb4a..31dde0d 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,8 @@ PDFs have two passwords: the open (user) password and the permissions (owner) pa | owner password | any | fully decrypted | | – | owner password only, no open password | refused: lifting restrictions requires the owner password | +One permission is not carried over: "content access for accessibility" (screen readers). PDF 2.0 deprecates it and requires readers to ignore it, so AES-256 cannot express it. Only old RC4 files (R2/R3) can deny it. unpassword treats it as granted. All other restrictions are kept. + ## Guardrails unpassword is built so that it is useless for attacking files you have no password for: diff --git a/docs/SECURITY.md b/docs/SECURITY.md index c32ef79..1345638 100644 --- a/docs/SECURITY.md +++ b/docs/SECURITY.md @@ -38,7 +38,7 @@ | One password per call, no candidate lists | `Unlocker` type in `src/core/types.ts`; zip.js is used through its core API with a single `password` | | Rate limit: 2 free failures, then exponential delay from 5 s, lock after 10 | `src/core/guard.ts`, applied in `src/core/session.ts` inside the worker | | Wrong ZipCrypto passwords that pass the 1-byte check are still rejected | CRC verification, test with 600 wrong passwords in `tests/zip.test.ts` | -| PDF restrictions preserved without the owner password | `src/core/pdf/policy.ts` (pure, table-tested), with verification after re-encryption | +| PDF restrictions preserved without the owner password | `src/core/pdf/policy.ts` (pure, table-tested), with verification after re-encryption. Exception: the accessibility permission, which PDF 2.0 deprecates and readers must ignore; AES-256 cannot carry it | | No owner-password recovery, no "remove restrictions only" mode | policy refuses `owner-password-required` | | No hash or verifier export | guardrail test | | Unsupported protection is refused rather than worked around | DRM / certificate handlers → `unsupported` | diff --git a/scripts/gen-fixtures.py b/scripts/gen-fixtures.py index fac2543..cc313b1 100644 --- a/scripts/gen-fixtures.py +++ b/scripts/gen-fixtures.py @@ -69,6 +69,10 @@ def save_pdf(name: str, **enc): print_highres=True, ) RESTRICTED = pikepdf.Permissions(extract=False, print_highres=False, modify_other=False, modify_assembly=False) +# as Acrobat writes it with "text access for screen readers" unchecked +RESTRICTED_NO_ACCESS = pikepdf.Permissions( + accessibility=False, extract=False, print_highres=False, modify_other=False, modify_assembly=False +) def gen_pdf(): @@ -80,6 +84,17 @@ def gen_pdf(): save_pdf("restricted-aes256.pdf", user=USER_PW, owner=OWNER_PW, R=6, allow=RESTRICTED) save_pdf("restricted-aes128.pdf", user=USER_PW, owner=OWNER_PW, R=4, aes=True, allow=RESTRICTED) save_pdf("restricted-rc4.pdf", user=USER_PW, owner=OWNER_PW, R=3, aes=False, metadata=False, allow=RESTRICTED) + # accessibility denied: only R2/R3 can express it, AES-256 cannot + save_pdf( + "restricted-rc4-noaccess.pdf", + user=USER_PW, + owner=OWNER_PW, + R=3, + aes=False, + metadata=False, + allow=RESTRICTED_NO_ACCESS, + ) + save_pdf("restricted-rc4-40.pdf", user=USER_PW, owner=OWNER_PW, R=2, aes=False, metadata=False, allow=RESTRICTED) # no open password, only owner restrictions save_pdf("owner-only.pdf", user="", owner=OWNER_PW, R=6, allow=RESTRICTED) diff --git a/web/src/core/pdf/policy.ts b/web/src/core/pdf/policy.ts index 33bd562..40545b2 100644 --- a/web/src/core/pdf/policy.ts +++ b/web/src/core/pdf/policy.ts @@ -32,23 +32,30 @@ export type PdfAction = | { kind: 'preserve-restrictions'; capabilities: PdfCapabilities } | { kind: 'refuse'; reason: 'not-encrypted' | 'wrong-password' | 'owner-password-required' }; +/** + * Permissions that can still be enforced. `accessibility` is left out: PDF 2.0 + * deprecates the bit and requires readers to ignore it, so qpdf neither writes + * nor reports it for R ≥ 4 and an AES-256 copy cannot carry it. Only R2/R3 + * files can deny it, and for R2 it merely mirrors `extract`, which is kept. + */ +const ENFORCEABLE: readonly (keyof PdfCapabilities)[] = [ + 'extract', + 'modify', + 'modifyannotations', + 'modifyassembly', + 'modifyforms', + 'modifyother', + 'printhigh', + 'printlow', +]; + export function hasRestrictions(c: PdfCapabilities): boolean { - return !( - c.accessibility && - c.extract && - c.modify && - c.modifyannotations && - c.modifyassembly && - c.modifyforms && - c.modifyother && - c.printhigh && - c.printlow - ); + return ENFORCEABLE.some((k) => !c[k]); } -/** True if every permission denied in `original` is still denied in `result`. */ +/** True if every enforceable permission denied in `original` is still denied in `result`. */ export function restrictionsPreserved(original: PdfCapabilities, result: PdfCapabilities): boolean { - return (Object.keys(original) as (keyof PdfCapabilities)[]).every((k) => original[k] || !result[k]); + return ENFORCEABLE.every((k) => original[k] || !result[k]); } /** diff --git a/web/tests/fixtures/README.md b/web/tests/fixtures/README.md index 6360f08..6f68759 100644 --- a/web/tests/fixtures/README.md +++ b/web/tests/fixtures/README.md @@ -10,6 +10,8 @@ All passwords are test values and intentionally public. | `pdf/restricted-aes256.pdf` | user `user-Pässwort1`, owner `owner-Pässwort2` | AES-256 (R6); no extract, low-res print only, no assembly, no other modification | | `pdf/restricted-aes128.pdf` | same | AES-128 (R4), same restrictions | | `pdf/restricted-rc4.pdf` | same | RC4-128 (R3), same restrictions | +| `pdf/restricted-rc4-noaccess.pdf` | same | RC4-128 (R3), same restrictions plus accessibility denied | +| `pdf/restricted-rc4-40.pdf` | same | RC4-40 (R2), same restrictions; R2 ties accessibility to extract | | `pdf/owner-only.pdf` | owner `owner-Pässwort2` (empty user password) | opens without password, restrictions only | | `office/plain.docx` | – | minimal unencrypted DOCX | | `office/agile.docx` | `Büro-Geheim 42` | ECMA-376 Agile (msoffcrypto-tool) | diff --git a/web/tests/fixtures/pdf/restricted-rc4-40.pdf b/web/tests/fixtures/pdf/restricted-rc4-40.pdf new file mode 100644 index 0000000000000000000000000000000000000000..56180325a7007b8a9f44b6a36b1b9d766e0d7e34 GIT binary patch literal 1004 zcmah|y=xRf6i3iz3mZYP5FWynap(I3&MT5jFsS6XM4KQoyE_|CHoIYWB8eb~SO`H# zq5psg7WOuRDQ(5d%Gyo@Q5(gX&0X$@g|oZt@SFGE?|uBHGw3hP;Y&2?9Q=IoC&GZB zO72G8F2n<~VH=FwPX!2K0K7Ewf-qW*pW=uCUOREK}MjMN-XlDz!?4 zF;rrbD~xmH(LC+Vga`~1&V_2bbbukgI?5VY2iCIRn(!Z{G-VIRQ`gej(lx+apZL1X zMrOX+@llX}2s-?HtbwV$;iR&mZK~}$^`(ZQmnwhKZgBgXH;rwv_=+v=SvN`z#H-th zYx@zp;*&jWPHWZ+m4$Z z^66oJ?@`Io~8NyMPQ!YfRctW@m8L`v|VT2__ zWt3xVRU(Z<);}`KQ$s|8W#$_&fH|Td9s+^*26T}z2r)yM)v3)TmxfEhL~z5oPBN5m zL205;nhTN|mI_6*G>CgKSs`*C@upYCboe{9%_9UdLPrY(=afSZM;P`U4-3jECcJEp zVVHX5AIG%U%`uD$`47glzXTIISDR7cM;RovT;`Q8zP&%gstUSp_p77(7NkG#!+Muu zhPfuf=dHBVlu*S45sXqriPXIJe+w_^#WJmTw;Y5K4?~X{XKL4WNN62(I*Thy(O(?P B6OjM_ literal 0 HcmV?d00001 diff --git a/web/tests/fixtures/pdf/restricted-rc4-noaccess.pdf b/web/tests/fixtures/pdf/restricted-rc4-noaccess.pdf new file mode 100644 index 0000000000000000000000000000000000000000..c0396633f71e4d1b10f19814e6039cab7fb8c6ac GIT binary patch literal 1007 zcmah|J8u&~5JsZAlA2;0R}gOBdn;Kk5+{HliscxZfM6fv;K(D;wcZ{d{mux&sIq%myN&Xm z*@X#W5$U6RaIg<3Uo*jst6kLTWKjBKNS7Q2qc8^4ww^dPq)G{Efk=ftm)?4>og)|u zqo}u?r4Wf>!MRpV%YX>VZ;ku}?I7N;-(T>bq;$rfj?Y}n0A-jU(fH(>;74Y)n#CwM zz7(JQF;>si(Qr{&A12kbcCpk{^hOm=nhj}|RWpGG%WuH=351a|DBqsi(CCp=iOHUb z)0lNqW$BxPDdVMDQ*MidZ$r5o9wEUa>u4OHj_cS2=z^q7m^t7z+y~EB+(jM4V(~; z6*1gvg1IiZBQfS(M3>~8)iBI3QW-vdkQ9OmG(@KuiK3?mm1mfYlXDIuEDry9OvlkV zhcT)C!MKk1U||<(GaAQkrYX8v4y%}PbA|d { }); }); + it('user password, only the deprecated accessibility permission denied → decrypt', () => { + expect( + decidePdfAction(info({ userPasswordMatched: true, capabilities: { ...ALL, accessibility: false } }), false), + ).toEqual({ kind: 'decrypt' }); + }); + it('owner password → decrypt, whatever the restrictions', () => { expect(decidePdfAction(info({ ownerPasswordMatched: true, capabilities: RESTRICTED }), false)).toEqual({ kind: 'decrypt', @@ -75,4 +81,10 @@ describe('restrictionsPreserved', () => { expect(restrictionsPreserved(RESTRICTED, { ...RESTRICTED, extract: true })).toBe(false); expect(restrictionsPreserved(RESTRICTED, { ...RESTRICTED, printlow: false })).toBe(true); }); + + it('ignores the deprecated accessibility permission', () => { + const noAccess = { ...RESTRICTED, accessibility: false }; + expect(restrictionsPreserved(noAccess, RESTRICTED)).toBe(true); + expect(restrictionsPreserved(noAccess, { ...RESTRICTED, extract: true })).toBe(false); + }); }); diff --git a/web/tests/pdf.test.ts b/web/tests/pdf.test.ts index d969749..173d6ef 100644 --- a/web/tests/pdf.test.ts +++ b/web/tests/pdf.test.ts @@ -1,5 +1,6 @@ import { beforeAll, describe, expect, it } from 'vitest'; import { unlockPdf } from '../src/core/pdf/index'; +import type { PdfCapabilities } from '../src/core/pdf/policy'; import { inspectEncryption } from '../src/core/pdf/qpdf'; import { UnpasswordError } from '../src/core/types'; import { PW, fixture, useNodeQpdf } from './helpers'; @@ -41,15 +42,36 @@ describe('unlockPdf', () => { }, ); - it.each(['restricted-aes256.pdf', 'restricted-aes128.pdf', 'restricted-rc4.pdf', 'owner-only.pdf'])( - '%s: owner password fully decrypts', + it.each(['restricted-rc4-noaccess.pdf', 'restricted-rc4-40.pdf'])( + '%s: a denied accessibility permission does not block removing the open password', async (file) => { - const r = await unlockPdf(fixture(`pdf/${file}`), PW.pdfOwner); - expect(r.mode).toBe('decrypted'); - expect((await inspectEncryption(r.data, ''))?.encrypted).toBe(false); + // PDF 2.0 deprecates the accessibility bit; the AES-256 copy cannot carry it + const original = await inspectEncryption(fixture(`pdf/${file}`), PW.pdfUser); + expect(original?.capabilities.accessibility).toBe(false); + const r = await unlockPdf(fixture(`pdf/${file}`), PW.pdfUser); + expect(r.mode).toBe('open-password-removed'); + const after = await inspectEncryption(r.data, ''); + expect(after).toMatchObject({ encrypted: true, userPasswordMatched: true, ownerPasswordMatched: false }); + for (const [k, allowed] of Object.entries(original!.capabilities)) { + if (k !== 'accessibility' && !allowed) expect(after?.capabilities[k as keyof PdfCapabilities]).toBe(false); + } + expect(after?.capabilities.extract).toBe(false); }, ); + it.each([ + 'restricted-aes256.pdf', + 'restricted-aes128.pdf', + 'restricted-rc4.pdf', + 'restricted-rc4-noaccess.pdf', + 'restricted-rc4-40.pdf', + 'owner-only.pdf', + ])('%s: owner password fully decrypts', async (file) => { + const r = await unlockPdf(fixture(`pdf/${file}`), PW.pdfOwner); + expect(r.mode).toBe('decrypted'); + expect((await inspectEncryption(r.data, ''))?.encrypted).toBe(false); + }); + it('accepts the password regardless of how the producer encoded it', async () => { // R3/R4 fixtures store PDFDocEncoding bytes, R6 stores UTF-8 for (const f of ['restricted-rc4.pdf', 'restricted-aes128.pdf', 'restricted-aes256.pdf']) {