-
Notifications
You must be signed in to change notification settings - Fork 10.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JS -- Add a sandbox based on quickjs
* quickjs-eval.js has been generated using /~https://github.com/calixteman/pdf.js.quickjs/ * lazy load of sandbox code
- Loading branch information
1 parent
83658c9
commit 9bbd5fc
Showing
9 changed files
with
344 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* Copyright 2020 Mozilla Foundation | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import ModuleLoader from "../../external/quickjs/quickjs-eval.js"; | ||
|
||
class Sandbox { | ||
constructor(module) { | ||
this._evalInSandbox = module.cwrap("evalInSandbox", null, ["string"]); | ||
this._dispatchEventName = null; | ||
this.module = module; | ||
} | ||
|
||
create(data) { | ||
const sandboxData = JSON.stringify(data); | ||
const extra = [ | ||
"send", | ||
"setTimeout", | ||
"clearTimeout", | ||
"setInterval", | ||
"clearInterval", | ||
"crackURL", | ||
]; | ||
const code = [ | ||
"exports = Object.create(null);", | ||
"module = Object.create(null);", | ||
`data = ${sandboxData};`, | ||
// Next line is replaced by code from initialization.js | ||
// when we create the bundle for the sandbox.x | ||
"/* INITIALIZATION_CODE */", | ||
`module.exports.initSandbox(data, {${extra.join(",")}}, this);`, | ||
"delete exports;", | ||
"delete module;", | ||
"delete data;", | ||
...extra.map(name => `delete ${name};`), | ||
]; | ||
this._evalInSandbox(code.join("\n")); | ||
this._dispatchEventName = data.dispatchEventName; | ||
this.dumpMemoryUse(); | ||
} | ||
|
||
dispatchEvent(event) { | ||
if (this._dispatchEventName === null) { | ||
throw new Error("Sandbox must have been initialized"); | ||
} | ||
event = JSON.stringify(event); | ||
this._evalInSandbox(`app['${this._dispatchEventName}'](${event});`); | ||
} | ||
|
||
dumpMemoryUse() { | ||
this.module.ccall("dumpMemoryUse", null, []); | ||
} | ||
|
||
nukeSandbox() { | ||
this._dispatchEventName = null; | ||
this.module.ccall("nukeSandbox", null, []); | ||
this.module = null; | ||
this._evalInSandbox = null; | ||
} | ||
} | ||
|
||
function QuickJSSandbox() { | ||
return ModuleLoader().then(module => { | ||
return new Sandbox(module); | ||
}); | ||
} | ||
|
||
export { QuickJSSandbox }; |
Oops, something went wrong.