7
7
*/
8
8
namespace Magento \Framework \Filesystem \Directory ;
9
9
10
+ use Magento \Framework \Exception \ValidatorException ;
10
11
use Magento \TestFramework \Helper \Bootstrap ;
11
12
12
13
/**
@@ -34,13 +35,66 @@ public function testGetAbsolutePath()
34
35
$ this ->assertContains ('_files/foo/bar ' , $ dir ->getAbsolutePath ('bar ' ));
35
36
}
36
37
38
+ public function testGetAbsolutePathOutside ()
39
+ {
40
+ $ exceptions = 0 ;
41
+ $ dir = $ this ->getDirectoryInstance ('foo ' );
42
+ try {
43
+ $ dir ->getAbsolutePath ('../../Directory/ReadTest.php ' );
44
+ } catch (ValidatorException $ exception ) {
45
+ $ exceptions ++;
46
+ }
47
+ try {
48
+ $ dir ->getAbsolutePath ('//./..///../Directory/ReadTest.php ' );
49
+ } catch (ValidatorException $ exception ) {
50
+ $ exceptions ++;
51
+ }
52
+ try {
53
+ $ dir ->getAbsolutePath ('\..\..\Directory\ReadTest.php ' );
54
+ } catch (ValidatorException $ exception ) {
55
+ $ exceptions ++;
56
+ }
57
+ $ this ->assertEquals (3 , $ exceptions );
58
+ }
59
+
37
60
public function testGetRelativePath ()
38
61
{
39
62
$ dir = $ this ->getDirectoryInstance ('foo ' );
63
+ $ this ->assertEquals (
64
+ 'file_three.txt ' ,
65
+ $ dir ->getRelativePath ('file_three.txt ' )
66
+ );
40
67
$ this ->assertEquals ('' , $ dir ->getRelativePath ());
41
68
$ this ->assertEquals ('bar ' , $ dir ->getRelativePath (__DIR__ . '/../_files/foo/bar ' ));
42
69
}
43
70
71
+ public function testGetRelativePathOutside ()
72
+ {
73
+ $ exceptions = 0 ;
74
+ $ dir = $ this ->getDirectoryInstance ('foo ' );
75
+ try {
76
+ $ dir ->getRelativePath (__DIR__ .'/ReadTest.php ' );
77
+ } catch (ValidatorException $ exception ) {
78
+ $ exceptions ++;
79
+ }
80
+ try {
81
+ $ dir ->getRelativePath (__DIR__ .'//./..////Directory/ReadTest.php ' );
82
+ } catch (ValidatorException $ exception ) {
83
+ $ exceptions ++;
84
+ }
85
+ try {
86
+ $ dir ->getRelativePath (__DIR__ .'\..\Directory\ReadTest.php ' );
87
+ } catch (ValidatorException $ exception ) {
88
+ $ exceptions ++;
89
+ }
90
+ try {
91
+ $ dir ->getRelativePath ('../../Directory/ReadTest.php ' );
92
+ } catch (ValidatorException $ exception ) {
93
+ $ exceptions ++;
94
+ }
95
+ $ this ->assertEquals (4 , $ exceptions );
96
+ }
97
+
44
98
/**
45
99
* Test for read method
46
100
*
@@ -72,6 +126,28 @@ public function readProvider()
72
126
];
73
127
}
74
128
129
+ public function testReadOutside ()
130
+ {
131
+ $ exceptions = 0 ;
132
+ $ dir = $ this ->getDirectoryInstance ('foo ' );
133
+ try {
134
+ $ dir ->read ('../../Directory/ReadTest.php ' );
135
+ } catch (ValidatorException $ exception ) {
136
+ $ exceptions ++;
137
+ }
138
+ try {
139
+ $ dir ->read ('//./..///../Directory/ReadTest.php ' );
140
+ } catch (ValidatorException $ exception ) {
141
+ $ exceptions ++;
142
+ }
143
+ try {
144
+ $ dir ->read ('\..\..\Directory\ReadTest.php ' );
145
+ } catch (ValidatorException $ exception ) {
146
+ $ exceptions ++;
147
+ }
148
+ $ this ->assertEquals (3 , $ exceptions );
149
+ }
150
+
75
151
/**
76
152
* Test for search method
77
153
*
@@ -103,6 +179,28 @@ public function searchProvider()
103
179
];
104
180
}
105
181
182
+ public function testSearchOutside ()
183
+ {
184
+ $ exceptions = 0 ;
185
+ $ dir = $ this ->getDirectoryInstance ('foo ' );
186
+ try {
187
+ $ dir ->search ('/*/*.txt ' , '../../Directory/ReadTest.php ' );
188
+ } catch (ValidatorException $ exception ) {
189
+ $ exceptions ++;
190
+ }
191
+ try {
192
+ $ dir ->search ('/*/*.txt ' , '//./..///../Directory/ReadTest.php ' );
193
+ } catch (ValidatorException $ exception ) {
194
+ $ exceptions ++;
195
+ }
196
+ try {
197
+ $ dir ->search ('/*/*.txt ' , '\..\..\Directory\ReadTest.php ' );
198
+ } catch (ValidatorException $ exception ) {
199
+ $ exceptions ++;
200
+ }
201
+ $ this ->assertEquals (3 , $ exceptions );
202
+ }
203
+
106
204
/**
107
205
* Test for isExist method
108
206
*
@@ -127,6 +225,28 @@ public function existsProvider()
127
225
return [['foo ' , 'bar ' , true ], ['foo ' , 'bar/baz/ ' , true ], ['foo ' , 'bar/notexists ' , false ]];
128
226
}
129
227
228
+ public function testIsExistOutside ()
229
+ {
230
+ $ exceptions = 0 ;
231
+ $ dir = $ this ->getDirectoryInstance ('foo ' );
232
+ try {
233
+ $ dir ->isExist ('../../Directory/ReadTest.php ' );
234
+ } catch (ValidatorException $ exception ) {
235
+ $ exceptions ++;
236
+ }
237
+ try {
238
+ $ dir ->isExist ('//./..///../Directory/ReadTest.php ' );
239
+ } catch (ValidatorException $ exception ) {
240
+ $ exceptions ++;
241
+ }
242
+ try {
243
+ $ dir ->isExist ('\..\..\Directory\ReadTest.php ' );
244
+ } catch (ValidatorException $ exception ) {
245
+ $ exceptions ++;
246
+ }
247
+ $ this ->assertEquals (3 , $ exceptions );
248
+ }
249
+
130
250
/**
131
251
* Test for stat method
132
252
*
@@ -168,6 +288,28 @@ public function statProvider()
168
288
return [['foo ' , 'bar ' ], ['foo ' , 'file_three.txt ' ]];
169
289
}
170
290
291
+ public function testStatOutside ()
292
+ {
293
+ $ exceptions = 0 ;
294
+ $ dir = $ this ->getDirectoryInstance ('foo ' );
295
+ try {
296
+ $ dir ->stat ('bar/../../../Directory ' );
297
+ } catch (ValidatorException $ exception ) {
298
+ $ exceptions ++;
299
+ }
300
+ try {
301
+ $ dir ->stat ('bar//./..///../../Directory ' );
302
+ } catch (ValidatorException $ exception ) {
303
+ $ exceptions ++;
304
+ }
305
+ try {
306
+ $ dir ->stat ('bar\..\..\..\Directory ' );
307
+ } catch (ValidatorException $ exception ) {
308
+ $ exceptions ++;
309
+ }
310
+ $ this ->assertEquals (3 , $ exceptions );
311
+ }
312
+
171
313
/**
172
314
* Test for isReadable method
173
315
*
@@ -182,6 +324,28 @@ public function testIsReadable($dirPath, $path, $readable)
182
324
$ this ->assertEquals ($ readable , $ dir ->isReadable ($ path ));
183
325
}
184
326
327
+ public function testIsReadableOutside ()
328
+ {
329
+ $ exceptions = 0 ;
330
+ $ dir = $ this ->getDirectoryInstance ('foo ' );
331
+ try {
332
+ $ dir ->isReadable ('../../Directory/ReadTest.php ' );
333
+ } catch (ValidatorException $ exception ) {
334
+ $ exceptions ++;
335
+ }
336
+ try {
337
+ $ dir ->isReadable ('//./..///../Directory/ReadTest.php ' );
338
+ } catch (ValidatorException $ exception ) {
339
+ $ exceptions ++;
340
+ }
341
+ try {
342
+ $ dir ->isReadable ('\..\..\Directory\ReadTest.php ' );
343
+ } catch (ValidatorException $ exception ) {
344
+ $ exceptions ++;
345
+ }
346
+ $ this ->assertEquals (3 , $ exceptions );
347
+ }
348
+
185
349
/**
186
350
* Test for isFile method
187
351
*
@@ -194,6 +358,28 @@ public function testIsFile($path, $isFile)
194
358
$ this ->assertEquals ($ isFile , $ this ->getDirectoryInstance ('foo ' )->isFile ($ path ));
195
359
}
196
360
361
+ public function testIsFileOutside ()
362
+ {
363
+ $ exceptions = 0 ;
364
+ $ dir = $ this ->getDirectoryInstance ('foo ' );
365
+ try {
366
+ $ dir ->isFile ('../../Directory/ReadTest.php ' );
367
+ } catch (ValidatorException $ exception ) {
368
+ $ exceptions ++;
369
+ }
370
+ try {
371
+ $ dir ->isFile ('//./..///../Directory/ReadTest.php ' );
372
+ } catch (ValidatorException $ exception ) {
373
+ $ exceptions ++;
374
+ }
375
+ try {
376
+ $ dir ->isFile ('\..\..\Directory\ReadTest.php ' );
377
+ } catch (ValidatorException $ exception ) {
378
+ $ exceptions ++;
379
+ }
380
+ $ this ->assertEquals (3 , $ exceptions );
381
+ }
382
+
197
383
/**
198
384
* Test for isDirectory method
199
385
*
@@ -206,6 +392,28 @@ public function testIsDirectory($path, $isDirectory)
206
392
$ this ->assertEquals ($ isDirectory , $ this ->getDirectoryInstance ('foo ' )->isDirectory ($ path ));
207
393
}
208
394
395
+ public function testIsDirectoryOutside ()
396
+ {
397
+ $ exceptions = 0 ;
398
+ $ dir = $ this ->getDirectoryInstance ('foo ' );
399
+ try {
400
+ $ dir ->isDirectory ('../../Directory ' );
401
+ } catch (ValidatorException $ exception ) {
402
+ $ exceptions ++;
403
+ }
404
+ try {
405
+ $ dir ->isDirectory ('//./..///../Directory/ReadTest.php ' );
406
+ } catch (ValidatorException $ exception ) {
407
+ $ exceptions ++;
408
+ }
409
+ try {
410
+ $ dir ->isDirectory ('\..\..\Directory\ReadTest.php ' );
411
+ } catch (ValidatorException $ exception ) {
412
+ $ exceptions ++;
413
+ }
414
+ $ this ->assertEquals (3 , $ exceptions );
415
+ }
416
+
209
417
/**
210
418
* Data provider for testIsReadable
211
419
*
@@ -246,6 +454,28 @@ public function testOpenFile()
246
454
$ this ->assertTrue ($ file instanceof \Magento \Framework \Filesystem \File \ReadInterface);
247
455
}
248
456
457
+ public function testOpenFileOutside ()
458
+ {
459
+ $ exceptions = 0 ;
460
+ $ dir = $ this ->getDirectoryInstance ('foo ' );
461
+ try {
462
+ $ dir ->openFile ('../../Directory/ReadTest.php ' );
463
+ } catch (ValidatorException $ exception ) {
464
+ $ exceptions ++;
465
+ }
466
+ try {
467
+ $ dir ->openFile ('//./..///../Directory/ReadTest.php ' );
468
+ } catch (ValidatorException $ exception ) {
469
+ $ exceptions ++;
470
+ }
471
+ try {
472
+ $ dir ->openFile ('\..\..\Directory\ReadTest.php ' );
473
+ } catch (ValidatorException $ exception ) {
474
+ $ exceptions ++;
475
+ }
476
+ $ this ->assertEquals (3 , $ exceptions );
477
+ }
478
+
249
479
/**
250
480
* Test readFile
251
481
*
@@ -268,10 +498,35 @@ public function readFileProvider()
268
498
{
269
499
return [
270
500
['popup.csv ' , 'var myData = 5; ' ],
271
- ['data.csv ' , '"field1", "field2" ' . "\n" . '"field3", "field4" ' . "\n" ]
501
+ [
502
+ 'data.csv ' ,
503
+ '"field1", "field2" ' . PHP_EOL . '"field3", "field4" ' . PHP_EOL
504
+ ]
272
505
];
273
506
}
274
507
508
+ public function testReadFileOutside ()
509
+ {
510
+ $ exceptions = 0 ;
511
+ $ dir = $ this ->getDirectoryInstance ('foo ' );
512
+ try {
513
+ $ dir ->readFile ('../../Directory/ReadTest.php ' );
514
+ } catch (ValidatorException $ exception ) {
515
+ $ exceptions ++;
516
+ }
517
+ try {
518
+ $ dir ->readFile ('//./..///../Directory/ReadTest.php ' );
519
+ } catch (ValidatorException $ exception ) {
520
+ $ exceptions ++;
521
+ }
522
+ try {
523
+ $ dir ->readFile ('\..\..\Directory\ReadTest.php ' );
524
+ } catch (ValidatorException $ exception ) {
525
+ $ exceptions ++;
526
+ }
527
+ $ this ->assertEquals (3 , $ exceptions );
528
+ }
529
+
275
530
/**
276
531
* Get readable file instance
277
532
* Get full path for files located in _files directory
@@ -301,4 +556,26 @@ public function testReadRecursively()
301
556
sort ($ expected );
302
557
$ this ->assertEquals ($ expected , $ actual );
303
558
}
559
+
560
+ public function testReadRecursivelyOutside ()
561
+ {
562
+ $ exceptions = 0 ;
563
+ $ dir = $ this ->getDirectoryInstance ('foo ' );
564
+ try {
565
+ $ dir ->readRecursively ('../../Directory ' );
566
+ } catch (ValidatorException $ exception ) {
567
+ $ exceptions ++;
568
+ }
569
+ try {
570
+ $ dir ->readRecursively ('//./..///../Directory ' );
571
+ } catch (ValidatorException $ exception ) {
572
+ $ exceptions ++;
573
+ }
574
+ try {
575
+ $ dir ->readRecursively ('\..\..\Directory ' );
576
+ } catch (ValidatorException $ exception ) {
577
+ $ exceptions ++;
578
+ }
579
+ $ this ->assertEquals (3 , $ exceptions );
580
+ }
304
581
}
0 commit comments