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 0000000..5618032 Binary files /dev/null and b/web/tests/fixtures/pdf/restricted-rc4-40.pdf differ 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 0000000..c039663 Binary files /dev/null and b/web/tests/fixtures/pdf/restricted-rc4-noaccess.pdf differ diff --git a/web/tests/pdf-policy.test.ts b/web/tests/pdf-policy.test.ts index 30395f6..a917cac 100644 --- a/web/tests/pdf-policy.test.ts +++ b/web/tests/pdf-policy.test.ts @@ -39,6 +39,12 @@ describe('decidePdfAction', () => { }); }); + 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']) {