Skip to content

Commit

Permalink
SONAR-10661 minimize normalize calls
Browse files Browse the repository at this point in the history
  • Loading branch information
sns-seb authored and SonarTech committed May 29, 2018
1 parent 00056ff commit 9c10956
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions sonar-plugin-api/src/main/java/org/sonar/api/utils/ZipUtils.java
Expand Up @@ -85,12 +85,13 @@ public static File unzip(InputStream stream, File toDir, Predicate<ZipEntry> fil
FileUtils.forceMkdir(toDir);
}

Path targetDirNormalizedPath = toDir.toPath().normalize();
ZipInputStream zipStream = new ZipInputStream(stream);
try {
ZipEntry entry;
while ((entry = zipStream.getNextEntry()) != null) {
if (filter.test(entry)) {
unzipEntry(entry, zipStream, toDir);
unzipEntry(entry, zipStream, targetDirNormalizedPath);
}
}
return toDir;
Expand All @@ -100,9 +101,9 @@ public static File unzip(InputStream stream, File toDir, Predicate<ZipEntry> fil
}
}

private static void unzipEntry(ZipEntry entry, ZipInputStream zipStream, File toDir) throws IOException {
File to = new File(toDir, entry.getName());
verifyInsideTargetDirectory(entry, to.toPath(), toDir.toPath());
private static void unzipEntry(ZipEntry entry, ZipInputStream zipStream, Path targetDirNormalized) throws IOException {
File to = targetDirNormalized.resolve(entry.getName()).toFile();
verifyInsideTargetDirectory(entry, to.toPath(), targetDirNormalized);

if (entry.isDirectory()) {
throwExceptionIfDirectoryIsNotCreatable(to);
Expand Down Expand Up @@ -245,8 +246,8 @@ private static void doZipDir(File dir, ZipOutputStream out) throws IOException {
}
}

private static void verifyInsideTargetDirectory(ZipEntry entry, Path entryPath, Path targetDirPath) {
if (!entryPath.normalize().startsWith(targetDirPath.normalize())) {
private static void verifyInsideTargetDirectory(ZipEntry entry, Path entryPath, Path targetDirNormalizedPath) {
if (!entryPath.normalize().startsWith(targetDirNormalizedPath)) {
// vulnerability - trying to create a file outside the target directory
throw new IllegalStateException("Unzipping an entry outside the target directory is not allowed: " + entry.getName());
}
Expand Down

0 comments on commit 9c10956

Please sign in to comment.