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

False-positive result while checking for slip zip if target path is not normalized #62

Closed
repolevedavaj opened this issue Sep 9, 2019 · 7 comments
Labels
bug Something isn't working

Comments

@repolevedavaj
Copy link

repolevedavaj commented Sep 9, 2019

If i use non normalized target paths while unzipping, the check for slip zip ends with a false-positive result.
The issue is, that the path of the extracted file is compared with a non normalized target path in AbstractExtractFileTask#extractFile(...):

if (!new File(completePath).getCanonicalPath().startsWith(new File(outPath).getPath())) {

This line should be changed to

!new File(completePath).getCanonicalPath().startsWith(new File(outPath).getCanonicalPath())

Test case (JUnit5 + AssertJ) for verification:

@Test
public void testUnzipFileZipSlipWithNotNormalizedTarget(@TempDir File tempDir) throws Exception {
	final File goodFile = new File(tempDir, "good.txt");
	assertThat(goodFile.createNewFile()).isTrue();
	FileUtils.write(goodFile, "good", StandardCharsets.UTF_8);

	final ZipParameters zipParameters = new ZipParameters();
	zipParameters.setFileNameInZip("good.txt");

	final ZipFile zip = new ZipFile(new File(tempDir, "test.zip"));
	zip.addFile(goodFile, zipParameters);

	zip.extractAll(new File(tempDir, "../" + tempDir.getName() + "/unzipped").getAbsolutePath());
}
@LeeYoung624
Copy link
Contributor

LeeYoung624 commented Sep 10, 2019

I tested this case and I think it's right. Only to modify the testcase a little bit:

@Test
  public void testUnzipFileZipSlipWithNotNormalizedTarget() throws IOException {
    ZipFile zipFile = new ZipFile(generatedZipFile, PASSWORD);
    zipFile.addFiles(FILES_TO_ADD);

    try {
      zipFile.extractAll(new File(outputFolder.getPath(),
              ".." + InternalZipConstants.FILE_SEPARATOR + outputFolder.getName()).getAbsolutePath());
    } catch (ZipException e) {
      fail("Should not throw an exception");
    }
  }

Further more, I found other exceptions like this with test case:

@Test
  public void testAddFolderAndFileWithCanonicalPath() throws IOException {
    ZipFile zipFile = new ZipFile(generatedZipFile);
    ZipParameters parameters = new ZipParameters();

    String folderToAddPath = TestUtils.getTestFileFromResources("").getPath() + InternalZipConstants.FILE_SEPARATOR + ".." + InternalZipConstants.FILE_SEPARATOR + TestUtils.getTestFileFromResources("").getName();
    File folderToAdd = new File(folderToAddPath);
    zipFile.addFolder(folderToAdd, parameters);
    zipFile.addFile(TestUtils.getTestFileFromResources("file_PDF_1MB.pdf"), parameters);
    ZipFileVerifier.verifyZipFileByExtractingAllFiles(generatedZipFile, outputFolder, 13);
  }

I'm trying to fix all these.

@srikanth-lingala
Copy link
Owner

@LeeYoung624 Along with this change, can you please also revert all the getCanonicalPath() changes in this commit and revert it back to getPath() as it was earlier. For example on this line? Thanks.

@srikanth-lingala
Copy link
Owner

@LeeYoung624 Just to be clear, please only revert the changes in that commit and only from the tests in that commit. Thanks.

@LeeYoung624
Copy link
Contributor

Sure @srikanth-lingala

@cbgxhub
Copy link

cbgxhub commented Sep 11, 2019

Hi srikanth-lingala

I ran into the same Problem trying to unzip a zip file that was ziped on Windows but trying to unzip it on Linux, and came to the same conclusion regarding a fix as LeeYoung624:

!new File(completePath).getCanonicalPath().startsWith(new File(outPath).getCanonicalPath())

Regards

@srikanth-lingala
Copy link
Owner

@cbgxhub A pull request for this issue is already open and work-in-progress. I will make a new release as soon as it is merged.

srikanth-lingala pushed a commit that referenced this issue Sep 12, 2019
Zip slip fix with canonical path
@srikanth-lingala srikanth-lingala added the bug Something isn't working label Sep 13, 2019
@srikanth-lingala
Copy link
Owner

Issue fixed in v2.1.4 released today.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants