Unbounded agent-written SQL can overreach the approved data surface, return too much data, or produce unsafe queries if the model’s text is trusted directly. A safer design parses the query, restricts it to approved tables and columns, rebuilds it, and applies execution and result limits before the database sees it.
Why This Matters for Security Teams
Letting an agent write SQL is not just a coding shortcut. It turns a natural-language interface into a direct path to sensitive data, and the failure mode is usually overreach: broad table scans, accidental exposure of restricted columns, or query patterns that do more than the request intended. Current guidance from the OWASP Agentic AI Top 10 and the NIST AI Risk Management Framework points to runtime controls, not trust in model output, because the model cannot reliably distinguish harmless analytics from unsafe retrieval.
For NHI governance, the issue is not only access to the database. It is the identity and authority the agent carries into that database session. If the agent can generate arbitrary SQL, then row-level restrictions, schema boundaries, and result minimisation must be enforced outside the model, not implied by prompt instructions. That is why NHI Management Group treats database access as a workload identity and policy problem, not a text generation problem. The broader risk pattern also appears in Analysis of Claude Code Security and Replit AI Tool Database Deletion, where agentic execution crossed from assistance into unsafe action.
In practice, many security teams discover the problem only after an agent has already queried more data than anyone expected, rather than through intentional testing of query boundaries.
How It Works in Practice
The safer pattern is to treat agent-written SQL as an untrusted draft. The agent may propose intent, but the platform should parse and validate the query, enforce an allowlist of tables and columns, and rebuild the final statement from approved primitives before execution. This is especially important for autonomous workflows where the agent can chain tool use, retry failed requests, or pivot into adjacent datasets.
A practical control stack usually includes:
- Read-only database roles with no write, delete, or DDL privileges.
- Query parsing and AST validation so only approved SQL patterns can execute.
- Schema-level allowlists for tables, views, and columns.
- Row limits, timeouts, and pagination to prevent bulk extraction.
- Result filtering to suppress sensitive fields such as tokens, identifiers, or internal notes.
- Separate workload identity for the agent session, not shared human credentials.
That identity layer matters because agents are not static users. Controls such as SPIFFE/SPIRE or scoped OIDC tokens can bind a workload identity to a specific task, environment, and lifespan, which is much safer than long-lived database credentials. This maps cleanly to the risk picture described in the Ultimate Guide to NHIs, especially the prevalence of excessive privilege and poor visibility. The same runtime-control logic is reinforced by the CSA MAESTRO agentic AI threat modeling framework, which emphasises constrained execution and explicit trust boundaries. These controls tend to break down when the agent is allowed to join ad hoc reporting tools, because ad hoc joins can bypass simple allowlists and surface sensitive data through indirect relationships.
Common Variations and Edge Cases
Tighter read-only controls often increase implementation overhead, requiring organisations to balance speed of analysis against the risk of data overexposure. That tradeoff becomes sharper when teams want flexible, natural-language reporting across many schemas, because the more expressive the SQL surface, the harder it is to guarantee safety at runtime.
There is no universal standard for this yet, but current guidance suggests different handling for different environments. For narrow, high-risk datasets, the safest approach is a curated reporting layer with prebuilt views rather than free-form SQL. For broader analytics, a policy engine can permit limited joins while still blocking sensitive tables and write-capable statements. For multi-agent systems, each agent should receive only the minimum scope needed for the current task, with short-lived credentials revoked after completion.
This is also where false confidence is common. A prompt instruction such as “only read data” does not prevent unsafe query construction, and even a well-tuned model can infer sensitive relationships if the schema is too open. NHI Management Group’s OWASP NHI Top 10 and Moltbook AI agent keys breach coverage both reinforce the same point: when an agent can act with broad authority, the failure is usually privilege scope, not model intelligence.
Standards & Framework Alignment
This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.
OWASP Agentic AI Top 10, CSA MAESTRO and OWASP Non-Human Identity Top 10 address the attack and risk surface, while NIST AI RMF and NIST CSF 2.0 set the governance and control requirements practitioners need to meet.
| Framework | Control / Reference | Relevance |
|---|---|---|
| OWASP Agentic AI Top 10 | A3 | Addresses unsafe tool and data access by autonomous agents. |
| CSA MAESTRO | T-5 | Focuses on agent trust boundaries and runtime control of actions. |
| NIST AI RMF | GOVERN | Covers governance for high-impact AI behavior and accountability. |
| OWASP Non-Human Identity Top 10 | NHI-03 | Relates to excessive privilege and credential scope for non-human identities. |
| NIST CSF 2.0 | PR.AC-4 | Supports least-privilege access enforcement for workload identities. |
Assign ownership for agent SQL behavior and review controls as a governed risk.
Related resources from NHI Mgmt Group
- What breaks when parallel agents are allowed to scale without cost and quota controls?
- What breaks when AI assistants can read private repository context without strict content controls?
- What breaks when AI agents can read local files and execute shell commands without strong controls?
- What breaks when AI agents are allowed to manage security findings without clear approval controls?