Skip to content

Commit

Permalink
0.7.1 ZipSlip vunerability fix (fixes #18)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Yates committed Jul 2, 2018
1 parent 9b000d2 commit ea5d3fb
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
sourceCompatibility=1.7
targetCompatibility=1.7

version = '0.7.0'
version = '0.7.1'

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ class FileExtensionMethods {
while(entry = zipInput.nextEntry) {
if (!entry.isDirectory()) {
final file = new File(destination, entry.name)
checkForZipSlip(destination, file)
if( filter == null || filter( file ) ) {
file.parentFile?.mkdirs()

Expand All @@ -155,6 +156,7 @@ class FileExtensionMethods {
}
else {
final dir = new File(destination, entry.name)
checkForZipSlip(destination, dir)
if( filter == null || filter( dir ) ) {
dir.mkdirs()

Expand All @@ -167,6 +169,12 @@ class FileExtensionMethods {
unzippedFiles
}

private static void checkForZipSlip(File destination, File dir) {
if (!dir.canonicalPath.startsWith(destination.canonicalPath)) {
throw new IllegalArgumentException("Attempt to unzip ($dir.canonicalPath) outside of destination ($destination.canonicalPath) rejected")
}
}

private static void checkUnzipFileType(File self) {
if (!self.isFile()) throw new IllegalArgumentException("File#unzip() has to be called on a *.zip file.")

Expand Down
22 changes: 22 additions & 0 deletions src/test/groovy/tests/FileTests.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package tests

import spock.lang.Specification

import java.nio.file.Files

class FileTests extends Specification {

def "check for ZipSlip"() {
given:
def dest = Files.createTempDirectory("zipslip")
def zip = new File(FileTests.class.getResource("/zip-slip.zip").toURI())

when:
zip.unzip(dest.toFile())

then:
def ex = thrown(IllegalArgumentException)
ex.printStackTrace()
}

}
Binary file added src/test/resources/zip-slip.zip
Binary file not shown.

0 comments on commit ea5d3fb

Please sign in to comment.