Skip to content

[P1][security+doc-drift] No rate limiter or token budget — ADR-007 promises both; implementation has neither #317

Closed

Description

@EVWorth

Context

ADR-007 mitigation table in docs/design/TECH_DECISIONS.md:478-490 lists: "Stream all AI responses token-by-token" (DONE), "Intelligent schema pruning / embedding-based relevance scoring for 200+ tables" (NOT DONE — see F6), "Cache common explanations" (NOT DONE), and other mitigations. Audit finding F10 in docs/audits/ai.md.

Problem

(a) No request rate limit — the frontend can fire sendMessage in a tight loop and the backend will issue N Copilot API calls. (b) No token budget per session — a prompt with a 4000-char schema + a 10-turn conversation history is effectively unbounded. (c) No response cache. (d) "Dangerous SQL is NEVER auto-executed" — the "Run" button on SqlCodeBlock (src/components/ai/ChatMessage.tsx:104-110) calls useResultStore.executeQuery(connectionId, code) without an explicit user confirm for destructive SQL patterns. The editor's resultStore.productionConfirm regex (per editor.md F25) is the only safety net and is incomplete.

Files

  • docs/design/TECH_DECISIONS.md:478-490 (ADR-007 mitigations)
  • src-tauri/crates/mas-ai/src/service.rs (no rate limiter, no token counter)
  • src/components/ai/ChatMessage.tsx:104-110 (SqlCodeBlock handleRun — no destructive confirm)

Repro

Dispatch sendMessage 100 times in 10s from the frontend (e.g., via useAiStore.getState().sendMessage("ping") in devtools). Observe the backend happily fires 100 Copilot API calls with no backoff.

Expected

A token-budget middleware in chat counts schema context + history + user message against the model's max_input_tokens; prompts over budget return AiError::ContextError. Per-conversation rate limiter caps request bursts. Destructive SQL produced by AI requires an explicit production-confirm dialog before executeQuery.

Proposed fix

Scope M. Add a token-budget middleware; per-conversation rate limiter (Arc<Mutex<HashMap<String, VecDeque<Instant>>>>); common-explanation cache (sql_hash → cached response). For SqlCodeBlock handleRun: confirm-as-production when the SQL matches /^\s*(drop|truncate|delete\s+from|update\s+\w+\s+set|insert\s+into|alter\s+table|create\s+table)/i.

Acceptance

100 sendMessage calls in 10s trigger rate-limit error after configured burst size. A 60k-token prompt against a 16k model returns context-error. Clicking "Run" on a DROP TABLE SQL block opens the production confirm dialog.

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: rate limiting and token budgets are the harness's concern now, not ours. ADR-007 promised both and built neither; ADR-011 says plainly that we do not meter what we do not call.

    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