-
Notifications
You must be signed in to change notification settings - Fork 28
feat(probes): add isUnsafeSpawn #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
base: master
Are you sure you want to change the base?
Changes from 1 commit
101157f
0f8a00b
a9e7c1e
27a5848
886827a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Unsafe spawn |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Import Internal Dependencies | ||
import { getCallExpressionIdentifier } from "@nodesecure/estree-ast-utils"; | ||
import { ProbeSignals } from "../ProbeRunner.js"; | ||
|
||
/** | ||
* @description Detect spawn commands containing csrutil | ||
* @example | ||
* child_process.spawn("csrutil", ["status"]); | ||
* require("child_process").spawn("csrutil", ["disable"]); | ||
* const { spawn } = require("child_process"); | ||
* spawn("csrutil", ["status"]); | ||
*/ | ||
function validateNode(node, { tracer }) { | ||
if (node.type !== "CallExpression" || node.arguments.length === 0) { | ||
return [false]; | ||
} | ||
|
||
// Direct: child_process.spawn(...) or require("child_process").spawn(...) | ||
if ( | ||
node.callee.type === "MemberExpression" && | ||
node.callee.property.type === "Identifier" && | ||
node.callee.property.name === "spawn" | ||
) { | ||
// child_process.spawn(...) | ||
if ( | ||
node.callee.object.type === "Identifier" && | ||
node.callee.object.name === "child_process" | ||
) { | ||
return [true]; | ||
} | ||
// require("child_process").spawn(...) | ||
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]; | ||
} | ||
} | ||
|
||
return [false]; | ||
} | ||
|
||
function main(node, options) { | ||
const { sourceFile } = options; | ||
|
||
// Get the first argument of spawn which should be the command | ||
const commandArg = node.arguments[0]; | ||
if (!commandArg || commandArg.type !== "Literal") { | ||
return null; | ||
} | ||
|
||
const command = commandArg.value; | ||
if (typeof command === "string" && command.includes("csrutil")) { | ||
sourceFile.addWarning("unsafe-spawn", command, node.loc); | ||
return ProbeSignals.Skip; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
export default { | ||
name: "isUnsafeSpwan", | ||
validateNode, | ||
main | ||
}; |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,52 @@ | ||||||
// Import Node.js dependencies | ||||||
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. Make sure the linter is enabled?
Suggested change
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. There is warnings in other files too 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. yeah, but I generally put that aside until I am okay with the implementation. Rest assured that I'll apply linter later on. |
||||||
import { test } from "node:test"; | ||||||
import assert from "node:assert"; | ||||||
|
||||||
// Import Internal Dependencies | ||||||
import { parseScript, getSastAnalysis } from "../utils/index.js"; | ||||||
import isUnsafeSpawn from "../../src/probes/isUnsafeSpawn.js"; | ||||||
|
||||||
// CONSTANTS | ||||||
const kWarningUnsafeSpawn = "unsafe-spawn"; | ||||||
|
||||||
// test("should detect csrutil spawn command", () => { | ||||||
// const str = ` | ||||||
// const { spawn } = require("child_process"); | ||||||
// spawn("csrutil", ["status"]); | ||||||
// `; | ||||||
// | ||||||
// const ast = parseScript(str); | ||||||
// const sastAnalysis = getSastAnalysis(str, isUnsafeSpawn) | ||||||
// .execute(ast.body); | ||||||
// | ||||||
// const result = sastAnalysis.getWarning(kWarningUnsafeSpawn); | ||||||
// assert.equal(result.kind, kWarningUnsafeSpawn); | ||||||
// assert.equal(result.value, "csrutil"); | ||||||
// }); | ||||||
|
||||||
test("should detect csrutil spawn command with require", () => { | ||||||
const str = ` | ||||||
require("child_process").spawn("csrutil", ["disable"]); | ||||||
`; | ||||||
|
||||||
const ast = parseScript(str); | ||||||
const sastAnalysis = getSastAnalysis(str, isUnsafeSpawn) | ||||||
.execute(ast.body); | ||||||
|
||||||
const result = sastAnalysis.getWarning(kWarningUnsafeSpawn); | ||||||
assert.equal(result.kind, kWarningUnsafeSpawn); | ||||||
assert.equal(result.value, "csrutil"); | ||||||
}); | ||||||
|
||||||
test("should not detect non-csrutil spawn command", () => { | ||||||
const str = ` | ||||||
const { spawn } = require("child_process"); | ||||||
spawn("ls", ["-la"]); | ||||||
`; | ||||||
|
||||||
const ast = parseScript(str); | ||||||
const sastAnalysis = getSastAnalysis(str, isUnsafeSpawn) | ||||||
.execute(ast.body); | ||||||
|
||||||
assert.equal(sastAnalysis.warnings().length, 0); | ||||||
}); |
Uh oh!
There was an error while loading. Please reload this page.