1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-19 14:48:08 +02:00

[api-minor] Add validation for the PDFDocumentProxy.getPageIndex method

Currently we'll happily attempt to send any argument passed to this method over to the worker-thread, without doing any sort of validation.
That could obviously be quite bad, since there's first of all no protection against sending unclonable data. Secondly, it's also possible to pass data that will cause the `Ref.get` call in the worker-thread to fail immediately.

In order to address all of these issues, we'll now properly validate the argument passed to `PDFDocumentProxy.getPageIndex` and when necessary reject already on the main-thread instead.
This commit is contained in:
Jonas Jenwald 2022-02-24 12:01:51 +01:00
parent 2be8036eb7
commit 172d007598
3 changed files with 41 additions and 13 deletions

View file

@ -454,8 +454,8 @@ class WorkerMessageHandler {
});
});
handler.on("GetPageIndex", function wphSetupGetPageIndex({ ref }) {
const pageRef = Ref.get(ref.num, ref.gen);
handler.on("GetPageIndex", function wphSetupGetPageIndex(data) {
const pageRef = Ref.get(data.num, data.gen);
return pdfManager.ensureCatalog("getPageIndex", [pageRef]);
});

View file

@ -2879,8 +2879,19 @@ class WorkerTransport {
}
getPageIndex(ref) {
if (
typeof ref !== "object" ||
ref === null ||
!Number.isInteger(ref.num) ||
ref.num < 0 ||
!Number.isInteger(ref.gen) ||
ref.gen < 0
) {
return Promise.reject(new Error("Invalid pageIndex request."));
}
return this.messageHandler.sendWithPromise("GetPageIndex", {
ref,
num: ref.num,
gen: ref.gen,
});
}

View file

@ -890,18 +890,35 @@ describe("api", function () {
});
it("gets invalid page index", async function () {
const ref = { num: 3, gen: 0 }; // Reference to a font dictionary.
const pageRefs = [
/* fontRef = */ { num: 3, gen: 0 },
/* invalidRef = */ { num: -1, gen: 0 },
/* nonRef = */ "qwerty",
/* nullRef = */ null,
];
try {
await pdfDocument.getPageIndex(ref);
const expectedErrors = [
{
exception: UnknownErrorException,
message: "The reference does not point to a /Page dictionary.",
},
{ exception: Error, message: "Invalid pageIndex request." },
{ exception: Error, message: "Invalid pageIndex request." },
{ exception: Error, message: "Invalid pageIndex request." },
];
// Shouldn't get here.
expect(false).toEqual(true);
} catch (reason) {
expect(reason instanceof UnknownErrorException).toEqual(true);
expect(reason.message).toEqual(
"The reference does not point to a /Page dictionary."
);
for (let i = 0, ii = pageRefs.length; i < ii; i++) {
try {
await pdfDocument.getPageIndex(pageRefs[i]);
// Shouldn't get here.
expect(false).toEqual(true);
} catch (reason) {
const { exception, message } = expectedErrors[i];
expect(reason instanceof exception).toEqual(true);
expect(reason.message).toEqual(message);
}
}
});