@@ -75,6 +75,9 @@ for (const badFile of [
75
75
[ badFile , [ '/d' , '/c' , 'cd' ] , { cwd : cwdUrl } ] :
76
76
[ badFile , [ ] , { cwd : cwdUrl } ] ;
77
77
78
+ // Passing an object that doesn't have shape of WHATWG URL object
79
+ // results in TypeError
80
+
78
81
throws (
79
82
( ) => cp . execFile ( ...pwdCommandAndOptions , mustNotCall ( ) ) ,
80
83
{ code : 'ERR_INVALID_ARG_TYPE' } ,
@@ -95,3 +98,195 @@ for (const badFile of [
95
98
{ code : 'ERR_INVALID_ARG_TYPE' } ,
96
99
) ;
97
100
}
101
+
102
+ // Test for non-file: URL objects
103
+ for ( const badFile of [
104
+ new URL ( 'https://nodejs.org/file:///' ) ,
105
+ new url . URL ( 'https://nodejs.org/file:///' ) ,
106
+ {
107
+ href : 'https://nodejs.org/file:///' ,
108
+ origin : 'https://nodejs.org' ,
109
+ protocol : 'https:' ,
110
+ username : '' ,
111
+ password : '' ,
112
+ host : 'nodejs.org' ,
113
+ hostname : 'nodejs.org' ,
114
+ port : '' ,
115
+ pathname : '/file:///' ,
116
+ search : '' ,
117
+ searchParams : new URLSearchParams ( ) ,
118
+ hash : ''
119
+ } ,
120
+ ] ) {
121
+ const pwdCommandAndOptions = isWindows ?
122
+ [ badFile , [ '/d' , '/c' , 'cd' ] , { cwd : cwdUrl } ] :
123
+ [ badFile , [ ] , { cwd : cwdUrl } ] ;
124
+
125
+ // Passing an URL object with protocol other than `file:`
126
+ // results in TypeError
127
+
128
+ throws (
129
+ ( ) => cp . execFile ( ...pwdCommandAndOptions , mustNotCall ( ) ) ,
130
+ { code : 'ERR_INVALID_URL_SCHEME' } ,
131
+ ) ;
132
+
133
+ throws (
134
+ ( ) => cp . execFileSync ( ...pwdCommandAndOptions ) ,
135
+ { code : 'ERR_INVALID_URL_SCHEME' } ,
136
+ ) ;
137
+
138
+ throws (
139
+ ( ) => cp . spawn ( ...pwdCommandAndOptions ) ,
140
+ { code : 'ERR_INVALID_URL_SCHEME' } ,
141
+ ) ;
142
+
143
+ throws (
144
+ ( ) => cp . spawnSync ( ...pwdCommandAndOptions ) ,
145
+ { code : 'ERR_INVALID_URL_SCHEME' } ,
146
+ ) ;
147
+ }
148
+
149
+ // Test for malformed file URL objects
150
+ for ( const badFile of [
151
+ new URL ( 'file://nodejs.org/file:///' ) ,
152
+ new url . URL ( 'file://nodejs.org/file:///' ) ,
153
+ {
154
+ href : 'file://nodejs.org/file:///' ,
155
+ origin : 'null' ,
156
+ protocol : 'file:' ,
157
+ username : '' ,
158
+ password : '' ,
159
+ host : 'nodejs.org' ,
160
+ hostname : 'nodejs.org' ,
161
+ port : '' ,
162
+ pathname : '/file:///' ,
163
+ search : '' ,
164
+ searchParams : new URLSearchParams ( ) ,
165
+ hash : ''
166
+ } ,
167
+ ] ) {
168
+ const pwdCommandAndOptions = isWindows ?
169
+ [ badFile , [ '/d' , '/c' , 'cd' ] , { cwd : cwdUrl } ] :
170
+ [ badFile , [ ] , { cwd : cwdUrl } ] ;
171
+
172
+ // Passing an URL object with non-empty host
173
+ // results in TypeError
174
+
175
+ throws (
176
+ ( ) => cp . execFile ( ...pwdCommandAndOptions , mustNotCall ( ) ) ,
177
+ { code : 'ERR_INVALID_FILE_URL_HOST' } ,
178
+ ) ;
179
+
180
+ throws (
181
+ ( ) => cp . execFileSync ( ...pwdCommandAndOptions ) ,
182
+ { code : 'ERR_INVALID_FILE_URL_HOST' } ,
183
+ ) ;
184
+
185
+ throws (
186
+ ( ) => cp . spawn ( ...pwdCommandAndOptions ) ,
187
+ { code : 'ERR_INVALID_FILE_URL_HOST' } ,
188
+ ) ;
189
+
190
+ throws (
191
+ ( ) => cp . spawnSync ( ...pwdCommandAndOptions ) ,
192
+ { code : 'ERR_INVALID_FILE_URL_HOST' } ,
193
+ ) ;
194
+ }
195
+
196
+ // Test for file URL objects with %2F in path
197
+ const urlWithSlash = new URL ( pwdWHATWGUrl ) ;
198
+ urlWithSlash . pathname += '%2F' ;
199
+ for ( const badFile of [
200
+ urlWithSlash ,
201
+ new url . URL ( urlWithSlash ) ,
202
+ {
203
+ href : urlWithSlash . href ,
204
+ origin : urlWithSlash . origin ,
205
+ protocol : urlWithSlash . protocol ,
206
+ username : urlWithSlash . username ,
207
+ password : urlWithSlash . password ,
208
+ host : urlWithSlash . host ,
209
+ hostname : urlWithSlash . hostname ,
210
+ port : urlWithSlash . port ,
211
+ pathname : urlWithSlash . pathname ,
212
+ search : urlWithSlash . search ,
213
+ searchParams : new URLSearchParams ( urlWithSlash . searchParams ) ,
214
+ hash : urlWithSlash . hash ,
215
+ } ,
216
+ ] ) {
217
+ const pwdCommandAndOptions = isWindows ?
218
+ [ badFile , [ '/d' , '/c' , 'cd' ] , { cwd : cwdUrl } ] :
219
+ [ badFile , [ ] , { cwd : cwdUrl } ] ;
220
+
221
+ // Passing an URL object with percent-encoded '/'
222
+ // results in TypeError
223
+
224
+ throws (
225
+ ( ) => cp . execFile ( ...pwdCommandAndOptions , mustNotCall ( ) ) ,
226
+ { code : 'ERR_INVALID_FILE_URL_PATH' } ,
227
+ ) ;
228
+
229
+ throws (
230
+ ( ) => cp . execFileSync ( ...pwdCommandAndOptions ) ,
231
+ { code : 'ERR_INVALID_FILE_URL_PATH' } ,
232
+ ) ;
233
+
234
+ throws (
235
+ ( ) => cp . spawn ( ...pwdCommandAndOptions ) ,
236
+ { code : 'ERR_INVALID_FILE_URL_PATH' } ,
237
+ ) ;
238
+
239
+ throws (
240
+ ( ) => cp . spawnSync ( ...pwdCommandAndOptions ) ,
241
+ { code : 'ERR_INVALID_FILE_URL_PATH' } ,
242
+ ) ;
243
+ }
244
+
245
+ // Test for file URL objects with %00 in path
246
+ const urlWithNul = new URL ( pwdWHATWGUrl ) ;
247
+ urlWithNul . pathname += '%00' ;
248
+ for ( const badFile of [
249
+ urlWithNul ,
250
+ new url . URL ( urlWithNul ) ,
251
+ {
252
+ href : urlWithNul . href ,
253
+ origin : urlWithNul . origin ,
254
+ protocol : urlWithNul . protocol ,
255
+ username : urlWithNul . username ,
256
+ password : urlWithNul . password ,
257
+ host : urlWithNul . host ,
258
+ hostname : urlWithNul . hostname ,
259
+ port : urlWithNul . port ,
260
+ pathname : urlWithNul . pathname ,
261
+ search : urlWithNul . search ,
262
+ searchParams : new URLSearchParams ( urlWithNul . searchParams ) ,
263
+ hash : urlWithNul . hash ,
264
+ } ,
265
+ ] ) {
266
+ const pwdCommandAndOptions = isWindows ?
267
+ [ badFile , [ '/d' , '/c' , 'cd' ] , { cwd : cwdUrl } ] :
268
+ [ badFile , [ ] , { cwd : cwdUrl } ] ;
269
+
270
+ // Passing an URL object with percent-encoded '\0'
271
+ // results in TypeError
272
+
273
+ throws (
274
+ ( ) => cp . execFile ( ...pwdCommandAndOptions , mustNotCall ( ) ) ,
275
+ { code : 'ERR_INVALID_ARG_VALUE' } ,
276
+ ) ;
277
+
278
+ throws (
279
+ ( ) => cp . execFileSync ( ...pwdCommandAndOptions ) ,
280
+ { code : 'ERR_INVALID_ARG_VALUE' } ,
281
+ ) ;
282
+
283
+ throws (
284
+ ( ) => cp . spawn ( ...pwdCommandAndOptions ) ,
285
+ { code : 'ERR_INVALID_ARG_VALUE' } ,
286
+ ) ;
287
+
288
+ throws (
289
+ ( ) => cp . spawnSync ( ...pwdCommandAndOptions ) ,
290
+ { code : 'ERR_INVALID_ARG_VALUE' } ,
291
+ ) ;
292
+ }
0 commit comments