T09 · Insecure Skill Coding Practices
Error
- Location
- scripts/execute_query.py:7
- Finding
- Caller-Controlled Driver Path Enables Arbitrary Python Code Execution## Vulnerability Details **File Location**: `scripts/execute_query.py:7-35` **Vulnerability Type**: Unsafe dynamic module loading **Risk Level**: High ### Vulnerable Code ```python def execute_query(dsn, sql, driver_path=None): # If no driver_path provided, look in the skill's assets/driver/psycopg2 if driver_path is None: script_dir = os.path.dirname(os.path.abspath(__file__)) driver_path = os.path.join(script_dir, "..", "assets", "driver", "psycopg2") if not os.path.exists(driver_path): return {"status": "error", "message": "Driver not found at {}".format(driver_path)} # Manual temporary directory for Python 2.7 compatibility tmp_dir = tempfile.mkdtemp() try: target_path = os.path.join(tmp_dir, "psycopg2") if not os.path.exists(target_path): os.symlink(os.path.abspath(driver_path), target_path) # Add the driver path to LD_LIBRARY_PATH for the .so libraries env = os.environ.copy() driver_abs_path = os.path.abspath(driver_path) if "LD_LIBRARY_PATH" in env: os.environ["LD_LIBRARY_PATH"] = "{}:{}".format( driver_abs_path, env['LD_LIBRARY_PATH']) else: os.environ["LD_LIBRARY_PATH"] = driver_abs_path # Add the temp directory to sys.path sys.path.insert(0, tmp_dir) try: import psycopg2 from psycopg2 import extras ``` The externally selectable path is exposed at `scripts/execute_query.py:66`: ```python parser.add_argument("--driver", required=False, help="Path to psycopg2 directory (optional, uses built-in if omitted)") ``` ### Technical Analysis The `--driver` argument accepts any existing filesystem path. The script creates a symbolic link from that path to a temporary package named `psycopg2`, places the temporary directory at the beg ...[truncated 2121 chars]
- Remediation
- ## Remediation Suggestions 1. Remove the `--driver` option if external driver substitution is not strictly required. 2. Package and import one pinned, reviewed driver from a fixed Skill-owned directory. 3. If external drivers are required, allow only paths beneath an administrator-configured trusted root. 4. Resolve paths with `realpath()` and reject paths, parent components, or files containing symbolic links. 5. Reject driver directories writable by untrusted users and validate file ownership and permissions. 6. Verify every driver file against a signed manifest or pinned cryptographic hashes before importing it. 7. Do not add caller-controlled directories to `LD_LIBRARY_PATH`; use fixed, integrity-verified native-library locations. 8. Execute unavoidable third-party drivers in an isolated, least-privileged process with restricted filesystem and network access. 9. Avoid retaining untrusted entries in global `sys.path` or process environment state.
