llmsql-benchmark icon indicating copy to clipboard operation
llmsql-benchmark copied to clipboard

[Security] Evaluation executes model-generated SQL on a writable DB connection without timeout

Open DzmitryPihulski opened this issue 1 month ago • 1 comments

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 with SELECT, but sqlite3.Cursor.execute will happily run e.g. SELECT 1; ... fragments or functions with side effects; more importantly nothing prevents future extractor changes from executing DROP/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 also PRAGMA query_only = ON).
  • Add a per-query time budget with conn.set_progress_handler(...) (e.g. default 5s, configurable via evaluate(..., query_timeout=...)).
  • Add tests: a DROP TABLE/DELETE candidate must fail and leave the DB intact; an infinite recursive CTE must time out and count as sql_error.

Acceptance criteria

  • [ ] read-only connection
  • [ ] configurable timeout, counted as SQL error
  • [ ] tests

DzmitryPihulski avatar Oct 04 '26 05:10 DzmitryPihulski

Hi! I'd like to take this one.

Plan (following the acceptance criteria in the issue):

  1. Read-only connection — connect_sqlite() opens file:<abs path>?mode=ro (URI mode) plus PRAGMA query_only = ON, so a DROP/DELETE/UPDATE candidate can only fail, never corrupt the cached sqlite_tables.db.
  2. Configurable timeout — execute_sql(conn, sql, query_timeout=...) installs conn.set_progress_handler(...) with a monotonic deadline; returning non-zero aborts the query, which execute_sql already turns into None and evaluate_sample counts as sql_error. Wired through evaluate_sample(..., query_timeout=...) and evaluate(..., query_timeout=...) with a default of 5s per query.
  3. Tests — a DROP TABLE candidate 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.

ddaihao2022 avatar Oct 12 '26 15:10 ddaihao2022