How to Give an AI Agent Database Access Without Giving It Everything

We ran one agent tool three times, changing nothing but the credential it carried. With the master key it reached every row, the stored provider key and the permissions screen.

The same agent tool run with three different credentials against a backend holding twelve notes for three people and one stored provider key: with the master key it read all twelve notes, read the provider key, listed every table, deleted another person's note and reopened the table to everyone, with one user's session it read only that user's four notes and everything else was refused, and with the public app id alone everything was refused

The agent could not read the table, so somebody gave it the key that reads every table. That is the entire story, and it is almost always told as a productivity decision rather than a security one.

You cannot prevent an agent being talked into doing something. You can decide what it is holding at the moment it is. The first is a research problem; the second is a line of configuration.

So we wrote one agent tool, ran it three times against the same backend, and changed nothing between runs except the credential in its hand.

Watch it instead

Agents holding production credentials is number 8 in our countdown of the security holes that turn up most often in apps built with AI.

Video: 31,000 AI-Built Apps Scanned: The 10 Security Holes That Actually Get You Hacked.
Watch it instead 31,000 AI-Built Apps Scanned: The 10 Security Holes That Actually Get You Hacked.
From the Back4app channel. This guide is the long version of its eighth position. Open on YouTube →

What did the same tool reach with each credential?

Everything, a quarter, and nothing — in that order:

The same agent tool measured with three credentials against twelve notes belonging to three people: with the master key it read all twelve, read the stored provider key, listed five tables, deleted another person's note and reopened the table to everyone, while with one person's session it read four notes and everything else was refused, and with the public app id alone everything was refused

Read the middle row as the recommendation. It is not a weaker agent, a smaller model or a narrower prompt — it is the same code reaching less, because the database is applying somebody’s permissions to every call it makes.

How we measured: one Parse Server 7.4.0 backend in Docker, twelve notes with per-row owners, one stored provider key, and a tool defined once and parameterised only by the credential. Identical across two clean runs — the lab is in the repo.

Why not just write a better prompt?

Because the thing you would be defending against is a sentence, and sentences are infinite. Instructions reaching a model through data it was asked to read is a demonstrated attack against shipping products, not a theoretical one, and no system prompt has closed it.

This lab deliberately does not try to measure whether a model can be fooled. It assumes it can, and measures the half you actually control.

Treat the agent as a user you have never met. You would not hand that user the master key, and the agent reads its instructions from strangers.

That reframing does more work than any amount of prompt hardening, because it turns an open research question into an access-control question that your database already knows how to answer.

What exactly does the master key switch off?

Every rule you wrote, including the ones you wrote last week and forgot. That is what it is for, which is why it is the thing that makes the agent “work”:

carrying the master key
  notes it can read:                          12 of 12
  the stored provider key:                    readable — sk-lab-0000-not-a-real-key
  the full list of tables:                    5 tables
  deleting one of Bruno's notes:              done
  opening the table to everyone:              done — and it stays done

The notes table in this backend was not careless: it required a signed-in reader, and every row carried its owner. None of that applied.

Line three is the one people underrate. Listing the schema is how an agent — or anyone holding that key — finds the tables nobody mentioned, including the one somebody used to store a credential.

What does the scoped version give up?

Less than you would expect, and the trade is visible in a single line:

carrying Ana's session
  notes it can read:                          4 of 12
  the stored provider key:                    not readable
  the full list of tables:                    refused
  deleting one of Bruno's notes:              refused
  opening the table to everyone:              refused

Four of twelve is not a limitation if the agent is helping Ana. It is the correct answer, and it arrives without the tool knowing anything about ownership — the database applied it.

What you do give up is the class of task that genuinely needs to see across people: a report, an admin summary, a migration. Those need their own function with its own narrow permission, not a general-purpose key handed to a conversation.

How fast is the bad case?

Faster than noticing. We told the master-key agent to clean up:

notes before:                12
notes after:                 0
how long the agent needed:   15 ms

Twelve rows is a toy number and fifteen milliseconds is a toy duration; multiply both by your real table and the shape stays the same. There is no window in which a human reacts, because the whole operation completes between two frames of a screen refresh.

One agent tool, three credentials
12 of 12notes reached with the master key
→
4 of 12with one person's session, same tool
15 msto empty the table when told to clean up
Parse Server 7.4.0, MongoDB 7, Docker 29.4.3. Measured 29 Oct 2026 and re-measured from a clean clone on 6 Nov; the deletion took 10 ms and 15 ms. Every destructive probe targets a row owned by somebody else.

Which of these would you actually notice?

The deletion, and only the deletion — which is why the other one is worse. An empty table announces itself: the app breaks, somebody calls, you start restoring.

A reopened table announces nothing. The app works exactly as before, every screen renders, no error appears anywhere, and the permissions stay open after the conversation is closed and the agent is gone.

So the monitoring that matters is not on rows. It is on configuration: permission changes, new tables, new keys. Those are rare enough that alerting on every one of them is realistic, and they are the changes that outlive whoever made them.

When does an agent need the big key?

When the task is genuinely administrative and there is no person on whose behalf it is acting. A nightly migration, a backfill, a schema change: there is no session to borrow because nobody is asking.

Even then the shape should be different. That key belongs to a server-side job with a fixed, reviewed script — not to a conversational agent whose next instruction arrives from outside. The distinction is not how powerful the credential is but whether what it does next was decided in advance.

And there is a real cost to the scoped approach worth stating: some things become genuinely harder. An agent that cannot list the schema cannot explore your data model, so it needs to be told. That is a worse developer experience and a better production posture, and in development — against a copy, with no real customers in it — the trade can reasonably go the other way.

What should you write down today?

For each tool your agent can call, write the credential it carries. Not what it is allowed to do in principle — the literal key or session in the code path.

If any of them says master, admin or service account, you already have the result of this post for your own app. Changing it to a session is usually a smaller edit than the conversation about whether to change it.

Then ask your agent for everything — every row of your largest table — and look at what comes back. The answer to that one question is the blast radius of every future instruction it is ever given.

Tagssecurityai-agentcredentialsprompt-injectionpermissionsvibe-coding

Frequently asked questions

Should I give an AI agent my database master key?

No, and the measurement is unambiguous. Carrying the master key, one agent tool read all 12 notes belonging to three different people, read a stored provider key, listed every table, deleted somebody else's row and reopened the table to the public. Carrying one person's session, the same tool read 4 notes and every other action was refused.

How do I stop prompt injection in my AI feature?

You do not stop the persuasion; you bound the consequence. Assume the instruction gets through, because it has against shipping products, and decide in advance what the tool can reach when it does. That is a credential decision rather than a wording one.

What is the worst an agent with production access can do?

Remove things faster than you can react and change settings that outlast the conversation. Ours emptied a twelve-row table in about 10 milliseconds when told to clean up, and separately reopened a locked-down table to everyone — a change that is still there after the session ends.

How should an AI agent read my data safely?

Have it hold a session for a real person, so the database applies that person's permissions to every call. In our run the session-carrying agent saw only that person's four notes, could not read the stored key, could not list the schema and could not delete anything belonging to anyone else — with no change to the tool.

Do I need a separate database for my agent to work on?

Separate apps for development and production are the cheap version of this and worth doing anyway. But a development copy and a scoped credential solve different halves: the copy limits which data exists, the credential limits what any single call can touch within it.

Why is reopening the permissions worse than deleting rows?

Because deletion is loud and a permissions change is silent. The deleted rows were noticed immediately; the reopened table looks exactly like the app working, and it keeps being open after the agent has finished and the conversation has been closed.

Further reading

Reviewed & verified by Back4app Engineering, Editorial review at Back4app

Tested Oct 29, 2026 — Local: Docker 29.4.3 on macOS, arm64 · Parse Server 7.4.0 with MongoDB 7 · Node 22.21.1, no dependencies.

More from Back4app Engineering

AI Agent · conceptual

Two Kinds of AI Agent: One Builds the App, One Operates the Backend

Backend · tutorial

Your Frontend Talks Straight to the Database. Here Is How to Lock It Down [Full Repo]

Web Deployment · tutorial

How to Undo Changes an AI Coding Assistant Made: What Git Recovers, What the Platform Recovers, What Is Gone