Skip to content

Commit 073a43c

Browse files
authored
Merge pull request github#5606 from erik-krogh/shellInput
Approved by esbena
2 parents 461d4e4 + c9f54ea commit 073a43c

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

javascript/ql/src/semmle/javascript/security/dataflow/UnsafeShellCommandConstructionCustomizations.qll

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,27 @@ module UnsafeShellCommandConstruction {
190190
override DataFlow::Node getAlertLocation() { result = this }
191191
}
192192

193+
/**
194+
* A joined path (`path.{resolve/join}(..)`) that is later executed as a shell command.
195+
* Joining a path is similar to string concatenation that automatically inserts slashes.
196+
*/
197+
class JoinedPathEndingInCommandExecutionSink extends Sink {
198+
DataFlow::MethodCallNode joinCall;
199+
SystemCommandExecution sys;
200+
201+
JoinedPathEndingInCommandExecutionSink() {
202+
this = joinCall.getAnArgument() and
203+
joinCall = DataFlow::moduleMember("path", ["resolve", "join"]).getACall() and
204+
joinCall = isExecutedAsShellCommand(DataFlow::TypeBackTracker::end(), sys)
205+
}
206+
207+
override string getSinkType() { result = "Path concatenation" }
208+
209+
override SystemCommandExecution getCommandExecution() { result = sys }
210+
211+
override DataFlow::Node getAlertLocation() { result = this }
212+
}
213+
193214
/**
194215
* A sanitizer like: "'"+name.replace(/'/g,"'\\''")+"'"
195216
* Which sanitizes on Unix.

javascript/ql/test/query-tests/Security/CWE-078/UnsafeShellCommandConstruction.expected

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,11 @@ nodes
237237
| lib/lib.js:446:20:446:23 | name |
238238
| lib/lib.js:447:25:447:28 | name |
239239
| lib/lib.js:447:25:447:28 | name |
240+
| lib/lib.js:477:33:477:38 | config |
241+
| lib/lib.js:477:33:477:38 | config |
242+
| lib/lib.js:478:27:478:32 | config |
243+
| lib/lib.js:478:27:478:46 | config.installedPath |
244+
| lib/lib.js:478:27:478:46 | config.installedPath |
240245
| lib/subLib/index.js:3:28:3:31 | name |
241246
| lib/subLib/index.js:3:28:3:31 | name |
242247
| lib/subLib/index.js:4:22:4:25 | name |
@@ -529,6 +534,10 @@ edges
529534
| lib/lib.js:446:20:446:23 | name | lib/lib.js:447:25:447:28 | name |
530535
| lib/lib.js:446:20:446:23 | name | lib/lib.js:447:25:447:28 | name |
531536
| lib/lib.js:446:20:446:23 | name | lib/lib.js:447:25:447:28 | name |
537+
| lib/lib.js:477:33:477:38 | config | lib/lib.js:478:27:478:32 | config |
538+
| lib/lib.js:477:33:477:38 | config | lib/lib.js:478:27:478:32 | config |
539+
| lib/lib.js:478:27:478:32 | config | lib/lib.js:478:27:478:46 | config.installedPath |
540+
| lib/lib.js:478:27:478:32 | config | lib/lib.js:478:27:478:46 | config.installedPath |
532541
| lib/subLib/index.js:3:28:3:31 | name | lib/subLib/index.js:4:22:4:25 | name |
533542
| lib/subLib/index.js:3:28:3:31 | name | lib/subLib/index.js:4:22:4:25 | name |
534543
| lib/subLib/index.js:3:28:3:31 | name | lib/subLib/index.js:4:22:4:25 | name |
@@ -605,5 +614,6 @@ edges
605614
| lib/lib.js:436:19:436:22 | last | lib/lib.js:414:40:414:43 | name | lib/lib.js:436:19:436:22 | last | $@ based on $@ is later used in $@. | lib/lib.js:436:19:436:22 | last | Shell argument | lib/lib.js:414:40:414:43 | name | library input | lib/lib.js:428:2:428:70 | spawn(" ... WN_OPT) | shell command |
606615
| lib/lib.js:442:12:442:27 | "rm -rf " + name | lib/lib.js:441:39:441:42 | name | lib/lib.js:442:24:442:27 | name | $@ based on $@ is later used in $@. | lib/lib.js:442:12:442:27 | "rm -rf " + name | String concatenation | lib/lib.js:441:39:441:42 | name | library input | lib/lib.js:442:2:442:28 | asyncEx ... + name) | shell command |
607616
| lib/lib.js:447:13:447:28 | "rm -rf " + name | lib/lib.js:446:20:446:23 | name | lib/lib.js:447:25:447:28 | name | $@ based on $@ is later used in $@. | lib/lib.js:447:13:447:28 | "rm -rf " + name | String concatenation | lib/lib.js:446:20:446:23 | name | library input | lib/lib.js:447:3:447:29 | asyncEx ... + name) | shell command |
617+
| lib/lib.js:478:27:478:46 | config.installedPath | lib/lib.js:477:33:477:38 | config | lib/lib.js:478:27:478:46 | config.installedPath | $@ based on $@ is later used in $@. | lib/lib.js:478:27:478:46 | config.installedPath | Path concatenation | lib/lib.js:477:33:477:38 | config | library input | lib/lib.js:479:12:479:20 | exec(cmd) | shell command |
608618
| lib/subLib/index.js:4:10:4:25 | "rm -rf " + name | lib/subLib/index.js:3:28:3:31 | name | lib/subLib/index.js:4:22:4:25 | name | $@ based on $@ is later used in $@. | lib/subLib/index.js:4:10:4:25 | "rm -rf " + name | String concatenation | lib/subLib/index.js:3:28:3:31 | name | library input | lib/subLib/index.js:4:2:4:26 | cp.exec ... + name) | shell command |
609619
| lib/subLib/index.js:8:10:8:25 | "rm -rf " + name | lib/subLib/index.js:7:32:7:35 | name | lib/subLib/index.js:8:22:8:25 | name | $@ based on $@ is later used in $@. | lib/subLib/index.js:8:10:8:25 | "rm -rf " + name | String concatenation | lib/subLib/index.js:7:32:7:35 | name | library input | lib/subLib/index.js:8:2:8:26 | cp.exec ... + name) | shell command |

javascript/ql/test/query-tests/Security/CWE-078/lib/lib.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,3 +468,13 @@ Object.defineProperties(
468468
)
469469
)
470470
);
471+
472+
const path = require('path');
473+
const {promisify} = require('util');
474+
475+
const exec = promisify(require('child_process').exec);
476+
477+
module.exports = function check(config) {
478+
const cmd = path.join(config.installedPath, 'myBinary -v'); // NOT OK
479+
return exec(cmd);
480+
}

0 commit comments

Comments
 (0)