T09 · Insecure Skill Coding Practices
- Location
aegis-protocol.py:546- Finding
Shell Command Injection Through Unvalidated Session Identifiers
- Content
View full analysis
Vulnerability Details
File Location:
aegis-protocol.py:174-188,aegis-protocol.py:210-222, andaegis-protocol.py:546-550
Vulnerability Type: OS command injection through unquoted data passed to a shell
Risk Level: HighVulnerable Code
python def exec_cmd(cmd: str, timeout: int = 30, raise_on_error: bool = False) -> Tuple[int, str, str]: """Execute a shell command.""" try: result = subprocess.run( cmd, shell=True, capture_output=True, text=True, timeout=timeout ) if raise_on_error and result.returncode != 0: raise ExternalCommandError(cmd, result.returncode, result.stderr) return result.returncode, result.stdout, result.stderr except subprocess.TimeoutExpired: if raise_on_error: raise ExternalCommandError(cmd, -1, "Command timed out") return -1, "", "Command timed out" except Exception as e: if raise_on_error: raise ExternalCommandError(cmd, -1, str(e)) return -1, "", str(e)python code, out, err = exec_cmd("openclaw sessions list --limit 100") if code != 0: return { "status": "ok", "stuck_count": 0, "sessions": [], "note": "Session checking requires API support" } stuck_sessions: List[str] = [] try: sessions = re.findall(r'"key":"([^"]*)".*?"status":"([^"]*)"', out) for key, status in sessions: if status == "running" and key not in whitelist: stuck_sessions.append(key) except Exception: passpython def kill_session(session_key: str) -> bool: """Terminate a session.""" start = datetime.now() code, out, err = exec_cmd(f"openclaw sessions kill {session_key}") duration = (datetime.now() - start).total_seconds()Technical Analysis
`exec_cm ...[truncated 2091 chars]
- Remediation
View remediation
Remediation Suggestions
-
Eliminate
shell=Trueand represent every command as an argument array:python def exec_cmd( command: List[str], timeout: int = 30, raise_on_error: bool = False ) -> Tuple[int, str, str]: result = subprocess.run( command, shell=False, capture_output=True, text=True, timeout=timeout, check=False ) if raise_on_error and result.returncode != 0: raise ExternalCommandError( " ".join(command), result.returncode, result.stderr ) return result.returncode, result.stdout, result.stderr def kill_session(session_key: str) -> bool: if not re.fullmatch(r"[A-Za-z0-9._:@/-]+", session_key): raise RecoveryError("Invalid session identifier") code, out, err = exec_cmd( ["openclaw", "sessions", "kill", session_key] ) return code == 0 -
Use a documented, restrictive session-identifier grammar rather than the illustrative allowlist above.
-
Parse structured JSON from OpenClaw instead of applying a broad regular expression to arbitrary text.
-
Reject identifiers containing control characters, whitespace, or shell metacharacters.
-
Run the Skill under a dedicated, unprivileged service account and grant only narrowly scoped recovery permissions.
-
Add unit tests containing semicolons, command substitutions, pipes, redirections, quotes, whitespace, and newline characters.
-
Refactor all other shell pipelines to argument-based subprocess calls where possible.
-
