From 6d1e7f834785972301a8330ba292418e63036b53 Mon Sep 17 00:00:00 2001 From: fofolee Date: Thu, 21 Apr 2022 18:30:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B2=99=E7=AE=B1=E6=8D=A2=E6=88=90=20ses?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugin/lib/nodeFns.js | 2 -- plugin/package-lock.json | 13 ++++++++++++- plugin/package.json | 3 ++- plugin/preload.js | 39 +++++++++++++++------------------------ 4 files changed, 29 insertions(+), 28 deletions(-) diff --git a/plugin/lib/nodeFns.js b/plugin/lib/nodeFns.js index 55b04fe..f84bf1e 100644 --- a/plugin/lib/nodeFns.js +++ b/plugin/lib/nodeFns.js @@ -12,14 +12,12 @@ const nodeFns = { Float32Array, Float64Array, Function, - Infinity, Int16Array, Int32Array, Int8Array, Intl, JSON, Math, - NaN, Number, Object, RangeError, diff --git a/plugin/package-lock.json b/plugin/package-lock.json index 13e5e06..3ce7bdd 100644 --- a/plugin/package-lock.json +++ b/plugin/package-lock.json @@ -7,7 +7,8 @@ "dependencies": { "axios": "^0.24.0", "iconv-lite": "^0.6.3", - "lodash": "^4.17.21" + "lodash": "^4.17.21", + "ses": "^0.15.15" } }, "node_modules/axios": { @@ -57,6 +58,11 @@ "version": "2.1.2", "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" + }, + "node_modules/ses": { + "version": "0.15.15", + "resolved": "https://registry.npmmirror.com/ses/-/ses-0.15.15.tgz", + "integrity": "sha512-sJM4HRlM3VouA3RhRmS7wG5MRQPqZZnc6O4BvAefU7yeM+qp8EUfGAWQ9iB/X5cNh3+m5N9lC7DEpyxQ+E4D+w==" } }, "dependencies": { @@ -90,6 +96,11 @@ "version": "2.1.2", "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" + }, + "ses": { + "version": "0.15.15", + "resolved": "https://registry.npmmirror.com/ses/-/ses-0.15.15.tgz", + "integrity": "sha512-sJM4HRlM3VouA3RhRmS7wG5MRQPqZZnc6O4BvAefU7yeM+qp8EUfGAWQ9iB/X5cNh3+m5N9lC7DEpyxQ+E4D+w==" } } } diff --git a/plugin/package.json b/plugin/package.json index 2ab96cc..694e0ef 100644 --- a/plugin/package.json +++ b/plugin/package.json @@ -2,6 +2,7 @@ "dependencies": { "axios": "^0.24.0", "iconv-lite": "^0.6.3", - "lodash": "^4.17.21" + "lodash": "^4.17.21", + "ses": "^0.15.15" } } diff --git a/plugin/preload.js b/plugin/preload.js index fada7b0..57b5f91 100644 --- a/plugin/preload.js +++ b/plugin/preload.js @@ -8,6 +8,8 @@ const axios = require('axios'); const http = require('http'); const url = require('url') const nodeFns = require("./lib/nodeFns") +require('ses') + window._ = require("lodash") window.yuQueClient = axios.create({ baseURL: 'https://www.yuque.com/api/v2/', @@ -472,17 +474,17 @@ window.getuToolsLite = () => { let getSandboxFuns = () => { var sandbox = { utools: getuToolsLite(), - quickcommand: quickcommand, - electron: electron, - axios: axios, - Audio: Audio, - fetch: fetch, - _: _, + quickcommand, + electron, + axios, + Audio, + fetch, + _, // 兼容老版本 - fs: fs, - path: path, - os: os, - child_process: child_process, + fs, + path, + os, + child_process, } shortCodes.forEach(f => { sandbox[f.name] = f @@ -499,23 +501,12 @@ let liteErr = e => { utools.isDev() && (window.godMode = code => eval(code)) -// vm 模块将无法在渲染进程中使用,改用简单的沙箱来执行代码 -let createSandbox = (code, sandbox, async = false) => { - if (!async) code = `return (${code})` - const sandFn = new Function('sandbox', `with(sandbox){${code}}`); - const proxy = new Proxy(sandbox, { - has(target, key) { - return true; - } - }); - return sandFn(proxy); -} - +// vm 模块将无法在渲染进程中使用,改用 ses 来执行代码 window.evalCodeInSandbox = (code, userVars = {}) => { let sandbox = getSandboxFuns() let sandboxWithUV = Object.assign(userVars, sandbox) try { - return createSandbox(code, sandboxWithUV); + return new Compartment(sandboxWithUV).evaluate(code); } catch (error) { throw liteErr(error) } @@ -535,7 +526,7 @@ window.runCodeInSandbox = (code, callback, userVars = {}) => { } let sandboxWithUV = Object.assign(userVars, sandbox) try { - createSandbox(code, sandboxWithUV, true) + new Compartment(sandboxWithUV).evaluate(code) } catch (e) { console.log('Error: ', e) callback(null, liteErr(e))