Fix tests

This commit is contained in:
Adria Navarro 2024-02-19 17:27:01 +01:00
parent 09dbc694fa
commit ae7a978998
4 changed files with 33 additions and 19 deletions

View File

@ -113,6 +113,7 @@ const environment = {
process.env[key] = value process.env[key] = value
// @ts-ignore // @ts-ignore
environment[key] = value environment[key] = value
cleanVariables()
}, },
isTest: coreEnv.isTest, isTest: coreEnv.isTest,
isJest: coreEnv.isJest, isJest: coreEnv.isJest,
@ -126,24 +127,26 @@ const environment = {
getDefaults: () => { getDefaults: () => {
return DEFAULTS return DEFAULTS
}, },
useIsolatedVM: { ISOLATEDVM_QUERY_TRANSFORMERS: !!process.env.ISOLATEDVM_QUERY_TRANSFORMERS,
QUERY_TRANSFORMERS: !!process.env.QUERY_TRANSFORMERS_ISOLATEDVM, ISOLATEDVM_JS_RUNNER: !!process.env.ISOLATEDVM_JS_RUNNER,
JS_RUNNER: !!process.env.JS_RUNNER_ISOLATEDVM,
},
} }
// clean up any environment variable edge cases function cleanVariables() {
for (let [key, value] of Object.entries(environment)) { // clean up any environment variable edge cases
// handle the edge case of "0" to disable an environment variable for (let [key, value] of Object.entries(environment)) {
if (value === "0") { // handle the edge case of "0" to disable an environment variable
// @ts-ignore if (value === "0") {
environment[key] = 0 // @ts-ignore
} environment[key] = 0
// handle the edge case of "false" to disable an environment variable }
if (value === "false") { // handle the edge case of "false" to disable an environment variable
// @ts-ignore if (value === "false") {
environment[key] = 0 // @ts-ignore
environment[key] = 0
}
} }
} }
cleanVariables()
export default environment export default environment

View File

@ -7,7 +7,8 @@ import { BuiltInVM, IsolatedVM } from "./vm"
export function init() { export function init() {
setJSRunner((js: string, ctx: Record<string, any>) => { setJSRunner((js: string, ctx: Record<string, any>) => {
return tracer.trace("runJS", {}, span => { return tracer.trace("runJS", {}, span => {
if (!env.useIsolatedVM.JS_RUNNER) { const useIsolatedVm = env.ISOLATEDVM_JS_RUNNER
if (!useIsolatedVm) {
const vm = new BuiltInVM(ctx, span) const vm = new BuiltInVM(ctx, span)
return vm.execute(js) return vm.execute(js)
} }

View File

@ -7,18 +7,28 @@ const { runJsHelpersTests } = require("@budibase/string-templates/test/utils")
import tk from "timekeeper" import tk from "timekeeper"
import { init } from ".." import { init } from ".."
import TestConfiguration from "../../tests/utilities/TestConfiguration" import TestConfiguration from "../../tests/utilities/TestConfiguration"
import environment from "../../environment"
tk.freeze("2021-01-21T12:00:00") tk.freeze("2021-01-21T12:00:00")
describe("jsRunner", () => { describe.each([
["vm", false],
["isolated-vm", true],
])("jsRunner (using %s)", (_, useIsolatedVM) => {
const config = new TestConfiguration() const config = new TestConfiguration()
beforeAll(async () => { beforeAll(async () => {
environment._set("ISOLATEDVM_JS_RUNNER", useIsolatedVM)
// Register js runner // Register js runner
init() init()
await config.init() await config.init()
}) })
afterAll(() => {
config.end()
})
const processJS = (js: string, context?: object) => { const processJS = (js: string, context?: object) => {
return config.doInContext(config.getAppId(), async () => return config.doInContext(config.getAppId(), async () =>
processStringSync(encodeJSBinding(js), context || {}) processStringSync(encodeJSBinding(js), context || {})
@ -30,7 +40,7 @@ describe("jsRunner", () => {
expect(output).toBe(3) expect(output).toBe(3)
}) })
it("should prevent sandbox escape", async () => { it.only("should prevent sandbox escape", async () => {
const output = await processJS( const output = await processJS(
`return this.constructor.constructor("return process")()` `return this.constructor.constructor("return process")()`
) )

View File

@ -129,7 +129,7 @@ class QueryRunner {
// transform as required // transform as required
if (transformer) { if (transformer) {
let runner: VM let runner: VM
if (!environment.useIsolatedVM.QUERY_TRANSFORMERS) { if (!environment.ISOLATEDVM_QUERY_TRANSFORMERS) {
runner = new VM2({ runner = new VM2({
data: rows, data: rows,
params: enrichedParameters, params: enrichedParameters,