Skip to content
zechim
Back to blog
4 min readsecurityagentspostgresservicesai-consulting

Security review for agents that query your data

If a model writes SQL that you then run, the prompt is not your boundary. Five questions we ask, what we review in the database and the application, the fixes we apply, and how we deliver it.

by Zechim

A system prompt tells a model how to behave. It does not stop anyone from reading data the model can reach. When an agent writes queries that your system executes, the real boundary is the database role those queries run as, and most teams have never checked what that role can read.

This post describes the security review we run for agents that query data, what we look at, and what we change.

The five questions

  1. What role does the query execute as, and what can that role read? Not what you granted. What the role can reach, including what table ownership and security definer functions hand it for free.
  2. Does your filter restrict verbs or nouns? Most filters block insert, update and drop. Reading is the exfiltration path, and a read is always allowed.
  3. What else lives in that database? Secrets, tokens and user records often sit next to the agent's legitimate data, separated only by grants that ownership bypasses.
  4. If the model emitted the worst query you can imagine, what happens? If the answer depends on the model choosing not to, you have a preference and no boundary.
  5. Could you prove nobody used it? Without query logs you can only infer innocence from evidence you never collected.

What we review

The execution role

We trace the path from the model's output to the database: which function or connection runs the query, who owns it, what it can read, and whether row level security and grants actually apply to it. Ownership bypasses both, so a function owned by an administrative role makes your revokes decorative.

The filter

If a filter exists, we test it against reads of tables it should not reach, not only against writes. A keyword list is a statement about what a query may do, and says nothing about what it may see.

Where secrets live

Tokens, keys and user data in the same database as the agent's data, readable by the same role. We look for plaintext credentials and for tables the agent role can select from that it has no reason to.

Logging and detection

Whether every query the agent runs is recorded with who asked and what came back, and whether anyone would notice an unusual one.

Behavior under pressure

We test whether the agent holds when a user pushes back or asks again. A refusal that comes from the prompt is a behavior and may not survive a second request.

What we change

  • A least privilege role that owns the execution path and can read only the schema the agent is meant to use. The worst query the model can write then returns a permission error.
  • Separation of data: secrets and user records in a different schema or database from the agent's data.
  • Secrets encrypted at rest, with the key kept outside the database, so a dump or an unexpected read yields ciphertext.
  • Query logging for every statement the agent runs, with enough context to answer who and when.
  • Limits on statement time and result size, so a bad query cannot become an outage.

One trap we see often: removing a schema from the search_path looks like a fix. A query that names the schema explicitly still resolves, and a "relation does not exist" error on the unqualified name reads as reassuring while the data stays reachable. The fix is the privilege itself.

What we do not do

  • We do not guarantee that a system is secure. We report what we found, what we changed, and what we did not test.
  • We do not rely on a better prompt or a better regex. A fix that depends on predicting the model's output is the same bet at slightly better odds.
  • We do not run attacks on systems you do not own. The review covers your system with your permission.

How we deliver it

  1. 2 week Discovery. We map the path from model to data, review roles, filters, logs and secrets, and return findings ranked by risk with a fix for each.
  2. 4 to 8 week Pilot. We apply the fixes that matter most, verify each one with a test that would have failed before, and document what remains.
  3. Optional ongoing mode. A Fractional AI Lead reviews changes to the agent and the data model before they ship.

You keep the code, the tests and the findings.

What you need to start

  • Read access to the database schema, roles and grants.
  • The agent code, including the function or service that runs the queries.
  • Someone who can tell us which tables hold sensitive data.

If you run an agent that writes SQL against a database with anything sensitive in it, worth 30 minutes to go through these five questions against your setup.