|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import JSZip from 'jszip' |
| 5 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 6 | + |
| 7 | +const { mockParseOfficeAsync, mockExtractRawText } = vi.hoisted(() => ({ |
| 8 | + mockParseOfficeAsync: vi.fn(), |
| 9 | + mockExtractRawText: vi.fn(), |
| 10 | +})) |
| 11 | + |
| 12 | +vi.mock('officeparser', () => ({ parseOfficeAsync: mockParseOfficeAsync })) |
| 13 | +vi.mock('mammoth', () => ({ |
| 14 | + default: { extractRawText: mockExtractRawText }, |
| 15 | + extractRawText: mockExtractRawText, |
| 16 | +})) |
| 17 | + |
| 18 | +import { DocParser } from '@/lib/file-parsers/doc-parser' |
| 19 | + |
| 20 | +const CENTRAL_DIRECTORY_HEADER_SIGNATURE = 0x02014b50 |
| 21 | + |
| 22 | +/** |
| 23 | + * Build a small OOXML-shaped archive whose central directory *declares* a huge |
| 24 | + * uncompressed size. The guard reads declared sizes without inflating anything, |
| 25 | + * so this reproduces a zip bomb's central directory at a few hundred bytes. |
| 26 | + */ |
| 27 | +async function buildDeclaredOversizeArchive(declaredUncompressedBytes: number): Promise<Buffer> { |
| 28 | + const zip = new JSZip() |
| 29 | + zip.file('word/document.xml', '<w:document><w:body>A</w:body></w:document>') |
| 30 | + const buffer = await zip.generateAsync({ type: 'nodebuffer', compression: 'DEFLATE' }) |
| 31 | + |
| 32 | + for (let offset = 0; offset + 28 <= buffer.length; offset++) { |
| 33 | + if (buffer.readUInt32LE(offset) === CENTRAL_DIRECTORY_HEADER_SIGNATURE) { |
| 34 | + buffer.writeUInt32LE(declaredUncompressedBytes, offset + 24) |
| 35 | + return buffer |
| 36 | + } |
| 37 | + } |
| 38 | + throw new Error('No central directory header found in generated archive') |
| 39 | +} |
| 40 | + |
| 41 | +/** A legacy OLE compound-file `.doc` — not a ZIP, so the guard must no-op. */ |
| 42 | +function buildLegacyOleDoc(): Buffer { |
| 43 | + const buffer = Buffer.alloc(512) |
| 44 | + Buffer.from([0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1]).copy(buffer, 0) |
| 45 | + return buffer |
| 46 | +} |
| 47 | + |
| 48 | +describe('DocParser.parseBuffer', () => { |
| 49 | + beforeEach(() => { |
| 50 | + vi.clearAllMocks() |
| 51 | + }) |
| 52 | + |
| 53 | + it('rejects a ZIP-shaped .doc whose declared expanded size exceeds the cap', async () => { |
| 54 | + const bomb = await buildDeclaredOversizeArchive(2 * 1024 * 1024 * 1024) |
| 55 | + |
| 56 | + await expect(new DocParser().parseBuffer(bomb)).rejects.toThrow(/exceeds the maximum allowed/) |
| 57 | + }) |
| 58 | + |
| 59 | + it('rejects the bomb before either decompression library sees the buffer', async () => { |
| 60 | + const bomb = await buildDeclaredOversizeArchive(2 * 1024 * 1024 * 1024) |
| 61 | + |
| 62 | + await expect(new DocParser().parseBuffer(bomb)).rejects.toThrow() |
| 63 | + expect(mockParseOfficeAsync).not.toHaveBeenCalled() |
| 64 | + expect(mockExtractRawText).not.toHaveBeenCalled() |
| 65 | + }) |
| 66 | + |
| 67 | + it('rejects a ZIP-shaped .doc whose central directory cannot be parsed', async () => { |
| 68 | + const buffer = Buffer.alloc(64) |
| 69 | + buffer.writeUInt32LE(0x04034b50, 0) |
| 70 | + |
| 71 | + await expect(new DocParser().parseBuffer(buffer)).rejects.toThrow( |
| 72 | + /refusing to parse an unverifiable ZIP-shaped archive/ |
| 73 | + ) |
| 74 | + expect(mockParseOfficeAsync).not.toHaveBeenCalled() |
| 75 | + }) |
| 76 | + |
| 77 | + it('still parses a well-formed OOXML archive renamed to .doc', async () => { |
| 78 | + const zip = new JSZip() |
| 79 | + zip.file('word/document.xml', '<w:document><w:body>hello</w:body></w:document>') |
| 80 | + const buffer = await zip.generateAsync({ type: 'nodebuffer', compression: 'DEFLATE' }) |
| 81 | + mockParseOfficeAsync.mockResolvedValue('hello') |
| 82 | + |
| 83 | + const result = await new DocParser().parseBuffer(buffer) |
| 84 | + |
| 85 | + expect(result.content).toBe('hello') |
| 86 | + expect(result.metadata.extractionMethod).toBe('officeparser') |
| 87 | + }) |
| 88 | + |
| 89 | + it('no-ops the guard for a legacy OLE .doc and parses it', async () => { |
| 90 | + mockParseOfficeAsync.mockResolvedValue('legacy doc text') |
| 91 | + |
| 92 | + const result = await new DocParser().parseBuffer(buildLegacyOleDoc()) |
| 93 | + |
| 94 | + expect(mockParseOfficeAsync).toHaveBeenCalledOnce() |
| 95 | + expect(result.content).toBe('legacy doc text') |
| 96 | + }) |
| 97 | + |
| 98 | + it('rejects an empty buffer', async () => { |
| 99 | + await expect(new DocParser().parseBuffer(Buffer.alloc(0))).rejects.toThrow('Empty buffer') |
| 100 | + }) |
| 101 | +}) |
0 commit comments