1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-22 16:18:08 +02:00

In order to simplify m-c code, move some in pdf.js

* move set/clear|Timeout/Interval and crackURL code in pdf.js
 * remove the "backdoor" in the proxy (used to dispatch event) and so return the dispatch function in the initializer
 * remove listeners if an error occured during sandbox initialization
 * add support for alert and prompt in the sandbox
 * add a function to eval in the global scope
This commit is contained in:
Calixte Denizet 2020-12-04 00:39:50 +01:00
parent 3447f7c703
commit 8bff4f1ea9
14 changed files with 472 additions and 265 deletions

View file

@ -19,7 +19,6 @@ import {
AutoPrintRegExp,
DEFAULT_SCALE_VALUE,
EventBus,
generateRandomStringForSandbox,
getActiveOrFocusedElement,
getPDFFileNameFromURL,
isValidRotation,
@ -1483,10 +1482,6 @@ const PDFViewerApplication = {
const { id, command, value } = detail;
if (!id) {
switch (command) {
case "alert":
// eslint-disable-next-line no-alert
window.alert(value);
break;
case "clear":
console.clear();
break;
@ -1501,7 +1496,7 @@ const PDFViewerApplication = {
return;
case "print":
this.triggerPrinting();
return;
break;
case "println":
console.log(value);
break;
@ -1511,7 +1506,7 @@ const PDFViewerApplication = {
} else {
this.pdfViewer.currentScale = value;
}
return;
break;
}
return;
}
@ -1551,8 +1546,6 @@ const PDFViewerApplication = {
window.addEventListener("mouseup", mouseUp);
this._scriptingInstance.events.set("mouseup", mouseUp);
const dispatchEventName = generateRandomStringForSandbox(objects);
if (!this._contentLength) {
// Always waiting for the entire PDF document to be loaded will, most
// likely, delay sandbox-creation too much in the general case for all
@ -1569,24 +1562,28 @@ const PDFViewerApplication = {
const filename =
this._contentDispositionFilename || getPDFFileNameFromURL(this.url);
scripting.createSandbox({
objects,
dispatchEventName,
calculationOrder,
appInfo: {
platform: navigator.platform,
language: navigator.language,
},
docInfo: {
...this.documentInfo,
baseURL: this.baseUrl,
filesize: this._contentLength,
filename,
metadata: this.metadata,
numPages: pdfDocument.numPages,
URL: this.url,
},
});
try {
await scripting.createSandbox({
objects,
calculationOrder,
appInfo: {
platform: navigator.platform,
language: navigator.language,
},
docInfo: {
...this.documentInfo,
baseURL: this.baseUrl,
filesize: this._contentLength,
filename,
metadata: this.metadata,
numPages: pdfDocument.numPages,
URL: this.url,
},
});
} catch (error) {
console.error(error);
this._destroyScriptingInstance();
}
},
/**

View file

@ -256,15 +256,21 @@ class FirefoxComDataRangeTransport extends PDFDataRangeTransport {
class FirefoxScripting {
static createSandbox(data) {
FirefoxCom.requestSync("createSandbox", data);
return new Promise(resolve => {
FirefoxCom.request("createSandbox", data, resolve);
}).then(success => {
if (!success) {
throw new Error("Cannot start sandbox");
}
});
}
static dispatchEventInSandbox(event) {
FirefoxCom.requestSync("dispatchEventInSandbox", event);
static async dispatchEventInSandbox(event) {
FirefoxCom.request("dispatchEventInSandbox", event);
}
static destroySandbox() {
FirefoxCom.requestSync("destroySandbox", null);
static async destroySandbox() {
FirefoxCom.request("destroySandbox", null);
}
}

View file

@ -1015,42 +1015,6 @@ function getActiveOrFocusedElement() {
return curActiveOrFocused;
}
/**
* Generate a random string which is not define somewhere in actions.
*
* @param {Object} objects - The value returned by `getFieldObjects` in the API.
* @returns {string} A unique string.
*/
function generateRandomStringForSandbox(objects) {
const allObjects = Object.values(objects).flat(2);
const actions = allObjects
.filter(obj => !!obj.actions)
.map(obj => Object.values(obj.actions))
.flat(2);
while (true) {
const name = new Uint8Array(64);
if (typeof crypto !== "undefined") {
crypto.getRandomValues(name);
} else {
for (let i = 0, ii = name.length; i < ii; i++) {
name[i] = Math.floor(256 * Math.random());
}
}
const nameString =
"_" +
btoa(
Array.from(name)
.map(x => String.fromCharCode(x))
.join("")
);
if (actions.every(action => !action.includes(nameString))) {
return nameString;
}
}
}
export {
AutoPrintRegExp,
CSS_UNITS,
@ -1074,7 +1038,6 @@ export {
NullL10n,
EventBus,
ProgressBar,
generateRandomStringForSandbox,
getPDFFileNameFromURL,
noContextMenuHandler,
parseQueryString,