Skip to content

Commit 62625bb

Browse files
Potential fix for code scanning alert no. 5: Arbitrary file access during archive extraction ("Zip Slip")
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
1 parent 7693617 commit 62625bb

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/main/java/de/igslandstuhl/database/server/resources/ResourceHelper.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import java.io.InputStreamReader;
1010
import java.nio.charset.StandardCharsets;
1111
import java.nio.file.NoSuchFileException;
12+
import java.nio.file.Path;
13+
import java.nio.file.Paths;
1214
import java.util.ArrayList;
1315
import java.util.Collection;
1416
import java.util.Collections;
@@ -28,6 +30,28 @@
2830
*/
2931
public class ResourceHelper {
3032

33+
/**
34+
* Checks if a zip entry name is safe (no path traversal, not absolute).
35+
*/
36+
private static boolean isSafeZipEntryName(String entryName) {
37+
// Reject absolute paths
38+
Path path = Paths.get(entryName).normalize();
39+
if (path.isAbsolute()) {
40+
return false;
41+
}
42+
// Reject entries containing ".." as a path segment
43+
for (Path part : path) {
44+
if (part.toString().equals("..")) {
45+
return false;
46+
}
47+
}
48+
// Reject entries starting with "/" or "\"
49+
if (entryName.startsWith("/") || entryName.startsWith("\\")) {
50+
return false;
51+
}
52+
return true;
53+
}
54+
3155
/**
3256
* For all elements of java.class.path get a Collection of resources Pattern
3357
* pattern = Pattern.compile(".*"); gets all resources
@@ -87,6 +111,10 @@ private static Collection<String> getResourcesFromJarFile(final File file, final
87111
while (e.hasMoreElements()) {
88112
final ZipEntry ze = e.nextElement();
89113
final String fileName = ze.getName();
114+
if (!isSafeZipEntryName(fileName)) {
115+
// Optionally log or throw, here we skip unsafe entries
116+
continue;
117+
}
90118
final boolean accept = pattern.matcher(fileName).matches();
91119
if (accept) {
92120
retval.add(fileName);

0 commit comments

Comments
 (0)