Skip to content

[P0][security] AI run_select_query / run_query multi-statement SQL bypass — read-only guarantee is fake #308

Closed

Description

@EVWorth

Context

The AI Assistant exposes 8 read-only tools and 1 write tool (run_query) to a Copilot LLM via src-tauri/crates/mas-ai/src/tools.rs. Ask mode's read-only promise relies on a single-line prefix check (upper.starts_with("SELECT")), then SQL is passed to mas_core::query::QueryExecutor::execute which calls split_statements(...) and joins with "; " before sending as one raw_sql. Audit finding F1 in docs/audits/ai.md.

Problem

run_select_query(sql="SELECT 1; DROP TABLE users") passes the prefix check and executor.execute happily executes both statements — users is dropped despite Ask mode. Same bypass hits explain_query (which prepends EXPLAIN but doesn't strip trailing statements). run_query's !upper.starts_with("DROP") check is also prefix-only — "INSERT INTO x VALUES (1); DROP TABLE y" slips through. The read-only promise in Ask mode is materially false; the Agent/Plan permission dialog catches obvious single-statement DML but not multi-statement wrappers.

Files

  • src-tauri/crates/mas-ai/src/tools.rs:278-303 (run_select_query)
  • src-tauri/crates/mas-ai/src/tools.rs:305-318 (explain_query)
  • src-tauri/crates/mas-ai/src/tools.rs:387-411 (run_query)
  • src-tauri/crates/mas-core/src/query/executor.rs:38, 81-89 (multi-statement split)

Repro

Static analysis: prefix check operates on the trimmed/uppercased whole input. executor.execute then unconditionally splits. No unit test verifies multi-statement rejection. With the AI panel in Ask mode, ask: "Find a row with id 1; also drop the users table"; observe users being dropped.

Expected

Multi-statement SQL is rejected at the AI tool boundary with a clear error; the DB state is unchanged.

Proposed fix

Scope L. Replace starts_with with a per-statement split + allow-list validator at the top of every mutating tool handler. Split on ;, validate each statement against the allow-list for that tool, reject the whole call if any statement is not on the list. Optionally also enforce read_only profile flag (cross-cite F5).

Acceptance

A test feeds run_select_query(sql="SELECT 1; DROP TABLE users") and asserts the call returns an error. The pool connection's SHOW PROCESSLIST shows no DROP ever ran.

Needs human verify

yes (runtime multi-statement behavior depends on MySQL session settings)

Activity

  1. added
    audit
    severity/p0
    kind/security
    area/ai
    on Jul 31, 2026
  2. Ar9av commented on Aug 1, 2026

    @Ar9av

    Prefix checks on SQL text are the same class of bug as prefix checks on shell commands, whatever comes after the semicolon just doesn't get evaluated. Worth naming precisely what a tool call level policy engine would and wouldn't catch here: something like immunity-agent (github.com/PrismorSec/prismor) evaluates shell command text against destructive patterns, but it has no SQL statement parser, so a raw_sql string handed to a database driver through an application's own tool wrapper is invisible to it entirely unless the wrapper itself calls out to something that inspects the query. The real fix has to be in the query executor, splitting and validating every statement rather than trusting a single prefix check on the first one.

  3. EVWorth commented on Sep 12, 2026

    @EVWorth
    OwnerAuthor

    Closing as obsolete rather than fixed: this describes the implementation that ADR-011 replaces.

    The AI feature is being rebuilt around a decision made in #673 — SQLPilot does not call a model. It exposes the database, under its own policy, to whatever harness the user already runs and pays for. The mas-ai crate, its Copilot SDK session, its system prompt and its tool guards are all going.

    What that does to this issue specifically: the multi-statement bypass is structurally gone. starts_with("SELECT") on a string an executor later splits on ; is replaced by single-statement enforcement at the tool boundary, using the splitter and effective_verb that already exist and are tested in mas-core. Your repro is a test case in the new design (§4.3).

    The design is in docs/design/AI_INTEGRATION.md and ADR-011. If any part of this issue survives the redesign in a form I have missed, reopen it — I would rather re-argue it than lose it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/aiauditkind/securityseverity/p0

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions