@@ -6,9 +6,12 @@ const assert = require('assert');
6
6
const fs = require ( 'fs' ) ;
7
7
const path = require ( 'path' ) ;
8
8
9
- // This should not affect how the permission model resolves paths.
10
9
const { resolve } = path ;
11
- path . resolve = ( s ) => s ;
10
+ // This should not affect how the permission model resolves paths.
11
+ try {
12
+ path . resolve = ( s ) => s ;
13
+ assert . fail ( 'should not be called' ) ;
14
+ } catch { }
12
15
13
16
const blockedFolder = process . env . BLOCKEDFOLDER ;
14
17
const allowedFolder = process . env . ALLOWEDFOLDER ;
@@ -96,6 +99,26 @@ const uint8ArrayTraversalPath = new TextEncoder().encode(traversalPath);
96
99
} ) ) ;
97
100
}
98
101
102
+ // Monkey-patching path module should also not allow path traversal.
103
+ {
104
+ const fs = require ( 'fs' ) ;
105
+ const path = require ( 'path' ) ;
106
+
107
+ const cwd = Buffer . from ( '.' ) ;
108
+ try {
109
+ path . toNamespacedPath = ( path ) => { return traversalPath ; } ;
110
+ assert . fail ( 'should throw error when pacthing' ) ;
111
+ } catch { }
112
+
113
+ assert . throws ( ( ) => {
114
+ fs . readFile ( cwd , common . mustNotCall ( ) ) ;
115
+ } , common . expectsError ( {
116
+ code : 'ERR_ACCESS_DENIED' ,
117
+ permission : 'FileSystemRead' ,
118
+ resource : resolve ( cwd . toString ( ) ) ,
119
+ } ) ) ;
120
+ }
121
+
99
122
// Monkey-patching Buffer internals should also not allow path traversal.
100
123
{
101
124
const extraChars = '.' . repeat ( 40 ) ;
0 commit comments