File tree Expand file tree Collapse file tree 3 files changed +38
-0
lines changed Expand file tree Collapse file tree 3 files changed +38
-0
lines changed Original file line number Diff line number Diff line change
1
+ unreleased :
2
+ fixed bugs :
3
+ - GH-907 Defined `Error.prepareStackTrace` to prevent stack trace pollution
4
+
1
5
4.2.4 :
2
6
date : 2023-03-10
3
7
fixed bugs :
Original file line number Diff line number Diff line change 54
54
// @note this deletes the constructor as well to make sure one can't recreate the same scope
55
55
contextObject = Object . getPrototypeOf ( contextObject ) ;
56
56
} while ( contextObject && contextObject . constructor !== Object ) ;
57
+
58
+ // define custom Error.prepareStackTrace
59
+ Object . defineProperty ( Error , 'prepareStackTrace' , {
60
+ value : function ( error , structuredStackTrace ) {
61
+ const errorString = String ( error ) ;
62
+
63
+ if ( Array . isArray ( structuredStackTrace ) && structuredStackTrace . length ) {
64
+ return `${ errorString } \n at ${ structuredStackTrace . join ( '\n at ' ) } ` ;
65
+ }
66
+
67
+ return errorString ;
68
+ } ,
69
+ configurable : false ,
70
+ enumerable : false ,
71
+ writable : false
72
+ } ) ;
57
73
} ( ) ) ;
58
74
59
75
// do include json purse
Original file line number Diff line number Diff line change @@ -57,6 +57,24 @@ describe('sandbox', function () {
57
57
} ) ;
58
58
} ) ;
59
59
60
+ it ( 'should not be able to mutate Error.prepareStackTrace' , function ( done ) {
61
+ Sandbox . createContext ( function ( err , ctx ) {
62
+ if ( err ) { return done ( err ) ; }
63
+ ctx . on ( 'error' , done ) ;
64
+
65
+ ctx . execute ( `
66
+ var assert = require('assert');
67
+ var fn = Error.prepareStackTrace;
68
+
69
+ Error.prepareStackTrace = () => {};
70
+ assert.equal(Error.prepareStackTrace, fn);
71
+
72
+ var err = new Error('Test');
73
+ assert.equal(err.stack.split('\\n')[0], 'Error: Test');
74
+ ` , done ) ;
75
+ } ) ;
76
+ } ) ;
77
+
60
78
it ( 'should not have access to global properties' , function ( done ) {
61
79
Sandbox . createContext ( { debug : true } , function ( err , ctx ) {
62
80
if ( err ) { return done ( err ) ; }
You can’t perform that action at this time.
0 commit comments