Skip to content

Commit

Permalink
Support parsing encrypted documents in XRef.indexObjects (issue 15893)
Browse files Browse the repository at this point in the history
*Please note:* The reduced test-case is *not* a perfect reproduction of the original PDF document, since this one fails to open in e.g. Adobe Reader, but I do believe that it captures the most important points here.

For corrupt *and* encrypted PDF documents, it's possible that only some trailer dictionaries actually contain an /Encrypt-entry. Previously we'd could easily miss that, since we generally pick the first not obviously corrupt trailer dictionary, and the solution implemented here is to simply pre-parse all trailer dictionaries to see if there's any /Encrypt-entries.
  • Loading branch information
Snuffleupagus authored and pull[bot] committed May 20, 2023
1 parent c05ef97 commit 1778484
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
38 changes: 27 additions & 11 deletions src/core/xref.js
Original file line number Diff line number Diff line change
Expand Up @@ -581,16 +581,11 @@ class XRef {
this.startXRefQueue.push(xrefStm);
this.readXRef(/* recoveryMode */ true);
}
// finding main trailer
let trailerDict, trailerError;
for (const trailer of [...trailers, "generationFallback", ...trailers]) {
if (trailer === "generationFallback") {
if (!trailerError) {
break; // No need to fallback if there were no validation errors.
}
this._generationFallback = true;
continue;
}

const trailerDicts = [];
// Pre-parsing the trailers to check if the document is possibly encrypted.
let isEncrypted = false;
for (const trailer of trailers) {
stream.pos = trailer;
const parser = new Parser({
lexer: new Lexer(stream),
Expand All @@ -607,6 +602,23 @@ class XRef {
if (!(dict instanceof Dict)) {
continue;
}
trailerDicts.push(dict);

if (dict.has("Encrypt")) {
isEncrypted = true;
}
}

// finding main trailer
let trailerDict, trailerError;
for (const dict of [...trailerDicts, "genFallback", ...trailerDicts]) {
if (dict === "genFallback") {
if (!trailerError) {
break; // No need to fallback if there were no validation errors.
}
this._generationFallback = true;
continue;
}
// Do some basic validation of the trailer/root dictionary candidate.
let validPagesDict = false;
try {
Expand All @@ -628,7 +640,11 @@ class XRef {
continue;
}
// taking the first one with 'ID'
if (validPagesDict && dict.has("ID")) {
if (
validPagesDict &&
(!isEncrypted || dict.has("Encrypt")) &&
dict.has("ID")
) {
return dict;
}
// The current dictionary is a candidate, but continue searching.
Expand Down
1 change: 1 addition & 0 deletions test/pdfs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@
!issue2128r.pdf
!bug1703683_page2_reduced.pdf
!issue5540.pdf
!issue15893_reduced.pdf
!issue5549.pdf
!visibility_expressions.pdf
!issue5475.pdf
Expand Down
Binary file added test/pdfs/issue15893_reduced.pdf
Binary file not shown.
7 changes: 7 additions & 0 deletions test/test_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,13 @@
"lastPage": 4,
"type": "eq"
},
{ "id": "issue15893_reduced",
"file": "pdfs/issue15893_reduced.pdf",
"md5": "cf889b927f9f53d164622a99378bf39d",
"rounds": 1,
"type": "eq",
"password": "test"
},
{ "id": "bug1727053",
"file": "pdfs/bug1727053.pdf",
"md5": "8ed1e52da64000f9fdcb8b732f5a58f8",
Expand Down

0 comments on commit 1778484

Please sign in to comment.