Skip to content

Commit

Permalink
feat(#306): module whitelisting support
Browse files Browse the repository at this point in the history
  • Loading branch information
helloanoop committed Oct 6, 2023
1 parent 0f3a8a8 commit e3679c9
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/bruno-js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@usebruno/js",
"version": "0.6.0",
"version": "0.8.0",
"license": "MIT",
"main": "src/index.js",
"files": [
Expand Down
33 changes: 31 additions & 2 deletions packages/bruno-js/src/runtime/script-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const zlib = require('zlib');
const url = require('url');
const punycode = require('punycode');
const fs = require('fs');
const { get } = require('lodash');
const Bru = require('../bru');
const BrunoRequest = require('../bruno-request');
const BrunoResponse = require('../bruno-response');
Expand Down Expand Up @@ -38,10 +39,23 @@ class ScriptRuntime {
collectionPath,
onConsoleLog,
processEnvVars,
allowScriptFilesystemAccess
scriptingConfig
) {
const bru = new Bru(envVariables, collectionVariables, processEnvVars, collectionPath);
const req = new BrunoRequest(request);
const allowScriptFilesystemAccess = get(scriptingConfig, 'filesystemAccess.allow', false);
const moduleWhitelist = get(scriptingConfig, 'moduleWhitelist', []);

const whitelistedModules = {};

for (let module of moduleWhitelist) {
try {
whitelistedModules[module] = require(module);
} catch (e) {
// Ignore
console.warn(e);
}
}

const context = {
bru,
Expand Down Expand Up @@ -89,6 +103,7 @@ class ScriptRuntime {
chai,
'node-fetch': fetch,
'crypto-js': CryptoJS,
...whitelistedModules,
fs: allowScriptFilesystemAccess ? fs : undefined
}
}
Expand All @@ -111,11 +126,24 @@ class ScriptRuntime {
collectionPath,
onConsoleLog,
processEnvVars,
allowScriptFilesystemAccess
scriptingConfig
) {
const bru = new Bru(envVariables, collectionVariables, processEnvVars, collectionPath);
const req = new BrunoRequest(request);
const res = new BrunoResponse(response);
const allowScriptFilesystemAccess = get(scriptingConfig, 'filesystemAccess.allow', false);
const moduleWhitelist = get(scriptingConfig, 'moduleWhitelist', []);

const whitelistedModules = {};

for (let module of moduleWhitelist) {
try {
whitelistedModules[module] = require(module);
} catch (e) {
// Ignore
console.warn(e);
}
}

const context = {
bru,
Expand Down Expand Up @@ -163,6 +191,7 @@ class ScriptRuntime {
axios,
'node-fetch': fetch,
'crypto-js': CryptoJS,
...whitelistedModules,
fs: allowScriptFilesystemAccess ? fs : undefined
}
}
Expand Down
17 changes: 16 additions & 1 deletion packages/bruno-js/src/runtime/test-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const zlib = require('zlib');
const url = require('url');
const punycode = require('punycode');
const fs = require('fs');
const { get } = require('lodash');
const Bru = require('../bru');
const BrunoRequest = require('../bruno-request');
const BrunoResponse = require('../bruno-response');
Expand Down Expand Up @@ -38,11 +39,24 @@ class TestRuntime {
collectionPath,
onConsoleLog,
processEnvVars,
allowScriptFilesystemAccess
scriptingConfig
) {
const bru = new Bru(envVariables, collectionVariables, processEnvVars, collectionPath);
const req = new BrunoRequest(request);
const res = new BrunoResponse(response);
const allowScriptFilesystemAccess = get(scriptingConfig, 'filesystemAccess.allow', false);
const moduleWhitelist = get(scriptingConfig, 'moduleWhitelist', []);

const whitelistedModules = {};

for (let module of moduleWhitelist) {
try {
whitelistedModules[module] = require(module);
} catch (e) {
// Ignore
console.warn(e);
}
}

const __brunoTestResults = new TestResults();
const test = Test(__brunoTestResults, chai);
Expand Down Expand Up @@ -106,6 +120,7 @@ class TestRuntime {
nanoid,
chai,
'crypto-js': CryptoJS,
...whitelistedModules,
fs: allowScriptFilesystemAccess ? fs : undefined
}
}
Expand Down

0 comments on commit e3679c9

Please sign in to comment.