T09 · Insecure Skill Coding Practices
- Location
scripts/safe_ros2_control_introspection.py:42- Finding
Shell Command Injection Through Unquoted ROS Setup Path
- Content
View full analysis
Vulnerability Details
File Location:
scripts/safe_ros2_control_introspection.py, lines 42–55
Vulnerability Type: OS command injection through unsafe shell-string construction
Risk Level: HighVulnerable Code
python ros_setup_path = config.get("ros_setup_path") if not ros_setup_path or not os.path.isfile(ros_setup_path): print("Error: ROS_SETUP_PATH invalid. Run setup.sh.", file=sys.stderr) sys.exit(1) # Build the environment env_command = f"source {ros_setup_path} && env" try: env_proc = subprocess.run(['bash', '-c', env_command], stdout=subprocess.PIPE, text=True, check=True) ros_env = {} for line in env_proc.stdout.splitlines(): if '=' in line: key, val = line.split('=', 1) ros_env[key] = valTechnical Analysis
The
ros_setup_pathvalue is loaded fromconfig/config.jsonand interpolated directly into a command string executed bybash -c. Because the path is neither quoted nor passed as a positional argument, shell metacharacters in the configured filename are interpreted as Bash syntax.The
os.path.isfile(ros_setup_path)check does not prevent injection. It only verifies that a file exists at the complete literal path. Unix filenames can contain spaces and shell metacharacters such as semicolons and comment characters. Consequently, a specially named existing file can satisfy this validation while its path is parsed as multiple commands by Bash.This also contradicts the security assurance in
SKILL.mdthat command injection is prevented throughsubprocesswithshell=False. Although the final ROS command avoids a shell, this earlier environment-construction operation explicitly invokesbash -c.Attack Path
- An attacker gains the ability to modify
config/config.jsonor influences setup through a crafted local ROS installation path. - The attacker creates a file whose path contains Bash c ...[truncated 1198 chars]
- An attacker gains the ability to modify
- Remediation
View remediation
Remediation Suggestions
Do not interpolate the setup path into executable shell text. Supply it as a positional parameter and quote that parameter inside the fixed Bash program:
python env_proc = subprocess.run( ["bash", "-c", 'source "$1" && env', "bash", ros_setup_path], stdout=subprocess.PIPE, text=True, check=True, )In addition:
- Resolve the setup path with
Path.resolve()and require it to reside under an explicitly trusted ROS installation root. - Reject configuration files that are writable by untrusted users, and ensure the generated
configdirectory andconfig.jsonhave restrictive ownership and permissions. - Treat ROS setup files as executable code and only source trusted, administrator- or user-controlled files.
- Generate
config.jsonwith a proper JSON encoder rather than manually interpolating shell-derived values insetup.sh. - Add regression tests using paths containing spaces, semicolons, command substitutions, quotes, and newline characters to verify they remain literal arguments.
- Update
SKILL.mdso its command-injection claims cover every subprocess invocation, not only the finalros2execution.
- Resolve the setup path with
