Skip to content

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

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions docs/unsafe-spawn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Unsafe spawn
4 changes: 3 additions & 1 deletion src/ProbeRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import isBinaryExpression from "./probes/isBinaryExpression.js";
import isArrayExpression from "./probes/isArrayExpression.js";
import isESMExport from "./probes/isESMExport.js";
import isFetch from "./probes/isFetch.js";
import isUnsafeSpawn from "./probes/isUnsafeSpawn.js"

// Import Internal Dependencies
import { SourceFile } from "./SourceFile.js";
Expand Down Expand Up @@ -50,7 +51,8 @@ export class ProbeRunner {
isImportDeclaration,
isWeakCrypto,
isBinaryExpression,
isArrayExpression
isArrayExpression,
isUnsafeSpawn
];

/**
Expand Down
69 changes: 69 additions & 0 deletions src/probes/isUnsafeSpawn.js
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
};
5 changes: 5 additions & 0 deletions src/warnings.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ export const warnings = Object.freeze({
i18n: "sast_warnings.shady_link",
severity: "Warning",
experimental: false
},
"unsafe-spawn": {
i18n: "sast_warnings.unsafe-spawn",
severity: "Warning",
experimental: true
}
});

Expand Down
52 changes: 52 additions & 0 deletions test/probes/isUnsafeSpawn.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Import Node.js dependencies
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure the linter is enabled?

Suggested change
// Import Node.js dependencies
// Import Node.js Dependencies

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is warnings in other files too

Copy link
Member Author

Choose a reason for hiding this comment

The 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);
});
3 changes: 2 additions & 1 deletion types/warnings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ type WarningNameWithValue = "parsing-error"
| "suspicious-file"
| "obfuscated-code"
| "weak-crypto"
| "shady-link";
| "shady-link"
| "unsafe-spwan";
type WarningName = WarningNameWithValue | "unsafe-import";

type WarningLocation = [[number, number], [number, number]];
Expand Down