2
0

ZipSlipVulnerabilityTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of the nelexa/zip package.
  5. * (c) Ne-Lexa <https://github.com/Ne-Lexa/php-zip>
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. namespace PhpZip\Tests;
  10. use PhpZip\Exception\ZipException;
  11. use PhpZip\Util\FilesUtil;
  12. use PhpZip\ZipFile;
  13. /**
  14. * Class ZipSlipVulnerabilityTest.
  15. *
  16. * @see https://github.com/Ne-Lexa/php-zip/issues/39 Issue#31
  17. * @see https://snyk.io/research/zip-slip-vulnerability Zip Slip Vulnerability
  18. *
  19. * @internal
  20. *
  21. * @small
  22. */
  23. class ZipSlipVulnerabilityTest extends ZipTestCase
  24. {
  25. /**
  26. * @throws ZipException
  27. */
  28. public function testCreateSlipVulnerabilityFile(): void
  29. {
  30. $localFile = '../dir/./../../file.txt';
  31. $zipFile = new ZipFile();
  32. $zipFile->addFromString($localFile, 'contents');
  33. static::assertContains($localFile, $zipFile->getListFiles());
  34. $zipFile->close();
  35. }
  36. /**
  37. * @throws ZipException
  38. */
  39. public function testUnpack(): void
  40. {
  41. static::assertTrue(mkdir($this->outputDirname, 0755, true));
  42. $zipFile = new ZipFile();
  43. $zipFile->addFromString('../dir/./../../file.txt', 'contents');
  44. $zipFile->extractTo($this->outputDirname);
  45. $zipFile->close();
  46. $expectedExtractedFile = $this->outputDirname . '/dir/file.txt';
  47. static::assertTrue(is_file($expectedExtractedFile));
  48. }
  49. /**
  50. * Backslash traversal entry names must not escape the extraction
  51. * directory, on any platform. See issue #100.
  52. *
  53. * @dataProvider provideBackslashTraversalEntryNames
  54. *
  55. * @throws ZipException
  56. */
  57. public function testUnpackBackslashTraversalIsSanitized(string $entryName, string $expectedEntryPath): void
  58. {
  59. $root = $this->outputDirname . '/root';
  60. $dest = $root . '/a/b';
  61. static::assertTrue(mkdir($dest, 0755, true));
  62. $zipFile = new ZipFile();
  63. $zipFile->addFromString($entryName, 'contents');
  64. $zipFile->extractTo($dest);
  65. $zipFile->close();
  66. static::assertFileExists($dest . '/' . $expectedEntryPath);
  67. // Nothing may be written outside the extraction directory.
  68. static::assertFileDoesNotExist($root . '/evil.txt');
  69. }
  70. /**
  71. * @return \Generator<string, array{string, string}>
  72. */
  73. public function provideBackslashTraversalEntryNames(): \Generator
  74. {
  75. yield 'plain backslash traversal' => ['..\\..\\evil.txt', 'evil.txt'];
  76. yield 'mixed separators traversal' => ['safe/..\\..\\evil.txt', 'safe/evil.txt'];
  77. yield 'backslash dot segments' => ['.\\..\\evil.txt', 'evil.txt'];
  78. yield 'backslash as directory separator' => ['dir\\sub\\evil.txt', 'dir/sub/evil.txt'];
  79. }
  80. /**
  81. * Drive-relative and stream-wrapper entry names are rejected outright.
  82. * See issue #100.
  83. *
  84. * @dataProvider provideUnsafeEntryNames
  85. *
  86. * @throws ZipException
  87. */
  88. public function testUnpackUnsafeEntryNameThrows(string $entryName): void
  89. {
  90. static::assertTrue(mkdir($this->outputDirname, 0755, true));
  91. $zipFile = new ZipFile();
  92. $zipFile->addFromString($entryName, 'contents');
  93. $this->expectException(ZipException::class);
  94. $this->expectExceptionMessage('Invalid zip entry name');
  95. $zipFile->extractTo($this->outputDirname);
  96. }
  97. /**
  98. * @return \Generator<string, array{string}>
  99. */
  100. public function provideUnsafeEntryNames(): \Generator
  101. {
  102. yield 'drive absolute path' => ['C:\\evil.txt'];
  103. yield 'drive relative path' => ['C:evil.txt'];
  104. yield 'stream wrapper path' => ['phar://evil.phar/evil.txt'];
  105. }
  106. /**
  107. * Unsafe entry names are rejected directly, including names that cannot
  108. * pass through addFromString because it trims leading separators
  109. * (UNC paths). See issue #100.
  110. *
  111. * @dataProvider provideUnsafeEntryNamesForNormalizer
  112. */
  113. public function testNormalizeZipPathRejectsUnsafeNames(string $entryName): void
  114. {
  115. $this->expectException(ZipException::class);
  116. $this->expectExceptionMessage('Invalid zip entry name');
  117. FilesUtil::normalizeZipPath($entryName);
  118. }
  119. /**
  120. * @return \Generator<string, array{string}>
  121. */
  122. public function provideUnsafeEntryNamesForNormalizer(): \Generator
  123. {
  124. yield 'unc path' => ['\\\\server\\share\\evil.txt'];
  125. yield 'nul byte' => ["evil\0.txt"];
  126. yield 'drive absolute path' => ['C:\\evil.txt'];
  127. yield 'stream wrapper path' => ['phar://evil.phar/evil.txt'];
  128. }
  129. /**
  130. * @dataProvider provideSanitizedEntryNames
  131. */
  132. public function testNormalizeZipPathSanitizes(string $entryName, string $expectedPath): void
  133. {
  134. static::assertSame($expectedPath, FilesUtil::normalizeZipPath($entryName));
  135. }
  136. /**
  137. * @return \Generator<string, array{string, string}>
  138. */
  139. public function provideSanitizedEntryNames(): \Generator
  140. {
  141. yield 'unix traversal' => ['../dir/./../../file.txt', 'dir/file.txt'];
  142. yield 'backslash traversal' => ['..\\..\\evil.txt', 'evil.txt'];
  143. yield 'mixed separators' => ['safe/..\\..\\evil.txt', 'safe/evil.txt'];
  144. yield 'double slashes kept for BC' => ['a//b.txt', 'a//b.txt'];
  145. yield 'plain name' => ['file.txt', 'file.txt'];
  146. }
  147. }