mirror of
https://github.com/mozilla/pdf.js.git
synced 2026-12-01 16:46:07 +01:00
Fix script error reporting
Handle thrown null values and failures in string conversion or stack access. Report field errors through the document, since fields without widgets have no send callback. This prevents error reporting from skipping subsequent actions, field formatting, and OpenAction.
This commit is contained in:
committed by
Calixte Denizet
parent
756e3ccc88
commit
1e846788a8
@@ -21,9 +21,20 @@ const FORMS_VERSION = 21.00720099;
|
||||
const USERACTIVATION_CALLBACKID = 0;
|
||||
const USERACTIVATION_MAXTIME_VALIDITY = 5000;
|
||||
|
||||
function serializeError(error) {
|
||||
const value = `${error.toString()}\n${error.stack}`;
|
||||
return { command: "error", value };
|
||||
function serializeError(error, prefix = "") {
|
||||
// Scripts can throw non-Error values.
|
||||
let message;
|
||||
try {
|
||||
message = String(error);
|
||||
} catch {
|
||||
message = "Unknown error";
|
||||
}
|
||||
let stack;
|
||||
try {
|
||||
stack = error?.stack;
|
||||
} catch {}
|
||||
const value = typeof stack === "string" ? `${message}\n${stack}` : message;
|
||||
return { command: "error", value: `${prefix}${value}` };
|
||||
}
|
||||
|
||||
// Helpers for simple `Map.prototype.getOrInsertComputed()` invocations,
|
||||
|
||||
@@ -197,9 +197,9 @@ class Doc extends PDFObject {
|
||||
try {
|
||||
this._globalEval(action);
|
||||
} catch (error) {
|
||||
const serializedError = serializeError(error);
|
||||
serializedError.value = `Error when executing "${name}" for document\n${serializedError.value}`;
|
||||
this._send(serializedError);
|
||||
this._send(
|
||||
serializeError(error, `Error when executing "${name}" for document\n`)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -545,12 +545,16 @@ class Field extends PDFObject {
|
||||
const actions = this._actions.get(eventName);
|
||||
for (const action of actions) {
|
||||
try {
|
||||
// Action evaluation must happen in the global scope
|
||||
// Evaluate actions in the global scope.
|
||||
this._globalEval(action);
|
||||
} catch (error) {
|
||||
const serializedError = serializeError(error);
|
||||
serializedError.value = `Error when executing "${eventName}" for field "${this._id}"\n${serializedError.value}`;
|
||||
this._send(serializedError);
|
||||
// Fields without widgets have no send callback.
|
||||
this._document.obj._send(
|
||||
serializeError(
|
||||
error,
|
||||
`Error when executing "${eventName}" for field "${this._id}"\n`
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -540,6 +540,103 @@ describe("Scripting", function () {
|
||||
});
|
||||
});
|
||||
|
||||
it("should report null and run the next action", async () => {
|
||||
const refId = getId();
|
||||
sandbox.createSandbox({
|
||||
appInfo: { language: "en-US", platform: "Linux x86_64" },
|
||||
objects: {
|
||||
field: [
|
||||
{
|
||||
id: refId,
|
||||
value: "",
|
||||
actions: {
|
||||
custom: ["throw null;", "globalThis.nextAction = true;"],
|
||||
},
|
||||
type: "text",
|
||||
},
|
||||
],
|
||||
},
|
||||
calculationOrder: [],
|
||||
});
|
||||
|
||||
await sandbox.dispatchEventInSandbox({ id: refId, name: "custom" });
|
||||
expect(send_queue.get("error")).toEqual({
|
||||
command: "error",
|
||||
value: `Error when executing "custom" for field "${refId}"\nnull`,
|
||||
});
|
||||
send_queue.delete("error");
|
||||
expect(await myeval(`globalThis.nextAction`)).toEqual(true);
|
||||
});
|
||||
|
||||
it("should report errors when string conversion or stack access throws", async () => {
|
||||
const refId = getId();
|
||||
const cases = {
|
||||
throwingToString: ["({ toString() { throw 1; } })", "Unknown error"],
|
||||
nullPrototype: ["Object.create(null)", "Unknown error"],
|
||||
throwingStack: ["({ get stack() { throw 2; } })", "[object Object]"],
|
||||
};
|
||||
const actions = Object.create(null);
|
||||
for (const [name, [thrown]] of Object.entries(cases)) {
|
||||
actions[name] = [`throw ${thrown};`];
|
||||
}
|
||||
sandbox.createSandbox({
|
||||
appInfo: { language: "en-US", platform: "Linux x86_64" },
|
||||
objects: {
|
||||
field: [{ id: refId, value: "", actions, type: "text" }],
|
||||
},
|
||||
calculationOrder: [],
|
||||
});
|
||||
|
||||
for (const [name, [, expected]] of Object.entries(cases)) {
|
||||
send_queue.delete("error");
|
||||
await sandbox.dispatchEventInSandbox({ id: refId, name });
|
||||
expect(send_queue.get("error"))
|
||||
.withContext(name)
|
||||
.toEqual({
|
||||
command: "error",
|
||||
value: `Error when executing "${name}" for field "${refId}"\n${expected}`,
|
||||
});
|
||||
}
|
||||
send_queue.delete("error");
|
||||
});
|
||||
|
||||
it("should report errors from fields without widgets", async () => {
|
||||
const refIds = [getId(), getId()];
|
||||
sandbox.createSandbox({
|
||||
appInfo: { language: "en-US", platform: "Linux x86_64" },
|
||||
docInfo: { actions: { OpenAction: ["globalThis.openAction = true;"] } },
|
||||
objects: {
|
||||
A: [
|
||||
{
|
||||
id: refIds[0],
|
||||
type: "",
|
||||
kidIds: [refIds[1]],
|
||||
actions: { Format: [`throw new Error("Broken");`] },
|
||||
},
|
||||
],
|
||||
"A.B": [
|
||||
{
|
||||
id: refIds[1],
|
||||
value: "",
|
||||
actions: { Format: ["globalThis.kidFormat = true;"] },
|
||||
type: "text",
|
||||
},
|
||||
],
|
||||
},
|
||||
calculationOrder: [],
|
||||
});
|
||||
|
||||
await sandbox.dispatchEventInSandbox({ id: "doc", name: "Open" });
|
||||
expect(send_queue.get("error").value.split("\n").slice(0, 2)).toEqual([
|
||||
`Error when executing "Format" for field "${refIds[0]}"`,
|
||||
"Error: Broken",
|
||||
]);
|
||||
send_queue.delete("error");
|
||||
expect(
|
||||
await myeval(`[globalThis.kidFormat, globalThis.openAction]`)
|
||||
).toEqual([true, true]);
|
||||
});
|
||||
|
||||
it("should trigger an event added with setAction", async () => {
|
||||
const refId = getId();
|
||||
const data = {
|
||||
|
||||
Reference in New Issue
Block a user