mirror of
https://github.com/AikidoSec/safe-chain.git
synced 2026-05-26 12:10:49 +00:00
Merge pull request #99 from AikidoSec/remove-sync
Remove `safeSpawnSync` (unused)
This commit is contained in:
commit
dc4352bffb
2 changed files with 44 additions and 69 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
import { spawnSync, spawn } from "child_process";
|
import { spawn } from "child_process";
|
||||||
|
|
||||||
function escapeArg(arg) {
|
function escapeArg(arg) {
|
||||||
// If argument contains spaces or quotes, wrap in double quotes and escape double quotes
|
// If argument contains spaces or quotes, wrap in double quotes and escape double quotes
|
||||||
|
|
@ -13,11 +13,6 @@ function buildCommand(command, args) {
|
||||||
return `${command} ${escapedArgs.join(" ")}`;
|
return `${command} ${escapedArgs.join(" ")}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function safeSpawnSync(command, args, options = {}) {
|
|
||||||
const fullCommand = buildCommand(command, args);
|
|
||||||
return spawnSync(fullCommand, { ...options, shell: true });
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function safeSpawn(command, args, options = {}) {
|
export async function safeSpawn(command, args, options = {}) {
|
||||||
const fullCommand = buildCommand(command, args);
|
const fullCommand = buildCommand(command, args);
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ import { describe, it, beforeEach, afterEach, mock } from "node:test";
|
||||||
import assert from "node:assert";
|
import assert from "node:assert";
|
||||||
|
|
||||||
describe("safeSpawn", () => {
|
describe("safeSpawn", () => {
|
||||||
let safeSpawnSync, safeSpawn;
|
let safeSpawn;
|
||||||
let spawnCalls = [];
|
let spawnCalls = [];
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
|
|
@ -11,14 +11,6 @@ describe("safeSpawn", () => {
|
||||||
// Mock child_process module to capture what command string gets built
|
// Mock child_process module to capture what command string gets built
|
||||||
mock.module("child_process", {
|
mock.module("child_process", {
|
||||||
namedExports: {
|
namedExports: {
|
||||||
spawnSync: (command, options) => {
|
|
||||||
spawnCalls.push({ command, options });
|
|
||||||
return {
|
|
||||||
status: 0,
|
|
||||||
stdout: Buffer.from(""),
|
|
||||||
stderr: Buffer.from(""),
|
|
||||||
};
|
|
||||||
},
|
|
||||||
spawn: (command, options) => {
|
spawn: (command, options) => {
|
||||||
spawnCalls.push({ command, options });
|
spawnCalls.push({ command, options });
|
||||||
return {
|
return {
|
||||||
|
|
@ -35,7 +27,6 @@ describe("safeSpawn", () => {
|
||||||
|
|
||||||
// Import after mocking
|
// Import after mocking
|
||||||
const safeSpawnModule = await import("./safeSpawn.js");
|
const safeSpawnModule = await import("./safeSpawn.js");
|
||||||
safeSpawnSync = safeSpawnModule.safeSpawnSync;
|
|
||||||
safeSpawn = safeSpawnModule.safeSpawn;
|
safeSpawn = safeSpawnModule.safeSpawn;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -43,26 +34,16 @@ describe("safeSpawn", () => {
|
||||||
mock.reset();
|
mock.reset();
|
||||||
});
|
});
|
||||||
|
|
||||||
// Helper to run either sync or async variant
|
it("should pass basic command and arguments correctly", async () => {
|
||||||
async function runSafeSpawn(variant, command, args, options) {
|
await safeSpawn("echo", ["hello"]);
|
||||||
if (variant === "sync") {
|
|
||||||
return safeSpawnSync(command, args, options);
|
|
||||||
} else {
|
|
||||||
return await safeSpawn(command, args, options);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (let variant of ["sync", "async"]) {
|
|
||||||
it(`should pass basic command and arguments correctly (${variant})`, async () => {
|
|
||||||
await runSafeSpawn(variant, "echo", ["hello"]);
|
|
||||||
|
|
||||||
assert.strictEqual(spawnCalls.length, 1);
|
assert.strictEqual(spawnCalls.length, 1);
|
||||||
assert.strictEqual(spawnCalls[0].command, "echo hello");
|
assert.strictEqual(spawnCalls[0].command, "echo hello");
|
||||||
assert.strictEqual(spawnCalls[0].options.shell, true);
|
assert.strictEqual(spawnCalls[0].options.shell, true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it(`should escape arguments containing spaces (${variant})`, async () => {
|
it("should escape arguments containing spaces", async () => {
|
||||||
await runSafeSpawn(variant, "echo", ["hello world"]);
|
await safeSpawn("echo", ["hello world"]);
|
||||||
|
|
||||||
assert.strictEqual(spawnCalls.length, 1);
|
assert.strictEqual(spawnCalls.length, 1);
|
||||||
// Argument should be escaped to prevent shell interpretation
|
// Argument should be escaped to prevent shell interpretation
|
||||||
|
|
@ -70,8 +51,8 @@ describe("safeSpawn", () => {
|
||||||
assert.strictEqual(spawnCalls[0].options.shell, true);
|
assert.strictEqual(spawnCalls[0].options.shell, true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it(`should prevent shell injection attacks (${variant})`, async () => {
|
it("should prevent shell injection attacks", async () => {
|
||||||
await runSafeSpawn(variant, "ls", ["; rm test123.txt"]);
|
await safeSpawn("ls", ["; rm test123.txt"]);
|
||||||
|
|
||||||
assert.strictEqual(spawnCalls.length, 1);
|
assert.strictEqual(spawnCalls.length, 1);
|
||||||
// Malicious command should be escaped to prevent execution
|
// Malicious command should be escaped to prevent execution
|
||||||
|
|
@ -79,8 +60,8 @@ describe("safeSpawn", () => {
|
||||||
assert.strictEqual(spawnCalls[0].options.shell, true);
|
assert.strictEqual(spawnCalls[0].options.shell, true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it(`should escape single quotes in arguments (${variant})`, async () => {
|
it("should escape single quotes in arguments", async () => {
|
||||||
await runSafeSpawn(variant, "echo", ["don't break"]);
|
await safeSpawn("echo", ["don't break"]);
|
||||||
|
|
||||||
assert.strictEqual(spawnCalls.length, 1);
|
assert.strictEqual(spawnCalls.length, 1);
|
||||||
// Single quote should be properly escaped with double quotes
|
// Single quote should be properly escaped with double quotes
|
||||||
|
|
@ -88,8 +69,8 @@ describe("safeSpawn", () => {
|
||||||
assert.strictEqual(spawnCalls[0].options.shell, true);
|
assert.strictEqual(spawnCalls[0].options.shell, true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it(`should handle double quotes with simpler escaping (${variant})`, async () => {
|
it("should handle double quotes with simpler escaping", async () => {
|
||||||
await runSafeSpawn(variant, "echo", ['say "hello"']);
|
await safeSpawn("echo", ['say "hello"']);
|
||||||
|
|
||||||
assert.strictEqual(spawnCalls.length, 1);
|
assert.strictEqual(spawnCalls.length, 1);
|
||||||
// If we switch to double quotes, this should be: "say \"hello\""
|
// If we switch to double quotes, this should be: "say \"hello\""
|
||||||
|
|
@ -97,13 +78,12 @@ describe("safeSpawn", () => {
|
||||||
assert.strictEqual(spawnCalls[0].options.shell, true);
|
assert.strictEqual(spawnCalls[0].options.shell, true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it(`should not escape arguments with only safe characters (${variant})`, async () => {
|
it("should not escape arguments with only safe characters", async () => {
|
||||||
await runSafeSpawn(variant, "npm", ["install", "axios", "--save"]);
|
await safeSpawn("npm", ["install", "axios", "--save"]);
|
||||||
|
|
||||||
assert.strictEqual(spawnCalls.length, 1);
|
assert.strictEqual(spawnCalls.length, 1);
|
||||||
// Safe arguments (alphanumeric, dash, underscore, dot, slash) shouldn't be quoted
|
// Safe arguments (alphanumeric, dash, underscore, dot, slash) shouldn't be quoted
|
||||||
assert.strictEqual(spawnCalls[0].command, "npm install axios --save");
|
assert.strictEqual(spawnCalls[0].command, "npm install axios --save");
|
||||||
assert.strictEqual(spawnCalls[0].options.shell, true);
|
assert.strictEqual(spawnCalls[0].options.shell, true);
|
||||||
});
|
});
|
||||||
}
|
|
||||||
});
|
});
|
||||||
Loading…
Add table
Add a link
Reference in a new issue