llmsql-benchmark
llmsql-benchmark copied to clipboard
[Security] Evaluation executes model-generated SQL on a writable DB connection without timeout
Problem
llmsql/utils/evaluation_utils.py::connect_sqlite opens sqlite_tables.db with a regular read-write connection, and execute_sql runs up to 10 model-extracted statements per sample without any guard.
find_sql()only requires the candidate to start withSELECT, butsqlite3.Cursor.executewill happily run e.g.SELECT 1; ...fragments or functions with side effects; more importantly nothing prevents future extractor changes from executingDROP/UPDATE.- A pathological query (e.g. a large cartesian product via
WITH RECURSIVE, or a self-join emitted by a model) can hang evaluation forever — there is no timeout / progress handler. - Since the downloaded DB is cached in
workdir_path, a destructive statement would silently corrupt all later evaluations.
Proposal
- Open the DB read-only:
sqlite3.connect(f"file:{db_path}?mode=ro", uri=True)(optionally alsoPRAGMA query_only = ON). - Add a per-query time budget with
conn.set_progress_handler(...)(e.g. default 5s, configurable viaevaluate(..., query_timeout=...)). - Add tests: a
DROP TABLE/DELETEcandidate must fail and leave the DB intact; an infinite recursive CTE must time out and count assql_error.
Acceptance criteria
- [ ] read-only connection
- [ ] configurable timeout, counted as SQL error
- [ ] tests
Hi! I'd like to take this one.
Plan (following the acceptance criteria in the issue):
- Read-only connection —
connect_sqlite()opensfile:<abs path>?mode=ro(URI mode) plusPRAGMA query_only = ON, so aDROP/DELETE/UPDATEcandidate can only fail, never corrupt the cachedsqlite_tables.db. - Configurable timeout —
execute_sql(conn, sql, query_timeout=...)installsconn.set_progress_handler(...)with a monotonic deadline; returning non-zero aborts the query, whichexecute_sqlalready turns intoNoneandevaluate_samplecounts assql_error. Wired throughevaluate_sample(..., query_timeout=...)andevaluate(..., query_timeout=...)with a default of 5s per query. - Tests — a
DROP TABLEcandidate fails and leaves the DB intact, and an unbounded recursive CTE is cut off and counted as a SQL error.
Will keep the change confined to llmsql/utils/evaluation_utils.py + llmsql/evaluation/evaluate.py + tests. PR up shortly.