Skip to content

Commit

Permalink
fix(core): Disallow code generation in task runner
Browse files Browse the repository at this point in the history
  • Loading branch information
tomi committed Jan 9, 2025
1 parent bb6cd79 commit df24dd9
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
5 changes: 4 additions & 1 deletion docker/images/n8n/n8n-task-runners.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
"runner-type": "javascript",
"workdir": "/home/node",
"command": "/usr/local/bin/node",
"args": ["/usr/local/lib/node_modules/n8n/node_modules/@n8n/task-runner/dist/start.js"],
"args": [
"--disallow-code-generation-from-strings",
"/usr/local/lib/node_modules/n8n/node_modules/@n8n/task-runner/dist/start.js"
],
"allowed-env": [
"PATH",
"GENERIC_TIMEZONE",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ describe('JsTaskRunner', () => {
['typeof clearInterval', 'function'],
['typeof clearImmediate', 'function'],
],
eval: [['eval("1+2")', 3]],
'JS built-ins': [
['typeof btoa', 'function'],
['typeof atob', 'function'],
Expand Down
14 changes: 5 additions & 9 deletions packages/@n8n/task-runner/src/js-task-runner/js-task-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import type {
} from 'n8n-workflow';
import * as a from 'node:assert';
import { inspect } from 'node:util';
import { runInNewContext, type Context } from 'node:vm';
import { type Context, createContext, runInContext } from 'node:vm';

import type { MainConfig } from '@/config/main-config';
import { UnsupportedFunctionError } from '@/js-task-runner/errors/unsupported-function.error';
Expand Down Expand Up @@ -160,8 +160,6 @@ export class JsTaskRunner extends TaskRunner {
return {
// Exposed Node.js globals in vm2
Buffer,
Function,
eval,
setTimeout,
setInterval,
setImmediate,
Expand Down Expand Up @@ -205,7 +203,7 @@ export class JsTaskRunner extends TaskRunner {

signal.addEventListener('abort', abortHandler, { once: true });

const taskResult = runInNewContext(
const taskResult = runInContext(
`globalThis.global = globalThis; module.exports = async function VmCodeWrapper() {${settings.code}\n}()`,
context,
{ timeout: this.taskTimeout * 1000 },
Expand Down Expand Up @@ -268,7 +266,7 @@ export class JsTaskRunner extends TaskRunner {

signal.addEventListener('abort', abortHandler);

const taskResult = runInNewContext(
const taskResult = runInContext(
`module.exports = async function VmCodeWrapper() {${settings.code}\n}()`,
context,
{ timeout: this.taskTimeout * 1000 },
Expand Down Expand Up @@ -470,7 +468,7 @@ export class JsTaskRunner extends TaskRunner {
dataProxy: IWorkflowDataProxyData,
additionalProperties: Record<string, unknown> = {},
): Context {
const context: Context = {
return createContext({
[inspect.custom]: () => '[[ExecutionContext]]',
require: this.requireResolver,
module: {},
Expand All @@ -480,8 +478,6 @@ export class JsTaskRunner extends TaskRunner {
...dataProxy,
...this.buildRpcCallObject(taskId),
...additionalProperties,
};

return context;
});
}
}
2 changes: 1 addition & 1 deletion packages/cli/src/task-runners/task-runner-process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export class TaskRunnerProcess extends TypedEmitter<TaskRunnerProcessEventMap> {
startNode(grantToken: string, taskBrokerUri: string) {
const startScript = require.resolve('@n8n/task-runner/start');

return spawn('node', [startScript], {
return spawn('node', ['--disallow-code-generation-from-strings', startScript], {
env: this.getProcessEnvVars(grantToken, taskBrokerUri),
});
}
Expand Down

0 comments on commit df24dd9

Please sign in to comment.