T09 · Insecure Skill Coding Practices
Error
- Location
- SKILL.md:57
- Finding
- Unqualified EXPLAIN ANALYZE Guidance Can Execute State-Changing SQL## Vulnerability Details **File Location**: `SKILL.md`, lines 57–62 **Vulnerability Type**: Unsafe execution guidance for arbitrary SQL **Risk Level**: High **Complete Code Snippet**: ```sql -- The agent recommends running: EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT) <your query>; -- For even more detail: EXPLAIN (ANALYZE, BUFFERS, VERBOSE, SETTINGS, WAL, FORMAT TEXT) <your query>; ``` ### Technical Analysis PostgreSQL's `EXPLAIN ANALYZE` does not merely inspect a statement. It executes the supplied SQL and collects runtime measurements. The skill accepts arbitrary slow SQL but recommends this command without restricting it to read-only statements, validating the statement type, requiring a non-production environment, or placing execution inside a rollback transaction. Consequently, using this guidance with `INSERT`, `UPDATE`, `DELETE`, `MERGE`, or SQL that invokes side-effecting functions can change database state. Even read-oriented statements can consume substantial resources or acquire locks when executed against large production datasets. ### Attack Path 1. An attacker or another untrusted source supplies a state-changing or resource-intensive SQL statement as a query-optimization request. 2. The skill recommends wrapping the supplied statement in `EXPLAIN (ANALYZE, BUFFERS)`. 3. A user executes the generated command against a live PostgreSQL database. 4. PostgreSQL executes the underlying statement. 5. The statement modifies data, invokes triggers or side-effecting functions, acquires disruptive locks, or exhausts database resources. ### Impact Assessment The operation runs with the privileges of the database role used by the user. It does not independently elevate privileges, but it may exercise every data-access and mutation privilege already granted to that role. Potential scope includes unauthorized or unintended insertion, modification, or deletion of accessible records; trigger execution; lock contention; excessive CPU, memory, an ...[truncated 138 chars]
- Remediation
- ## Remediation Suggestions 1. Inspect and classify the SQL statement before recommending `EXPLAIN ANALYZE`. 2. For untrusted or state-changing SQL, recommend plain `EXPLAIN` first because it does not execute the statement. 3. Require runtime analysis of mutation statements to occur on an isolated staging database populated with non-sensitive test data. 4. Where transaction rollback is appropriate, provide guarded instructions such as: ```sql BEGIN; SET LOCAL statement_timeout = '30s'; EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT) <statement>; ROLLBACK; ``` 5. Explicitly warn that rollback does not neutralize every possible side effect, including sequence changes, external calls made by functions, autonomous external effects, and resource consumption. 6. Recommend a least-privileged database role, a restrictive `statement_timeout`, and appropriate lock and resource limits. 7. Require explicit confirmation before analyzing any non-`SELECT` statement on a live database.
