A Stranger Broke Our AI Agent’s Guardrails. Here Is What It Taught Us

A few weeks ago we published a technical walkthrough on a problem we see in almost every AI project we take on: how to stop an AI agent from reading data it shouldn’t.The short version was that you give the agent its own database role, grant it only the columns it needs, pin every query to one customer with row-level security, and write a test that tries to leak data.
It’s the pattern we recommend, and we tested every snippet before publishing.
The same day, a developer we had never met left a comment. He had rebuilt the whole setup from the article, seeded it with two test customers, and run our tests. They passed. Then he tried to break it, and found three separate ways to read one customer’s data while logged in as another.
He was right about all three. We reproduced each one before changing anything, fixed the article, and credited him at the top. But the bugs themselves matter less than what they say about how most teams are approaching AI agent security right now, ourselves included.
The most common mistake in “chat with your data” features is treating instructions to the model as access control. A system prompt that says “only answer questions about the current customer” is a request. Models follow it most of the time, and then a cleverly worded question, a confusing document or a prompt injection gets past it.
OWASP’s list of the top risks for LLM applications has a name for the underlying problem: excessive agency, meaning an AI system that has been given more permissions than its job requires.Their advice is the same advice security engineers have given for decades.
Limit what the component can do, and enforce the limit somewhere the component cannot talk its way past.It’s the same instinct behind the argument that AI agents need job descriptions, not better prompts: an agent’s authority should be defined outside the model, not inside it.
For an agent that queries a database, that somewhere is the database itself. Our article put the limits there, and we still got it wrong in three places. That’s the part worth learning from.
We granted the AI role read access to two tables, orders and products. We switched on row-level security for orders. We never switched it on for products.
Every test we wrote was about orders, because orders were what we were worried about. The reader asked the agent’s “low stock” tool a question and got back both customers’ product catalogues.
The lesson isn’t “remember every table.” People don’t remember every table.The lesson is that a test written from memory only covers what you remembered.His fix was better than ours: a test that asks the database for every table the AI role can read, and fails if any of them lacks a policy.
It doesn’t depend on anyone’s memory, and it keeps working when a new table is added next year by someone who never read the article.
Our design told the database which customer a request belonged to by setting a session value, and the security policy compared every row against that value. The application set it, never the model.
That holds as long as the model can only call the fixed queries we wrote.We had also mentioned, as a later option, letting the model write its own SQL.The reader showed what that would mean.Postgres lets any role call a built-in configuration function that changes session settings.
A single query could change the customer value halfway through and read another customer’s orders.The same function could switch off the timeout we had set to protect the database from runaway queries.
This is the pattern that worries me most about the current wave of agent tooling.Every capability we give an agent is a capability an attacker can try to use through it.The security guidance for the Model Context Protocol makes the same point at the protocol level: tools should get the narrowest permissions their task needs.
Letting a model write free-form SQL is the opposite of narrow.We now tell clients plainly that named, fixed queries are the default, and model-written SQL only runs after it passes a strict allowlist that checks what the statement actually does.
We had designed the policy to fail closed. If a request forgot to say which customer it belonged to, the policy returned nothing rather than everything. We tested it, and it worked.
It worked on a fresh database connection. In production, applications reuse connections through a pool, and on a connection that had already served a request, the “missing” value came back as an empty string rather than as nothing at all. The query didn’t leak data, but it crashed instead of returning an empty result, and whether it crashed depended on which connection the pool handed out.
No one would have found this from reading the code. It only showed up because someone ran it the way it would really run. The fix was one function call. Finding it took an outsider who didn’t share our assumptions about how the code would be used.
The article is fixed, but the more useful changes are in our pro