Skip to content

Commit 37f7e55

Browse files
committed
fix: fixed_sandbox_bug
1 parent 8e1f654 commit 37f7e55

File tree

4 files changed

+26
-8
lines changed

4 files changed

+26
-8
lines changed

exts/yapi-plugin-advanced-mock/server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,6 @@ module.exports = function() {
207207

208208
// mock 脚本
209209
let script = data.mock_script;
210-
yapi.commons.handleMockScript(script, context);
210+
await yapi.commons.handleMockScript(script, context);
211211
});
212212
};

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,17 @@
7474
"node-schedule": "^1.3.2",
7575
"nodemailer": "4.0.1",
7676
"os": "0.1.1",
77+
"qs": "^6.7.0",
7778
"request": "2.81.0",
79+
"safeify": "^5.0.5",
7880
"sha.js": "2.4.9",
7981
"sha1": "1.1.1",
8082
"swagger-client": "^3.5.1",
8183
"tslib": "1.8.0",
8284
"underscore": "1.8.3",
8385
"url": "0.11.0",
84-
"yapi-plugin-qsso": "^1.1.0",
85-
"qs": "^6.7.0",
86-
"vm2": "^3.8.4"
86+
"vm2": "^3.8.4",
87+
"yapi-plugin-qsso": "^1.1.0"
8788
},
8889
"devDependencies": {
8990
"antd": "3.2.2",

server/utils/commons.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const json5 = require('json5');
1313
const _ = require('underscore');
1414
const Ajv = require('ajv');
1515
const Mock = require('mockjs');
16+
const sandboxFn = require('./sandbox')
1617

1718

1819

@@ -576,7 +577,7 @@ ${JSON.stringify(schema, null, 2)}`)
576577
// script 是断言
577578
if (globalScript) {
578579
logs.push('执行脚本:' + globalScript)
579-
result = yapi.commons.sandbox(context, globalScript);
580+
result = await sandboxFn(context, globalScript);
580581
}
581582
}
582583

@@ -585,7 +586,7 @@ ${JSON.stringify(schema, null, 2)}`)
585586
// script 是断言
586587
if (script) {
587588
logs.push('执行脚本:' + script)
588-
result = yapi.commons.sandbox(context, script);
589+
result = await sandboxFn(context, script);
589590
}
590591
result.logs = logs;
591592
return yapi.commons.resReturn(result);
@@ -613,7 +614,7 @@ exports.getUserdata = async function getUserdata(uid, role) {
613614
};
614615

615616
// 处理mockJs脚本
616-
exports.handleMockScript = function (script, context) {
617+
exports.handleMockScript = async function (script, context) {
617618
let sandbox = {
618619
header: context.ctx.header,
619620
query: context.ctx.query,
@@ -632,7 +633,7 @@ exports.handleMockScript = function (script, context) {
632633
var parts = Cookie.split('=');
633634
sandbox.cookie[parts[0].trim()] = (parts[1] || '').trim();
634635
});
635-
sandbox = yapi.commons.sandbox(sandbox, script);
636+
sandbox = await sandboxFn(sandbox, script);
636637
sandbox.delay = isNaN(sandbox.delay) ? 0 : +sandbox.delay;
637638

638639
context.mockJson = sandbox.mockJson;

server/utils/sandbox.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const Safeify = require('safeify').default;
2+
3+
module.exports = async function sandboxFn(context, script) {
4+
// 创建 safeify 实例
5+
const safeVm = new Safeify({
6+
timeout: 3000,
7+
asyncTimeout: 60000
8+
})
9+
10+
// 执行动态代码
11+
const result = await safeVm.run(script, context)
12+
13+
// 释放资源
14+
safeVm.destroy()
15+
return result
16+
}

0 commit comments

Comments
 (0)