Skip to content

[P1][security] AI run_query ignores the connection's read_only profile flag — Agent mode in read-only DB can DELETE #312

Closed

Description

@EVWorth

Context

ConnectionProfile.read_only is a per-profile "Read-only mode" flag that — per connection.md and admin.md F1 — should gate destructive SQL at the executor level. handle_run_query in src-tauri/crates/mas-ai/src/tools.rs:387-411 checks !upper.starts_with("DROP") and WHERE-less DELETE/UPDATE client-side but does not call connection_manager.get_profile(connection_id).read_only. Audit finding F5 in docs/audits/ai.md.

Problem

A user who created a profile with read_only=true opens the AI sidebar in Agent mode and the model issues DELETE FROM staging.users — the AI tool happily forwards it to executor.execute(...) which executes against the so-called "read-only" database. The Agent mode permission dialog will ask the user before executing so it's not silent, but the prompt "The AI wants to DELETE FROM staging.users. Approve?" is materially less safe than the tool simply being unavailable. In Ask mode the read-only DDL/DML tool set is stripped before the session so Ask does honor read-only (read tools only); but Agent does not.

Files

  • src-tauri/crates/mas-ai/src/tools.rs:82-133 (build_tool_handlers takes connection_id but not profile)
  • src-tauri/crates/mas-ai/src/tools.rs:387-411 (handle_run_query)
  • src-tauri/crates/mas-core/src/connection/ConnectionManager::get_profile (call site for the check)

Repro

Static analysis: trace handle_run_query; observe no .read_only lookup. The connection_pool stores profiles keyed by profile_id; get_profile(connection_id) is a known accessor (cross-cite connection.md F1).

Expected

With a read_only=true profile active, run_query(sql="INSERT INTO x VALUES (1)") returns an error "Profile is read-only; mutating operations are not allowed via AI tools" without reaching the executor.

Proposed fix

Scope M. Pass ConnectionManager into build_tool_handlers (already Arc-shared via tools.rs:120-133); in handle_run_query, call connection_manager.get_profile(connection_id) and check .read_only. If true, return an error before any executor call. Surface a banner in AIChatPanel mode selector when the active profile is read-only and the user toggles to Agent/Plan mode.

Acceptance

With read_only=true profile active, Agent mode run_query(sql="SELECT 1") succeeds but run_query(sql="INSERT INTO x VALUES (1)") returns the read-only error. A unit test mocks ConnectionManager::get_profile to return read_only: true and asserts handle_run_query returns the read-only error.

Needs human verify

yes

Labels: audit, area/ai, severity/p1, kind/security, kind/doc-drift

Activity

  1. added
    audit
    severity/p1
    kind/security
    kind/doc-drift
    area/ai
    on Jul 31, 2026
  2. 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: already enforced, and will stay enforced from a different direction. The executor refuses writes on a read-only profile before the batch is assembled (#429), so the agent path inherits it rather than re-implementing it — and the new design adds a second lock at the tool boundary (§5).

    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/doc-driftkind/securityseverity/p1

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions