Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
73a7d40
add query for useless use of cat
erik-krogh Feb 18, 2020
344060e
accept IO redirections as OK
erik-krogh Feb 19, 2020
bdab9ee
change useless cat query to only flag instances that can be re-writte…
erik-krogh Feb 19, 2020
56f3e43
update expected output
erik-krogh Feb 20, 2020
d4e73df
remove dead predicate
erik-krogh Feb 20, 2020
a5fdcb6
restricts alerts to the first line
erik-krogh Feb 20, 2020
558beb7
simplify the output file argument
erik-krogh Feb 20, 2020
a193cb1
support arrow functions in the callbacks
erik-krogh Feb 20, 2020
b5ef45e
add isSync predicate to SystemCommandExecution
erik-krogh Feb 20, 2020
12c0291
require that an options object has a known set of properties
erik-krogh Feb 20, 2020
b1cbfce
use SystemCommandExecution and a few small fixes
erik-krogh Feb 20, 2020
b2ccec2
require the file to be non-empty
erik-krogh Feb 20, 2020
924272a
insert placeholder qhelp
erik-krogh Feb 20, 2020
6ea1453
small changes based on review
erik-krogh Feb 21, 2020
75410e5
big refactor of UselessUseOfCal
erik-krogh Feb 21, 2020
44db0f4
better printing of the options arg
erik-krogh Feb 21, 2020
75c1852
doc changes from review
erik-krogh Feb 24, 2020
473787a
refactor the getOptionsArg predicate into the SystemCommandExecution …
erik-krogh Feb 24, 2020
a768e93
complete qldoc
erik-krogh Feb 24, 2020
051de24
change regexpMatch to regexpFind
erik-krogh Feb 24, 2020
fb94af9
remove the last dependency on PrettyPrinting
erik-krogh Feb 24, 2020
a779ae5
add qhelp
erik-krogh Feb 24, 2020
b72404d
add change note
erik-krogh Feb 24, 2020
b20e852
add default message if not pretty printed call can be created
erik-krogh Feb 24, 2020
afd6ea2
small correction in doc + autoformat
erik-krogh Feb 24, 2020
d540cae
Apply suggestions from code review
erik-krogh Feb 25, 2020
87d283a
add tests for third party command execution libraries (and two small …
erik-krogh Feb 25, 2020
8d26f32
arg -> param
erik-krogh Feb 25, 2020
c83c27c
add extra sanity-check that the output looks good
erik-krogh Feb 25, 2020
bb911bb
Apply suggestions from code review
erik-krogh Feb 27, 2020
a872d7c
add comment about negative optionsArg
erik-krogh Feb 27, 2020
17f1974
Apply suggestions from code review
erik-krogh Feb 28, 2020
922779e
remove double a/an and adjust line lenghts
erik-krogh Feb 28, 2020
d8a96dd
change name to suggestion from previous code review
erik-krogh Feb 28, 2020
ce9cd53
Merge remote-tracking branch 'upstream/master' into UselessCat
erik-krogh Feb 28, 2020
5e0ae7b
add end </p> tag
erik-krogh Feb 28, 2020
391b6a8
add link to The Useless Use of Cat Award
erik-krogh Mar 2, 2020
019266e
change name of Useless cat
erik-krogh Mar 2, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions change-notes/1.24/analysis-javascript.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
| Polynomial regular expression used on uncontrolled data (`js/polynomial-redos`) | security, external/cwe/cwe-730, external/cwe/cwe-400 | Highlights expensive regular expressions that may be used on malicious input. Results are shown on LGTM by default. |
| Prototype pollution in utility function (`js/prototype-pollution-utility`) | security, external/cwe/cwe-400, external/cwe/cwe-471 | Highlights recursive copying operations that are susceptible to prototype pollution. Results are shown on LGTM by default. |
| Unsafe jQuery plugin (`js/unsafe-jquery-plugin`) | Highlights potential XSS vulnerabilities in unsafely designed jQuery plugins. Results are shown on LGTM by default. |
| Unnecessary use of `cat` process (`js/unnecessary-use-of-cat`) | correctness, security, maintainability | Highlights command executions of `cat` where the fs API should be used instead. Results are shown on LGTM by default. |


## Changes to existing queries

Expand Down
45 changes: 45 additions & 0 deletions javascript/ql/src/Security/CWE-078/UselessUseOfCat.qhelp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>Using the unix command <code>cat</code> only to read a file is an
unnecessarily complex way to achieve something that can be done in a simpler
and safer manner using the Node.js <code>fs.readFile</code> API.
</p>
<p>
The use of <code>cat</code> for simple file reads leads to code that is
unportable, inefficient, complex, and can lead to subtle bugs or even
security vulnerabilities.
</p>
</overview>
<recommendation>
<p>
Use <code>fs.readFile</code> or <code>fs.readFileSync</code> to read files
from the file system.
</p>
</recommendation>
<example>

<p>The following example shows code that reads a file using <code>cat</code>:</p>

<sample src="examples/useless-cat.js"/>

<p>The code in the example will break if the input <code>name</code> contains
special characters (including space). Additionally, it does not work on Windows
and if the input is user-controlled, a command injection attack can happen.</p>

<p>The <code>fs.readFile</code> API should be used to avoid these potential issues: </p>

<sample src="examples/useless-cat-fixed.js"/>

</example>
<references>

<li>OWASP: <a href="https://www.owasp.org/index.php/Command_Injection">Command Injection</a>.</li>
<li>Node.js: <a href="https://nodejs.org/api/fs.html">File System API</a>.</li>
<li><a href="http://porkmail.org/era/unix/award.html#cat">The Useless Use of Cat Award</a>.</li>


</references>
</qhelp>
25 changes: 25 additions & 0 deletions javascript/ql/src/Security/CWE-078/UselessUseOfCat.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @name Unnecessary use of `cat` process
* @description Using the `cat` process to read a file is unnecessarily complex, inefficient, unportable, and can lead to subtle bugs, or even security vulnerabilities.
* @kind problem
* @problem.severity error
* @precision high
* @id js/unnecessary-use-of-cat
* @tags correctness
* security
* maintainability
*/

import javascript
import semmle.javascript.security.UselessUseOfCat
import semmle.javascript.RestrictedLocations

from UselessCat cat, string message
where
message = " Can be replaced with: " + PrettyPrintCatCall::createReadFileCall(cat)
or
not exists(PrettyPrintCatCall::createReadFileCall(cat)) and
if cat.isSync()
then message = " Can be replaced with a call to fs.readFileSync(..)."
else message = " Can be replaced with a call to fs.readFile(..)."
select cat.asExpr().(FirstLineOf), "Unnecessary use of `cat` process." + message
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var fs = require('fs');

module.exports = function (name) {
return fs.readFileSync(name).toString();
};
5 changes: 5 additions & 0 deletions javascript/ql/src/Security/CWE-078/examples/useless-cat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var child_process = require('child_process');

module.exports = function (name) {
return child_process.execSync("cat " + name).toString();
};
8 changes: 8 additions & 0 deletions javascript/ql/src/semmle/javascript/Concepts.qll
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ abstract class SystemCommandExecution extends DataFlow::Node {
* to the command.
*/
DataFlow::Node getArgumentList() { none() }

/** Holds if the command execution happens synchronously. */
abstract predicate isSync();

/**
* Gets the data-flow node (if it exists) for an options argument.
*/
abstract DataFlow::Node getOptionsArg();
}

/**
Expand Down
16 changes: 16 additions & 0 deletions javascript/ql/src/semmle/javascript/frameworks/NodeJSLib.qll
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,22 @@ module NodeJSLib {
// all of the above methods take the argument list as their second argument
result = getArgument(1)
}

override predicate isSync() {
"Sync" = methodName.suffix(methodName.length() - 4)
}

override DataFlow::Node getOptionsArg() {
not result.getALocalSource() instanceof DataFlow::FunctionNode and // looks like callback
not result.getALocalSource() instanceof DataFlow::ArrayCreationNode and // looks like argumentlist
not result = getArgument(0) and
// fork/spawn and all sync methos always has options as the last argument
if methodName.regexpMatch("fork.*") or methodName.regexpMatch("spawn.*") or methodName.regexpMatch(".*Sync") then
result = getLastArgument()
else
// the rest (exec/execFile) has the options argument as their second last.
result = getArgument(this.getNumArgument() - 2)
}
}

/**
Expand Down
9 changes: 9 additions & 0 deletions javascript/ql/src/semmle/javascript/frameworks/ShellJS.qll
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,15 @@ module ShellJS {
override DataFlow::Node getACommandArgument() { result = getArgument(0) }

override predicate isShellInterpreted(DataFlow::Node arg) { arg = getACommandArgument() }

override predicate isSync() {none ()}

override DataFlow::Node getOptionsArg() {
result = getLastArgument() and
not result = getArgument(0) and
not result.getALocalSource() instanceof DataFlow::FunctionNode and // looks like callback

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am on the fence in suggestion that we move these two instanceof checks to the abstract class, or remove them completely. I assume you have encountered a problem when we did not have the checks? Or is this just a leftover from the catch-all heuristic we had prior to this commit?
At the very least, it would be nice with an explicitly test that exercises these instanceof cases.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume you have encountered a problem when we did not have the checks?

Yep.

All of these command executions methods have variations of the same API: exec(command[, options][, callback]).

The API is implemented using runtime detection of the types of the arguments, so we have to do something similar.

At the very least, it would be nice with an explicitly test that exercises these instanceof cases.

👍
(I found a bug or two while making those tests)

not result.getALocalSource() instanceof DataFlow::ArrayCreationNode // looks like argumentlist
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ import javascript

private class SystemCommandExecutors extends SystemCommandExecution, DataFlow::InvokeNode {
int cmdArg;
int optionsArg; // either a positive number representing the n'th argument, or a negative number representing the n'th last argument (e.g. -2 is the second last argument).
boolean shell;
boolean sync;

SystemCommandExecutors() {
exists(string mod, DataFlow::SourceNode callee |
exists(string method |
mod = "cross-spawn" and method = "sync" and cmdArg = 0 and shell = false
mod = "cross-spawn" and method = "sync" and cmdArg = 0 and shell = false and optionsArg = -1
or
mod = "execa" and
optionsArg = -1 and
(
shell = false and
(
Expand All @@ -30,27 +33,30 @@ private class SystemCommandExecutors extends SystemCommandExecution, DataFlow::I
) and
cmdArg = 0
|
callee = DataFlow::moduleMember(mod, method)
callee = DataFlow::moduleMember(mod, method) and
sync = getSync(method)
)
or
sync = false and
(
shell = false and
(
mod = "cross-spawn" and cmdArg = 0
mod = "cross-spawn" and cmdArg = 0 and optionsArg = -1
or
mod = "cross-spawn-async" and cmdArg = 0
mod = "cross-spawn-async" and cmdArg = 0 and optionsArg = -1
or
mod = "exec-async" and cmdArg = 0
mod = "exec-async" and cmdArg = 0 and optionsArg = -1
or
mod = "execa" and cmdArg = 0
mod = "execa" and cmdArg = 0 and optionsArg = -1
)
or
shell = true and
(
mod = "exec" and
optionsArg = -2 and
cmdArg = 0
or
mod = "remote-exec" and cmdArg = 1
mod = "remote-exec" and cmdArg = 1 and optionsArg = -1
)
) and
callee = DataFlow::moduleImport(mod)
Expand All @@ -64,4 +70,30 @@ private class SystemCommandExecutors extends SystemCommandExecution, DataFlow::I
override predicate isShellInterpreted(DataFlow::Node arg) {
arg = getACommandArgument() and shell = true
}

override DataFlow::Node getArgumentList() { shell = false and result = getArgument(1) }

override predicate isSync() { sync = true }

override DataFlow::Node getOptionsArg() {
(
if optionsArg < 0
then
result = getArgument(getNumArgument() + optionsArg) and
getNumArgument() + optionsArg > cmdArg
else result = getArgument(optionsArg)
) and
not result.getALocalSource() instanceof DataFlow::FunctionNode and // looks like callback
not result.getALocalSource() instanceof DataFlow::ArrayCreationNode // looks like argumentlist
}
}

/**
* Gets a boolean reflecting if the name ends with "sync" or "Sync".
*/
bindingset[name]
private boolean getSync(string name) {
if name.suffix(name.length() - 4) = "Sync" or name.suffix(name.length() - 4) = "sync"
then result = true
else result = false
}
Loading