1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-26 19:47:20 +00:00

Change evalSandboxed to use only one context for all modules (#4624)

* Run modules in one new context

* Fix polyfill running on require

* Add code to check for global assignments

* Allow globals in separate context

This lets the caller request a separate context which may be polluted.

* Jasmine needs to pollute the global

* Formatting

* Add polyfill and cleanup code

* Convert to ES5

* One bug fix

* Some formatting

* Remove console.log line!

* Cleanup

* revert css-escape-polyfill.js to hopefully remove it from the PR

* Remove JSDOC directives
This commit is contained in:
Arlen22 2023-05-06 07:11:18 -04:00 committed by GitHub
parent 2bfefe3880
commit 2b95daf59b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 9 deletions

View File

@ -569,10 +569,32 @@ $tw.utils.getTypeEncoding = function(ext) {
return typeInfo ? typeInfo.encoding : "utf8"; return typeInfo ? typeInfo.encoding : "utf8";
}; };
var polyfill =[
"// this polyfills the globalThis variable",
"// using the this variable on a getter ",
"// inserted into the prototype of globalThis",
"(function() {",
" if (typeof globalThis === 'object') return;",
" // node.green says this is available since 0.10.48",
" Object.prototype.__defineGetter__('__temp__', function() {",
" return this;",
" });",
" __temp__.globalThis = __temp__;",
" delete Object.prototype.__temp__;",
"}());"
].join("\n");
var globalCheck =[
" if(Object.keys(globalThis).length){",
" console.log(Object.keys(globalThis));",
" throw \"Global assignment is not allowed within modules on node.\";",
" }"
].join('\n');
/* /*
Run code globally with specified context variables in scope Run code globally with specified context variables in scope
*/ */
$tw.utils.evalGlobal = function(code,context,filename) { $tw.utils.evalGlobal = function(code,context,filename,sandbox,allowGlobals) {
var contextCopy = $tw.utils.extend(Object.create(null),context); var contextCopy = $tw.utils.extend(Object.create(null),context);
// Get the context variables as a pair of arrays of names and values // Get the context variables as a pair of arrays of names and values
var contextNames = [], contextValues = []; var contextNames = [], contextValues = [];
@ -581,25 +603,39 @@ $tw.utils.evalGlobal = function(code,context,filename) {
contextValues.push(value); contextValues.push(value);
}); });
// Add the code prologue and epilogue // Add the code prologue and epilogue
code = "(function(" + contextNames.join(",") + ") {(function(){\n" + code + "\n;})();\nreturn exports;\n})\n"; code = [
(!$tw.browser ? polyfill : ""),
"(function(" + contextNames.join(",") + ") {",
" (function(){\n" + code + "\n;})();",
(!$tw.browser && sandbox && !allowGlobals) ? globalCheck : "",
" return exports;\n",
"})"
].join("\n");
// Compile the code into a function // Compile the code into a function
var fn; var fn;
if($tw.browser) { if($tw.browser) {
fn = window["eval"](code + "\n\n//# sourceURL=" + filename); fn = window["eval"](code + "\n\n//# sourceURL=" + filename);
} else {
if(sandbox){
fn = vm.runInContext(code,sandbox,filename)
} else { } else {
fn = vm.runInThisContext(code,filename); fn = vm.runInThisContext(code,filename);
} }
}
// Call the function and return the exports // Call the function and return the exports
return fn.apply(null,contextValues); return fn.apply(null,contextValues);
}; };
$tw.utils.sandbox = !$tw.browser ? vm.createContext({}) : undefined;
/* /*
Run code in a sandbox with only the specified context variables in scope Run code in a sandbox with only the specified context variables in scope
*/ */
$tw.utils.evalSandboxed = $tw.browser ? $tw.utils.evalGlobal : function(code,context,filename) { $tw.utils.evalSandboxed = $tw.browser ? $tw.utils.evalGlobal : function(code,context,filename,allowGlobals) {
var sandbox = $tw.utils.extend(Object.create(null),context); return $tw.utils.evalGlobal(
vm.runInNewContext(code,sandbox,filename); code,context,filename,
return sandbox.exports; allowGlobals ? vm.createContext({}) : $tw.utils.sandbox,
allowGlobals
);
}; };
/* /*

View File

@ -76,7 +76,7 @@ exports.runTests = function(callback,specFilter) {
} }
return $tw.modules.execute(moduleTitle,title); return $tw.modules.execute(moduleTitle,title);
}; };
var contextExports = $tw.utils.evalSandboxed(code,context,title); var contextExports = $tw.utils.evalSandboxed(code,context,title,true);
// jasmine/jasmine.js assigns directly to `module.exports`: check // jasmine/jasmine.js assigns directly to `module.exports`: check
// for it first. // for it first.
return context.module.exports || contextExports; return context.module.exports || contextExports;