T09 · Insecure Skill Coding Practices
Error
- Location
- scripts/setup_envs.py:105
- Finding
- Shell Command Injection in Generated Startup Scripts<![CDATA[ ## Vulnerability Details **File Location**: `scripts/setup_envs.py:105-157`; `scripts/setup_envs_v2.py:107-198` **Vulnerability Type**: Unsanitized CLI values embedded into executable shell scripts **Risk Level**: Critical ### Vulnerable Code From `scripts/setup_envs.py`: ```python def generate_start_script(env: str, config: dict) -> str: """Generate startup shell script for a specific environment.""" env_label = {"dev": "Development", "test": "Testing", "prod": "Production"}[env] port = config[f"{env}_port"] app_module = config["app_module"] name = config["name"] user_line = "" if env == "dev" and config.get("dev_user"): user_line = f'\necho "👩💻 Happy coding, {config["dev_user"]}!"' elif env == "test" and config.get("test_user"): user_line = f'\necho "🧪 Happy testing, {config["test_user"]}!"' reload_flag = "\n --reload \\" if env == "dev" else "" workers = "$(nproc)" if env == "prod" else "1" workers_line = "" if env == "dev" else f"\n --workers {workers} \\" log_level = {"dev": "debug", "test": "info", "prod": "warning"}[env] return f"""#!/bin/bash # {'=' * 50} # {name} - {env_label} Environment # {'=' * 50} set -e SCRIPT_DIR="$(cd "$(dirname "${{BASH_SOURCE[0]}}")" && pwd)" PROJECT_DIR="$(dirname "$SCRIPT_DIR")" export ENV_FILE="$PROJECT_DIR/.env.{env}" echo "🚀 Starting {name} {env_label} Environment..." echo "📍 Port: {port}" echo "📍 Environment: $ENV_FILE" echo "📍 Database: ./data/{env}/"{user_line} echo "" cd "$PROJECT_DIR" [ -d "venv" ] && source venv/bin/activate uvicorn {app_module} \\ --host 127.0.0.1 \\ --port {port} \\{reload_flag}{workers_line} --log-level {log_level} """ ``` From `scripts/setup_envs_v2.py`: ```python def generate_frontend_script(env: str, config: dict) -> str: """Generate frontend startup shell script for a specific environment.""" env_label = {"dev": "Development", "test": "Testing", "prod": "Production"}[e ...[truncated 3202 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions 1. Do not generate executable shell source containing raw user-controlled values. 2. Apply strict allowlist validation: - Require ports to be within `1` through `65535`. - Restrict application modules to an expected Python import format such as `package.module:attribute`. - Resolve and validate `frontend_dir` as a path below the selected project directory. - Reject control characters and shell metacharacters in display-only fields. 3. Use `shlex.quote()` for every value that must be represented as a shell argument: ```python import shlex safe_app_module = shlex.quote(config["app_module"]) safe_name = shlex.quote(config["name"]) ``` 4. Prefer passing values through positional parameters or a safely parsed configuration file rather than embedding them into generated Bash. 5. For informational messages, pass the value as data: ```bash printf '%s\n' "$APP_DISPLAY_NAME" ``` 6. Add security tests using payloads containing command substitutions, quotes, semicolons, newlines, backticks, and redirection operators. 7. Treat previously generated scripts as potentially unsafe and regenerate them after implementing validation and escaping. ]]>
