T09 · Insecure Skill Coding Practices
- Location
scripts/validate.py:20- Finding
Migration Validator Executes Untrusted SQL with Filesystem Access
- Content
View full analysis
Vulnerability Details
File Location:
scripts/validate.py, lines 20–27
Vulnerability Type: Unsafe execution of untrusted migration scripts
Risk Level: HighVulnerable Code
python with open(filepath) as f: sql = f.read() try: conn = sqlite3.connect(":memory:") cur = conn.cursor() cur.executescript(sql) conn.close() except Exception as e: validation_errors.append({"migration": m, "error": str(e)})Technical Analysis
The Skill claims to validate SQL syntax, but it passes every discovered
.sqlmigration to SQLite'sexecutescript(). This API executes all statements rather than merely parsing them.Using an in-memory primary database does not create a security boundary. SQLite statements such as
ATTACH DATABASEcan open or create database files on disk. Subsequent statements can then create or alter objects in the attached file. No SQLite authorizer, statement allowlist, filesystem isolation, or privilege restriction is applied.Consequently, anyone able to introduce or modify a migration file can cause side effects during validation, before the migration has been approved for normal execution.
Attack Path
- An attacker gains the ability to add or modify a
.sqlfile in the configured migration directory, such as through a compromised contribution or CI input. - The attacker inserts statements similar to:
sql ATTACH DATABASE '/writable/path/attacker.db' AS target; CREATE TABLE target.payload(data TEXT); INSERT INTO target.payload VALUES ('attacker-controlled content'); - The validator enumerates the migration directory and reads the malicious file.
cur.executescript(sql)executes all supplied statements.- SQLite creates or modifies the targeted database file using the validator process's filesystem permissions.
- The validator may still return
"status": "completed"because that status is unconditiona ...[truncated 594 chars]
- An attacker gains the ability to add or modify a
- Remediation
View remediation
Remediation Suggestions
- Replace
executescript()with a non-executing SQL parser capable of checking the intended SQLite grammar. - If SQLite execution is unavoidable, install a
sqlite3.Connection.set_authorizer()callback before processing input and deny, at minimum:ATTACHandDETACH;- data mutation operations;
- schema creation, alteration, and deletion;
- transaction and pragma operations not explicitly required;
- extension-related or other filesystem-affecting functionality.
- Use an explicit allowlist of statements accepted by the migration policy rather than attempting to block only known-dangerous statements.
- Validate each migration in a fresh, isolated process or container with no network access, a read-only filesystem, no sensitive mounted paths, and a dedicated unprivileged account.
- Ensure every opened connection is closed through a context manager or
finallyblock, including whenexecutescript()raises an exception. - Return a failed validation status when any validation error occurs rather than always returning
"completed". - Add regression tests containing
ATTACH DATABASEand other side-effecting statements to confirm that validation cannot create or modify disk files.
- Replace
