mirror of
https://github.com/AikidoSec/safe-chain.git
synced 2026-05-26 12:10:49 +00:00
Escape special chars in shell scripts
This commit is contained in:
parent
04cb001006
commit
486a4b8f68
2 changed files with 18 additions and 3 deletions
|
|
@ -1,9 +1,15 @@
|
|||
import { spawnSync, spawn } from "child_process";
|
||||
|
||||
function escapeArg(arg) {
|
||||
// If argument contains spaces or quotes, wrap in double quotes and escape double quotes
|
||||
if (arg.includes(" ") || arg.includes('"') || arg.includes("'")) {
|
||||
return '"' + arg.replaceAll('"', '\\"') + '"';
|
||||
// Shell metacharacters that need escaping
|
||||
// These characters have special meaning in shells and need to be quoted
|
||||
const shellMetaChars = /[ "&'|;<>()$`\\!*?[\]{}~#]/;
|
||||
|
||||
// If argument contains shell metacharacters, wrap in double quotes
|
||||
// and escape characters that are special even inside double quotes
|
||||
if (shellMetaChars.test(arg)) {
|
||||
// Inside double quotes, we need to escape: " $ ` \
|
||||
return '"' + arg.replace(/(["`$\\])/g, '\\$1') + '"';
|
||||
}
|
||||
return arg;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue