Skip to content

Commit dfffa1e

Browse files
Apply suggestions from code review
Co-authored-by: Sam Browning <[email protected]>
1 parent d147fab commit dfffa1e

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

javascript/ql/src/Security/CWE-022/TaintedPath.qhelp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ Validate user input before using it to construct a file path.
1717
</p>
1818

1919
<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.
2121
</p>
2222

2323
<p>
2424
In the former case, a common strategy is to make sure that the constructed file path is contained within a safe root folder.
2525
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.
2828
</p>
2929

3030
<p>
@@ -39,17 +39,17 @@ Finally, the simplest (but most restrictive) option is to use an allow list of s
3939

4040
<example>
4141
<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.
4444
</p>
4545

4646
<sample src="examples/TaintedPath.js" />
4747

4848
<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.
5353
</p>
5454

5555
<sample src="examples/TaintedPathGood.js" />

javascript/ql/src/Security/CWE-022/examples/TaintedPath.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ const ROOT = "/var/www/";
77
var server = http.createServer(function(req, res) {
88
let filePath = url.parse(req.url, true).query.path;
99

10-
// BAD: This could read any file on the file system
10+
// BAD: This function uses unsanitized input that can read any file on the file system.
1111
res.write(fs.readFileSync(ROOT + filePath, 'utf8'));
1212
});

javascript/ql/test/query-tests/Security/CWE-022/TaintedPath/examples/TaintedPath.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ const ROOT = "/var/www/";
77
var server = http.createServer(function(req, res) {
88
let filePath = url.parse(req.url, true).query.path;
99

10-
// BAD: This could read any file on the file system
10+
// BAD: This function uses unsanitized input that can read any file on the file system.
1111
res.write(fs.readFileSync(ROOT + filePath, 'utf8'));
1212
});

0 commit comments

Comments
 (0)