-
Notifications
You must be signed in to change notification settings - Fork 28
feat(probes): add unsafe-command
#327
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
Open
tony-go
wants to merge
24
commits into
master
Choose a base branch
from
is-unsafe-spawn
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
101157f
feat(probes): add isUnsafeSpawn
tony-go 0f8a00b
refactor(probes): add isUnsafeCommand helper
tony-go a9e7c1e
feat(probes): handle standalone spawn func
tony-go 27a5848
test: add member
tony-go 886827a
test: add last (missing) case
tony-go 3660cce
test: remove child_process member test and skip hide one
tony-go c3206e6
Merge branch 'master' into is-unsafe-spawn
tony-go 9db1db3
fix: lint
tony-go 2ede3c1
doc: add unsafe-spawn
tony-go 0a1502a
doc: update readme
tony-go 37ecb5e
refactor(probes): switch to unsafe-command
tony-go 7d9f597
fix: lint
tony-go 656e4f7
test(probes): switch assert to strict
tony-go 2d78a19
fix: add missing comment
tony-go 58e1e9a
refactor(probes): simplify isUnsafeCommand helper
tony-go d677a79
doc: fix path in readme
tony-go 0265b77
test(probes): add exec cases
tony-go b0975f3
fix: remove deps
tony-go 08d05fb
fix(probes): rebuild full command for spawned case
tony-go aa08db9
test: use strict assert
tony-go f2284dd
feat(probes): add spawnSync and execSync to unsafe-command
tony-go 6312aed
refactor(probes): add helper to detect func name
tony-go 1eb157c
docs(probes): update for sync
tony-go 38224e2
fix: lint
tony-go 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,22 @@ | ||
# Unsafe command | ||
|
||
| Code | Severity | i18n | Experimental | | ||
| --- | --- | --- | :-: | | ||
| unsafe-command | `Warning` | `sast_warnings.unsafe_command` | ✅ | | ||
|
||
## Introduction | ||
|
||
This warning identifies potentially dangerous use of the `spawn()` or `exec()` function from the `child_process` module. | ||
Spawning system-level commands can introduce security risks, especially if user-controlled input is involved or if the | ||
command itself is sensitive (e.g., tools that query or change system configurations). This warning identifies also | ||
commands passed to `spawnSync()` and `execSync()`. | ||
|
||
> [!NOTE] | ||
> This rule is experimental. The list of suspicious commands is not exhaustive and will evolve over time. | ||
|
||
## Example | ||
|
||
```js | ||
const { spawn } = require("child_process"); | ||
spawn("csrutil", ["status"]); | ||
``` |
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,111 @@ | ||||||
// Import Internal Dependencies | ||||||
import { ProbeSignals } from "../ProbeRunner.js"; | ||||||
|
||||||
// CONSTANTS | ||||||
const kUnsafeCommands = ["csrutil"]; | ||||||
|
||||||
function isUnsafeCommand(command) { | ||||||
return kUnsafeCommands.some((unsafeCommand) => command.includes(unsafeCommand)); | ||||||
} | ||||||
|
||||||
function isSpwanOrExec(name) { | ||||||
return name === "spawn" || | ||||||
name === "exec" || | ||||||
name === "spawnSync" || | ||||||
name === "execSync"; | ||||||
} | ||||||
|
||||||
/** | ||||||
* @description Detect spawn or exec unsafe commands | ||||||
* @example | ||||||
* child_process.spawn("csrutil", ["status"]); | ||||||
* | ||||||
* require("child_process").spawn("csrutil", ["disable"]); | ||||||
* | ||||||
* const { exec } = require("child_process"); | ||||||
* exec("csrutil status"); | ||||||
*/ | ||||||
function validateNode(node) { | ||||||
if (node.type !== "CallExpression" || node.arguments.length === 0) { | ||||||
return [false]; | ||||||
} | ||||||
|
||||||
// const { spawn } = require("child_process"); | ||||||
// spawn("...", ["..."]); | ||||||
// or | ||||||
// const { exec } = require("child_process"); | ||||||
// exec(...); | ||||||
if (node.type === "CallExpression" && | ||||||
node.callee.type === "Identifier" && | ||||||
isSpwanOrExec(node.callee.name) | ||||||
) { | ||||||
return [true, node.callee.name]; | ||||||
} | ||||||
|
||||||
// child_process.spawn(...) or require("child_process").spawn(...) | ||||||
// child_process.exec(...) or require("child_process").exec(...) | ||||||
if ( | ||||||
node.callee.type === "MemberExpression" && | ||||||
node.callee.property.type === "Identifier" && | ||||||
isSpwanOrExec(node.callee.property.name) | ||||||
) { | ||||||
// child_process.spawn(...) | ||||||
// child_process.exec(...) | ||||||
if ( | ||||||
node.callee.object.type === "Identifier" && | ||||||
node.callee.object.name === "child_process" | ||||||
) { | ||||||
return [true, node.callee.property.name]; | ||||||
} | ||||||
// require("child_process").spawn(...) | ||||||
// require("child_process").exec(...) | ||||||
if ( | ||||||
node.callee.object.type === "CallExpression" && | ||||||
node.callee.object.callee.type === "Identifier" && | ||||||
node.callee.object.callee.name === "require" && | ||||||
node.callee.object.arguments.length === 1 && | ||||||
node.callee.object.arguments[0].type === "Literal" && | ||||||
node.callee.object.arguments[0].value === "child_process" | ||||||
) { | ||||||
return [true, node.callee.property.name]; | ||||||
} | ||||||
} | ||||||
|
||||||
return [false]; | ||||||
} | ||||||
|
||||||
function main(node, options) { | ||||||
const { sourceFile } = options; | ||||||
|
||||||
const commandArg = node.arguments[0]; | ||||||
if (!commandArg || commandArg.type !== "Literal") { | ||||||
return null; | ||||||
} | ||||||
|
||||||
let command = commandArg.value; | ||||||
if (typeof command === "string" && isUnsafeCommand(command)) { | ||||||
// Spwaned command arguments are filled into an Array | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
// as second arguments. This is why we should add them | ||||||
// manually to the command string. | ||||||
if (options.data === "spawn" || options.data === "spawnSync") { | ||||||
const args = node.arguments.at(1); | ||||||
if (args && Array.isArray(args.elements)) { | ||||||
args.elements.forEach((element) => { | ||||||
command += ` ${element.value}`; | ||||||
}); | ||||||
} | ||||||
} | ||||||
|
||||||
sourceFile.addWarning("unsafe-command", command, node.loc); | ||||||
|
||||||
return ProbeSignals.Skip; | ||||||
} | ||||||
|
||||||
return null; | ||||||
} | ||||||
|
||||||
export default { | ||||||
name: "isUnsafeCommand", | ||||||
validateNode, | ||||||
main | ||||||
}; |
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.