Skip to content

Commit 4975ccc

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 8f02ad3 commit 4975ccc

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/test/java/org/ohdsi/usagi/TestUtils.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,19 @@
1111

1212
public class TestUtils {
1313
public static void unzipResource(String resourceName, Path targetDir) throws IOException {
14+
Path targetDirNormalized = targetDir.toAbsolutePath().normalize();
1415
try (InputStream is = TestUtils.class.getResourceAsStream(resourceName)) {
1516
if (is == null) {
1617
throw new IOException("Resource not found: " + resourceName);
1718
}
1819
try (ZipInputStream zis = new ZipInputStream(is)) {
1920
ZipEntry entry;
2021
while ((entry = zis.getNextEntry()) != null) {
21-
File newFile = new File(targetDir.toFile(), entry.getName());
22+
Path resolvedPath = targetDirNormalized.resolve(entry.getName()).normalize();
23+
if (!resolvedPath.startsWith(targetDirNormalized)) {
24+
throw new IOException("Entry is outside of the target dir: " + entry.getName());
25+
}
26+
File newFile = resolvedPath.toFile();
2227
if (entry.isDirectory()) {
2328
newFile.mkdirs();
2429
} else {

0 commit comments

Comments
 (0)