Skip to content

Commit f3ad691

Browse files
committed
Reject paths with funky whitespace.
1 parent 1ac14e9 commit f3ad691

File tree

4 files changed

+46
-11
lines changed

4 files changed

+46
-11
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.php_cs.cache
2+
.phpunit.result.cache
23
php-cs-fixer
34
bin
45
composer.lock

src/CorruptedPathDetected.php

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace League\Flysystem;
4+
5+
use LogicException;
6+
7+
class CorruptedPathDetected extends LogicException implements FilesystemException
8+
{
9+
/**
10+
* @param string $path
11+
* @return CorruptedPathDetected
12+
*/
13+
public static function forPath($path)
14+
{
15+
return new CorruptedPathDetected("Corrupted path detected: " . $path);
16+
}
17+
}

src/Util.php

+10-9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use League\Flysystem\Util\MimeType;
66
use LogicException;
77

8+
use function strcmp;
9+
810
class Util
911
{
1012
/**
@@ -102,8 +104,7 @@ public static function normalizePath($path)
102104
public static function normalizeRelativePath($path)
103105
{
104106
$path = str_replace('\\', '/', $path);
105-
$path = static::removeFunkyWhiteSpace($path);
106-
107+
$path = static::removeFunkyWhiteSpace($path);
107108
$parts = [];
108109

109110
foreach (explode('/', $path) as $part) {
@@ -127,22 +128,22 @@ public static function normalizeRelativePath($path)
127128
}
128129
}
129130

130-
return implode('/', $parts);
131+
$path = implode('/', $parts);
132+
133+
return $path;
131134
}
132135

133136
/**
134-
* Removes unprintable characters and invalid unicode characters.
137+
* Rejects unprintable characters and invalid unicode characters.
135138
*
136139
* @param string $path
137140
*
138141
* @return string $path
139142
*/
140143
protected static function removeFunkyWhiteSpace($path)
141144
{
142-
// We do this check in a loop, since removing invalid unicode characters
143-
// can lead to new characters being created.
144-
while (preg_match('#\p{C}+|^\./#u', $path)) {
145-
$path = preg_replace('#\p{C}+|^\./#u', '', $path);
145+
if (preg_match('#\p{C}+#u', $path)) {
146+
throw CorruptedPathDetected::forPath($path);
146147
}
147148

148149
return $path;
@@ -205,7 +206,7 @@ public static function emulateDirectories(array $listing)
205206
$listedDirectories = [];
206207

207208
foreach ($listing as $object) {
208-
list($directories, $listedDirectories) = static::emulateObjectDirectories($object, $directories, $listedDirectories);
209+
[$directories, $listedDirectories] = static::emulateObjectDirectories($object, $directories, $listedDirectories);
209210
}
210211

211212
$directories = array_diff(array_unique($directories), array_unique($listedDirectories));

tests/UtilTests.php

+18-2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,23 @@ public function testContentSize()
3535
$this->assertEquals(3, Util::contentSize('135'));
3636
}
3737

38+
/**
39+
* @dataProvider dbCorruptedPath
40+
*/
41+
public function testRejectingPathWithFunkyWhitespace($path)
42+
{
43+
$this->expectException(CorruptedPathDetected::class);
44+
Util::normalizePath($path);
45+
}
46+
47+
/**
48+
* @return array
49+
*/
50+
public function dbCorruptedPath()
51+
{
52+
return [["some\0/path.txt"], ["s\x09i.php"]];
53+
}
54+
3855
public function mapProvider()
3956
{
4057
return [
@@ -95,7 +112,7 @@ public function invalidPathProvider()
95112
}
96113

97114
/**
98-
* @dataProvider invalidPathProvider
115+
* @dataProvider invalidPathProvider
99116
*/
100117
public function testOutsideRootPath($path)
101118
{
@@ -125,7 +142,6 @@ public function pathProvider()
125142
['example/path/..txt', 'example/path/..txt'],
126143
['\\example\\path.txt', 'example/path.txt'],
127144
['\\example\\..\\path.txt', 'path.txt'],
128-
["some\0/path.txt", 'some/path.txt'],
129145
];
130146
}
131147

0 commit comments

Comments
 (0)