-
Notifications
You must be signed in to change notification settings - Fork 2k
JS: add query for useless use of cat #2867
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
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 344060e
accept IO redirections as OK
erik-krogh bdab9ee
change useless cat query to only flag instances that can be re-writte…
erik-krogh 56f3e43
update expected output
erik-krogh d4e73df
remove dead predicate
erik-krogh a5fdcb6
restricts alerts to the first line
erik-krogh 558beb7
simplify the output file argument
erik-krogh a193cb1
support arrow functions in the callbacks
erik-krogh b5ef45e
add isSync predicate to SystemCommandExecution
erik-krogh 12c0291
require that an options object has a known set of properties
erik-krogh b1cbfce
use SystemCommandExecution and a few small fixes
erik-krogh b2ccec2
require the file to be non-empty
erik-krogh 924272a
insert placeholder qhelp
erik-krogh 6ea1453
small changes based on review
erik-krogh 75410e5
big refactor of UselessUseOfCal
erik-krogh 44db0f4
better printing of the options arg
erik-krogh 75c1852
doc changes from review
erik-krogh 473787a
refactor the getOptionsArg predicate into the SystemCommandExecution …
erik-krogh a768e93
complete qldoc
erik-krogh 051de24
change regexpMatch to regexpFind
erik-krogh fb94af9
remove the last dependency on PrettyPrinting
erik-krogh a779ae5
add qhelp
erik-krogh b72404d
add change note
erik-krogh b20e852
add default message if not pretty printed call can be created
erik-krogh afd6ea2
small correction in doc + autoformat
erik-krogh d540cae
Apply suggestions from code review
erik-krogh 87d283a
add tests for third party command execution libraries (and two small …
erik-krogh 8d26f32
arg -> param
erik-krogh c83c27c
add extra sanity-check that the output looks good
erik-krogh bb911bb
Apply suggestions from code review
erik-krogh a872d7c
add comment about negative optionsArg
erik-krogh 17f1974
Apply suggestions from code review
erik-krogh 922779e
remove double a/an and adjust line lenghts
erik-krogh d8a96dd
change name to suggestion from previous code review
erik-krogh ce9cd53
Merge remote-tracking branch 'upstream/master' into UselessCat
erik-krogh 5e0ae7b
add end </p> tag
erik-krogh 391b6a8
add link to The Useless Use of Cat Award
erik-krogh 019266e
change name of Useless cat
erik-krogh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
5 changes: 5 additions & 0 deletions
5
javascript/ql/src/Security/CWE-078/examples/useless-cat-fixed.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
instanceofchecks 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
instanceofcases.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
👍
(I found a bug or two while making those tests)