Skip to content

Commit 95f05aa

Browse files
authored
Merge pull request #4829 from Countly/ar2rsawseen/master
[hooks] update to new isolated vm
2 parents d249de5 + 75a6dcf commit 95f05aa

File tree

6 files changed

+1105
-43
lines changed

6 files changed

+1105
-43
lines changed

.github/workflows/main.yml

+7-1
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,18 @@ jobs:
199199
shell: bash
200200
run: "sed -i 's/port: 3001,/port: 3001, workers: 1,/' /opt/countly/api/config.js"
201201

202+
- name: Install plugins
203+
shell: bash
204+
run: |
205+
cp plugins/plugins.default.json /opt/countly/plugins/plugins.json
206+
node /opt/countly/bin/scripts/install_plugins.js --force
207+
202208
- name: Run tests
203209
shell: bash
204210
working-directory: /opt/countly
205211
run: |
206-
/sbin/my_init &
207212
npm install
213+
/sbin/my_init &
208214
node bin/scripts/test.connection.js
209215
npx grunt mochaTest
210216
ui-test:

bin/scripts/install_plugins.js

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ var manager = require('../../plugins/pluginManager.js');
33
var plugins = manager.getPlugins();
44
require('../../api/utils/log').setLevel('db:write', 'mute');
55

6+
if (process.argv && process.argv.length > 2 && process.argv[2] === '--force') {
7+
process.env.FORCE_NPM_INSTALL = true;
8+
}
9+
610
if (plugins.length > 0) {
711
manager.connectToAllDatabases().then(async() => {
812
if (!manager.getConfig("api").offline_mode) {

plugins/hooks/api/parts/effects/custom_code.js

+17-20
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
const utils = require("../../utils");
22
const common = require('../../../../../api/utils/common.js');
33
const log = common.log("hooks:api:api_custom_code_effect");
4-
const request = require("countly-request");
5-
const {NodeVM} = require('vm2');
4+
const {Sandbox} = require("v8-sandbox");
65

76
/**
87
* custom code effect
@@ -39,25 +38,23 @@ class CustomCodeEffect {
3938
};
4039

4140
genCode = `
42-
const CUSTOM_MAIN = async () => {
43-
try {
44-
${code}
45-
}
46-
catch(e) {
47-
CUSTOM_CODE_ERROR_CALLBACK(e);
48-
CUSTOM_CODE_RESOLVER();
49-
}
50-
CUSTOM_CODE_RESOLVER();
51-
}
52-
CUSTOM_MAIN();
41+
${code}
42+
setResult({ value: params });
5343
`;
54-
const vm = new NodeVM({
55-
timeout: 30000,
56-
console: 'inherit',
57-
sandbox: {params, setTimeout, request, CUSTOM_CODE_RESOLVER, CUSTOM_CODE_ERROR_CALLBACK},
58-
require: false,
59-
});
60-
vm.run(genCode, 'vm.js');
44+
const sandbox = new Sandbox();
45+
46+
(async() => {
47+
const { error, value } = await sandbox.execute({ code: genCode, timeout: 3000, globals: { params } });
48+
49+
await sandbox.shutdown();
50+
51+
if (error) {
52+
CUSTOM_CODE_ERROR_CALLBACK(error);
53+
}
54+
options.params = value;
55+
log.d("Resolved value:", value);
56+
CUSTOM_CODE_RESOLVER();
57+
})();
6158
});
6259
}
6360
catch (e) {

0 commit comments

Comments
 (0)