| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- <?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\Constants\ZipOptions;
- use PhpZip\Util\FilesUtil;
- use PhpZip\ZipFile;
- use Symfony\Component\Finder\Finder;
- /**
- * @internal
- *
- * @small
- */
- final class SymlinkTest extends ZipTestCase
- {
- /**
- * @dataProvider provideAllowSymlink
- *
- * @throws \Exception
- */
- public function testSymlink(bool $allowSymlink): void
- {
- self::skipTestForWindows();
- if (!is_dir($this->outputDirname)) {
- self::assertTrue(mkdir($this->outputDirname, 0755, true));
- }
- $contentsFile = random_bytes(100);
- $filePath = $this->outputDirname . '/file.bin';
- $symlinkPath = $this->outputDirname . '/symlink.bin';
- $symlinkTarget = basename($filePath);
- self::assertNotFalse(file_put_contents($filePath, $contentsFile));
- self::assertTrue(symlink($symlinkTarget, $symlinkPath));
- $finder = (new Finder())->in($this->outputDirname);
- $zipFile = new ZipFile();
- $zipFile->addFromFinder($finder);
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- self::assertCorrectZipArchive($this->outputFilename);
- FilesUtil::removeDir($this->outputDirname);
- self::assertDirectoryDoesNotExist($this->outputDirname);
- self::assertTrue(mkdir($this->outputDirname, 0755, true));
- $zipFile->openFile($this->outputFilename);
- $zipFile->extractTo($this->outputDirname, null, [
- ZipOptions::EXTRACT_SYMLINKS => $allowSymlink,
- ]);
- $zipFile->close();
- $splFileInfo = new \SplFileInfo($symlinkPath);
- if ($allowSymlink) {
- self::assertTrue($splFileInfo->isLink());
- self::assertSame($splFileInfo->getLinkTarget(), $symlinkTarget);
- } else {
- self::assertFalse($splFileInfo->isLink());
- self::assertStringEqualsFile($symlinkPath, $symlinkTarget);
- }
- }
- public function provideAllowSymlink(): \Generator
- {
- yield 'allow' => [true];
- yield 'deny' => [false];
- }
- /**
- * A symlink entry whose target resolves outside the extraction directory
- * must be skipped instead of created. See issue #101.
- *
- * @throws \Exception
- */
- public function testExtractSkipsSymlinkTargetOutsideRoot(): void
- {
- self::skipTestForWindows();
- $root = $this->outputDirname;
- $payloadDirname = $root . '/payload';
- $dest = $root . '/extract-root/a/b';
- self::assertTrue(mkdir($payloadDirname, 0755, true));
- self::assertTrue(mkdir($dest, 0755, true));
- self::assertNotFalse(file_put_contents($root . '/outside.txt', 'outside'));
- // When extracted into $dest, "link" resolves to
- // "extract-root/a/outside.txt", which is outside $dest.
- self::assertTrue(symlink('../outside.txt', $payloadDirname . '/link'));
- $zipFile = new ZipFile();
- $zipFile->addFromFinder((new Finder())->in($payloadDirname));
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- $zipFile->openFile($this->outputFilename);
- $zipFile->extractTo($dest, null, [
- ZipOptions::EXTRACT_SYMLINKS => true,
- ], $extractedEntries);
- $zipFile->close();
- self::assertArrayNotHasKey($dest . '/link', $extractedEntries);
- self::assertFalse(is_link($dest . '/link'));
- self::assertFileDoesNotExist($root . '/extract-root/a/outside.txt');
- }
- /**
- * A symlink entry with an absolute target must be skipped.
- * See issue #101.
- *
- * @throws \Exception
- */
- public function testExtractSkipsSymlinkWithAbsoluteTarget(): void
- {
- self::skipTestForWindows();
- $root = $this->outputDirname;
- $payloadDirname = $root . '/payload';
- $dest = $root . '/extract-root';
- self::assertTrue(mkdir($payloadDirname, 0755, true));
- self::assertTrue(mkdir($dest, 0755, true));
- self::assertNotFalse(file_put_contents($payloadDirname . '/file.txt', 'contents'));
- self::assertTrue(symlink($payloadDirname . '/file.txt', $payloadDirname . '/abslink'));
- $zipFile = new ZipFile();
- $zipFile->addFromFinder((new Finder())->in($payloadDirname));
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- $zipFile->openFile($this->outputFilename);
- $zipFile->extractTo($dest, null, [
- ZipOptions::EXTRACT_SYMLINKS => true,
- ], $extractedEntries);
- $zipFile->close();
- self::assertArrayNotHasKey($dest . '/abslink', $extractedEntries);
- self::assertFalse(is_link($dest . '/abslink'));
- }
- /**
- * A symlink entry whose target resolves inside the extraction directory
- * is still created. See issue #101.
- *
- * @throws \Exception
- */
- public function testExtractCreatesSymlinkTargetInsideRoot(): void
- {
- self::skipTestForWindows();
- $root = $this->outputDirname;
- $payloadDirname = $root . '/payload';
- $dest = $root . '/extract-root';
- self::assertTrue(mkdir($payloadDirname . '/sub', 0755, true));
- self::assertTrue(mkdir($dest, 0755, true));
- self::assertNotFalse(file_put_contents($payloadDirname . '/sub/file.txt', 'contents'));
- // "sub/link" resolves to "extract-root/sub/file.txt" - inside $dest.
- self::assertTrue(symlink('file.txt', $payloadDirname . '/sub/link'));
- $zipFile = new ZipFile();
- $zipFile->addFromFinder((new Finder())->in($payloadDirname));
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- $zipFile->openFile($this->outputFilename);
- $zipFile->extractTo($dest, null, [
- ZipOptions::EXTRACT_SYMLINKS => true,
- ], $extractedEntries);
- $zipFile->close();
- self::assertArrayHasKey($dest . '/sub/link', $extractedEntries);
- self::assertTrue(is_link($dest . '/sub/link'));
- self::assertSame('file.txt', (string) (new \SplFileInfo($dest . '/sub/link'))->getLinkTarget());
- }
- }
|