Skip to content

Commit

Permalink
Fix #680: Avoid trailing slashes in tar entry filenames (#808)
Browse files Browse the repository at this point in the history
  • Loading branch information
rnorth committed Jul 30, 2018
1 parent 8907226 commit 3593c12
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,14 @@ private void recursiveTar(String entryFilename, String rootPath, String itemPath
final File sourceRootFile = new File(rootPath).getCanonicalFile(); // e.g. /foo
final String relativePathToSourceFile = sourceRootFile.toPath().relativize(sourceFile.toPath()).toFile().toString(); // e.g. /bar/baz

final TarArchiveEntry tarEntry = new TarArchiveEntry(sourceFile, entryFilename + "/" + relativePathToSourceFile); // entry filename e.g. /xyz/bar/baz
final String tarEntryFilename;
if (relativePathToSourceFile.isEmpty()) {
tarEntryFilename = entryFilename; // entry filename e.g. xyz => xyz
} else {
tarEntryFilename = entryFilename + "/" + relativePathToSourceFile; // entry filename e.g. /xyz/bar/baz => /foo/bar/baz
}

final TarArchiveEntry tarEntry = new TarArchiveEntry(sourceFile, tarEntryFilename.replaceAll("^/", ""));

// TarArchiveEntry automatically sets the mode for file/directory, but we can update to ensure that the mode is set exactly (inc executable bits)
tarEntry.setMode(getUnixFileMode(itemPath));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package org.testcontainers.utility;

import lombok.Cleanup;
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.jetbrains.annotations.NotNull;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.function.Consumer;

import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals;
import static org.rnorth.visibleassertions.VisibleAssertions.assertFalse;
import static org.rnorth.visibleassertions.VisibleAssertions.assertTrue;
import static org.rnorth.visibleassertions.VisibleAssertions.*;

public class MountableFileTest {

Expand Down Expand Up @@ -79,7 +84,7 @@ public void forHostPathWithPlus() throws Exception {
@Test
public void forClasspathResourceWithPermission() throws Exception {
final MountableFile mountableFile = MountableFile.forClasspathResource("mappable-resource/test-resource.txt",
TEST_FILE_MODE);
TEST_FILE_MODE);

performChecks(mountableFile);
assertEquals("Valid file mode.", BASE_FILE_MODE | TEST_FILE_MODE, mountableFile.getFileMode());
Expand All @@ -101,6 +106,31 @@ public void forHostDirPathWithPermission() throws Exception {
assertEquals("Valid dir mode.", BASE_DIR_MODE | TEST_FILE_MODE, mountableFile.getFileMode());
}

@Test
public void noTrailingSlashesInTarEntryNames() throws Exception {
final MountableFile mountableFile = MountableFile.forClasspathResource("mappable-resource/test-resource.txt");

@Cleanup final TarArchiveInputStream tais = intoTarArchive((taos) -> {
mountableFile.transferTo(taos, "/some/path.txt");
mountableFile.transferTo(taos, "/path.txt");
mountableFile.transferTo(taos, "path.txt");
});

ArchiveEntry entry;
while ((entry = tais.getNextEntry()) != null) {
assertFalse("no entries should have a trailing slash", entry.getName().endsWith("/"));
}
}

private TarArchiveInputStream intoTarArchive(Consumer<TarArchiveOutputStream> consumer) throws IOException {
@Cleanup final ByteArrayOutputStream baos = new ByteArrayOutputStream();
@Cleanup final TarArchiveOutputStream taos = new TarArchiveOutputStream(baos);
consumer.accept(taos);
taos.close();

return new TarArchiveInputStream(new ByteArrayInputStream(baos.toByteArray()));
}

@SuppressWarnings("ResultOfMethodCallIgnored")
@NotNull
private Path createTempFile(final String name) throws IOException {
Expand Down

0 comments on commit 3593c12

Please sign in to comment.