65 static std::string
escapeArg(
const std::string& arg)
73 if (isSafeString(arg))
80 std::string result =
"'";
111 result.reserve(arg.size() * 2);
156 char first = name[0];
157 if (!((first >=
'A' && first <=
'Z') || (first >=
'a' && first <=
'z') || first ==
'_'))
163 for (
size_t i = 1; i < name.size(); ++i)
166 if (!((c >=
'A' && c <=
'Z') || (c >=
'a' && c <=
'z') || (c >=
'0' && c <=
'9') ||
184 static std::string
formatEnvVar(
const std::string& name,
const std::string& value)
200 static std::string
formatEnvVars(
const std::map<std::string, std::string>& envVars,
201 bool skipInvalid =
true)
206 for (
const auto& [name, value] : envVars)
241 const std::map<std::string, std::string>& envVars = {})
246 if (!envVars.empty())
254 for (
size_t i = 0; i < args.size(); ++i)
274 static std::vector<std::string>
splitArgs(
const std::string& args)
276 std::vector<std::string> tokens;
285 for (
size_t i = 0; i < args.size(); ++i)
294 else if (c ==
' ' || c ==
'\t')
296 if (!current.empty())
298 tokens.push_back(current);
305 else if (q == Q::Single)
316 else if (c ==
'\\' && i + 1 < args.size())
318 char next = args[i + 1];
319 if (next ==
'"' || next ==
'\\')
331 if (!current.empty())
332 tokens.push_back(current);
345 const std::map<std::string, std::string>& envVars = {})
350 if (!envVars.empty())
361 for (
size_t i = 0; i < args.size(); ++i)
388 if (path.find(
'\0') != std::string::npos)
392 if (path.find(
'\n') != std::string::npos || path.find(
'\r') != std::string::npos)
398 while ((pos = path.find(
"..", pos)) != std::string::npos)
401 bool atStart = (pos == 0);
402 bool atEnd = (pos + 2 == path.size());
403 bool precededBySlash = (pos > 0 && path[pos - 1] ==
'/');
404 bool followedBySlash = (pos + 2 < path.size() && path[pos + 2] ==
'/');
406 if ((atStart || precededBySlash) && (atEnd || followedBySlash))
422 static std::string
escapePath(
const std::string& path,
bool allowTraversal =
false)
443 result.reserve(str.size() * 2);
471 if (
static_cast<unsigned char>(c) < 0x20)
475 snprintf(buf,
sizeof(buf),
"\\u%04x",
static_cast<unsigned char>(c));
495 std::string result =
"{";
498 for (
const auto& [key, value] : pairs)
518 std::string result =
"[";
521 for (
const auto& item : items)
540 static bool isSafeString(
const std::string& str)
545 if (!((c >=
'A' && c <=
'Z') || (c >=
'a' && c <=
'z') || (c >=
'0' && c <=
'9') ||
546 c ==
'-' || c ==
'_' || c ==
'.' || c ==
'/' || c ==
':'))
Utilities for safely building shell commands.
static bool isSafePath(const std::string &path)
Check if a path is safe (no directory traversal, no special chars).
static std::string buildCommand(const std::vector< std::string > &args, const std::map< std::string, std::string > &envVars={})
Build a complete shell command from parts.
static std::string formatEnvVar(const std::string &name, const std::string &value)
Format an environment variable assignment safely.
static std::string escapeJson(const std::string &str)
Escape a string for JSON.
static std::string escapeArg(const std::string &arg)
Escape a string for safe use as a shell argument.
static bool isValidEnvName(const std::string &name)
Validate an environment variable name.
static std::string buildJsonArray(const std::vector< std::string > &items)
Build a JSON array from strings.
static std::string escapeForDoubleQuotes(const std::string &arg)
Escape a string for use in double quotes.
static std::string formatEnvVars(const std::map< std::string, std::string > &envVars, bool skipInvalid=true)
Format multiple environment variables safely.
static std::vector< std::string > splitArgs(const std::string &args)
Split a shell argument string into tokens, respecting quoting.
static std::string buildCommandWithWrapper(const std::string &wrapper, const std::vector< std::string > &args, const std::map< std::string, std::string > &envVars={})
Build command with a wrapper (e.g., game-performance, mangohud).
static std::string buildJsonObject(const std::map< std::string, std::string > &pairs)
Build a JSON object from key-value pairs.
static std::string escapePath(const std::string &path, bool allowTraversal=false)
Escape a path for shell use with validation.
@ None
No specific store (generic fixes only).