T09 · Insecure Skill Coding Practices
- Location
scripts/execute_query.py:7- Finding
Caller-Controlled Driver Path Enables Arbitrary Python Code Execution
- Content
View full analysis
Vulnerability Details
File Location:
scripts/execute_query.py:7-35
Vulnerability Type: Unsafe dynamic module loading
Risk Level: HighVulnerable 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 extrasThe 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
--driverargument accepts any existing filesystem path. The script creates a symbolic link from that path to a temporary package namedpsycopg2, places the temporary directory at the beg ...[truncated 2121 chars]- Remediation
View remediation
Remediation Suggestions
- Remove the
--driveroption if external driver substitution is not strictly required. - Package and import one pinned, reviewed driver from a fixed Skill-owned directory.
- If external drivers are required, allow only paths beneath an administrator-configured trusted root.
- Resolve paths with
realpath()and reject paths, parent components, or files containing symbolic links. - Reject driver directories writable by untrusted users and validate file ownership and permissions.
- Verify every driver file against a signed manifest or pinned cryptographic hashes before importing it.
- Do not add caller-controlled directories to
LD_LIBRARY_PATH; use fixed, integrity-verified native-library locations. - Execute unavoidable third-party drivers in an isolated, least-privileged process with restricted filesystem and network access.
- Avoid retaining untrusted entries in global
sys.pathor process environment state.
- Remove the
