Skip to content

Commit

Permalink
Reject funky whitspace paths.
Browse files Browse the repository at this point in the history
  • Loading branch information
frankdejonge committed Jun 23, 2021
1 parent 27ba26d commit a3c694d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
13 changes: 13 additions & 0 deletions src/CorruptedPathDetected.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace League\Flysystem;

use RuntimeException;

final class CorruptedPathDetected extends RuntimeException implements FilesystemException
{
public static function forPath(string $path): CorruptedPathDetected
{
return new CorruptedPathDetected("Corrupted path detected: " . $path);
}
}
13 changes: 4 additions & 9 deletions src/WhitespacePathNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,16 @@ class WhitespacePathNormalizer implements PathNormalizer
public function normalizePath(string $path): string
{
$path = str_replace('\\', '/', $path);
$path = $this->removeFunkyWhiteSpace($path);
$this->rejectFunkyWhiteSpace($path);

return $this->normalizeRelativePath($path);
}

private function removeFunkyWhiteSpace(string $path): string
private function rejectFunkyWhiteSpace(string $path): void
{
// Remove unprintable characters and invalid unicode characters.
// 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 = (string) preg_replace('#\p{C}+|^\./#u', '', $path);
if (preg_match('#\p{C}+#u', $path)) {
throw CorruptedPathDetected::forPath($path);
}

return $path;
}

private function normalizeRelativePath(string $path): string
Expand Down
16 changes: 15 additions & 1 deletion src/WhitespacePathNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public function pathProvider(): array
['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 All @@ -69,6 +68,21 @@ public function guarding_against_path_traversal(string $input): void
$this->normalizer->normalizePath($input);
}

/**
* @test
* @dataProvider dpFunkyWhitespacePaths
*/
public function rejecting_funky_whitespace(string $path): void
{
self::expectException(CorruptedPathDetected::class);
$this->normalizer->normalizePath($path);
}

public function dpFunkyWhitespacePaths(): iterable
{
return [["some\0/path.txt"], ["s\x09i.php"]];
}

/**
* @return array<array<string>>
*/
Expand Down

0 comments on commit a3c694d

Please sign in to comment.