ZipSlipVulnerabilityTest.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace PhpZip\Tests;
  3. use PhpZip\Exception\ZipException;
  4. use PhpZip\ZipFile;
  5. /**
  6. * Class ZipSlipVulnerabilityTest.
  7. *
  8. * @see https://github.com/Ne-Lexa/php-zip/issues/39 Issue#31
  9. * @see https://snyk.io/research/zip-slip-vulnerability Zip Slip Vulnerability
  10. *
  11. * @internal
  12. *
  13. * @small
  14. */
  15. class ZipSlipVulnerabilityTest extends ZipTestCase
  16. {
  17. /**
  18. * @throws ZipException
  19. */
  20. public function testCreateSlipVulnerabilityFile()
  21. {
  22. $localFile = '../dir/./../../file.txt';
  23. $zipFile = new ZipFile();
  24. $zipFile->addFromString($localFile, 'contents');
  25. static::assertContains($localFile, $zipFile->getListFiles());
  26. $zipFile->close();
  27. }
  28. /**
  29. * @throws ZipException
  30. */
  31. public function testUnpack()
  32. {
  33. static::assertTrue(mkdir($this->outputDirname, 0755, true));
  34. $zipFile = new ZipFile();
  35. $zipFile->addFromString('../dir/./../../file.txt', 'contents');
  36. $zipFile->extractTo($this->outputDirname);
  37. $zipFile->close();
  38. $expectedExtractedFile = $this->outputDirname . '/dir/file.txt';
  39. static::assertTrue(is_file($expectedExtractedFile));
  40. }
  41. }