File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -21,6 +21,19 @@ export default (
2121) => {
2222 const logger = options . jobLogger ?? console ;
2323 const globals = options . globals || { } ;
24+
25+ // Workaround for https://github.com/nodejs/node/issues/4660
26+ // The Buffer class we share directly with users will throw if used
27+ // All _internal_ Buffer references use the original nodejs interface
28+ class SafeBuffer extends Buffer {
29+ constructor ( x : any ) {
30+ throw new Error (
31+ 'Do not call Buffer() constructor directly. Use Buffer.from() instead.'
32+ ) ;
33+ super ( x ) ; // keeps types happy
34+ }
35+ }
36+
2437 const context = vm . createContext (
2538 freezeAll (
2639 {
@@ -34,7 +47,7 @@ export default (
3447 setInterval,
3548 setTimeout,
3649 state, // TODO will be dropped as a global one day, see https://github.com/OpenFn/kit/issues/17
37- Buffer,
50+ Buffer : SafeBuffer ,
3851 } ,
3952 { state : true }
4053 ) ,
Original file line number Diff line number Diff line change @@ -60,3 +60,27 @@ test("doesn't allow eval inside a job", async (t) => {
6060 message : / I l l e g a l e v a l s t a t e m e n t d e t e c t e d / ,
6161 } ) ;
6262} ) ;
63+
64+ test ( 'Buffer.from() works inside a job' , async ( t ) => {
65+ const expression = `
66+ export default [
67+ (s) => { s.data = Buffer.from('6a6f65', 'hex').toString(); return s; }
68+ ];` ;
69+ const input = { } ;
70+
71+ const result = await run ( expression , input ) ;
72+ t . is ( result . data as any , 'joe' ) ;
73+ } ) ;
74+
75+ test ( 'Buffer constructor throws inside a job' , async ( t ) => {
76+ const expression = `
77+ export default [
78+ (s) => { s.data = new Buffer('6a6f65', 'hex').toString(); return s; }
79+ ];` ;
80+ const input = { } ;
81+
82+ const result = await run ( expression , input ) ;
83+ const err = result . errors ! [ 'job-1' ] ;
84+ t . is ( err . name , 'JobError' ) ;
85+ t . regex ( err . message , / d o n o t c a l l B u f f e r \( \) c o n s t r u c t o r / i) ;
86+ } ) ;
You can’t perform that action at this time.
0 commit comments