Skip to content

Commit 6331c37

Browse files
authored
Merge pull request github#12735 from JLLeitschuh/doc/JLL/fix-partial-path-documentation
Fix partial path traversal Java example Again
2 parents e5c7c88 + 0d774a6 commit 6331c37

File tree

4 files changed

+14
-7
lines changed

4 files changed

+14
-7
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
public class PartialPathTraversalBad {
22
public void example(File dir, File parent) throws IOException {
33
if (!dir.getCanonicalPath().startsWith(parent.getCanonicalPath())) {
4-
throw new IOException("Invalid directory: " + dir.getCanonicalPath());
4+
throw new IOException("Path traversal attempt: " + dir.getCanonicalPath());
55
}
66
}
77
}
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import java.io.File;
2+
13
public class PartialPathTraversalGood {
24
public void example(File dir, File parent) throws IOException {
3-
if (!dir.getCanonicalPath().startsWith(parent.getCanonicalPath() + File.separator)) {
4-
throw new IOException("Invalid directory: " + dir.getCanonicalPath());
5+
if (!dir.toPath().normalize().startsWith(parent.toPath())) {
6+
throw new IOException("Path traversal attempt: " + dir.getCanonicalPath());
57
}
68
}
79
}

java/ql/src/Security/CWE/CWE-023/PartialPathTraversalRemainder.inc.qhelp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@ and not just children of <code>parent</code>, which is a security issue.
2626

2727
<p>
2828

29-
In this example, the <code>if</code> statement checks if <code>parent.getCanonicalPath() + File.separator </code>
30-
is a prefix of <code>dir.getCanonicalPath()</code>. Because <code>parent.getCanonicalPath() + File.separator</code> is
31-
indeed slash-terminated, the user supplying <code>dir</code> can only access children of
32-
<code>parent</code>, as desired.
29+
In this example, the <code>if</code> statement checks if <code>parent.toPath()</code>
30+
is a prefix of <code>dir.normalize()</code>. Because <code>Path#startsWith</code> does the correct check that
31+
<code>dir</code> is a child of <code>parent</code>, users will not be able to access siblings of <code>parent</code>, as desired.
3332

3433
</p>
3534

java/ql/test/query-tests/security/CWE-023/semmle/tests/PartialPathTraversalTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,12 @@ void foo24(File dir, File parent) throws IOException {
225225
}
226226
}
227227

228+
public void doesNotFlagOptimalSafeVersion(File dir, File parent) throws IOException {
229+
if (!dir.toPath().normalize().startsWith(parent.toPath())) { // Safe
230+
throw new IOException("Path traversal attempt: " + dir.getCanonicalPath());
231+
}
232+
}
233+
228234
public void doesNotFlag() {
229235
"hello".startsWith("goodbye");
230236
}

0 commit comments

Comments
 (0)