Skip to content

Commit

Permalink
Add filename check for unTar (#4222)
Browse files Browse the repository at this point in the history
* add filename check for unTar

* update code
  • Loading branch information
hangc0276 authored Mar 4, 2024
1 parent 7576f22 commit 48b7d1e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,11 @@ public static void dumpContainerLogDirToTarget(DockerClient docker, String conta
TarArchiveEntry entry = stream.getNextTarEntry();
while (entry != null) {
if (entry.isFile()) {
File output = new File(getTargetDirectory(containerId), entry.getName().replace("/", "-"));
File targetDir = getTargetDirectory(containerId);
File output = new File(targetDir, entry.getName().replace("/", "-"));
if (!output.toPath().normalize().startsWith(targetDir.toPath())) {
throw new IOException("Bad zip entry");
}
try (FileOutputStream os = new FileOutputStream(output)) {
byte[] block = new byte[readBlockSize];
int read = stream.read(block, 0, readBlockSize);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,10 @@ private static void unTar(final File inputFile, final File outputDir) throws Exc
TarArchiveEntry entry;
while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) {
final File outputFile = new File(outputDir, entry.getName());
if (!outputFile.toPath().normalize().startsWith(outputDir.toPath())) {
throw new IOException("Bad zip entry");
}

if (!outputFile.getParentFile().exists()) {
outputFile.getParentFile().mkdirs();
}
Expand Down

0 comments on commit 48b7d1e

Please sign in to comment.