Production Context & Why This Matters
Allowing internal enterprise copilots to synthesize ad-hoc SQL strings directly against production database credentials exposes sensitive tables whenever untrusted ticket or email text enters the prompt context.
Asking an LLM nicely inside system prompts not to run `DROP` or query `users_secrets` offers zero real security boundary. Robust hardening treats generated SQL strictly as untrusted user input.
Architectural Mechanics & Reference Implementation
Parsing every candidate query via a SQL AST analyzer against allowlisted read-only views—and running execution under a `READ ONLY` role guarded by PostgreSQL RLS—enforces defense in depth.
CREATE ROLE ai_copilot_reader NOINHERIT LOGIN;
SET ROLE ai_copilot_reader;
SET default_transaction_read_only = on;
SET statement_timeout = '3000ms';
Verified Operational Benchmarks
Red-team exercises attempting encoded union injections and schema enumeration failed cleanly against AST validation + read-only RLS roles.
- Primary Delta: Blocked 100% of indirect prompt-injection & cross-table exfiltration vectors
- Reliability Gate: Zero write/DDL statements or system-catalog queries permitted on copilot pools
- Ownership Boundary: 100% client-owned IaC & telemetry pipelines audited by Randeep Kaur
Implementation Checklist
- Expose only curated analytical views to text-to-SQL agents, never raw core tables.
- Parse generated SQL structurally with an AST whitelist allowing SELECT statements only.
- Enforce hard 3-second statement timeouts so runaway joins cannot exhaust IOPS.