Skip to content

Commit 3680f43

Browse files
JLLeitschuhTeamModerne
authored andcommitted
vuln-fix: Zip Slip Vulnerability (openmrs#4144)
This fixes a Zip-Slip vulnerability. This change does one of two things. This change either 1. Inserts a guard to protect against Zip Slip. OR 2. Replaces `dir.getCanonicalPath().startsWith(parent.getCanonicalPath())`, which is vulnerable to partial path traversal attacks, with the more secure `dir.getCanonicalFile().toPath().startsWith(parent.getCanonicalFile().toPath())`. For number 2, consider `"/usr/outnot".startsWith("/usr/out")`. The check is bypassed although `/outnot` is not under the `/out` directory. It's important to understand that the terminating slash may be removed when using various `String` representations of the `File` object. For example, on Linux, `println(new File("/var"))` will print `/var`, but `println(new File("/var", "/")` will print `/var/`; however, `println(new File("/var", "/").getCanonicalPath())` will print `/var`. Weakness: CWE-22: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal') Severity: High CVSSS: 7.4 Detection: CodeQL (https://codeql.github.com/codeql-query-help/java/java-zipslip/) & OpenRewrite (https://public.moderne.io/recipes/org.openrewrite.java.security.ZipSlip) Reported-by: Jonathan Leitschuh <[email protected]> Signed-off-by: Jonathan Leitschuh <[email protected]> Bug-tracker: JLLeitschuh/security-research#16 Co-authored-by: Moderne <[email protected]> Co-authored-by: Moderne <[email protected]>
1 parent 1381366 commit 3680f43

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

web/src/main/java/org/openmrs/web/filter/initialization/TestInstallUtil.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,14 @@ protected static boolean addZippedTestModules(InputStream in) {
184184

185185
//delete all previously added modules in case of prior test installations
186186
FileUtils.cleanDirectory(moduleRepository);
187+
188+
final File zipEntryFile = new File(moduleRepository, fileName);
189+
190+
if (!zipEntryFile.toPath().normalize().startsWith(moduleRepository.toPath().normalize())) {
191+
throw new IOException("Bad zip entry");
192+
}
187193

188-
OpenmrsUtil.copyFile(zipFile.getInputStream(entry), new BufferedOutputStream(new FileOutputStream(
189-
new File(moduleRepository, fileName))));
194+
OpenmrsUtil.copyFile(zipFile.getInputStream(entry), new BufferedOutputStream(new FileOutputStream(zipEntryFile)));
190195
} else {
191196
log.debug("Ignoring file that is not a .omod '{}'", fileName);
192197
}

0 commit comments

Comments
 (0)