Skip to content

Commit 4686138

Browse files
authored
Check that extracting extension jar doesn't escape designated directory (#7908)
Mostly to appease code scanners.
1 parent dc12a5f commit 4686138

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/ExtensionClassLoader.java

+12-2
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,21 @@ private static void includeEmbeddedExtensionsIfFound(
8383
File tempDirectory = null;
8484
while (entryEnumeration.hasMoreElements()) {
8585
JarEntry jarEntry = entryEnumeration.nextElement();
86+
String name = jarEntry.getName();
8687

87-
if (jarEntry.getName().startsWith(prefix) && !jarEntry.isDirectory()) {
88+
if (name.startsWith(prefix) && !jarEntry.isDirectory()) {
8889
tempDirectory = ensureTempDirectoryExists(tempDirectory);
8990

90-
File tempFile = new File(tempDirectory, jarEntry.getName().substring(prefix.length()));
91+
File tempFile = new File(tempDirectory, name.substring(prefix.length()));
92+
// reject extensions that would be extracted outside of temp directory
93+
// https://security.snyk.io/research/zip-slip-vulnerability
94+
if (name.indexOf("..") != -1
95+
&& !tempFile
96+
.getCanonicalFile()
97+
.toPath()
98+
.startsWith(tempDirectory.getCanonicalFile().toPath())) {
99+
throw new IllegalStateException("Invalid extension " + name);
100+
}
91101
if (tempFile.createNewFile()) {
92102
tempFile.deleteOnExit();
93103
extractFile(jarFile, jarEntry, tempFile);

0 commit comments

Comments
 (0)