Merge pull request #14600 from Snuffleupagus/getPageIndex-more-validation
[api-minor] Add validation for the `PDFDocumentProxy.getPageIndex` method
This commit is contained in:
commit
cf7ce0aa7e
@ -454,8 +454,8 @@ class WorkerMessageHandler {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
handler.on("GetPageIndex", function wphSetupGetPageIndex({ ref }) {
|
handler.on("GetPageIndex", function wphSetupGetPageIndex(data) {
|
||||||
const pageRef = Ref.get(ref.num, ref.gen);
|
const pageRef = Ref.get(data.num, data.gen);
|
||||||
return pdfManager.ensureCatalog("getPageIndex", [pageRef]);
|
return pdfManager.ensureCatalog("getPageIndex", [pageRef]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -2848,7 +2848,7 @@ class WorkerTransport {
|
|||||||
pageNumber <= 0 ||
|
pageNumber <= 0 ||
|
||||||
pageNumber > this._numPages
|
pageNumber > this._numPages
|
||||||
) {
|
) {
|
||||||
return Promise.reject(new Error("Invalid page request"));
|
return Promise.reject(new Error("Invalid page request."));
|
||||||
}
|
}
|
||||||
|
|
||||||
const pageIndex = pageNumber - 1,
|
const pageIndex = pageNumber - 1,
|
||||||
@ -2879,8 +2879,19 @@ class WorkerTransport {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getPageIndex(ref) {
|
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", {
|
return this.messageHandler.sendWithPromise("GetPageIndex", {
|
||||||
ref,
|
num: ref.num,
|
||||||
|
gen: ref.gen,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -815,40 +815,23 @@ describe("api", function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("gets non-existent page", async function () {
|
it("gets non-existent page", async function () {
|
||||||
let outOfRangePromise = pdfDocument.getPage(100);
|
const pageNumbers = [
|
||||||
let nonIntegerPromise = pdfDocument.getPage(2.5);
|
/* outOfRange = */ 100,
|
||||||
let nonNumberPromise = pdfDocument.getPage("1");
|
/* nonInteger = */ 2.5,
|
||||||
|
/* nonNumber = */ "1",
|
||||||
|
];
|
||||||
|
|
||||||
outOfRangePromise = outOfRangePromise.then(
|
for (const pageNumber of pageNumbers) {
|
||||||
function () {
|
try {
|
||||||
throw new Error("shall fail for out-of-range pageNumber parameter");
|
await pdfDocument.getPage(pageNumber);
|
||||||
},
|
|
||||||
function (reason) {
|
|
||||||
expect(reason instanceof Error).toEqual(true);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
nonIntegerPromise = nonIntegerPromise.then(
|
|
||||||
function () {
|
|
||||||
throw new Error("shall fail for non-integer pageNumber parameter");
|
|
||||||
},
|
|
||||||
function (reason) {
|
|
||||||
expect(reason instanceof Error).toEqual(true);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
nonNumberPromise = nonNumberPromise.then(
|
|
||||||
function () {
|
|
||||||
throw new Error("shall fail for non-number pageNumber parameter");
|
|
||||||
},
|
|
||||||
function (reason) {
|
|
||||||
expect(reason instanceof Error).toEqual(true);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
await Promise.all([
|
// Shouldn't get here.
|
||||||
outOfRangePromise,
|
expect(false).toEqual(true);
|
||||||
nonIntegerPromise,
|
} catch (reason) {
|
||||||
nonNumberPromise,
|
expect(reason instanceof Error).toEqual(true);
|
||||||
]);
|
expect(reason.message).toEqual("Invalid page request.");
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it("gets page, from /Pages tree with circular reference", async function () {
|
it("gets page, from /Pages tree with circular reference", async function () {
|
||||||
@ -907,18 +890,35 @@ describe("api", function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("gets invalid page index", async 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 {
|
const expectedErrors = [
|
||||||
await pdfDocument.getPageIndex(ref);
|
{
|
||||||
|
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.
|
for (let i = 0, ii = pageRefs.length; i < ii; i++) {
|
||||||
expect(false).toEqual(true);
|
try {
|
||||||
} catch (reason) {
|
await pdfDocument.getPageIndex(pageRefs[i]);
|
||||||
expect(reason instanceof UnknownErrorException).toEqual(true);
|
|
||||||
expect(reason.message).toEqual(
|
// Shouldn't get here.
|
||||||
"The reference does not point to a /Page dictionary."
|
expect(false).toEqual(true);
|
||||||
);
|
} catch (reason) {
|
||||||
|
const { exception, message } = expectedErrors[i];
|
||||||
|
|
||||||
|
expect(reason instanceof exception).toEqual(true);
|
||||||
|
expect(reason.message).toEqual(message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user