* 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 */ 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 */ 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 */ 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 { // The normalizer joins components with the platform directory separator. $expectedPath = str_replace('/', \DIRECTORY_SEPARATOR, $expectedPath); static::assertSame($expectedPath, FilesUtil::normalizeZipPath($entryName)); } /** * @return \Generator */ 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']; } }