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
+27-20Lines changed: 27 additions & 20 deletions
Original file line number
Diff line number
Diff line change
@@ -13,40 +13,47 @@ attacker being able to influence behavior by modifying unexpected files.
13
13
14
14
<recommendation>
15
15
<p>
16
-
Validate user input before using it to construct a file path, either using an off-the-shelf library
17
-
like the <code>sanitize-filename</code> npm package, or by performing custom validation.
16
+
Validate user input before using it to construct a file path.
18
17
</p>
19
18
20
19
<p>
21
-
Ideally, follow these rules:
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.
22
21
</p>
23
22
24
-
<ul>
25
-
<li>Do not allow more than a single "." character.</li>
26
-
<li>Do not allow directory separators such as "/" or "\" (depending on the file system).</li>
27
-
<li>Do not rely on simply replacing problematic sequences such as "../". For example, after
28
-
applying this filter to ".../...//", the resulting string would still be "../".</li>
29
-
<li>Use a whitelist of known good patterns.</li>
30
-
</ul>
23
+
<p>
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
+
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.
28
+
</p>
29
+
30
+
<p>
31
+
In the latter case, you can use a library like the <code>sanitize-filename</code> npm package to eliminate any special characters from the file path.
32
+
Note that it is <i>not</i> sufficient to only remove "../" sequences: for example, applying this filter to ".../...//" would still result in the string "../".
33
+
</p>
34
+
35
+
<p>
36
+
Finally, the simplest (but most restrictive) option is to use an allow list of safe patterns and make sure that the user input matches one of these patterns.
37
+
</p>
31
38
</recommendation>
32
39
33
40
<example>
34
41
<p>
35
-
In the first example, a file name is read from an HTTP request and then used to access a file.
36
-
However, a malicious user could enter a file name which is an absolute path, such as
37
-
<code>"/etc/passwd"</code>.
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.
38
44
</p>
39
45
46
+
<samplesrc="examples/TaintedPath.js" />
47
+
40
48
<p>
41
-
In the second example, it appears that the user is restricted to opening a file within the
42
-
<code>"user"</code> home directory. However, a malicious user could enter a file name containing
43
-
special characters. For example, the string <code>"../../etc/passwd"</code> will result in the code
44
-
reading the file located at <code>"/home/user/../../etc/passwd"</code>, which is the system's
45
-
password file. This file would then be sent back to the user, giving them access to all the
46
-
system's passwords.
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.
0 commit comments