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 regression when extracting tar archive to a path containig a symlink #864

Merged
merged 3 commits into from Dec 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -104,15 +104,15 @@ public void extract(String archive, String destinationDirectory) throws ArchiveE
tarIn = new TarArchiveInputStream(new GzipCompressorInputStream(fis));

TarArchiveEntry tarEntry = tarIn.getNextTarEntry();
String canonicalDestinationDirectory = new File(destinationDirectory).getCanonicalPath();
while (tarEntry != null) {
// Create a file for this tarEntry
final File destPath = new File(destinationDirectory + File.separator + tarEntry.getName());
prepDestination(destPath, tarEntry.isDirectory());


if (!startsWithPath(destPath.getCanonicalPath(), destinationDirectory)) {
if (!startsWithPath(destPath.getCanonicalPath(), canonicalDestinationDirectory)) {
throw new IOException(
"Expanding " + tarEntry.getName() + " would create file outside of " + destinationDirectory
"Expanding " + tarEntry.getName() + " would create file outside of " + canonicalDestinationDirectory
);
}

Expand Down
@@ -0,0 +1,68 @@
package com.github.eirslett.maven.plugins.frontend.lib;

import org.hamcrest.CoreMatchers;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue;

public class DefaultArchiveExtractorTest {

private final String BAD_TAR = "src/test/resources/bad.tgz";
private final String GOOD_TAR = "src/test/resources/good.tgz";

@Rule
public final TemporaryFolder temp = new TemporaryFolder();

private ArchiveExtractor extractor;

@Before
public void setup() {
extractor = new DefaultArchiveExtractor();
}

@Test
public void extractGoodTarFile() throws Exception {
File destination = temp.newFolder("destination");
extractor.extract(GOOD_TAR, destination.getPath());
}

@Test
public void extractGoodTarFileSymlink() throws Exception {
File destination = temp.newFolder("destination");
Path link = createSymlinkOrSkipTest(temp.getRoot().toPath().resolve("link"), destination.toPath());
extractor.extract(GOOD_TAR, link.toString());
}

@Test(expected = ArchiveExtractionException.class)
public void extractBadTarFile() throws Exception {
File destination = temp.newFolder("destination");
extractor.extract(BAD_TAR, destination.getPath());
}

@Test(expected = ArchiveExtractionException.class)
public void extractBadTarFileSymlink() throws Exception {
File destination = temp.newFolder("destination");
Path link = createSymlinkOrSkipTest(temp.getRoot().toPath().resolve("link"), destination.toPath());
extractor.extract(BAD_TAR, link.toString());
}

private Path createSymlinkOrSkipTest(Path link, Path target) {
try {
return Files.createSymbolicLink(link, target);
} catch (UnsupportedOperationException | IOException e) {
assumeTrue("symlinks not supported", false);
return null;
}
}
}
Binary file added frontend-plugin-core/src/test/resources/bad.tgz
Binary file not shown.
Binary file added frontend-plugin-core/src/test/resources/good.tgz
Binary file not shown.