pdf.sandbox.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* Copyright 2020 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. import ModuleLoader from "../external/quickjs/quickjs-eval.js";
  16. import { SandboxSupportBase } from "./pdf.sandbox.external.js";
  17. /* eslint-disable-next-line no-unused-vars */
  18. const pdfjsVersion = PDFJSDev.eval("BUNDLE_VERSION");
  19. /* eslint-disable-next-line no-unused-vars */
  20. const pdfjsBuild = PDFJSDev.eval("BUNDLE_BUILD");
  21. class SandboxSupport extends SandboxSupportBase {
  22. exportValueToSandbox(val) {
  23. // The communication with the Quickjs sandbox is based on strings
  24. // So we use JSON.stringfy to serialize
  25. return JSON.stringify(val);
  26. }
  27. importValueFromSandbox(val) {
  28. return val;
  29. }
  30. createErrorForSandbox(errorMessage) {
  31. return new Error(errorMessage);
  32. }
  33. }
  34. class Sandbox {
  35. constructor(win, module) {
  36. this.support = new SandboxSupport(win, this);
  37. // The "external" functions created in pdf.sandbox.external.js
  38. // are finally used here:
  39. // https://github.com/mozilla/pdf.js.quickjs/blob/main/src/myjs.js
  40. // They're called from the sandbox only.
  41. module.externalCall = this.support.createSandboxExternals();
  42. this._module = module;
  43. // 0 to display error using console.error
  44. // else display error using window.alert
  45. this._alertOnError = 0;
  46. }
  47. create(data) {
  48. if (PDFJSDev.test("!PRODUCTION || TESTING")) {
  49. this._module.ccall("nukeSandbox", null, []);
  50. }
  51. const code = [PDFJSDev.eval("PDF_SCRIPTING_JS_SOURCE")];
  52. if (PDFJSDev.test("!PRODUCTION || TESTING")) {
  53. code.push(
  54. `globalThis.sendResultForTesting = callExternalFunction.bind(null, "send");`
  55. );
  56. } else {
  57. code.push("delete dump;");
  58. }
  59. let success = false;
  60. let buf = 0;
  61. try {
  62. const sandboxData = JSON.stringify(data);
  63. // "pdfjsScripting.initSandbox..." MUST be the last line to be evaluated
  64. // since the returned value is used for the communication.
  65. code.push(`pdfjsScripting.initSandbox({ data: ${sandboxData} })`);
  66. buf = this._module.stringToNewUTF8(code.join("\n"));
  67. success = !!this._module.ccall(
  68. "init",
  69. "number",
  70. ["number", "number"],
  71. [buf, this._alertOnError]
  72. );
  73. } catch (error) {
  74. console.error(error);
  75. } finally {
  76. if (buf) {
  77. this._module.ccall("free", "number", ["number"], [buf]);
  78. }
  79. }
  80. if (success) {
  81. this.support.commFun = this._module.cwrap("commFun", null, [
  82. "string",
  83. "string",
  84. ]);
  85. } else {
  86. this.nukeSandbox();
  87. throw new Error("Cannot start sandbox");
  88. }
  89. }
  90. dispatchEvent(event) {
  91. this.support?.callSandboxFunction("dispatchEvent", event);
  92. }
  93. dumpMemoryUse() {
  94. if (this._module) {
  95. this._module.ccall("dumpMemoryUse", null, []);
  96. }
  97. }
  98. nukeSandbox() {
  99. if (this._module !== null) {
  100. this.support.destroy();
  101. this.support = null;
  102. this._module.ccall("nukeSandbox", null, []);
  103. this._module = null;
  104. }
  105. }
  106. evalForTesting(code, key) {
  107. if (PDFJSDev.test("!PRODUCTION || TESTING")) {
  108. this._module.ccall(
  109. "evalInSandbox",
  110. null,
  111. ["string", "int"],
  112. [
  113. `try {
  114. sendResultForTesting([{ id: "${key}", result: ${code} }]);
  115. } catch (error) {
  116. sendResultForTesting([{ id: "${key}", result: error.message }]);
  117. }`,
  118. this._alertOnError,
  119. ]
  120. );
  121. } else {
  122. throw new Error("Not implemented: evalForTesting");
  123. }
  124. }
  125. }
  126. function QuickJSSandbox() {
  127. return ModuleLoader().then(module => {
  128. return new Sandbox(window, module);
  129. });
  130. }
  131. export { QuickJSSandbox };