You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: javascript/ql/src/Security/CWE-022/TaintedPath.qhelp
+9-9Lines changed: 9 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -17,14 +17,14 @@ Validate user input before using it to construct a file path.
17
17
</p>
18
18
19
19
<p>
20
-
The choice of validation depends on whether you want to allow the user to specify complex paths with multiple components that may span multiple folders, or only simple filenames without a path component.
20
+
The validation method you should use depends on whether you want to allow the user to specify complex paths with multiple components that may span multiple folders, or only simple filenames without a path component.
21
21
</p>
22
22
23
23
<p>
24
24
In the former case, a common strategy is to make sure that the constructed file path is contained within a safe root folder.
25
25
First, normalize the path using <code>path.resolve</code> or <code>fs.realpathSync</code> to remove any ".." segments.
26
-
Then check that the normalized path starts with the root folder.
27
-
Note that the normalization step is important, since otherwise even a path that starts with the root folder could be used to access files outside the root folder.
26
+
You should always normalize the file path since an unnormalized path that starts with the root folder can still be used to access files outside the root folder.
27
+
Then, after you have normalized the path, check that the path starts with the root folder.
28
28
</p>
29
29
30
30
<p>
@@ -39,17 +39,17 @@ Finally, the simplest (but most restrictive) option is to use an allow list of s
39
39
40
40
<example>
41
41
<p>
42
-
In the first example, a file name is read from an HTTP request and then used to access a file within a root folder.
43
-
However, a malicious user could enter a file name containing "../" segments to navigate outside the root folder and access sensitive files.
42
+
In the first (bad) example, the code reads the file name from an HTTP request, then accesses that file within a root folder.
43
+
A malicious user could enter a file name containing "../" segments to navigate outside the root folder and access sensitive files.
44
44
</p>
45
45
46
46
<samplesrc="examples/TaintedPath.js" />
47
47
48
48
<p>
49
-
The second example shows how to fix this.
50
-
First, the file name is resolved relative to a root folder, which has the side effect of normalizing the path and removing any "../" segments.
51
-
Then, <code>fs.realpathSync</code> is used to resolve any symbolic links in the path.
52
-
Finally, we check that the normalized path starts with the root folder's path, which ensures that the file is contained within the root folder.
49
+
The second (good) example shows how to avoid access to sensitive files by sanitizing the file path.
50
+
First, the code resolves the file name relative to a root folder, normalizing the path and removing any "../" segments in the process.
51
+
Then, the code calls <code>fs.realpathSync</code> to resolve any symbolic links in the path.
52
+
Finally, the code checks that the normalized path starts with the path of the root folder, ensuring the file is contained within the root folder.
0 commit comments