2
0

SymlinkTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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\Constants\ZipOptions;
  11. use PhpZip\Util\FilesUtil;
  12. use PhpZip\ZipFile;
  13. use Symfony\Component\Finder\Finder;
  14. /**
  15. * @internal
  16. *
  17. * @small
  18. */
  19. final class SymlinkTest extends ZipTestCase
  20. {
  21. /**
  22. * @dataProvider provideAllowSymlink
  23. *
  24. * @throws \Exception
  25. */
  26. public function testSymlink(bool $allowSymlink): void
  27. {
  28. self::skipTestForWindows();
  29. if (!is_dir($this->outputDirname)) {
  30. self::assertTrue(mkdir($this->outputDirname, 0755, true));
  31. }
  32. $contentsFile = random_bytes(100);
  33. $filePath = $this->outputDirname . '/file.bin';
  34. $symlinkPath = $this->outputDirname . '/symlink.bin';
  35. $symlinkTarget = basename($filePath);
  36. self::assertNotFalse(file_put_contents($filePath, $contentsFile));
  37. self::assertTrue(symlink($symlinkTarget, $symlinkPath));
  38. $finder = (new Finder())->in($this->outputDirname);
  39. $zipFile = new ZipFile();
  40. $zipFile->addFromFinder($finder);
  41. $zipFile->saveAsFile($this->outputFilename);
  42. $zipFile->close();
  43. self::assertCorrectZipArchive($this->outputFilename);
  44. FilesUtil::removeDir($this->outputDirname);
  45. self::assertDirectoryDoesNotExist($this->outputDirname);
  46. self::assertTrue(mkdir($this->outputDirname, 0755, true));
  47. $zipFile->openFile($this->outputFilename);
  48. $zipFile->extractTo($this->outputDirname, null, [
  49. ZipOptions::EXTRACT_SYMLINKS => $allowSymlink,
  50. ]);
  51. $zipFile->close();
  52. $splFileInfo = new \SplFileInfo($symlinkPath);
  53. if ($allowSymlink) {
  54. self::assertTrue($splFileInfo->isLink());
  55. self::assertSame($splFileInfo->getLinkTarget(), $symlinkTarget);
  56. } else {
  57. self::assertFalse($splFileInfo->isLink());
  58. self::assertStringEqualsFile($symlinkPath, $symlinkTarget);
  59. }
  60. }
  61. public function provideAllowSymlink(): \Generator
  62. {
  63. yield 'allow' => [true];
  64. yield 'deny' => [false];
  65. }
  66. /**
  67. * A symlink entry whose target resolves outside the extraction directory
  68. * must be skipped instead of created. See issue #101.
  69. *
  70. * @throws \Exception
  71. */
  72. public function testExtractSkipsSymlinkTargetOutsideRoot(): void
  73. {
  74. self::skipTestForWindows();
  75. $root = $this->outputDirname;
  76. $payloadDirname = $root . '/payload';
  77. $dest = $root . '/extract-root/a/b';
  78. self::assertTrue(mkdir($payloadDirname, 0755, true));
  79. self::assertTrue(mkdir($dest, 0755, true));
  80. self::assertNotFalse(file_put_contents($root . '/outside.txt', 'outside'));
  81. // When extracted into $dest, "link" resolves to
  82. // "extract-root/a/outside.txt", which is outside $dest.
  83. self::assertTrue(symlink('../outside.txt', $payloadDirname . '/link'));
  84. $zipFile = new ZipFile();
  85. $zipFile->addFromFinder((new Finder())->in($payloadDirname));
  86. $zipFile->saveAsFile($this->outputFilename);
  87. $zipFile->close();
  88. $zipFile->openFile($this->outputFilename);
  89. $zipFile->extractTo($dest, null, [
  90. ZipOptions::EXTRACT_SYMLINKS => true,
  91. ], $extractedEntries);
  92. $zipFile->close();
  93. self::assertArrayNotHasKey($dest . '/link', $extractedEntries);
  94. self::assertFalse(is_link($dest . '/link'));
  95. self::assertFileDoesNotExist($root . '/extract-root/a/outside.txt');
  96. }
  97. /**
  98. * A symlink entry with an absolute target must be skipped.
  99. * See issue #101.
  100. *
  101. * @throws \Exception
  102. */
  103. public function testExtractSkipsSymlinkWithAbsoluteTarget(): void
  104. {
  105. self::skipTestForWindows();
  106. $root = $this->outputDirname;
  107. $payloadDirname = $root . '/payload';
  108. $dest = $root . '/extract-root';
  109. self::assertTrue(mkdir($payloadDirname, 0755, true));
  110. self::assertTrue(mkdir($dest, 0755, true));
  111. self::assertNotFalse(file_put_contents($payloadDirname . '/file.txt', 'contents'));
  112. self::assertTrue(symlink($payloadDirname . '/file.txt', $payloadDirname . '/abslink'));
  113. $zipFile = new ZipFile();
  114. $zipFile->addFromFinder((new Finder())->in($payloadDirname));
  115. $zipFile->saveAsFile($this->outputFilename);
  116. $zipFile->close();
  117. $zipFile->openFile($this->outputFilename);
  118. $zipFile->extractTo($dest, null, [
  119. ZipOptions::EXTRACT_SYMLINKS => true,
  120. ], $extractedEntries);
  121. $zipFile->close();
  122. self::assertArrayNotHasKey($dest . '/abslink', $extractedEntries);
  123. self::assertFalse(is_link($dest . '/abslink'));
  124. }
  125. /**
  126. * A symlink entry whose target resolves inside the extraction directory
  127. * is still created. See issue #101.
  128. *
  129. * @throws \Exception
  130. */
  131. public function testExtractCreatesSymlinkTargetInsideRoot(): void
  132. {
  133. self::skipTestForWindows();
  134. $root = $this->outputDirname;
  135. $payloadDirname = $root . '/payload';
  136. $dest = $root . '/extract-root';
  137. self::assertTrue(mkdir($payloadDirname . '/sub', 0755, true));
  138. self::assertTrue(mkdir($dest, 0755, true));
  139. self::assertNotFalse(file_put_contents($payloadDirname . '/sub/file.txt', 'contents'));
  140. // "sub/link" resolves to "extract-root/sub/file.txt" - inside $dest.
  141. self::assertTrue(symlink('file.txt', $payloadDirname . '/sub/link'));
  142. $zipFile = new ZipFile();
  143. $zipFile->addFromFinder((new Finder())->in($payloadDirname));
  144. $zipFile->saveAsFile($this->outputFilename);
  145. $zipFile->close();
  146. $zipFile->openFile($this->outputFilename);
  147. $zipFile->extractTo($dest, null, [
  148. ZipOptions::EXTRACT_SYMLINKS => true,
  149. ], $extractedEntries);
  150. $zipFile->close();
  151. self::assertArrayHasKey($dest . '/sub/link', $extractedEntries);
  152. self::assertTrue(is_link($dest . '/sub/link'));
  153. self::assertSame('file.txt', (string) (new \SplFileInfo($dest . '/sub/link'))->getLinkTarget());
  154. }
  155. }