Skip to content

Commit

Permalink
Merge pull request #4829 from Countly/ar2rsawseen/master
Browse files Browse the repository at this point in the history
[hooks] update to new isolated vm
  • Loading branch information
ar2rsawseen authored Jan 11, 2024
2 parents d249de5 + 75a6dcf commit 95f05aa
Show file tree
Hide file tree
Showing 6 changed files with 1,105 additions and 43 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,18 @@ jobs:
shell: bash
run: "sed -i 's/port: 3001,/port: 3001, workers: 1,/' /opt/countly/api/config.js"

- name: Install plugins
shell: bash
run: |
cp plugins/plugins.default.json /opt/countly/plugins/plugins.json
node /opt/countly/bin/scripts/install_plugins.js --force
- name: Run tests
shell: bash
working-directory: /opt/countly
run: |
/sbin/my_init &
npm install
/sbin/my_init &
node bin/scripts/test.connection.js
npx grunt mochaTest
ui-test:
Expand Down
4 changes: 4 additions & 0 deletions bin/scripts/install_plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ var manager = require('../../plugins/pluginManager.js');
var plugins = manager.getPlugins();
require('../../api/utils/log').setLevel('db:write', 'mute');

if (process.argv && process.argv.length > 2 && process.argv[2] === '--force') {
process.env.FORCE_NPM_INSTALL = true;
}

if (plugins.length > 0) {
manager.connectToAllDatabases().then(async() => {
if (!manager.getConfig("api").offline_mode) {
Expand Down
37 changes: 17 additions & 20 deletions plugins/hooks/api/parts/effects/custom_code.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
const utils = require("../../utils");
const common = require('../../../../../api/utils/common.js');
const log = common.log("hooks:api:api_custom_code_effect");
const request = require("countly-request");
const {NodeVM} = require('vm2');
const {Sandbox} = require("v8-sandbox");

/**
* custom code effect
Expand Down Expand Up @@ -39,25 +38,23 @@ class CustomCodeEffect {
};

genCode = `
const CUSTOM_MAIN = async () => {
try {
${code}
}
catch(e) {
CUSTOM_CODE_ERROR_CALLBACK(e);
CUSTOM_CODE_RESOLVER();
}
CUSTOM_CODE_RESOLVER();
}
CUSTOM_MAIN();
${code}
setResult({ value: params });
`;
const vm = new NodeVM({
timeout: 30000,
console: 'inherit',
sandbox: {params, setTimeout, request, CUSTOM_CODE_RESOLVER, CUSTOM_CODE_ERROR_CALLBACK},
require: false,
});
vm.run(genCode, 'vm.js');
const sandbox = new Sandbox();

(async() => {
const { error, value } = await sandbox.execute({ code: genCode, timeout: 3000, globals: { params } });

await sandbox.shutdown();

if (error) {
CUSTOM_CODE_ERROR_CALLBACK(error);
}
options.params = value;
log.d("Resolved value:", value);
CUSTOM_CODE_RESOLVER();
})();
});
}
catch (e) {
Expand Down
Loading

0 comments on commit 95f05aa

Please sign in to comment.