fune/toolkit/components/pdfjs/test/browser_pdfjs_js.js
Calixte Denizet 582df65115 Bug 1667973 - Add a sandbox to evaluate js embedded in PDF r=bdahl,dveditz,robwu
PDF files can embed some js code in order to validate, format, ... the data entered by a user in a PDF form.
So in order to safely execute this js code, we create a Cu.Sandbox with only what we need/want to expose to the scripts.

Differential Revision: https://phabricator.services.mozilla.com/D91746
2021-02-19 16:14:38 +00:00

51 lines
1.7 KiB
JavaScript

/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
const RELATIVE_DIR = "toolkit/components/pdfjs/test/";
const TESTROOT = "http://example.com/browser/" + RELATIVE_DIR;
// Test js in pdf file.
add_task(async function test_js_sandbox() {
await BrowserTestUtils.withNewTab(
{ gBrowser, url: "about:blank" },
async function(browser) {
await SpecialPowers.pushPrefEnv({
set: [["pdfjs.enableScripting", true]],
});
await Promise.all([
waitForPdfJSAnnotationLayer(browser, TESTROOT + "file_pdfjs_js.pdf"),
waitForPdfJSSandbox(browser),
]);
await SpecialPowers.spawn(browser, [], async () => {
const { PdfSandbox } = ChromeUtils.import(
"resource://pdf.js/PdfSandbox.jsm"
);
let sandboxDestroyCount = 0;
const originalDestroy = PdfSandbox.prototype.destroy;
PdfSandbox.prototype.destroy = function() {
const obj = this.sandbox.eval("({})");
originalDestroy.apply(this, arguments);
sandboxDestroyCount++;
ok(Cu.isDeadWrapper(obj), "Sandbox must have been nuked");
};
const document = content.document;
const button = document.querySelector(
"[data-annotation-id='16R'] :first-child"
);
button.dispatchEvent(new content.Event("click"));
const text = document.querySelector("#\\31 5R");
is(text.value, "test", "Text field must containt 'test' string");
content.addEventListener("unload", () => {
is(sandboxDestroyCount, 1, "Sandbox must have been destroyed");
});
});
}
);
});