2022-09-12 16:38:00 +02:00
|
|
|
=== How to fix it in Java SE
|
|
|
|
|
|
|
|
:canonicalization_function1: java.io.File.getCanonicalFile
|
|
|
|
:canonicalization_function2: java.io.File.getCanonicalPath
|
|
|
|
|
|
|
|
include::../../common/fix/code-rationale.adoc[]
|
|
|
|
|
2022-10-18 16:03:10 +02:00
|
|
|
==== Noncompliant code example
|
2022-09-15 10:28:08 +02:00
|
|
|
|
|
|
|
[source,java,diff-id=1,diff-type=noncompliant]
|
2022-09-12 16:38:00 +02:00
|
|
|
----
|
|
|
|
public class Example {
|
|
|
|
|
|
|
|
static private String targetDirectory = "/example/directory/";
|
|
|
|
|
|
|
|
public void ExtractEntry(ZipFile zipFile) throws IOException {
|
|
|
|
|
|
|
|
Enumeration<? extends ZipEntry> entries = zipFile.entries();
|
|
|
|
ZipEntry entry = entries.nextElement();
|
|
|
|
InputStream inputStream = zipFile.getInputStream(entry);
|
|
|
|
|
|
|
|
File file = new File(targetDirectory + entry.getName());
|
|
|
|
|
2022-09-15 10:28:08 +02:00
|
|
|
Files.copy(inputStream, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
2022-09-12 16:38:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
2022-09-15 10:28:08 +02:00
|
|
|
|
|
|
|
==== Compliant solution
|
|
|
|
|
|
|
|
[source,java,diff-id=1,diff-type=compliant]
|
2022-09-12 16:38:00 +02:00
|
|
|
----
|
|
|
|
public class Example {
|
|
|
|
|
|
|
|
static private String targetDirectory = "/example/directory/";
|
|
|
|
|
|
|
|
public void ExtractEntry(ZipFile zipFile) throws IOException {
|
|
|
|
|
|
|
|
Enumeration<? extends ZipEntry> entries = zipFile.entries();
|
|
|
|
ZipEntry entry = entries.nextElement();
|
|
|
|
InputStream inputStream = zipFile.getInputStream(entry);
|
|
|
|
|
|
|
|
File file = new File(targetDirectory + entry.getName());
|
|
|
|
|
|
|
|
String canonicalDestinationPath = file.getCanonicalPath();
|
|
|
|
|
|
|
|
if (canonicalDestinationPath.startsWith(targetDirectory)) {
|
|
|
|
Files.copy(inputStream, file.toPath(), StandardCopyOption.REPLACE_EXISTING, LinkOption.NOFOLLOW_LINKS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
|
|
|
=== How does this work?
|
|
|
|
|
|
|
|
include::../../common/fix/how-does-this-work.adoc[]
|
2022-09-14 19:49:53 +02:00
|
|
|
|
|
|
|
=== Pitfalls
|
|
|
|
|
|
|
|
include::../../common/pitfalls/partial-path-traversal.adoc[]
|
|
|
|
|
|
|
|
For example, the following code is vulnerable to partial path injection. Note
|
|
|
|
that the string `targetDirectory` does not end with a path separator:
|
|
|
|
|
|
|
|
|
|
|
|
[source, java]
|
|
|
|
----
|
|
|
|
static private String targetDirectory = "/Users/John";
|
|
|
|
|
|
|
|
public void ExtractEntry(ZipFile zipFile) throws IOException {
|
|
|
|
|
|
|
|
Enumeration<? extends ZipEntry> entries = zipFile.entries();
|
|
|
|
ZipEntry entry = entries.nextElement();
|
|
|
|
InputStream inputStream = zipFile.getInputStream(entry);
|
|
|
|
|
|
|
|
File file = new File(entry.getName());
|
|
|
|
|
|
|
|
String canonicalDestinationPath = file.getCanonicalPath();
|
|
|
|
|
|
|
|
if (canonicalDestinationPath.startsWith(targetDirectory)) {
|
|
|
|
Files.copy(inputStream, file.toPath(), StandardCopyOption.REPLACE_EXISTING, LinkOption.NOFOLLOW_LINKS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
|
|
|
This check can be bypassed because `"/Users/Johnny".startsWith("/Users/John")`
|
|
|
|
returns `true`. Thus, for validation, `"/Users/John"` should actually be
|
|
|
|
`"/Users/John/"`.
|
|
|
|
|
|
|
|
**Warning**: Some functions, such as `.getCanonicalPath`, remove the
|
|
|
|
terminating path separator in their return value. +
|
|
|
|
The validation code should be tested to ensure that it cannot be impacted by this
|
|
|
|
issue.
|
|
|
|
|
|
|
|
https://github.com/aws/aws-sdk-java/security/advisories/GHSA-c28r-hw5m-5gv3[Here is a real-life example of this vulnerability.]
|