Skip to content

Commit

Permalink
Reject paths with funky whitespace.
Browse files Browse the repository at this point in the history
  • Loading branch information
frankdejonge committed Jun 23, 2021
1 parent 1ac14e9 commit f3ad691
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.php_cs.cache
.phpunit.result.cache
php-cs-fixer
bin
composer.lock
Expand Down
17 changes: 17 additions & 0 deletions src/CorruptedPathDetected.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace League\Flysystem;

use LogicException;

class CorruptedPathDetected extends LogicException implements FilesystemException
{
/**
* @param string $path
* @return CorruptedPathDetected
*/
public static function forPath($path)
{
return new CorruptedPathDetected("Corrupted path detected: " . $path);
}
}
19 changes: 10 additions & 9 deletions src/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use League\Flysystem\Util\MimeType;
use LogicException;

use function strcmp;

class Util
{
/**
Expand Down Expand Up @@ -102,8 +104,7 @@ public static function normalizePath($path)
public static function normalizeRelativePath($path)
{
$path = str_replace('\\', '/', $path);
$path = static::removeFunkyWhiteSpace($path);

$path = static::removeFunkyWhiteSpace($path);
$parts = [];

foreach (explode('/', $path) as $part) {
Expand All @@ -127,22 +128,22 @@ public static function normalizeRelativePath($path)
}
}

return implode('/', $parts);
$path = implode('/', $parts);

return $path;
}

/**
* Removes unprintable characters and invalid unicode characters.
* Rejects unprintable characters and invalid unicode characters.
*
* @param string $path
*
* @return string $path
*/
protected static function removeFunkyWhiteSpace($path)
{
// We do this check in a loop, since removing invalid unicode characters
// can lead to new characters being created.
while (preg_match('#\p{C}+|^\./#u', $path)) {
$path = preg_replace('#\p{C}+|^\./#u', '', $path);
if (preg_match('#\p{C}+#u', $path)) {
throw CorruptedPathDetected::forPath($path);
}

return $path;
Expand Down Expand Up @@ -205,7 +206,7 @@ public static function emulateDirectories(array $listing)
$listedDirectories = [];

foreach ($listing as $object) {
list($directories, $listedDirectories) = static::emulateObjectDirectories($object, $directories, $listedDirectories);
[$directories, $listedDirectories] = static::emulateObjectDirectories($object, $directories, $listedDirectories);
}

$directories = array_diff(array_unique($directories), array_unique($listedDirectories));
Expand Down
20 changes: 18 additions & 2 deletions tests/UtilTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,23 @@ public function testContentSize()
$this->assertEquals(3, Util::contentSize('135'));
}

/**
* @dataProvider dbCorruptedPath
*/
public function testRejectingPathWithFunkyWhitespace($path)
{
$this->expectException(CorruptedPathDetected::class);
Util::normalizePath($path);
}

/**
* @return array
*/
public function dbCorruptedPath()
{
return [["some\0/path.txt"], ["s\x09i.php"]];
}

public function mapProvider()
{
return [
Expand Down Expand Up @@ -95,7 +112,7 @@ public function invalidPathProvider()
}

/**
* @dataProvider invalidPathProvider
* @dataProvider invalidPathProvider
*/
public function testOutsideRootPath($path)
{
Expand Down Expand Up @@ -125,7 +142,6 @@ public function pathProvider()
['example/path/..txt', 'example/path/..txt'],
['\\example\\path.txt', 'example/path.txt'],
['\\example\\..\\path.txt', 'path.txt'],
["some\0/path.txt", 'some/path.txt'],
];
}

Expand Down

2 comments on commit f3ad691

@EduemLibrarian1
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

obrigada pelo retorno

@EduemLibrarian1
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you

Please sign in to comment.