Fix duplicate class header and better fix using toPath()

This commit is contained in:
Shyam Mehta
2022-06-29 18:01:12 -04:00
parent 955e614563
commit 7ab8f0262c
2 changed files with 3 additions and 3 deletions

View File

@@ -39,7 +39,7 @@ and not just children of <code>parent</code>, which is a security issue.
<p>
In this example, the <code>if</code> statement checks if <code>parent.getCanonicalPath() + File.separator </code>
is a prefix of <code>dir.getCanonicalPath()</code>. Because <code>parent.getCanonicalPath() + File.separator</code> is
is a prefix of <code>dir.getCanonicalPath()</code>. Because <code>parent.getCanonicalPath().toPath()</code> is
indeed slash-terminated, the user supplying <code>dir</code> can only access children of
<code>parent</code>, as desired.

View File

@@ -1,8 +1,8 @@
import java.io.File;
public class PartialPathTraversalBad {
public class PartialPathTraversalGood {
public void esapiExample(File dir, File parent) throws IOException {
if (!dir.getCanonicalPath().startsWith(parent.getCanonicalPath() + File.separator)) {
if (!dir.getCanonicalPath().startsWith(parent.getCanonicalPath().toPath())) {
throw new IOException("Invalid directory: " + dir.getCanonicalPath());
}
}