T09 · Insecure Skill Coding Practices
- Location
SKILL.md:109- Finding
SQL Injection Through Direct Interpolation of User-Controlled Values
- Content
View full analysis
= '{start_time}' AND create_time < '{end_time}' {keyword_filter} GROUP BY {group_by} ORDER BY {order_by}; """ ``` ### Technical Analysis The Skill inserts `keyword`, `start_time`, and `end_time` values directly into an SQL statement through Python f-strings. No parameterized query, escaping mechanism, or strict input validation is applied. The keyword is especially dangerous because every token is placed between SQL string delimiters: ```python f"(order_title LIKE '%{k}%' OR order_desc LIKE '%{k}%')" ``` A token containing a quote can terminate the intended string literal and introduce SQL operators, expressions, or comments. The start and end times are similarly vulnerable because they are embedded directly between single quotes. Restricting the database account to read-only access reduces the potential for database modification, but it does not prevent attackers from changing query semantics, bypassing filters, accessing additional rows available ...[truncated 1984 chars]- Remediation
View remediation
= %s AND create_time < %s {keyword_filter} GROUP BY {group_by} ORDER BY {order_by} """ df = pd.read_sql(sql, conn, params=params) ``` 2. Parse dates with `datetime.strptime` or an equivalent strict parser and reject values that do not match the accepted formats. 3. Preserve a strict allowlist mapping for `DATE`, `HOUR`, and `MONTH`; never insert arbitrary user-provided identifier or expression text. 4. Define the keyword delimiter explicitly and impose limits on keyword count and length. 5. Retain the read-only database account and restrict it to only the required table and columns. 6. Configure query timeouts and row/resource limits to reduce denial-of-service exposure. 7. Avoid returning raw database error messages to users because they may disclose schema or connection details. 8. Add automated tests using quotes, comment markers, wildcard characters, malformed dates, and oversized inputs. ]]>
