T09 · Insecure Skill Coding Practices
- Location
museum.py:69- Finding
SQL Injection Through Unsanitized Museum Command Arguments
- Content
View full analysis
Vulnerability Details
File Location:
museum.py, lines 69-78, 90-100, and 146-161
Vulnerability Type: SQL injection
Risk Level: HighThe structured
list,get, andcheckcommands interpolate user-controlled values directly into SQL statements.python def list_museums(args): """List museums""" where_clauses = [] if args.status: where_clauses.append(f"status='{args.status}'") if args.location: where_clauses.append(f"location LIKE '%{args.location}%'") where_sql = "WHERE " + " AND ".join(where_clauses) if where_clauses else ""python def get_museum(args): """Get single museum details""" query = args.query # Check if ID (32 char hex) or name if len(query) == 32 and all(c in '0123456789abcdef' for c in query.lower()): sql = f"SELECT * FROM museums WHERE id='{query}';" else: sql = f"SELECT * FROM museums WHERE name LIKE '%{query}%' LIMIT 5;" result = run_sql(sql)python def check_data(args): """Check data integrity""" if args.id: # Check single sql = f""" SELECT name, CASE WHEN introduction IS NULL OR introduction = '' THEN 'missing' ELSE 'ok' END as intro, CASE WHEN top3_artifacts IS NULL OR top3_artifacts = '[]' OR top3_artifacts = '["to be supplemented"]' THEN 'missing' ELSE 'ok' END as artifacts, CASE WHEN building_photo IS NULL THEN 'missing' ELSE 'ok' END as photo, status FROM museums WHERE id='{args.id}'; """Technical Analysis
Values from
args.status,args.location,args.query, andargs.idare inserted into SQL using Python f-strings. No parameter binding, escaping, or adequate validation is performed before the resulting statement is passed tomycli -e.The hexa ...[truncated 1574 chars]
- Remediation
View remediation
Remediation Suggestions
- Replace
myclisubprocess execution with a maintained MySQL library that supports parameterized statements, such asmysql-connector-pythonor PyMySQL. - Bind every user-controlled value as a query parameter rather than performing manual quoting or string interpolation.
- Restrict
statusto an explicit allowlist such ascomplete,partial, andpending. - Validate all IDs against a strict expression equivalent to
^[0-9a-fA-F]{32}$. - Enforce safe numeric bounds for
limitandoffset, including nonnegative offsets and a reasonable maximum result limit. - Run the structured read commands through a database account with
SELECTpermission only. - Add regression tests containing quotes, SQL comments, escape characters, and attempted boolean or stacked-statement payloads.
- Replace
