-
Notifications
You must be signed in to change notification settings - Fork 3.8k
improvement(file-parsers): bound PDF text extraction #6425
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+233
−10
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import { deflateSync } from 'zlib' | ||
| import { describe, expect, it } from 'vitest' | ||
| import { MAX_PDF_TEXT_CHARS, PdfParser } from '@/lib/file-parsers/pdf-parser' | ||
|
|
||
| /** | ||
| * Builds a single-page PDF that draws 64 characters per repeat from a | ||
| * FlateDecode content stream, so a few dozen kilobytes of input yields millions | ||
| * of extracted characters — what made the unbounded extractor exhaust the heap | ||
| * and abort the process. | ||
| * | ||
| * Hand-assembled rather than built with `pdf-lib` because the fixture's whole | ||
| * point is the compression ratio of the content stream, which `pdf-lib` gives | ||
| * no way to control. | ||
| */ | ||
| function buildTextBombPdf(repeats: number): Buffer { | ||
| const unit = `BT /F1 12 Tf 10 700 Td (${'A'.repeat(64)}) Tj ET\n` | ||
| const compressed = deflateSync(Buffer.from(unit.repeat(repeats))) | ||
|
|
||
| const objects = [ | ||
| Buffer.from('<< /Type /Catalog /Pages 2 0 R >>'), | ||
| Buffer.from('<< /Type /Pages /Kids [3 0 R] /Count 1 >>'), | ||
| Buffer.from( | ||
| '<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Contents 4 0 R /Resources << /Font << /F1 5 0 R >> >> >>' | ||
| ), | ||
| Buffer.concat([ | ||
| Buffer.from(`<< /Length ${compressed.length} /Filter /FlateDecode >>\nstream\n`), | ||
| compressed, | ||
| Buffer.from('\nendstream'), | ||
| ]), | ||
| Buffer.from('<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>'), | ||
| ] | ||
|
|
||
| const chunks: Buffer[] = [Buffer.from('%PDF-1.4\n')] | ||
| const offsets: number[] = [] | ||
| let offset = chunks[0].length | ||
|
|
||
| objects.forEach((object, index) => { | ||
| offsets.push(offset) | ||
| const chunk = Buffer.concat([ | ||
| Buffer.from(`${index + 1} 0 obj\n`), | ||
| object, | ||
| Buffer.from('\nendobj\n'), | ||
| ]) | ||
| chunks.push(chunk) | ||
| offset += chunk.length | ||
| }) | ||
|
|
||
| const xrefRows = offsets | ||
| .map((value) => `${value.toString().padStart(10, '0')} 00000 n \n`) | ||
| .join('') | ||
| chunks.push( | ||
| Buffer.from( | ||
| `xref\n0 ${objects.length + 1}\n0000000000 65535 f \n${xrefRows}` + | ||
| `trailer\n<< /Size ${objects.length + 1} /Root 1 0 R >>\nstartxref\n${offset}\n%%EOF\n` | ||
| ) | ||
| ) | ||
|
|
||
| return Buffer.concat(chunks) | ||
| } | ||
|
|
||
| describe('PdfParser', () => { | ||
| it('bounds extracted text from a compression-bomb PDF instead of exhausting the heap', async () => { | ||
| const bomb = buildTextBombPdf(200_000) | ||
| expect(bomb.length).toBeLessThan(200 * 1024) | ||
|
|
||
| const result = await new PdfParser().parseBuffer(bomb) | ||
|
|
||
| expect(result.metadata?.truncated).toBe(true) | ||
| expect(result.metadata?.warning).toMatch(/parser limit/i) | ||
| expect(result.content.length).toBeLessThanOrEqual(MAX_PDF_TEXT_CHARS) | ||
| }, 120_000) | ||
|
|
||
| it('extracts a small PDF in full and does not flag it as truncated', async () => { | ||
| const result = await new PdfParser().parseBuffer(buildTextBombPdf(3)) | ||
|
|
||
| expect(result.metadata?.truncated).toBe(false) | ||
| expect(result.metadata?.warning).toBeUndefined() | ||
| expect(result.metadata?.pageCount).toBe(1) | ||
| expect(result.content).toContain('AAAA') | ||
| }, 30_000) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.