game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
ShellUtils Class Reference

Utilities for safely building shell commands. More...

#include <shell_utils.h>

Static Public Member Functions

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 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 buildJsonArray (const std::vector< std::string > &items)
 Build a JSON array from strings.
static std::string buildJsonObject (const std::map< std::string, std::string > &pairs)
 Build a JSON object from key-value pairs.
static std::string escapeArg (const std::string &arg)
 Escape a string for safe use as a shell argument.
static std::string escapeForDoubleQuotes (const std::string &arg)
 Escape a string for use in double quotes.
static std::string escapeJson (const std::string &str)
 Escape a string for JSON.
static std::string escapePath (const std::string &path, bool allowTraversal=false)
 Escape a path for shell use with validation.
static std::string formatEnvVar (const std::string &name, const std::string &value)
 Format an environment variable assignment safely.
static std::string formatEnvVars (const std::map< std::string, std::string > &envVars, bool skipInvalid=true)
 Format multiple environment variables safely.
static bool isSafePath (const std::string &path)
 Check if a path is safe (no directory traversal, no special chars).
static bool isValidEnvName (const std::string &name)
 Validate an environment variable name.
static std::vector< std::string > splitArgs (const std::string &args)
 Split a shell argument string into tokens, respecting quoting.

Detailed Description

Utilities for safely building shell commands.

Provides functions to escape shell arguments and environment variables to prevent command injection attacks. All methods are static.

Definition at line 47 of file shell_utils.h.

Member Function Documentation

◆ buildCommand()

std::string ShellUtils::buildCommand ( const std::vector< std::string > & args,
const std::map< std::string, std::string > & envVars = {} )
inlinestatic

Build a complete shell command from parts.

Parameters
argsCommand and arguments (args[0] is the command)
envVarsOptional environment variables to prepend
Returns
Complete command string safe for shell execution

Example: buildCommand({"wine", "/path/to/game.exe", "--flag"}, {{"WINEPREFIX", "/home/user/.wine"}}) Returns: "WINEPREFIX='/home/user/.wine' wine '/path/to/game.exe' '--flag'"

Definition at line 240 of file shell_utils.h.

◆ buildCommandWithWrapper()

std::string ShellUtils::buildCommandWithWrapper ( const std::string & wrapper,
const std::vector< std::string > & args,
const std::map< std::string, std::string > & envVars = {} )
inlinestatic

Build command with a wrapper (e.g., game-performance, mangohud).

Parameters
wrapperThe wrapper command (e.g., "game-performance", "mangohud")
argsCommand and arguments
envVarsEnvironment variables
Returns
Complete command with wrapper

Definition at line 344 of file shell_utils.h.

◆ buildJsonArray()

std::string ShellUtils::buildJsonArray ( const std::vector< std::string > & items)
inlinestatic

Build a JSON array from strings.

Parameters
itemsVector of strings
Returns
JSON array string

Definition at line 516 of file shell_utils.h.

References escapeJson().

◆ buildJsonObject()

std::string ShellUtils::buildJsonObject ( const std::map< std::string, std::string > & pairs)
inlinestatic

Build a JSON object from key-value pairs.

Parameters
pairsMap of keys to values
Returns
JSON object string

Definition at line 493 of file shell_utils.h.

References escapeJson().

◆ escapeArg()

◆ escapeForDoubleQuotes()

std::string ShellUtils::escapeForDoubleQuotes ( const std::string & arg)
inlinestatic

Escape a string for use in double quotes.

Parameters
argThe argument to escape
Returns
Escaped string (without surrounding quotes)

Escapes characters that have special meaning in double quotes: $, backtick, \\endiskip, ", !, and newlines.

Note: Caller must add the surrounding double quotes. Prefer escapeArg() with single quotes when possible.

Definition at line 108 of file shell_utils.h.

Referenced by ProcessExecutor::getProcessPid(), and ProcessExecutor::isProcessRunning().

◆ escapeJson()

std::string ShellUtils::escapeJson ( const std::string & str)
inlinestatic

Escape a string for JSON.

Parameters
strThe string to escape
Returns
JSON-safe string (without surrounding quotes)

Definition at line 440 of file shell_utils.h.

Referenced by buildJsonArray(), buildJsonObject(), and HLTBService::search().

◆ escapePath()

std::string ShellUtils::escapePath ( const std::string & path,
bool allowTraversal = false )
inlinestatic

Escape a path for shell use with validation.

Parameters
pathThe path to escape
allowTraversalIf false (default), reject paths with ..
Returns
Escaped path, or empty string if validation fails

Definition at line 422 of file shell_utils.h.

References escapeArg(), and isSafePath().

◆ formatEnvVar()

std::string ShellUtils::formatEnvVar ( const std::string & name,
const std::string & value )
inlinestatic

Format an environment variable assignment safely.

Parameters
nameVariable name (must be valid - checked)
valueVariable value (will be escaped)
Returns
Safe "NAME='value'" string, or empty if name is invalid

Example: formatEnvVar("PATH", "/usr/bin:/tmp") returns "PATH='/usr/bin:/tmp'"

Definition at line 184 of file shell_utils.h.

References escapeArg(), and isValidEnvName().

Referenced by formatEnvVars().

◆ formatEnvVars()

std::string ShellUtils::formatEnvVars ( const std::map< std::string, std::string > & envVars,
bool skipInvalid = true )
inlinestatic

Format multiple environment variables safely.

Parameters
envVarsMap of variable names to values
skipInvalidIf true, skip invalid names; if false, return empty on any invalid
Returns
Space-separated environment variable assignments

Definition at line 200 of file shell_utils.h.

References formatEnvVar(), and isValidEnvName().

◆ isSafePath()

bool ShellUtils::isSafePath ( const std::string & path)
inlinestatic

Check if a path is safe (no directory traversal, no special chars).

Parameters
pathThe path to check
Returns
true if path appears safe

Rejects paths with:

  • Parent directory references (..)
  • Null bytes
  • Newlines

Definition at line 385 of file shell_utils.h.

Referenced by escapePath().

◆ isValidEnvName()

bool ShellUtils::isValidEnvName ( const std::string & name)
inlinestatic

Validate an environment variable name.

Parameters
nameThe variable name to validate
Returns
true if name is a valid shell variable name

Valid names match: [A-Za-z_][A-Za-z0-9_]*

Definition at line 150 of file shell_utils.h.

Referenced by UMULauncher::buildLaunchCommand(), UMULauncher::buildLaunchComponents(), formatEnvVar(), and formatEnvVars().

◆ splitArgs()

std::vector< std::string > ShellUtils::splitArgs ( const std::string & args)
inlinestatic

Split a shell argument string into tokens, respecting quoting.

Parameters
argsThe argument string (e.g., "--arg \"value with spaces\" --other")
Returns
Vector of unquoted token strings, each safe to pass through escapeArg()

Handles single-quoted and double-quoted groups. Each returned token is the raw (unquoted) value; pass each through escapeArg() before appending to a shell command. This replaces the insecure "use as-is when quotes detected" pattern that was a shell-injection vector.

Definition at line 274 of file shell_utils.h.

References None.


The documentation for this class was generated from the following file: