From 4c7d476280b88b11642529e6ca8a4b3e33c9b8b6 Mon Sep 17 00:00:00 2001
From: Shyam Mehta Accessing paths controlled by users can allow an attacker to access unexpected resources. This
+can result in sensitive information being revealed or deleted, or an attacker being able to influence
+behavior by modifying unexpected files. Paths that are naively constructed from data controlled by a user may contain unexpected special characters,
+such as "..". Such a path may potentially point to any directory on the file system. Validate user input before using it to construct a file path. Ideally, follow these rules: In this example, a file name is read from a Accessing paths controlled by users can allow an attacker to access unexpected resources. This
-can result in sensitive information being revealed or deleted, or an attacker being able to influence
-behavior by modifying unexpected files. User inputted file paths can often pose security risks if a program does not correctly handle them. In particular, if a user
+is meant to access files under a certain directory but does not enters a path under that directory, they can gain access to
+(and potentially modify/delete) unexpected, possibly sensitive resources. Paths that are naively constructed from data controlled by a user may contain unexpected special characters,
-such as "..". Such a path may potentially point to any directory on the file system. Suppose a program is to only accept paths that point to files/folders within directory Validate user input before using it to construct a file path. Ideally, follow these rules: If the user should only access items within a certain directory In this example, a file name is read from a
+
+
+In this example, the
+
+In this example, the
In this example, the User inputted file paths can often pose security risks if a program does not correctly handle them. In particular, if a user
+ User supplied file paths can often pose security risks if a program does not correctly handle them. In particular, if a user
is meant to access files under a certain directory but does not enters a path under that directory, they can gain access to
(and potentially modify/delete) unexpected, possibly sensitive resources. Suppose a program is to only accept paths that point to files/folders within directory Suppose a program is to only accept paths that point to files/folders within directory
+
+
+java.net.Socket and then used to access a file in the
+user's home directory and send it back over the socket. However, a malicious user could enter a file name which contains special
+characters. For example, the string "../../etc/passwd" will result in the code reading the file located at
+"/home/[user]/../../etc/passwd", which is the system's password file. This file would then be sent back to the user,
+giving them access to all the system's passwords.DIR.
+To ensure that a user inputted path, say SUBDIR, is a subdirectory of DIR, the
+program verifies that DIR is a prefix of SUBDIR.
+However, this check is not satisfactory: unless DIR is not slash-terminated,
+SUBDIR may be allowed to also access siblings of DIR and not
+just children of DIR, which is a security issue.
-
+DIR, first ensure that DIR is slash-terminated,
+and then proceed (as normal) to verify that DIR is a prefix of the user-provided path, SUBDIR. Note, Java's getCanonicalPath()
+returns a non-slash-terminated path string, so a "/" must be added to DIR if that method is used. java.net.Socket and then used to access a file in the
-user's home directory and send it back over the socket. However, a malicious user could enter a file name which contains special
-characters. For example, the string "../../etc/passwd" will result in the code reading the file located at
-"/home/[user]/../../etc/passwd", which is the system's password file. This file would then be sent back to the user,
-giving them access to all the system's passwords.if statement checks if parent.getCanonicalPath()
+is a prefix of dir.getCanonicalPath(). However, parent.getCanonicalPath() is
+not slash-terminated. So, the user that supplies dir may be allowed to access siblings of parent
+and not just children of parent, which is a security issue.
+
+if statement checks if parent.getCanonicalPath() + File.separator
+is a prefix of dir.getCanonicalPath(). Because parent.getCanonicalPath() + File.separator is
+indeed slash-terminated, the user supplying dir can only access children of
+parent, as desired.
+
+parent, which is a security issue.
if statement checks if parent.getCanonicalPath() + File.separator
-is a prefix of dir.getCanonicalPath(). Because parent.getCanonicalPath() + File.separator is
+is a prefix of dir.getCanonicalPath(). Because parent.getCanonicalPath().toPath() is
indeed slash-terminated, the user supplying dir can only access children of
parent, as desired.
diff --git a/java/ql/src/Security/CWE/CWE-023/PartialPathTraversalGood.java b/java/ql/src/Security/CWE/CWE-023/PartialPathTraversalGood.java
index 365c71d70c8c..daf4adeca577 100644
--- a/java/ql/src/Security/CWE/CWE-023/PartialPathTraversalGood.java
+++ b/java/ql/src/Security/CWE/CWE-023/PartialPathTraversalGood.java
@@ -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());
}
}
From 16814071df1d58a82479986362fc37bc9167c71b Mon Sep 17 00:00:00 2001
From: Shyam Mehta "/" must be
In this example, the if statement checks if parent.getCanonicalPath()
-is a prefix of dir.getCanonicalPath(). However, parent.getCanonicalPath() is
+is a prefix of dir.getCanonicalPath(). However, parent.getCanonicalPath() is
not slash-terminated. So, the user that supplies dir may be allowed to access siblings of parent
and not just children of parent, which is a security issue.
From c6f2f61bfb75e0286469065a2e5a7f6c12db640d Mon Sep 17 00:00:00 2001
From: smehta23 <47726705+smehta23@users.noreply.github.com>
Date: Fri, 1 Jul 2022 10:39:46 -0400
Subject: [PATCH 07/34] Update
java/ql/src/Security/CWE/CWE-023/PartialPathTraversalBad.java
Co-authored-by: Jonathan Leitschuh dir can only access chi
dir can only access chi
DIR.
-To ensure that a user inputted path, say SUBDIR, is a subdirectory of DIR, the
-program verifies that DIR is a prefix of SUBDIR.
-However, this check is not satisfactory: unless DIR is not slash-terminated,
+DIR.
+To ensure that a user supplied path, say SUBDIR, is a subdirectory of DIR, the
+program verifies, using string comparisons, that DIR is a prefix of SUBDIR.
+However, if DIR is not slash-terminated, such a check would not be sufficient.
SUBDIR may be allowed to also access siblings of DIR and not
-just children of DIR, which is a security issue. DIR, which is a security vulnerability.
User supplied file paths can often pose security risks if a program does not correctly handle them. In particular, if a user -is meant to access files under a certain directory but does not enters a path under that directory, they can gain access to -(and potentially modify/delete) unexpected, possibly sensitive resources.
+User supplied file paths can often pose security risks if a program does not handle them correctly. In particular, if a user +is meant to access files under a certain directory but does not enter a path under that directory, they can +unexpectedly gain access to (and potentially modify/delete) possibly sensitive resources.
Suppose a program is to only accept paths that point to files/folders within directory User supplied file paths can often pose security risks if a program does not handle them correctly. In particular, if a user
-is meant to access files under a certain directory but does not enter a path under that directory, they can
-unexpectedly gain access to (and potentially modify/delete) possibly sensitive resources. Suppose a program is to only accept paths that point to files/folders within directory A common way to check that a user-supplied path If the user should only access items within a certain directory If the user should only access items within a certain directory A common way to check that a user-supplied path See also If the user should only access items within a certain directory
-
-
-In this example, the
-
-In this example, the A common way to check that a user-supplied path If the user should only access items within a certain directory
+
+
+In this example, the
+
+In this example, the See also See also DIR.
To ensure that a user supplied path, say SUBDIR, is a subdirectory of DIR, the
From 01cec0490beca9b8fc5b92a2f6a1be03902ed616 Mon Sep 17 00:00:00 2001
From: Chris Smowton DIR.
-To ensure that a user supplied path, say SUBDIR, is a subdirectory of DIR, the
-program verifies, using string comparisons, that DIR is a prefix of SUBDIR.
-However, if DIR is not slash-terminated, such a check would not be sufficient.
-SUBDIR may be allowed to also access siblings of DIR and not
-just children of DIR, which is a security vulnerability.SUBDIR falls inside a directory DIR
+is to use getCanonicalPath() to remove any path-traversal elements and then check that DIR
+is a prefix. However, if DIR is not slash-terminated, this can unexpectedly allow accessing siblings of DIR.DIR, first ensure that DIR is slash-terminated,
-and then proceed (as normal) to verify that DIR is a prefix of the user-provided path, SUBDIR. Note, Java's getCanonicalPath()
-returns a non-slash-terminated path string, so a "/" must be added to DIR if that method is used. DIR, ensure that DIR is slash-terminated
+before checking that DIR is a prefix of the user-provided path, SUBDIR. Note, Java's getCanonicalPath()
+returns a non-slash-terminated path string, so a slash must be added to DIR if that method is used.SUBDIR falls inside a directory DIR
-is to use getCanonicalPath() to remove any path-traversal elements and then check that DIR
-is a prefix. However, if DIR is not slash-terminated, this can unexpectedly allow accessing siblings of DIR.java/partial-path-traversal-from-remote, which is similar to this query but only flags instances with evidence of remote exploitabilityDIR, ensure that DIR is slash-terminated
-before checking that DIR is a prefix of the user-provided path, SUBDIR. Note, Java's getCanonicalPath()
-returns a non-slash-terminated path string, so a slash must be added to DIR if that method is used.if statement checks if parent.getCanonicalPath()
-is a prefix of dir.getCanonicalPath(). However, parent.getCanonicalPath() is
-not slash-terminated. So, the user that supplies dir may be allowed to access siblings of parent
-and not just children of parent, which is a security issue.
-
-if statement checks if parent.getCanonicalPath() + File.separator
-is a prefix of dir.getCanonicalPath(). Because parent.getCanonicalPath().toPath() is
-indeed slash-terminated, the user supplying dir can only access children of
-parent, as desired.
-
-SUBDIR falls inside a directory DIR
+is to use getCanonicalPath() to remove any path-traversal elements and then check that DIR
+is a prefix. However, if DIR is not slash-terminated, this can unexpectedly allow accessing siblings of DIR.DIR, ensure that DIR is slash-terminated
+before checking that DIR is a prefix of the user-provided path, SUBDIR. Note, Java's getCanonicalPath()
+returns a non-slash-terminated path string, so a slash must be added to DIR if that method is used.if statement checks if parent.getCanonicalPath()
+is a prefix of dir.getCanonicalPath(). However, parent.getCanonicalPath() is
+not slash-terminated. So, the user that supplies dir may be allowed to access siblings of parent
+and not just children of parent, which is a security issue.
+
+if statement checks if parent.getCanonicalPath() + File.separator
+is a prefix of dir.getCanonicalPath(). Because parent.getCanonicalPath().toPath() is
+indeed slash-terminated, the user supplying dir can only access children of
+parent, as desired.
+
+java/partial-path-traversal, which is similar to this query,
+but may also flag non-exploitable instances of Partial Path Traversal.java/partial-path-traversal, which is similar to this query,
-but may also flag non-exploitable instances of Partial Path Traversal.
See also java/partial-path-traversal, which is similar to this query,
but may also flag non-remotely-exploitable instances of Partial Path Traversal.
See also java/partial-path-traversal-from-remote, which is similar to this query but only flags instances with evidence of remote exploitability
See also java/partial-path-traversal-from-remote, which is similar to this query but only flags instances with evidence of remote exploitability
at the top isn't allowed, and for some reason the inclusion is required to be a valid qhelp file.
---
.../Security/CWE/CWE-023/PartialPathTraversal.qhelp | 5 ++++-
.../CWE/CWE-023/PartialPathTraversalFromRemote.qhelp | 5 ++++-
.../CWE/CWE-023/PartialPathTraversalOverview.inc.qhelp | 10 ----------
3 files changed, 8 insertions(+), 12 deletions(-)
delete mode 100644 java/ql/src/Security/CWE/CWE-023/PartialPathTraversalOverview.inc.qhelp
diff --git a/java/ql/src/Security/CWE/CWE-023/PartialPathTraversal.qhelp b/java/ql/src/Security/CWE/CWE-023/PartialPathTraversal.qhelp
index f83ee17b5500..afb8847a7d48 100644
--- a/java/ql/src/Security/CWE/CWE-023/PartialPathTraversal.qhelp
+++ b/java/ql/src/Security/CWE/CWE-023/PartialPathTraversal.qhelp
@@ -3,7 +3,10 @@
"qhelp.dtd">
A common way to check that a user-supplied path See also A common way to check that a user-supplied path See also A common way to check that a user-supplied path SUBDIR falls inside a directory DIR
+is to use getCanonicalPath() to remove any path-traversal elements and then check that DIR
+is a prefix. However, if DIR is not slash-terminated, this can unexpectedly allow accessing siblings of DIR.java/partial-path-traversal-from-remote, which is similar to this query but only flags instances with evidence of remote exploitabilitySUBDIR falls inside a directory DIR
+is to use getCanonicalPath() to remove any path-traversal elements and then check that DIR
+is a prefix. However, if DIR is not slash-terminated, this can unexpectedly allow accessing siblings of DIR.java/partial-path-traversal, which is similar to this query,
but may also flag non-remotely-exploitable instances of Partial Path Traversal.SUBDIR falls inside a directory DIR
-is to use getCanonicalPath() to remove any path-traversal elements and then check that DIR
-is a prefix. However, if DIR is not slash-terminated, this can unexpectedly allow accessing siblings of DIR.getCanonicalPath() to remove any path-traversal elements and then check that DIR
is a prefix. However, if DIR is not slash-terminated, this can unexpectedly allow accessing siblings of DIR.
See also java/partial-path-traversal-from-remote, which is similar to this query but only flags instances with evidence of remote exploitability
See also java/partial-path-traversal-from-remote, which is similar to this query but only flags instances with evidence of remote exploitability.
getCanonicalPath() to remove any path-traversal elements
is a prefix. However, if DIR is not slash-terminated, this can unexpectedly allow accessing siblings of DIR.
See also java/partial-path-traversal, which is similar to this query,
-but may also flag non-remotely-exploitable instances of Partial Path Traversal.
if statement checks if parent.getCanonicalPath()
is a prefix of dir.getCanonicalPath(). However, parent.getCanonicalPath() is
-not slash-terminated. So, the user that supplies dir may be allowed to access siblings of parent
+not slash-terminated. This means that users that supply dir may be also allowed to access siblings of parent
and not just children of parent, which is a security issue.
@@ -40,9 +40,9 @@ indeed slash-terminated, the user supplying dir can only access chi
A common way to check that a user-supplied path SUBDIR falls inside a directory DIR
is to use getCanonicalPath() to remove any path-traversal elements and then check that DIR
-is a prefix. However, if DIR is not slash-terminated, this can unexpectedly allow accessing siblings of DIR.
DIR is not slash-terminated, this can unexpectedly allow access to siblings of DIR.
See also java/partial-path-traversal-from-remote, which is similar to this query but only flags instances with evidence of remote exploitability.
dir can only access chi