fix(docx): read single-quoted protection hash salt spin sid (#376)

Some producers emit single-quoted XML attributes. edit/enforcement already handled both quote styles, but hash/salt/spin/sid and recommended only matched double quotes, dropping credentials and weakening protection verification.

Co-authored-by: Aniruddha Adak <aniruddhaadak80@users.noreply.github.com>
This commit is contained in:
ANIRUDDHA ADAK
2026-09-15 11:20:29 +08:00
committed by GitHub
co-authored by Aniruddha Adak
parent 692c929c8d
commit e35048ec71
2 changed files with 41 additions and 9 deletions
+17 -9
View File
@@ -128,10 +128,14 @@ export async function parseProtection(zip: JSZip): Promise<DocProtection | null>
if (!edit || edit === 'none') return null
const enforcementMatch = /w:enforcement=(?:"([^"]+)"|'([^']+)')/.exec(tag)
const enforcement = enforcementMatch?.[1] ?? enforcementMatch?.[2]
const hash = /w:hash="([^"]+)"/.exec(tag)?.[1]
const salt = /w:salt="([^"]+)"/.exec(tag)?.[1]
const spin = /w:cryptSpinCount="(\d+)"/.exec(tag)?.[1]
const sid = /w:cryptAlgorithmSid="(\d+)"/.exec(tag)?.[1]
const hashMatch = /w:hash=(?:"([^"]+)"|'([^']+)')/.exec(tag)
const hash = hashMatch?.[1] ?? hashMatch?.[2]
const saltMatch = /w:salt=(?:"([^"]+)"|'([^']+)')/.exec(tag)
const salt = saltMatch?.[1] ?? saltMatch?.[2]
const spinMatch = /w:cryptSpinCount=(?:"(\d+)"|'(\d+)')/.exec(tag)
const spin = spinMatch?.[1] ?? spinMatch?.[2]
const sidMatch = /w:cryptAlgorithmSid=(?:"(\d+)"|'(\d+)')/.exec(tag)
const sid = sidMatch?.[1] ?? sidMatch?.[2]
return {
edit,
enforced: enforcement === '1' || enforcement === 'true' || enforcement === 'on',
@@ -148,11 +152,15 @@ export async function parseWriteProtection(zip: JSZip): Promise<WriteProtection
if (!file) return null
const tag = /<w:writeProtection\b[^>]*?(?:\/>|>)/.exec(await file.async('string'))?.[0]
if (!tag) return null
const recommended = /w:recommended="(?:1|true|on)"/.test(tag)
const hash = /w:hash="([^"]+)"/.exec(tag)?.[1]
const salt = /w:salt="([^"]+)"/.exec(tag)?.[1]
const spin = /w:cryptSpinCount="(\d+)"/.exec(tag)?.[1]
const sid = /w:cryptAlgorithmSid="(\d+)"/.exec(tag)?.[1]
const recommended = /w:recommended=(?:"(?:1|true|on)"|'(?:1|true|on)')/.test(tag)
const hashMatch = /w:hash=(?:"([^"]+)"|'([^']+)')/.exec(tag)
const hash = hashMatch?.[1] ?? hashMatch?.[2]
const saltMatch = /w:salt=(?:"([^"]+)"|'([^']+)')/.exec(tag)
const salt = saltMatch?.[1] ?? saltMatch?.[2]
const spinMatch = /w:cryptSpinCount=(?:"(\d+)"|'(\d+)')/.exec(tag)
const spin = spinMatch?.[1] ?? spinMatch?.[2]
const sidMatch = /w:cryptAlgorithmSid=(?:"(\d+)"|'(\d+)')/.exec(tag)
const sid = sidMatch?.[1] ?? sidMatch?.[2]
if (!recommended && !hash) return null
return {
...(recommended ? { recommended } : {}),
@@ -385,4 +385,28 @@ describe('protection tag forms', () => {
)
expect(await parseProtection(mixed)).toMatchObject({ edit: 'comments', enforced: true })
})
it('reads single-quoted protection credentials (hash/salt/spin/sid)', async () => {
const zip = await settingsZip(
"<w:documentProtection w:edit='readOnly' w:enforcement='1' w:hash='abc=' w:salt='def=' w:cryptSpinCount='100000' w:cryptAlgorithmSid='14'/>",
)
expect(await parseProtection(zip)).toMatchObject({
edit: 'readOnly',
enforced: true,
hash: 'abc=',
salt: 'def=',
spinCount: 100000,
algorithmSid: 14,
})
const write = await settingsZip(
"<w:writeProtection w:recommended='1' w:hash='h=' w:salt='s=' w:cryptSpinCount='50000' w:cryptAlgorithmSid='14'/>",
)
expect(await parseWriteProtection(write)).toMatchObject({
recommended: true,
hash: 'h=',
salt: 's=',
spinCount: 50000,
algorithmSid: 14,
})
})
})