T09 · Insecure Skill Coding Practices
- Location
scripts/search_client.py:276- Finding
SQL Injection Through Unsanitized Dynamic Query Construction
- Content
View full analysis
Vulnerability Details
File Location:
scripts/search_client.py:276-298
Vulnerability Type: SQL injection
Risk Level: HighVulnerable Code
python def vector_search(self, table_name: str, query_embedding: List[float], top_k: int = 10, filter_condition: str = None, metric: str = "COSINE", hnsw_ef_s: int = 200) -> List[Dict]: """Pure vector search""" distance_func = "cosineDistance" if metric == "COSINE" else "L2Distance" sql = f""" SELECT id, content_type, content, title, metadata, create_time, {distance_func}(embedding, {query_embedding}) AS score FROM {table_name} {f"WHERE {filter_condition}" if filter_condition else ""} ORDER BY score ASC LIMIT {top_k} SETTINGS enable_new_ann = 1, hnsw_ef_s = {hnsw_ef_s} """ rows = self._execute_sql(sql, query_type="select") columns = ['id', 'content_type', 'content', 'title', 'metadata', 'create_time', 'score'] return [dict(zip(columns, row)) for row in rows]The same unsafe construction pattern also occurs in:
scripts/search_client.py:139-167, wheretable_name, index settings, and related options are interpolated into DDL.scripts/search_client.py:196-204, wheretable_nameand inserted values are interpolated into anINSERTstatement.scripts/search_client.py:337-344, wheretable_name,query_text,filter_condition, and limits are used to build a hybrid-search query.scripts/search_client.py:376-400, where caller-provided filter conditions are appended to generated search filters.
Technical Analysis
The client builds SQL statements using Python f-strings and forwards them directly to ByteHouse or the MCP database tool. Caller-controlled values such as
table_nameandfilter_conditionare treated as executable ...[truncated 1963 chars]- Remediation
View remediation
Remediation Suggestions
- Use database-driver parameter binding for every data value, including text, IDs, vectors, and limits where supported.
- Do not expose raw SQL through
filter_condition. Replace it with a structured filter representation containing approved columns, operators, and typed values. - Validate table names and other identifiers against a strict identifier pattern and, preferably, an application-maintained allowlist.
- Allowlist
metric,index_type, and other enum-like options before query construction. - Convert numeric settings with strict type checks and enforce safe minimum and maximum values.
- Use driver-supported identifier-quoting facilities where dynamic identifiers are unavoidable.
- Ensure the database account has only the minimum required permissions. Separate read-only search credentials from DDL and insertion credentials.
- Add security tests covering malicious identifiers, injected predicates, comment sequences, unions, and statement delimiters.
