Skip to content

Commit

Permalink
Test symlinks
Browse files Browse the repository at this point in the history
  • Loading branch information
reimic committed Mar 28, 2024
1 parent ee692ff commit 09054fa
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
7 changes: 7 additions & 0 deletions src/WordPress/Zip/ZipException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace WordPress\Zip;

use Exception;

class ZipException extends Exception {}
8 changes: 7 additions & 1 deletion src/WordPress/Zip/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@ function zip_extract_to( $fp, $to_path ) {
continue;
}

// prevent zip slip -> using relative path to access otherwise inaccessible files
if ( false !== strpos( $entry->path ,'..') ) {
continue;
throw new ZipException("Relative paths in zips are not allowed.");
}

// prevent zip with symlinks -> using a symbolic link to access otherwise inaccessible files
if ( is_link( $entry->path ) ) {
throw new ZipException("Semantic links in zips are not allowed.");
}

$path = Path::canonicalize( $to_path . '/' . $entry->path );
Expand Down
18 changes: 13 additions & 5 deletions tests/unit/zip/ZipFunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,25 @@
namespace unit\zip;

use PHPUnitTestCase;
use Symfony\Component\Filesystem\Path;
use WordPress\Zip\ZipException;
use function WordPress\Zip\zip_extract_to;

class ZipFunctionsTest extends PHPUnitTestCase {
public function testIsImmuneToZipSlipVulnerability() {
public function testThrowsExceptionWhenZipContainsFilesWithRelativePaths() {
// zipped file named: "../../../../../../../../tmp/zip-slip-test.txt"
$zip = __DIR__ . '/resources/zip-slip-test.zip';

zip_extract_to( fopen( $zip, 'rb' ), dirname( $zip ) );
self::expectException(ZipException::class);
self::expectExceptionMessage("Relative paths in zips are not allowed.");
zip_extract_to(fopen($zip, 'rb'), dirname($zip));
}

public function testThrowsExceptionWhenZipContainsFilesWithSymlinks() {
// zipped semantic link
$zip = __DIR__ . '/resources/zip-symlinks-test.zip';

$slipped_file = Path::canonicalize(__DIR__ . "../../../../../../../../tmp/zip-slip-test.txt");
self::assertFileDoesNotExist( $slipped_file );
self::expectException(ZipException::class);
self::expectExceptionMessage("Relative paths in zips are not allowed.");
zip_extract_to(fopen($zip, 'rb'), dirname($zip));
}
}
Binary file added tests/unit/zip/resources/zip-symlinks-test.zip
Binary file not shown.

0 comments on commit 09054fa

Please sign in to comment.