T09 · Insecure Skill Coding Practices
Error
- Location
- SKILL.md:59
- Finding
- Environment Variables Injected Directly into Executable Python Source<![CDATA[ ## Vulnerability Details **File Location**: `SKILL.md`, lines 59–90 **Vulnerability Type**: Python code injection through unsafe shell interpolation **Risk Level**: High ### Complete Vulnerable Code ```bash # Application-level heartbeat probe (write timestamp to primary, read from replica) python3 -c " import time, psycopg2 primary = psycopg2.connect(host='$PRIMARY_HOST', dbname='$DB_NAME', user='$DB_USER') replica = psycopg2.connect(host='$REPLICA_HOST', dbname='$DB_NAME', user='$DB_USER') # Write heartbeat to primary with primary.cursor() as cur: cur.execute('CREATE TABLE IF NOT EXISTS _repl_heartbeat (id int PRIMARY KEY, ts timestamptz)') cur.execute('INSERT INTO _repl_heartbeat VALUES (1, now()) ON CONFLICT (id) DO UPDATE SET ts = now()') primary.commit() cur.execute('SELECT ts FROM _repl_heartbeat WHERE id = 1') write_ts = cur.fetchone()[0] time.sleep(0.5) # Read heartbeat from replica with replica.cursor() as cur: cur.execute('SELECT ts FROM _repl_heartbeat WHERE id = 1') read_ts = cur.fetchone()[0] lag = (write_ts - read_ts).total_seconds() if write_ts > read_ts else 0 print(f'Application-level replication lag: {lag:.3f}s') print(f'Assessment: {\"HEALTHY\" if lag < 1 else \"WARNING\" if lag < 10 else \"CRITICAL\"}') " ``` ### Technical Analysis The shell expands `PRIMARY_HOST`, `REPLICA_HOST`, `DB_NAME`, and `DB_USER` directly inside Python string literals before passing the resulting source to `python3 -c`. These values are treated as executable source text rather than as data. If an attacker can influence one of these environment variables, a single quote can terminate the intended Python literal. Additional Python expressions or statements can then be introduced. Because the generated source is executed by `python3`, successful exploitation can run arbitrary local commands with the privileges of the user executing the Skill. This behavior is unnecessary for replication assessment. Python can safely read ...[truncated 1441 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions - Pass connection values as data rather than embedding them into Python source. - Read the existing environment variables through `os.environ`. - Validate hostnames and database names according to strict expected formats. - Use a dedicated, least-privilege monitoring account for the heartbeat operation. - Avoid granting the monitoring account general schema-creation privileges. Provision the heartbeat table separately where possible. A safer pattern is: ```bash python3 - <<'PY' import os import time import psycopg2 primary = psycopg2.connect( host=os.environ["PRIMARY_HOST"], dbname=os.environ["DB_NAME"], user=os.environ["DB_USER"], ) replica = psycopg2.connect( host=os.environ["REPLICA_HOST"], dbname=os.environ["DB_NAME"], user=os.environ["DB_USER"], ) # Continue with the heartbeat check. PY ``` For stronger isolation, implement the probe in a reviewed standalone script and pass secrets through an approved secret manager or protected connection service. ]]>
