Skip to content

Commit b0de49c

Browse files
ivovtomi
authored andcommitted
Merge commit from fork
1 parent 23f5834 commit b0de49c

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

packages/@n8n/task-runner/src/js-task-runner/__tests__/js-task-runner.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,26 @@ describe('JsTaskRunner', () => {
131131
});
132132
};
133133

134+
describe('Buffer security', () => {
135+
it('should redirect Buffer.allocUnsafe to Buffer.alloc', async () => {
136+
const outcome = await executeForAllItems({
137+
code: 'const buf = Buffer.allocUnsafe(10); return [{ json: { allZeros: buf.every(b => b === 0) } }]',
138+
inputItems: [{ a: 1 }],
139+
});
140+
141+
expect(outcome.result).toEqual([wrapIntoJson({ allZeros: true })]);
142+
});
143+
144+
it('should redirect Buffer.allocUnsafeSlow to Buffer.alloc', async () => {
145+
const outcome = await executeForAllItems({
146+
code: 'const buf = Buffer.allocUnsafeSlow(10); return [{ json: { allZeros: buf.every(b => b === 0) } }]',
147+
inputItems: [{ a: 1 }],
148+
});
149+
150+
expect(outcome.result).toEqual([wrapIntoJson({ allZeros: true })]);
151+
});
152+
});
153+
134154
describe('console', () => {
135155
test.each<[CodeExecutionMode]>([['runOnceForAllItems'], ['runOnceForEachItem']])(
136156
'should make an rpc call for console log in %s mode',

packages/@n8n/task-runner/src/js-task-runner/js-task-runner.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,19 @@ export class JsTaskRunner extends TaskRunner {
211211
}
212212

213213
private getNativeVariables() {
214+
const { mode } = this;
214215
return {
215216
// Exposed Node.js globals
216-
Buffer,
217+
Buffer: new Proxy(Buffer, {
218+
get(target, prop) {
219+
if (mode === 'insecure') return target[prop as keyof typeof Buffer];
220+
if (prop === 'allocUnsafe' || prop === 'allocUnsafeSlow') {
221+
// eslint-disable-next-line @typescript-eslint/unbound-method
222+
return Buffer.alloc;
223+
}
224+
return target[prop as keyof typeof Buffer];
225+
},
226+
}),
217227
setTimeout,
218228
setInterval,
219229
setImmediate,

0 commit comments

Comments
 (0)