Skip to content

Commit 1d3e344

Browse files
author
Max Schaefer
committed
Add example of manual sanitisation.
1 parent 240e079 commit 1d3e344

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

javascript/ql/src/Security/CWE-078/CommandInjection.inc.qhelp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ you can use a library like <code>shell-quote</code> to parse the user input into
4343
an array of arguments without risking command injection:</p>
4444

4545
<sample src="examples/command-injection_shellquote.js" />
46+
47+
<p>Alternatively, the original example can be made safe by checking the filename
48+
against an allowlist of safe characters before using it:</p>
49+
50+
<sample src="examples/command-injection_allowlist.js" />
4651
</example>
4752

4853
<references>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var cp = require("child_process"),
2+
http = require('http'),
3+
url = require('url');
4+
5+
var server = http.createServer(function(req, res) {
6+
let file = url.parse(req.url, true).query.path;
7+
8+
// only allow safe characters in file name
9+
if (file.match(/^[\w\.\-\/]+$/)) {
10+
cp.execSync(`wc -l ${file}`); // GOOD
11+
}
12+
});

0 commit comments

Comments
 (0)