Independent Submission
Request for Comments: 2610
Category: Informational
I. S. Hudzaifah
Bandung, Indonesia
August 2026

← Section 3, Publications

Letting AI agents query a database without handing over the keys

Abstract

How DBPortal gives people and AI agents the same narrow, audited access to our production databases, and why the database itself ends up being the last line of defense.

1. Where We Started

Not that long ago, getting data out of a prod database at work went like this. SSH into the server, docker exec into the DB container, log in with the shared root password, and type carefully. Everyone had the same access, which was all of it (salary tables, personal records, the lot), and nothing was logged.

Nobody designed it that way. It just grew, one urgent request at a time. Who can read the salary table? Everyone. Who deleted those rows last Tuesday? No idea.

Then AI agents showed up. Engineers wanted an assistant to look up data or debug a report, and the easy answer was to give the agent the same connection string everyone else had, which tbh would have meant handing a root password to a program that reads untrusted text for a living.

So we built DBPortal instead.

2. Agents Are Just Users

The main design decision is pretty simple. An AI agent is a user, acting on behalf of a person, with exactly that person’s permissions.

An engineer creates an API key in the web UI, and the key belongs to them. When the agent calls DBPortal with it, the key gets resolved to the owner’s current groups and permissions, not some copy taken when the key was made. So if the owner loses access to a table tomorrow, the agent loses it too.

The query path is literally the same code. The MCP tool that runs a query calls the same service function as the web editor does:

result, err := d.Query.Execute(ctx,
    user.GroupIDs, user.GroupName, user.Username, user.ClientIP,
    query.ExecuteRequest{ConnectionID: in.ConnectionID, SQL: in.SQL})

No second implementation that could drift, no “agent mode” w/ looser rules. An agent can’t see a table its owner can’t.

The keys are treated like passwords: random 32-byte values, stored only as a hash, with an expiry date, revocable in one click. Requests are rate-limited too.

Permissions are granted to groups, per connection and per table (SELECT, INSERT, UPDATE, DELETE), and a single user can get an explicit grant or deny on top, where deny always wins.

It’s closed by default. A table with no permission row isn’t readable, so a freshly created table (which might well contain something sensitive) stays invisible until someone decides otherwise. The schema browser follows the same rule. An agent can’t get curious about a table it doesn’t know exists.

3. Checking a Query Before It Runs

Permissions only help if every query is checked against them, so each query gets parsed into a syntax tree before it runs. No regexes. The checks, roughly:

  1. One statement only. A semicolon outside strings, quotes and comments is rejected, so SELECT 1; DROP TABLE users never even reaches the permission check.
  2. Dangerous statements are always blocked: GRANT, REVOKE, user and role management, SET GLOBAL, KILL, LOAD DATA, RENAME TABLE, prepared statements, and USE (which could switch to another database and step around the connection’s permissions).
  3. Schema changes are denied for everyone. CREATE, DROP, ALTER, TRUNCATE. Schema changes belong in migrations that get reviewed.
  4. UPDATE and DELETE need a WHERE clause, checked on the tree and not the text, so a comment or a line break can’t fool it.
  5. Every table the query touches is checked against the user’s permissions for that statement type, including the ones hiding in joins, subqueries and CTEs.
  6. Results are capped: 30 seconds max run time and 1,000 rows by default, per connection.

Parsing SQL is harder than it looks. The parser has hundreds of compatibility tests now, and a lot of the recent fixes were false positives, e.g. a CTE alias mistaken for a table. A safety system that blocks legit work just gets bypassed, so imo false positives are bugs too.

4. The Database Is the Last Line of Defense

All of the above is application code, and application code has bugs. So we don’t rely on it alone.

Each connection is set up with two DB accounts: a read-only user for everyday queries, and a separate account for the few operations that need more. Their passwords are encrypted at rest with AES-256-GCM and never returned by any API, not even the admin tools. Ordinary queries run as the read-only user.

The line in our security notes goes something like: even if every application-level check fails, the read-only database user physically cannot run a schema change. The parser is the first gate, the permission check is the second, and the database’s own grants are the one that doesn’t depend on our code being correct. It’s one of six layers overall, and none of them has to be perfect.

And every query goes to an audit log whether it succeeds or not: who, the full text, the tables, and whether it succeeded, got denied or failed. The denials matter as much as the successes. A burst of them from one agent means something is off, maybe it read a document that told it to try something it shouldn’t. Since the log records the agent’s owner, there’s always a person to go ask.

That last bit is really about prompt injection. An agent that reads a ticket or an email or a doc can be told, by that text, to do something else, and you can’t fully stop a model from being persuaded. What you can do is make sure that when it is persuaded, the worst it can do is run a capped, logged SELECT on tables its owner could already read.

We didn’t build DBPortal because of AI btw. Shared root passwords were already a problem. The agents just made it impossible to ignore, and having one audited front door turned out to be what made it safe to let them in.

HudzaifahInformational[Page 1]