Skip to content

Commit fd80f8d

Browse files
committed
Fix host UID/GID leakage in copyFileToContainer TAR entries
1 parent deb78e1 commit fd80f8d

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

core/src/main/java/org/testcontainers/utility/MountableFile.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,10 @@ private void recursiveTar(
353353
}
354354

355355
final TarArchiveEntry tarEntry = new TarArchiveEntry(sourceFile, tarEntryFilename.replaceAll("^/", ""));
356+
tarEntry.setUserId(0);
357+
tarEntry.setGroupId(0);
358+
tarEntry.setUserName("root");
359+
tarEntry.setGroupName("root");
356360

357361
// TarArchiveEntry automatically sets the mode for file/directory, but we can update to ensure that the mode is set exactly (inc executable bits)
358362
tarEntry.setMode(getUnixFileMode(itemPath));

core/src/test/java/org/testcontainers/utility/MountableFileTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import lombok.Cleanup;
44
import org.apache.commons.compress.archivers.ArchiveEntry;
5+
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
56
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
67
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
78
import org.jetbrains.annotations.NotNull;
@@ -32,6 +33,27 @@ void forClasspathResource() throws Exception {
3233
performChecks(mountableFile);
3334
}
3435

36+
@Test
37+
void tarEntriesShouldUseRootOwnership() throws Exception {
38+
final Path file = createTempFile("ownership-test.txt");
39+
final MountableFile mountableFile = MountableFile.forHostPath(file);
40+
41+
@Cleanup
42+
final TarArchiveInputStream tais = intoTarArchive(taos -> {
43+
mountableFile.transferTo(taos, "/ownership-test.txt");
44+
});
45+
46+
final TarArchiveEntry tarEntry = tais.getNextEntry();
47+
48+
assertThat(tarEntry).isNotNull();
49+
50+
System.out.println("UID = " + tarEntry.getLongUserId());
51+
System.out.println("GID = " + tarEntry.getLongGroupId());
52+
53+
assertThat(tarEntry.getLongUserId()).isZero();
54+
assertThat(tarEntry.getLongGroupId()).isZero();
55+
}
56+
3557
@Test
3658
void forClasspathResourceWithAbsolutePath() throws Exception {
3759
final MountableFile mountableFile = MountableFile.forClasspathResource("/mappable-resource/test-resource.txt");

0 commit comments

Comments
 (0)