forked from directus/directus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1be56aa
commit d2113b1
Showing
2 changed files
with
11 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,12 @@ | ||
import { defineOperationApi, toArray } from '@smartnews/directus-utils'; | ||
import { isBuiltin } from 'node:module'; | ||
import type { NodeVMOptions } from 'vm2'; | ||
import { NodeVM, VMScript } from 'vm2'; | ||
import { defineOperationApi } from '@smartnews/directus-utils'; | ||
|
||
type Options = { | ||
code: string; | ||
}; | ||
|
||
export default defineOperationApi<Options>({ | ||
id: 'exec', | ||
handler: async ({ code }, { data, env }) => { | ||
const allowedModules = env['FLOWS_EXEC_ALLOWED_MODULES'] ? toArray(env['FLOWS_EXEC_ALLOWED_MODULES']) : []; | ||
const allowedModulesBuiltIn: string[] = []; | ||
const allowedModulesExternal: string[] = []; | ||
const allowedEnv = data['$env'] ?? {}; | ||
|
||
const opts: NodeVMOptions = { | ||
eval: false, | ||
wasm: false, | ||
env: allowedEnv, | ||
}; | ||
|
||
for (const module of allowedModules) { | ||
if (isBuiltin(module)) { | ||
allowedModulesBuiltIn.push(module); | ||
} else { | ||
allowedModulesExternal.push(module); | ||
} | ||
} | ||
|
||
if (allowedModules.length > 0) { | ||
opts.require = { | ||
builtin: allowedModulesBuiltIn, | ||
external: { | ||
modules: allowedModulesExternal, | ||
transitive: false, | ||
}, | ||
}; | ||
} | ||
|
||
const vm = new NodeVM(opts); | ||
|
||
const script = new VMScript(code).compile(); | ||
const fn = await vm.run(script); | ||
|
||
return await fn(data); | ||
handler: async () => { | ||
throw new Error('permission denied'); | ||
}, | ||
}); |