Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion docs/SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |
Expand Down
15 changes: 15 additions & 0 deletions scripts/gen-fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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)

Expand Down
33 changes: 20 additions & 13 deletions web/src/core/pdf/policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions web/tests/fixtures/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
Expand Down
Binary file added web/tests/fixtures/pdf/restricted-rc4-40.pdf
Binary file not shown.
Binary file added web/tests/fixtures/pdf/restricted-rc4-noaccess.pdf
Binary file not shown.
12 changes: 12 additions & 0 deletions web/tests/pdf-policy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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);
});
});
32 changes: 27 additions & 5 deletions web/tests/pdf.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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']) {
Expand Down
Loading