Skip to content

Commit 7938d83

Browse files
authored
Avoid codeql zip slip warning (#13193)
1 parent 943fecb commit 7938d83

File tree

1 file changed

+45
-19
lines changed

1 file changed

+45
-19
lines changed

javaagent-bootstrap/src/main/java/io/opentelemetry/javaagent/bootstrap/AgentClassLoader.java

+45-19
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,11 @@ public Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundExce
169169
}
170170

171171
private Class<?> findAgentClass(String name) throws ClassNotFoundException {
172-
JarEntry jarEntry = findJarEntry(name.replace('.', '/') + ".class");
173-
if (jarEntry != null) {
172+
AgentJarResource jarResource = findAgentJarResource(name.replace('.', '/') + ".class");
173+
if (jarResource != null) {
174174
byte[] bytes;
175175
try {
176-
bytes = getJarEntryBytes(jarEntry);
176+
bytes = getJarEntryBytes(jarResource.getJarEntry());
177177
} catch (IOException exception) {
178178
throw new ClassNotFoundException(name, exception);
179179
}
@@ -236,18 +236,20 @@ private static String getPackageName(String className) {
236236
return index == -1 ? null : className.substring(0, index);
237237
}
238238

239-
private JarEntry findJarEntry(String name) {
239+
private AgentJarResource findAgentJarResource(String name) {
240240
// shading renames .class to .classdata
241241
boolean isClass = name.endsWith(".class");
242242
if (isClass) {
243243
name += getClassSuffix();
244244
}
245245

246-
JarEntry jarEntry = jarFile.getJarEntry(jarEntryPrefix + name);
246+
String jarEntryName = jarEntryPrefix + name;
247+
JarEntry jarEntry = jarFile.getJarEntry(jarEntryName);
248+
AgentJarResource jarResource = AgentJarResource.create(jarEntryName, jarEntry);
247249
if (MULTI_RELEASE_JAR_ENABLE) {
248-
jarEntry = findVersionedJarEntry(jarEntry, name);
250+
jarResource = findVersionedAgentJarResource(jarResource, name);
249251
}
250-
return jarEntry;
252+
return jarResource;
251253
}
252254

253255
// suffix appended to class resource names
@@ -256,22 +258,23 @@ protected String getClassSuffix() {
256258
return "data";
257259
}
258260

259-
private JarEntry findVersionedJarEntry(JarEntry jarEntry, String name) {
261+
private AgentJarResource findVersionedAgentJarResource(
262+
AgentJarResource jarResource, String name) {
260263
// same logic as in JarFile.getVersionedEntry
261264
if (!name.startsWith(META_INF)) {
262265
// search for versioned entry by looping over possible versions form high to low
263266
int version = JAVA_VERSION;
264267
while (version >= MIN_MULTI_RELEASE_JAR_JAVA_VERSION) {
265-
JarEntry versionedJarEntry =
266-
jarFile.getJarEntry(jarEntryPrefix + META_INF_VERSIONS + version + "/" + name);
268+
String versionedJarEntryName = jarEntryPrefix + META_INF_VERSIONS + version + "/" + name;
269+
JarEntry versionedJarEntry = jarFile.getJarEntry(versionedJarEntryName);
267270
if (versionedJarEntry != null) {
268-
return versionedJarEntry;
271+
return AgentJarResource.create(versionedJarEntryName, versionedJarEntry);
269272
}
270273
version--;
271274
}
272275
}
273276

274-
return jarEntry;
277+
return jarResource;
275278
}
276279

277280
@Override
@@ -296,17 +299,17 @@ public URL findResource(String name) {
296299
}
297300

298301
private URL findJarResource(String name) {
299-
JarEntry jarEntry = findJarEntry(name);
300-
return getJarEntryUrl(jarEntry);
302+
AgentJarResource jarResource = findAgentJarResource(name);
303+
return getAgentJarResourceUrl(jarResource);
301304
}
302305

303-
private URL getJarEntryUrl(JarEntry jarEntry) {
304-
if (jarEntry != null) {
306+
private URL getAgentJarResourceUrl(AgentJarResource jarResource) {
307+
if (jarResource != null) {
305308
try {
306-
return new URL(jarBase, jarEntry.getName());
309+
return new URL(jarBase, jarResource.getName());
307310
} catch (MalformedURLException e) {
308311
throw new IllegalStateException(
309-
"Failed to construct url for jar entry " + jarEntry.getName(), e);
312+
"Failed to construct url for jar entry " + jarResource.getName(), e);
310313
}
311314
}
312315

@@ -374,7 +377,8 @@ public URL getResource(String resourceName) {
374377
// find from agent jar
375378
if (agentClassLoader != null) {
376379
JarEntry jarEntry = agentClassLoader.jarFile.getJarEntry(resourceName);
377-
return agentClassLoader.getJarEntryUrl(jarEntry);
380+
AgentJarResource jarResource = AgentJarResource.create(resourceName, jarEntry);
381+
return agentClassLoader.getAgentJarResourceUrl(jarResource);
378382
}
379383
return null;
380384
}
@@ -385,6 +389,28 @@ protected Class<?> findClass(String name) throws ClassNotFoundException {
385389
}
386390
}
387391

392+
private static class AgentJarResource {
393+
private final String name;
394+
private final JarEntry jarEntry;
395+
396+
private AgentJarResource(String name, JarEntry jarEntry) {
397+
this.name = name;
398+
this.jarEntry = jarEntry;
399+
}
400+
401+
String getName() {
402+
return name;
403+
}
404+
405+
JarEntry getJarEntry() {
406+
return jarEntry;
407+
}
408+
409+
static AgentJarResource create(String name, JarEntry jarEntry) {
410+
return jarEntry != null ? new AgentJarResource(name, jarEntry) : null;
411+
}
412+
}
413+
388414
private static class AgentClassLoaderUrlStreamHandler extends URLStreamHandler {
389415
private final JarFile jarFile;
390416

0 commit comments

Comments
 (0)