| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- <?php
- declare(strict_types=1);
- /*
- * This file is part of the nelexa/zip package.
- * (c) Ne-Lexa <https://github.com/Ne-Lexa/php-zip>
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace PhpZip\Tests;
- use PhpZip\Exception\ZipException;
- use PhpZip\Util\FilesUtil;
- use PhpZip\ZipFile;
- /**
- * Class ZipSlipVulnerabilityTest.
- *
- * @see https://github.com/Ne-Lexa/php-zip/issues/39 Issue#31
- * @see https://snyk.io/research/zip-slip-vulnerability Zip Slip Vulnerability
- *
- * @internal
- *
- * @small
- */
- class ZipSlipVulnerabilityTest extends ZipTestCase
- {
- /**
- * @throws ZipException
- */
- public function testCreateSlipVulnerabilityFile(): void
- {
- $localFile = '../dir/./../../file.txt';
- $zipFile = new ZipFile();
- $zipFile->addFromString($localFile, 'contents');
- static::assertContains($localFile, $zipFile->getListFiles());
- $zipFile->close();
- }
- /**
- * @throws ZipException
- */
- public function testUnpack(): void
- {
- static::assertTrue(mkdir($this->outputDirname, 0755, true));
- $zipFile = new ZipFile();
- $zipFile->addFromString('../dir/./../../file.txt', 'contents');
- $zipFile->extractTo($this->outputDirname);
- $zipFile->close();
- $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'];
- }
- }
|