Skip to content

JS: Modeling of ShellJS functions #19422

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 8 commits into from
May 2, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Prev Previous commit
Next Next commit
Added support for shelljs.cmd and async-shelljs.asyncExec
  • Loading branch information
Napalys committed Apr 30, 2025
commit 18cea2d6a5dec92818986f34d5730f769926ead8
23 changes: 17 additions & 6 deletions javascript/ql/lib/semmle/javascript/frameworks/ShellJS.qll
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module ShellJS {
.getMember([
"exec", "cd", "cp", "touch", "chmod", "pushd", "find", "ls", "ln", "mkdir", "mv",
"rm", "cat", "head", "sort", "tail", "uniq", "grep", "sed", "to", "toEnd", "echo",
"which",
"which", "cmd", "asyncExec"
])
.getReturn()
}
Expand Down Expand Up @@ -154,16 +154,27 @@ module ShellJS {
}

/**
* A call to `shelljs.exec()` modeled as command execution.
* A call to `shelljs.exec()`, `shelljs.cmd()`, or `async-shelljs.asyncExec()` modeled as command execution.
*/
private class ShellJSExec extends SystemCommandExecution, ShellJSCall {
ShellJSExec() { name = "exec" }

override DataFlow::Node getACommandArgument() { result = this.getArgument(0) }
ShellJSExec() { name = ["exec", "cmd", "asyncExec"] }

override DataFlow::Node getACommandArgument() {
if name = "cmd"
then
result = this.getArgument(_) and
not (
result = this.getLastArgument() and
exists(this.getOptionsArg())
)
else
// For exec/asyncExec: only first argument is command
result = this.getArgument(0)
}

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

override predicate isSync() { none() }
override predicate isSync() { name = "cmd" }

override DataFlow::Node getOptionsArg() {
result = this.getLastArgument() and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ test_SystemCommandExecution
| tst.js:14:1:14:27 | shelljs ... ts, cb) |
| tst.js:60:1:60:51 | shelljs ... ec(cmd) |
| tst.js:61:1:61:27 | shelljs ... ec(cmd) |
| tst.js:63:1:63:37 | shelljs ... ptions) |
| tst.js:64:1:64:16 | shelljs.cmd(cmd) |
| tst.js:68:1:68:36 | shelljs ... ts, cb) |
test_FileNameSource
| tst.js:15:1:15:26 | shelljs ... file2) |
| tst.js:24:1:24:16 | shelljs.ls(file) |
Expand Down