Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | |
3 * Copyright (C) 2006-2016 Eyeo GmbH | |
4 * | |
5 * Adblock Plus is free software: you can redistribute it and/or modify | |
6 * it under the terms of the GNU General Public License version 3 as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * Adblock Plus is distributed in the hope that it will be useful, | |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 * GNU General Public License for more details. | |
13 * | |
14 * You should have received a copy of the GNU General Public License | |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | |
16 */ | |
17 | |
18 "use strict"; | |
19 | |
20 let fs = require("fs"); | |
21 let path = require("path"); | |
22 let SandboxedModule = require("sandboxed-module"); | |
23 | |
24 let globals = { | |
25 atob: data => new Buffer(data, "base64").toString("binary"), | |
26 btoa: data => new Buffer(data, "binary").toString("base64"), | |
27 Ci: { | |
28 }, | |
29 Cu: { | |
30 import: () => | |
31 { | |
32 } | |
33 }, | |
34 navigator: { | |
35 }, | |
36 onShutdown: { | |
37 add: () => | |
38 { | |
39 } | |
40 }, | |
41 Services: { | |
42 obs: { | |
43 addObserver: () => | |
44 { | |
45 } | |
46 } | |
47 }, | |
48 XPCOMUtils: { | |
49 generateQI: () => | |
50 { | |
51 } | |
52 } | |
53 }; | |
54 | |
55 let knownModules = new Map(); | |
56 for (let dir of [path.join(__dirname, "stub-modules"), | |
57 path.join(__dirname, "..", "lib")]) | |
58 for (let file of fs.readdirSync(path.resolve(dir))) | |
59 if (path.extname(file) == ".js") | |
60 knownModules[path.basename(file, ".js")] = path.resolve(dir, file); | |
61 | |
62 function addExports(exports) | |
63 { | |
64 return function(source) | |
65 { | |
66 let extraExports = exports[path.basename(this.filename, ".js")]; | |
67 if (extraExports) | |
68 for (let name of extraExports) | |
Wladimir Palant
2016/10/04 11:02:57
Wrong indentation here, single space instead of tw
kzar
2016/10/04 12:06:22
Whoops, Done.
| |
69 source += ` | |
70 Object.defineProperty(exports, "${name}", {get: () => ${name}});`; | |
71 return source; | |
72 }; | |
73 } | |
74 | |
75 function rewriteRequires(source) | |
76 { | |
77 function escapeString(str) | |
78 { | |
79 return str.replace(/(["'\\])/g, "\\$1"); | |
80 } | |
81 | |
82 return source.replace(/(\brequire\(["'])([^"']+)/g, (match, prefix, request) = > | |
83 { | |
84 if (request in knownModules) | |
85 return prefix + escapeString(knownModules[request]); | |
86 return match; | |
87 }); | |
88 } | |
89 | |
90 exports.createSandbox = function(extraExports) | |
91 { | |
92 let sourceTransformers = [rewriteRequires]; | |
93 if (extraExports) | |
94 sourceTransformers.push(addExports(extraExports)); | |
95 | |
96 // This module loads itself into a sandbox, keeping track of the require | |
97 // function which can be used to load further modules into the sandbox. | |
98 // (Before returning the require function we wrap it, allowing paths for known | |
99 // modules to be omitted for convenience.) | |
Wladimir Palant
2016/10/04 11:02:57
I don't consider this a good idea. As I mentioned
kzar
2016/10/04 12:06:22
Done.
| |
100 let sandboxedRequire = SandboxedModule.require("./common", { | |
101 globals: globals, | |
102 sourceTransformers: sourceTransformers | |
103 }).require; | |
104 return module => sandboxedRequire(knownModules[module] || module); | |
105 }; | |
106 | |
107 exports.require = require; | |
OLD | NEW |