Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix zip slip vulnerability, other zip issues and tests #93

Open
wants to merge 6 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"php": ">=7.0"
},
"require-dev": {
"yoast/phpunit-polyfills": "2.0.0"
"yoast/phpunit-polyfills": "2.0.0",
"ext-zip": "*"
},
"config": {
"optimize-autoloader": true,
Expand Down
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 {}
14 changes: 12 additions & 2 deletions src/WordPress/Zip/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,21 @@ function zip_extract_to( $fp, $to_path ) {
continue;
}

// prevent zip slip -> using relative path to access otherwise inaccessible files
if ( false !== strpos( $entry->path ,'..') ) {
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 );
$parent = Path::getDirectory( $path );
if ( ! is_dir( $parent ) ) {
if(is_file($parent)) {
unlink($parent);
if( is_file( $parent ) ) {
unlink( $parent );
}
mkdir( $parent, 0777, true );
}
Expand Down
64 changes: 64 additions & 0 deletions tests/unit/zip/ZipFunctionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace unit\zip;

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

class ZipFunctionsTest extends PHPUnitTestCase {

/**
* @var string name of the temporary directory for handling zips in tests
*/
const TMP_DIRECTORY = __DIR__ . "/tmp";

/**
* @var Filesystem
*/
private $filesystem;

/**
* @before
*/
public function before() {
$this->filesystem = new Filesystem();
$this->filesystem->mkdir( self::TMP_DIRECTORY );
}

/**
* @after
*/
public function after(){
$this->filesystem->remove( self::TMP_DIRECTORY );
}

public function testThrowsExceptionWhenZipContainsFilesWithRelativePaths() {
$filename = self::TMP_DIRECTORY . '/zip-slip-test.zip';

$zip = new ZipArchive();
$zip->open( $filename, ZipArchive::CREATE );
$zip->addFromString( "../../evilfile.txt","zip-slip-test" );
$zip->close();

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

public function testThrowsExceptionWhenZipContainsFilesWithSymlinks() {
$filename = self::TMP_DIRECTORY . '/zip-symlink-test.zip';

$zip = new ZipArchive();
$zip->open( $filename, ZipArchive::CREATE );
$symlink = symlink( self::TMP_DIRECTORY, 'evillink.txt' );
$zip->addFile( $symlink,"zip-symlink-test" );
$zip->close();

self::expectException( ZipException::class );
self::expectExceptionMessage( "Semantic links in zips are not allowed." );
zip_extract_to( fopen( $filename, 'rb' ), dirname( $filename ) );
}
}
Loading