|
|
@@ -12,6 +12,7 @@ declare(strict_types=1);
|
|
|
namespace PhpZip\Tests;
|
|
|
|
|
|
use PhpZip\Exception\ZipException;
|
|
|
+use PhpZip\Util\FilesUtil;
|
|
|
use PhpZip\ZipFile;
|
|
|
|
|
|
/**
|
|
|
@@ -53,4 +54,114 @@ class ZipSlipVulnerabilityTest extends ZipTestCase
|
|
|
$expectedExtractedFile = $this->outputDirname . '/dir/file.txt';
|
|
|
static::assertTrue(is_file($expectedExtractedFile));
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Backslash traversal entry names must not escape the extraction
|
|
|
+ * directory, on any platform. See issue #100.
|
|
|
+ *
|
|
|
+ * @dataProvider provideBackslashTraversalEntryNames
|
|
|
+ *
|
|
|
+ * @throws ZipException
|
|
|
+ */
|
|
|
+ public function testUnpackBackslashTraversalIsSanitized(string $entryName, string $expectedEntryPath): void
|
|
|
+ {
|
|
|
+ $root = $this->outputDirname . '/root';
|
|
|
+ $dest = $root . '/a/b';
|
|
|
+ static::assertTrue(mkdir($dest, 0755, true));
|
|
|
+
|
|
|
+ $zipFile = new ZipFile();
|
|
|
+ $zipFile->addFromString($entryName, 'contents');
|
|
|
+ $zipFile->extractTo($dest);
|
|
|
+ $zipFile->close();
|
|
|
+
|
|
|
+ static::assertFileExists($dest . '/' . $expectedEntryPath);
|
|
|
+ // Nothing may be written outside the extraction directory.
|
|
|
+ static::assertFileDoesNotExist($root . '/evil.txt');
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return \Generator<string, array{string, string}>
|
|
|
+ */
|
|
|
+ public function provideBackslashTraversalEntryNames(): \Generator
|
|
|
+ {
|
|
|
+ yield 'plain backslash traversal' => ['..\\..\\evil.txt', 'evil.txt'];
|
|
|
+ yield 'mixed separators traversal' => ['safe/..\\..\\evil.txt', 'safe/evil.txt'];
|
|
|
+ yield 'backslash dot segments' => ['.\\..\\evil.txt', 'evil.txt'];
|
|
|
+ yield 'backslash as directory separator' => ['dir\\sub\\evil.txt', 'dir/sub/evil.txt'];
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Drive-relative and stream-wrapper entry names are rejected outright.
|
|
|
+ * See issue #100.
|
|
|
+ *
|
|
|
+ * @dataProvider provideUnsafeEntryNames
|
|
|
+ *
|
|
|
+ * @throws ZipException
|
|
|
+ */
|
|
|
+ public function testUnpackUnsafeEntryNameThrows(string $entryName): void
|
|
|
+ {
|
|
|
+ static::assertTrue(mkdir($this->outputDirname, 0755, true));
|
|
|
+
|
|
|
+ $zipFile = new ZipFile();
|
|
|
+ $zipFile->addFromString($entryName, 'contents');
|
|
|
+
|
|
|
+ $this->expectException(ZipException::class);
|
|
|
+ $this->expectExceptionMessage('Invalid zip entry name');
|
|
|
+ $zipFile->extractTo($this->outputDirname);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return \Generator<string, array{string}>
|
|
|
+ */
|
|
|
+ public function provideUnsafeEntryNames(): \Generator
|
|
|
+ {
|
|
|
+ yield 'drive absolute path' => ['C:\\evil.txt'];
|
|
|
+ yield 'drive relative path' => ['C:evil.txt'];
|
|
|
+ yield 'stream wrapper path' => ['phar://evil.phar/evil.txt'];
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Unsafe entry names are rejected directly, including names that cannot
|
|
|
+ * pass through addFromString because it trims leading separators
|
|
|
+ * (UNC paths). See issue #100.
|
|
|
+ *
|
|
|
+ * @dataProvider provideUnsafeEntryNamesForNormalizer
|
|
|
+ */
|
|
|
+ public function testNormalizeZipPathRejectsUnsafeNames(string $entryName): void
|
|
|
+ {
|
|
|
+ $this->expectException(ZipException::class);
|
|
|
+ $this->expectExceptionMessage('Invalid zip entry name');
|
|
|
+ FilesUtil::normalizeZipPath($entryName);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return \Generator<string, array{string}>
|
|
|
+ */
|
|
|
+ public function provideUnsafeEntryNamesForNormalizer(): \Generator
|
|
|
+ {
|
|
|
+ yield 'unc path' => ['\\\\server\\share\\evil.txt'];
|
|
|
+ yield 'nul byte' => ["evil\0.txt"];
|
|
|
+ yield 'drive absolute path' => ['C:\\evil.txt'];
|
|
|
+ yield 'stream wrapper path' => ['phar://evil.phar/evil.txt'];
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @dataProvider provideSanitizedEntryNames
|
|
|
+ */
|
|
|
+ public function testNormalizeZipPathSanitizes(string $entryName, string $expectedPath): void
|
|
|
+ {
|
|
|
+ static::assertSame($expectedPath, FilesUtil::normalizeZipPath($entryName));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return \Generator<string, array{string, string}>
|
|
|
+ */
|
|
|
+ public function provideSanitizedEntryNames(): \Generator
|
|
|
+ {
|
|
|
+ yield 'unix traversal' => ['../dir/./../../file.txt', 'dir/file.txt'];
|
|
|
+ yield 'backslash traversal' => ['..\\..\\evil.txt', 'evil.txt'];
|
|
|
+ yield 'mixed separators' => ['safe/..\\..\\evil.txt', 'safe/evil.txt'];
|
|
|
+ yield 'double slashes kept for BC' => ['a//b.txt', 'a//b.txt'];
|
|
|
+ yield 'plain name' => ['file.txt', 'file.txt'];
|
|
|
+ }
|
|
|
}
|