Skip to content

Commit

Permalink
fix: 沙盒兼容问题&某些情况下sourceUrl不显示的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
CodFrm committed Nov 10, 2021
1 parent 106de02 commit 85cb130
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 26 deletions.
8 changes: 3 additions & 5 deletions src/apps/script/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -587,8 +587,7 @@ export class ScriptManager extends Manager {
return;
}
let cache = await this.controller.buildScriptCache(script);
cache.code = dealScript(chrome.runtime.getURL('/' + cache.name + '.user.js#uuid=' + cache.uuid), `window['${cache.flag}']=function(context){\n` +
cache.code + `\n}`);
cache.code = dealScript(`window['${cache.flag}']=function(context){\n` + cache.code + `\n}`);
script.metadata['match']?.forEach(val => {
this.match.add(val, cache);
});
Expand All @@ -603,8 +602,7 @@ export class ScriptManager extends Manager {
this.scriptList({ type: SCRIPT_TYPE_NORMAL }).then(items => {
items.forEach(async script => {
let cache = await this.controller.buildScriptCache(script);
cache.code = dealScript(chrome.runtime.getURL('/' + cache.name + '.user.js#uuid=' + cache.uuid), `window['${cache.flag}']=function(context){\n` +
cache.code + `\n}`);
cache.code = dealScript(`window['${cache.flag}']=function(context){\n` + cache.code + `\n}`);
script.metadata['match']?.forEach(val => {
this.match.add(val, cache);
});
Expand All @@ -618,7 +616,7 @@ export class ScriptManager extends Manager {
});
let injectedSource = '';
get(chrome.runtime.getURL('src/injected.js'), (source: string) => {
injectedSource = dealScript(chrome.runtime.getURL('src/injected.js'), `(function (ScriptFlag) {\n${source}\n})('${scriptFlag}')`);
injectedSource = dealScript(`(function (ScriptFlag) {\n${source}\n})('${scriptFlag}')`);
});
chrome.runtime.onMessage.addListener((msg, detail, send) => {
if (msg !== 'runScript') {
Expand Down
4 changes: 3 additions & 1 deletion src/pkg/sandbox/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ export function compileScriptCode(script: ScriptCache): string {
}
});
code = require + code;
return 'with (context) return (()=>{\n' + code + '\n})()'
return 'with (context) return ((context, fapply, CDATA, uneval, define, module, exports)=>{\n' +
code + '\n//# sourceURL=' + chrome.runtime.getURL('/' + encodeURI(script.name) + '.user.js') +
'\n})(context)'
}

export function compileScript(script: ScriptCache): Function {
Expand Down
31 changes: 13 additions & 18 deletions src/pkg/sandbox/sandbox.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { SandboxContext, ScriptContext } from "@App/apps/grant/frontend";
import { ScriptCache, Script } from "@App/model/do/script";

export function buildWindow(): any {
return {
localStorage: global.localStorage,
}
}

let sandboxGlobal: any = {
let writables: any = {
"addEventListener": global.addEventListener,
"removeEventListener": global.removeEventListener,
"dispatchEvent": global.dispatchEvent,
Expand All @@ -20,32 +18,29 @@ export let init = new Map<string, boolean>();
let descs = Object.getOwnPropertyDescriptors(global);
for (const key in descs) {
let desc = descs[key];
if (desc && desc.writable && !sandboxGlobal[key]) {
sandboxGlobal[key] = desc.value;
if (desc && desc.writable && !writables[key]) {
writables[key] = desc.value;
} else {
init.set(key, true);
try {
sandboxGlobal[key] = (<any>global)[key];
} catch (e) {
}
}
}


// 处理有多层结构的(先只对特殊的做处理)
['console'].forEach(obj => {
let descs = Object.getOwnPropertyDescriptors((<any>global)[obj]);
sandboxGlobal[obj] = {};// 清零
writables[obj] = {};// 清零
for (const key in descs) {
let desc = descs[key];
if (desc && desc.writable) {
sandboxGlobal[obj][key] = desc.value;
writables[obj][key] = desc.value;
}
}
});

//TODO:做一些恶意操作拦截等
export function buildThis(global: any, context: any) {
let special = Object.assign({}, sandboxGlobal);
let special = Object.assign({}, writables);
// 后台脚本要不要考虑不能使用eval?
let _this: any = { eval: global.eval };
let proxy: any = new Proxy(context, {
Expand All @@ -57,7 +52,7 @@ export function buildThis(global: any, context: any) {
case 'window':
case 'global':
case 'globalThis':
return _this[name] || proxy;
return special[name] || proxy;
}
if (name !== 'undefined' && name !== Symbol.unscopables) {
if (context[name]) {
Expand All @@ -72,8 +67,9 @@ export function buildThis(global: any, context: any) {
}
return special[name];
}
if (init.has(<string>name)) {
if (global[name] !== undefined) {
if (typeof global[name] === 'function' && !global[name].prototype) {
console.log('b', name, global[name]);
return global[name].bind(global);
}
return global[name];
Expand All @@ -82,15 +78,14 @@ export function buildThis(global: any, context: any) {
return undefined;
},
has(_, name) {
// 全返回true,走get里面,如果返回false,不会进入get,会跑出沙盒取变量
return true;
},
set(_, name: string, val) {
switch (name) {
case 'window':
case 'global':
case 'globalThis':
_this[name] = val;
special[name] = val;
return true;
}
if (special[name]) {
Expand All @@ -114,9 +109,9 @@ export function buildThis(global: any, context: any) {
if (ret) {
return ret;
}
ret = Object.getOwnPropertyDescriptor(sandboxGlobal, name);
ret = Object.getOwnPropertyDescriptor(global, name);
return ret;
}
});
return proxy;
}
}
3 changes: 1 addition & 2 deletions src/pkg/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ export function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

export function dealScript(url: string, source: string): string {
source = "//# sourceURL=" + url + "\n" + source;
export function dealScript( source: string): string {
return dealSymbol(source);
}

Expand Down

0 comments on commit 85cb130

Please sign in to comment.