-
Notifications
You must be signed in to change notification settings - Fork 1.8k
JS: second-order-command-injection #11013
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
Conversation
QHelp previews: javascript/ql/src/Security/CWE-078/SecondOrderCommandInjection.qhelpSecond order command injectionSome shell commands, like RecommendationSanitize user input before passing it to the shell command. For example, ensure that URLs are valid and do not contain malicious commands. ExampleThe following example shows code that executes const express = require("express");
const app = express();
const cp = require("child_process");
app.get("/ls-remote", (req, res) => {
const remote = req.query.remote;
cp.execFile("git", ["ls-remote", remote]); // NOT OK
}); The problem has been fixed in the snippet below, where the URL is validated before being passed to the shell command. const express = require("express");
const app = express();
const cp = require("child_process");
app.get("/ls-remote", (req, res) => {
const remote = req.query.remote;
if (!(remote.startsWith("git@") || remote.startsWith("https://"))) {
throw new Error("Invalid remote: " + remote);
}
cp.execFile("git", ["ls-remote", remote]); // OK
}); References
|
...ipt/ql/lib/semmle/javascript/security/dataflow/SecondOrderCommandInjectionCustomizations.qll
Fixed
Show fixed
Hide fixed
...ipt/ql/lib/semmle/javascript/security/dataflow/SecondOrderCommandInjectionCustomizations.qll
Fixed
Show fixed
Hide fixed
...ipt/ql/lib/semmle/javascript/security/dataflow/SecondOrderCommandInjectionCustomizations.qll
Fixed
Show fixed
Hide fixed
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.
Looks great! Just a minor comment in the change note
--- | ||
* Added a new query, `js/second-order-command-line-injection`, to detect shell | ||
commands that may execute arbitrary code when the user has control over | ||
the arguments to a command-line program. |
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'd add something about which command names are currently recognized by the query, for example
This currently flags up unsafe invocations of
git
andhg
.
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.
Sorry, just a few more comments.
javascript/ql/src/Security/CWE-078/SecondOrderCommandInjection.qhelp
Outdated
Show resolved
Hide resolved
javascript/ql/src/Security/CWE-078/SecondOrderCommandInjection.ql
Outdated
Show resolved
Hide resolved
javascript/ql/src/Security/CWE-078/SecondOrderCommandInjection.qhelp
Outdated
Show resolved
Hide resolved
Co-authored-by: Asger F <asgerf@github.com>
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.
Great! LGTM
Lets get some eyes from the doc team. |
👋 Docs first responder here! I've put this on our review board for a writer to review |
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.
@erik-krogh - this LGTM from a Docs perspective ✨
I've made a comment about improving the query description and adding a missing whitespace in the References section. The rest is very minor. Feel free to ignore any comment you don't agree with.
@@ -0,0 +1,25 @@ | |||
/** | |||
* @name Second order command injection | |||
* @description Some shell programs allow arbitrary command execution via their command line arguments. |
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.
Could we rewrite this so it follows the preferred syntax for a query description which is "Syntax X causes behavior Y."
?
I think it would be clearer to users if we went straight to the point.
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've rewritten it to: Using user controlled data as arguments to some commands, such as git clone, can allow arbitrary commands to be executed.
<references> | ||
<li>Max Justicz: <a href="https://justi.cz/security/2021/04/20/cocoapods-rce.html">Hacking 3,000,000 apps at once through CocoaPods</a>.</li> | ||
<li>Git: <a href="https://git-scm.com/docs/git-ls-remote/2.22.0#Documentation/git-ls-remote.txt---upload-packltexecgt">Git - git-ls-remote Documentation</a>.</li> | ||
<li>OWASP:<a href="https://www.owasp.org/index.php/Command_Injection">Command Injection</a>.</li> |
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.
Missing witespace
<li>OWASP:<a href="https://www.owasp.org/index.php/Command_Injection">Command Injection</a>.</li> | |
<li>OWASP: <a href="https://www.owasp.org/index.php/Command_Injection">Command Injection</a>.</li> |
<overview> | ||
<p> | ||
Some shell commands, like <code>git ls-remote</code>, can execute | ||
arbitrary commands if a user provides a malicious URL that can start with |
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.
Should we simplify here, or isn't it accurate to say this:
arbitrary commands if a user provides a malicious URL that can start with | |
arbitrary commands if a user provides a malicious URL that starts with |
javascript/ql/src/Security/CWE-078/SecondOrderCommandInjection.ql
Outdated
Show resolved
Hide resolved
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.
LGTM ✨
Thanks for the docs updates!
Co-authored-by: mc <42146119+mchammer01@users.noreply.github.com>
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.
CVE-2022-25900: FN (dynamically chosen
git
cmd).CVE-2022-24437: TP/TN
CVE-2022-25865: TP/TN
CVE-2022-24066: FN (we don't recognize the source due to very dynamic initialization, and we don't recognize the sink due to some dynamic command construction).
CVE-2022-1440: FN (this is more for
js/shell-command-constructed-from-input
, but they roll their own shell sanitization, which it doens't track through).CVE-2022-25766: FN (dynamic construction of the arguments, so we don't recognize the
git
invocation).CVE-2022-24433: FN (same project as CVE-2022-24066).
This blog post is a nice introduction to the vulnerability: https://justi.cz/security/2021/04/20/cocoapods-rce.html
The sinks for this query are arguments to e.g.
cp.execFile("git", ["clone", <sink>])
.But also functions that just forwards their arguments to a shell executions, because I've seen that pattern in a few CVEs.
E.g.
Evaluation was uneventful.