Add example of manual sanitisation.

This commit is contained in:
Max Schaefer
2023-07-06 12:54:30 +01:00
parent 240e0799b0
commit 1d3e3440f2
2 changed files with 17 additions and 0 deletions

View File

@@ -43,6 +43,11 @@ you can use a library like <code>shell-quote</code> to parse the user input into
an array of arguments without risking command injection:</p>
<sample src="examples/command-injection_shellquote.js" />
<p>Alternatively, the original example can be made safe by checking the filename
against an allowlist of safe characters before using it:</p>
<sample src="examples/command-injection_allowlist.js" />
</example>
<references>

View File

@@ -0,0 +1,12 @@
var cp = require("child_process"),
http = require('http'),
url = require('url');
var server = http.createServer(function(req, res) {
let file = url.parse(req.url, true).query.path;
// only allow safe characters in file name
if (file.match(/^[\w\.\-\/]+$/)) {
cp.execSync(`wc -l ${file}`); // GOOD
}
});