Skip to content

Commit e13353f

Browse files
authored
Merge pull request github#3732 from erik-krogh/priv-file-polish
Approved by mchammer01
2 parents bfb2e9d + a17d152 commit e13353f

File tree

7 files changed

+86
-16
lines changed

7 files changed

+86
-16
lines changed

change-notes/1.25/analysis-javascript.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
| Incomplete HTML attribute sanitization (`js/incomplete-html-attribute-sanitization`) | security, external/cwe/cwe-20, external/cwe/cwe-079, external/cwe/cwe-116 | Highlights potential XSS vulnerabilities due to incomplete sanitization of HTML meta-characters. Results are shown on LGTM by default. |
3737
| Unsafe expansion of self-closing HTML tag (`js/unsafe-html-expansion`) | security, external/cwe/cwe-079, external/cwe/cwe-116 | Highlights potential XSS vulnerabilities caused by unsafe expansion of self-closing HTML tags. |
3838
| Unsafe shell command constructed from library input (`js/shell-command-constructed-from-input`) | correctness, security, external/cwe/cwe-078, external/cwe/cwe-088 | Highlights potential command injections due to a shell command being constructed from library inputs. Results are shown on LGTM by default. |
39+
| Exposure of private files (`js/exposure-of-private-files`) | security, external/cwe/cwe-200 | Highlights servers that serve private files. Results are shown on LGTM by default. |
3940
| Creating biased random numbers from a cryptographically secure source (`js/biased-cryptographic-random`) | security, external/cwe/cwe-327 | Highlights mathematical operations on cryptographically secure numbers that can create biased results. Results are shown on LGTM by default. |
4041
| Storage of sensitive information in build artifact (`js/build-artifact-leak`) | security, external/cwe/cwe-312 | Highlights storage of sensitive information in build artifacts. Results are shown on LGTM by default. |
4142
| Improper code sanitization (`js/bad-code-sanitization`) | security, external/cwe/cwe-094, external/cwe/cwe-079, external/cwe/cwe-116 | Highlights string concatenation where code is constructed without proper sanitization. Results are shown on LGTM by default. |

javascript/ql/src/Security/CWE-200/PrivateFileExposure.qhelp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,33 @@
55

66
<overview>
77
<p>
8-
Placeholder
8+
Libraries like <code>express</code> provide easy methods for serving entire
9+
directories of static files from a web server.
10+
However, using these can sometimes lead to accidental information exposure.
11+
If for example the <code>node_modules</code> folder is served, then an attacker
12+
can access the <code>_where</code> field from a <code>package.json</code> file,
13+
which gives access to the absolute path of the file.
914
</p>
1015
</overview>
1116

1217
<recommendation>
1318
<p>
14-
Placeholder
19+
Limit which folders of static files are served from a web server.
1520
</p>
1621
</recommendation>
1722

1823
<example>
1924
<p>
20-
Placeholder
25+
In the example below, all the files from the <code>node_modules</code> are served.
26+
This allows clients to easily access all the files inside that folder,
27+
which includes potentially private information inside <code>package.json</code> files.
2128
</p>
29+
<sample src="examples/PrivateFileExposure.js"/>
30+
<p>
31+
The issue has been fixed below by only serving specific folders within the
32+
<code>node_modules</code> folder.
33+
</p>
34+
<sample src="examples/PrivateFileExposureFixed.js"/>
2235
</example>
2336

2437
<references>

javascript/ql/src/Security/CWE-200/PrivateFileExposure.ql

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,33 @@ pragma[noinline]
6969
Folder getAPackageJSONFolder() { result = any(PackageJSON json).getFile().getParentContainer() }
7070

7171
/**
72-
* Gets a reference to `dirname` that might cause information to be leaked.
73-
* That can happen if there is a `package.json` file in the same folder.
74-
* (It is assumed that the presence of a `package.json` file means that a `node_modules` folder can also exist.
72+
* Gets a reference to `dirname`, the home folder, the current working folder, or the root folder.
73+
* All of these might cause information to be leaked.
74+
*
75+
* For `dirname` that can happen if there is a `package.json` file in the same folder.
76+
* It is assumed that the presence of a `package.json` file means that a `node_modules` folder can also exist.
77+
*
78+
* For the root/home/working folder, they contain so much information that they must leak information somehow (e.g. ssh keys in the `~/.ssh` folder).
7579
*/
76-
DataFlow::Node dirname() {
80+
DataFlow::Node getALeakingFolder(string description) {
7781
exists(ModuleScope ms | result.asExpr() = ms.getVariable("__dirname").getAnAccess()) and
78-
result.getFile().getParentContainer() = getAPackageJSONFolder()
82+
result.getFile().getParentContainer() = getAPackageJSONFolder() and
83+
description = "the folder " + result.getFile().getParentContainer().getRelativePath()
84+
or
85+
result = DataFlow::moduleImport("os").getAMemberCall("homedir") and
86+
description = "the home folder "
87+
or
88+
result.mayHaveStringValue("/") and
89+
description = "the root folder"
90+
or
91+
result.getStringValue() = [".", "./"] and
92+
description = "the current working folder"
7993
or
80-
result.getAPredecessor() = dirname()
94+
result.getAPredecessor() = getALeakingFolder(description)
8195
or
8296
exists(StringOps::ConcatenationRoot root | root = result |
8397
root.getNumOperand() = 2 and
84-
root.getOperand(0) = dirname() and
98+
root.getOperand(0) = getALeakingFolder(description) and
8599
root.getOperand(1).getStringValue() = "/"
86100
)
87101
}
@@ -94,18 +108,17 @@ DataFlow::Node getAPrivateFolderPath(string description) {
94108
result = getANodeModulePath(path) and description = "the folder \"" + path + "\""
95109
)
96110
or
97-
result = dirname() and
98-
description = "the folder " + result.getFile().getParentContainer().getRelativePath()
99-
or
100-
result.getStringValue() = [".", "./"] and
101-
description = "the current working folder"
111+
result = getALeakingFolder(description)
102112
}
103113

104114
/**
105115
* Gest a call that serves the folder `path` to the public.
106116
*/
107117
DataFlow::CallNode servesAPrivateFolder(string description) {
108-
result = DataFlow::moduleMember("express", "static").getACall() and
118+
result = DataFlow::moduleMember(["express", "connect"], "static").getACall() and
119+
result.getArgument(0) = getAPrivateFolderPath(description)
120+
or
121+
result = DataFlow::moduleImport("serve-static").getACall() and
109122
result.getArgument(0) = getAPrivateFolderPath(description)
110123
}
111124

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
var express = require('express');
3+
4+
var app = express();
5+
6+
app.use('/node_modules', express.static(path.resolve(__dirname, '../node_modules')));
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
var express = require('express');
3+
4+
var app = express();
5+
6+
app.use("jquery", express.static('./node_modules/jquery/dist'));
7+
app.use("bootstrap", express.static('./node_modules/bootstrap/dist'));

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,8 @@
1414
| private-file-exposure.js:18:1:18:74 | app.use ... les"))) | Serves the folder "/node_modules", which can contain private information. |
1515
| private-file-exposure.js:19:1:19:88 | app.use ... lar/')) | Serves the folder "/node_modules/angular/", which can contain private information. |
1616
| private-file-exposure.js:22:1:22:58 | app.use ... lar/')) | Serves the folder "/node_modules/angular/", which can contain private information. |
17+
| private-file-exposure.js:40:1:40:88 | app.use ... lar/')) | Serves the folder "/node_modules/angular/", which can contain private information. |
18+
| private-file-exposure.js:41:1:41:97 | app.use ... lar/')) | Serves the folder "/node_modules/angular/", which can contain private information. |
19+
| private-file-exposure.js:42:1:42:66 | app.use ... dir())) | Serves the home folder , which can contain private information. |
20+
| private-file-exposure.js:43:1:43:46 | app.use ... )("/")) | Serves the root folder, which can contain private information. |
21+
| private-file-exposure.js:51:5:51:88 | app.use ... les'))) | Serves the folder "../node_modules", which can contain private information. |

javascript/ql/test/query-tests/Security/CWE-200/private-file-exposure.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,28 @@ app.use('/js/', express.static('node_modules/bootstrap/dist/js'))
3535
app.use('/css/', express.static('node_modules/font-awesome/css'));
3636
app.use('basedir', express.static(__dirname)); // GOOD, because there is no package.json in the same folder.
3737
app.use('/monthly', express.static(__dirname + '/')); // GOOD, because there is no package.json in the same folder.
38+
39+
const connect = require("connect");
40+
app.use('/angular', connect.static(path.join(__dirname, "/node_modules") + '/angular/')); // NOT OK
41+
app.use('/angular', require('serve-static')(path.join(__dirname, "/node_modules") + '/angular/')); // NOT OK
42+
app.use('/home', require('serve-static')(require("os").homedir())); // NOT OK
43+
app.use('/root', require('serve-static')("/")); // NOT OK
44+
45+
// Bad documentation example
46+
function bad() {
47+
var express = require('express');
48+
49+
var app = express();
50+
51+
app.use('/node_modules', express.static(path.resolve(__dirname, '../node_modules'))); // NOT OK
52+
}
53+
54+
// Good documentation example
55+
function good() {
56+
var express = require('express');
57+
58+
var app = express();
59+
60+
app.use("jquery", express.static('./node_modules/jquery/dist')); // OK
61+
app.use("bootstrap", express.static('./node_modules/bootstrap/dist')); // OK
62+
}

0 commit comments

Comments
 (0)