File tree Expand file tree Collapse file tree 2 files changed +31
-1
lines changed
packages/@n8n/task-runner/src/js-task-runner Expand file tree Collapse file tree 2 files changed +31
-1
lines changed Original file line number Diff line number Diff 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' ,
Original file line number Diff line number Diff 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,
You can’t perform that action at this time.
0 commit comments