Skip to content

Commit 09054fa

Browse files
committed
Test symlinks
1 parent ee692ff commit 09054fa

File tree

4 files changed

+27
-6
lines changed

4 files changed

+27
-6
lines changed

src/WordPress/Zip/ZipException.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace WordPress\Zip;
4+
5+
use Exception;
6+
7+
class ZipException extends Exception {}

src/WordPress/Zip/functions.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,14 @@ function zip_extract_to( $fp, $to_path ) {
1414
continue;
1515
}
1616

17+
// prevent zip slip -> using relative path to access otherwise inaccessible files
1718
if ( false !== strpos( $entry->path ,'..') ) {
18-
continue;
19+
throw new ZipException("Relative paths in zips are not allowed.");
20+
}
21+
22+
// prevent zip with symlinks -> using a symbolic link to access otherwise inaccessible files
23+
if ( is_link( $entry->path ) ) {
24+
throw new ZipException("Semantic links in zips are not allowed.");
1925
}
2026

2127
$path = Path::canonicalize( $to_path . '/' . $entry->path );

tests/unit/zip/ZipFunctionsTest.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,25 @@
33
namespace unit\zip;
44

55
use PHPUnitTestCase;
6-
use Symfony\Component\Filesystem\Path;
6+
use WordPress\Zip\ZipException;
77
use function WordPress\Zip\zip_extract_to;
88

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

14-
zip_extract_to( fopen( $zip, 'rb' ), dirname( $zip ) );
14+
self::expectException(ZipException::class);
15+
self::expectExceptionMessage("Relative paths in zips are not allowed.");
16+
zip_extract_to(fopen($zip, 'rb'), dirname($zip));
17+
}
18+
19+
public function testThrowsExceptionWhenZipContainsFilesWithSymlinks() {
20+
// zipped semantic link
21+
$zip = __DIR__ . '/resources/zip-symlinks-test.zip';
1522

16-
$slipped_file = Path::canonicalize(__DIR__ . "../../../../../../../../tmp/zip-slip-test.txt");
17-
self::assertFileDoesNotExist( $slipped_file );
23+
self::expectException(ZipException::class);
24+
self::expectExceptionMessage("Relative paths in zips are not allowed.");
25+
zip_extract_to(fopen($zip, 'rb'), dirname($zip));
1826
}
1927
}
170 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)