Skip to content

Commit a779ae5

Browse files
committed
add qhelp
1 parent fb94af9 commit a779ae5

File tree

3 files changed

+42
-15
lines changed

3 files changed

+42
-15
lines changed

javascript/ql/src/Security/CWE-078/UselessUseOfCat.qhelp

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,45 @@
33
"qhelp.dtd">
44
<qhelp>
55
<overview>
6-
<p>
7-
Useless use of cat
8-
</p>
9-
10-
6+
<p>Using the unix command <code>cat</code> to simply read a file is a
7+
unnecessarily complex way to achieve something that can be done simpler and
8+
safer using the Node.js <code>fs.readFile</code> API.
9+
</p>
10+
<p>
11+
The use of <code>cat</code> for simple file reads leads to code that is
12+
unportable, inefficient, complex, and can lead to subtle bugs or even
13+
security vulnerabilities.
14+
</p>
1115
</overview>
1216
<recommendation>
13-
<p>
14-
TODO: This is a placeholder
15-
</p>
16-
17+
<p>
18+
Use <code>fs.readFile</code> or <code>fs.readFileSync</code> to read files
19+
from the file system.
20+
</p>
1721
</recommendation>
1822
<example>
19-
<p>
20-
</p>
23+
24+
<p>The following example shows code that reads a file using <code>cat</code>:</p>
25+
26+
<sample src="examples/useless-cat.js"/>
27+
28+
<p>The code in the example will break if the input <code>name</code> contain
29+
special characters (including space), the code does not work on windows,
30+
and if the input is user controlled a command injection attack can happen.</p>
31+
32+
<p>To avoid these potential issues the <code>fs.readFile</code> API can be
33+
used instead: </p>
34+
35+
<sample src="examples/useless-cat-fixed.js"/>
36+
2137
</example>
2238
<references>
2339

24-
<li>
25-
OWASP:
26-
<a href="https://www.owasp.org/index.php/Command_Injection">Command Injection</a>.
27-
</li>
40+
<li>
41+
OWASP: <a href="https://www.owasp.org/index.php/Command_Injection">Command Injection</a>.
42+
Node.js: <a href="https://nodejs.org/api/fs.html">File System API</a>.
43+
</li>
44+
2845

2946
</references>
3047
</qhelp>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var fs = require('fs');
2+
3+
module.exports = function (name) {
4+
return fs.readFileSync(name).toString();
5+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var child_process = require('child_process');
2+
3+
module.exports = function (name) {
4+
return child_process.execSync("cat " + name).toString();
5+
};

0 commit comments

Comments
 (0)