From code to access control: How vulnerability prevention evolves for agentic applications

by Nathan Brahms | Nov 28, 2026 | AI agents

7 min

Traditional application code paths compared with dynamic AI agent execution paths and resource-level access controls.

In October 2026, major U.S. banks began blocking third-party AI agents from accessing customer accounts. Some agents were logging in with customers’ credentials and reportedly attempting actions beyond the users’ instructions. The problem wasn’t authentication. The agents had valid credentials. The problem was that those credentials didn’t distinguish the agent from the user or limit what the agent could do on the user’s behalf.

This exposes a problem with traditional access control. Human access is typically governed through standing permissions and periodic reviews. Traditional machine workloads execute predictable operations against predefined resources. AI agents operate at machine speed but generate their own execution paths, select tools, construct queries and delegate tasks to other agents.

ExecutionAccess modelAccess expiration
HumansHuman-driven actionsStanding permissions or JIT accessPeriodic reviews or time-bound grants
Traditional workloadsPredictable, predefined operationsWorkload or service permissionsOften persistent until rotated or revoked
AI agentsDynamic tools, queries and delegationRuntime authorization based on user, agent and taskTask-scoped grants revoked when work ends

A permission granted when an agent starts may be inappropriate for an action it decides to take several steps later. The problem is especially clear with insecure direct object reference (IDOR). Traditional applications prevent IDOR by checking object ownership in developer-written code paths. An agent with a tool like execute_sql can generate those queries itself, including the WHERE clause that determines which records it reads.

Preventing IDOR in that environment requires moving authorization outside the agent’s control. The database must enforce access based on verified identity and policy, regardless of the SQL the agent generates.

This post examines how to do that using cryptographic identity propagation, policy-generated database roles and row-level security.

Traditional prevention: the control lives in the code path

In a traditional web or API app, a developer writes every query path before the code ships. Security checks attach to those paths. Parameterized queries stop SQL injection because the query’s shape is fixed when it is written. User input can only fill its slots.

IDOR follows the same pattern. OWASP ranks it first in its API Security Top 10, as Broken Object Level Authorization. The advice is simple. Any function that takes an ID from the client must check that the logged-in user may act on that record. In practice, the check is a rule written into the query:

SELECT * FROM tickets
WHERE id = $1
AND owner_email = $2 -- from the authenticated session, never the client

This works because the set of queries is fixed and known ahead of time. Each one can be reviewed and tested before release.

Agentic applications: the code path is written at runtime

An agent with a tool like execute_sql writes its own queries as it runs. The developer no longer writes the WHERE clause. The model does. It bases the query on the user’s request and on whatever content it reads along the way.

That content can include an attacker’s instructions, as the support ticket did. The UK National Cyber Security Centre says this problem may never be fixed the way SQL injection was. Models can’t reliably tell data from instructions, which makes them “inherently confusable.”

That matters for access control. A check that relies on the agent writing the right WHERE clause works only as often as the model behaves. A deterministic check, one that holds every time, has to sit where the agent can’t argue with it. The database is the natural place. It already has a proven way to control access to each row.

Two requirements for deterministic IDOR prevention

Moving the check into the database works only if the database knows whose request it is serving. That leads to two requirements.

Cryptographically guaranteed provenance. The user’s identity must come from a chain of signed tokens that no agent can forge or alter. It must then stay bound to the agent’s database connection.

Rules derived from policy. Row limits come from one central policy, checked against that identity. Nobody hand-writes them into each table or tool.

Carrying identity across agent hops

OAuth 2.0 Token Exchange, RFC 8693, covers the first half. When one agent calls another, it trades the token it received for a new one at the auth server. The server signs the new token and keeps the user as its subject. It also names the calling agent in an act (actor) claim. After two agent hops, the token that reaches the security gateway looks like this:


{
"iss": "https://auth.my.co",
"aud": "https://gateway.my.co",
"sub": "alice@my.co",
"act": {
"sub": "spiffe://my.co/agent/support-backend",
"act": { "sub": "spiffe://my.co/agent/support-frontend" }
},
"exp": 1793212800
}

RFC 8693 is clear about how to read this. For access control, only the subject and the outermost actor count. The nested actors belong in the audit trail and stay out of access decisions. So the policy checks two values: Alice, and the backend agent acting for her now. The RFC’s may_act claim can also limit which agents may act for a user at all.

This is the foundation of what we call blended identity: authorization based on both the human who initiated the request and the agent acting on their behalf. Neither identity alone tells the full story. A user may have permission to access a resource, but that doesn’t mean every agent acting for them should inherit that permission. Likewise, an agent may be authorized to perform an operation, but not for every user or every task.

Checking the earlier hops is still open work. The OAuth Actor Chain draft, which replaced draft-mw-spice-actor-chain, takes this on. In its stricter profiles, each agent signs a proof at every hop, and the auth server signs that it accepted it. A server could then check earlier actors directly. The draft also notes a limit. A signed chain doesn’t prove who is holding the token now. That takes a sender constraint such as DPoP. The draft is an individual proposal with no formal IETF standing yet.

The second half happens at the gateway. It checks the token’s signature, audience, and expiry. Then it turns the verified identity into a database role the agent can’t change. The next two sections show how.

A reference architecture

The design has five parts: the user, a frontend agent, a backend agent, a security gateway, and the database. The user’s identity moves forward at every hop. Only the gateway holds database rights.

Reference architecture · signed tokens to the gateway, a session role to the database

Reference architecture · signed tokens to the gateway, a session role to the database

  1. Alice signs in through her company’s identity provider. The frontend agent gets a token with Alice as its subject.
  2. The frontend agent hands work to the backend agent. First it trades Alice’s token under RFC 8693. The new token still names Alice and lists the frontend agent as actor.
  3. The backend agent trades the token again to call the gateway. It is now the current actor. This is the hop where identity often collapses into a shared service account. The MCP authorization spec forbids the shortcut of passing a token through unchanged.
  4. The gateway checks the chain and the policy. Then it creates a short-lived database role for the session. The agent gets a connection, never a database credential.

P0’s AI Gateway is one example of this pattern. On each session it issues a signed token that binds the user and the agent together. The agent never gets credentials for the system behind the gateway. For Postgres on RDS, the gateway signs in with a short-lived IAM token for the role the agent was granted.

Generating the database role from policy

When a session starts, the gateway turns one policy decision into one database role. Here is the policy, written in OPA‘s Rego. It limits both the agent and the user:


# METADATA
# scope: package
# compile:
#   unknowns:
#     - input.tickets
package tickets.filters

# input.actor is the outermost "act" claim of the verified token.
# Only the application's backend agent may act for users here.
app_agent if input.actor.sub == "spiffe://my.co/agent/support-backend"

# Users see the tickets they own
include if {
app_agent
input.tickets.owner_email == input.user.sub
}

When the backend agent opens a session for Alice, the gateway calls OPA’s compile API. It passes the verified token claims as known input. It marks the tickets rows as unknown. OPA then evaluates the policy as far as it can. The agent check resolves at once, because the actor is known. What remains is a condition on the rows. OPA turns it into SQL that matches owner_email = ‘alice@my.co’.

The gateway then sets up a role for the session:


-- One-time setup, by a role the agent never uses
ALTER TABLE tickets ENABLE ROW LEVEL SECURITY;
ALTER TABLE tickets FORCE ROW LEVEL SECURITY;

-- Per agent session, issued by the gateway
CREATE ROLE agent_s_7f3a LOGIN NOINHERIT;
GRANT SELECT ON tickets TO agent_s_7f3a;
CREATE POLICY agent_s_7f3a_tickets ON tickets
FOR SELECT TO agent_s_7f3a
USING (owner_email = 'alice@my.co');

-- At session end or expiry
DROP POLICY agent_s_7f3a_tickets ON tickets;
REVOKE ALL ON tickets FROM agent_s_7f3a;
DROP ROLE agent_s_7f3a;

Three things make this hold every time:

  • The identity is written into the policy. A common RLS pattern stores the user ID in a session variable and reads it with current_setting(). As EnterpriseDB has pointed out, that pattern breaks once a client can run any SQL it likes on the connection. An agent with a SQL tool can do exactly that. Here, the identity isn’t stored on the connection at all. The agent has nothing to change.
  • A different agent gets no role. Say some other agent presents Alice’s token. Then app_agent is false and every rule drops out. OPA reports that the query can never be true. The gateway denies the session, and the database is never touched. One policy makes both calls: which agent may act, and which rows it may see.
  • The agent holds a login and nothing else. Only the gateway can create policies. The Postgres docs note that superusers and BYPASSRLS roles always skip RLS. Table owners do too, unless the table forces it. The session role must be none of these. It also can’t belong to any role it could SET ROLE into.

At high session volumes, one policy per session adds churn to the database catalog. A variant uses one fixed policy. It looks up current_user in a mapping table that only the gateway can write. The guarantee stays the same.

Cedar can express the same policy, and it can also evaluate a policy part way. But it ships no SQL translator, so the gateway would need its own.

P0 now offers this per-user, policy-generated RLS as a preview feature.

Simpler alternatives, and where they fall short

Three other approaches come up whenever this design is discussed. Each has a place. None of them stops IDOR every time on its own.

Build an API layer. Wrap the database in endpoints with ownership checks. Then give the agent those endpoints instead of SQL. This is the traditional answer, and it works. But it means knowing every use case ahead of time. Users asking questions nobody planned for is the reason to build an agent in the first place.

Tool filtering. A gateway can allow or deny each tool call. AWS’s AgentCore Policy, for example, checks each tool call and its arguments against Cedar policies. The check runs outside the agent’s code. That control holds every time, and it’s worth having. For a tool like execute_sql, though, it can only decide whether this agent may run SQL. It can’t see which rows a query will return. P0’s own docs say checks at the protocol level may be too coarse for complex services. That is why P0 also enforces inside the target service.

Content filtering and DLP. Scanning what the agent returns can catch sensitive data on its way out. It’s a good addition, with three limits. It acts after the data has been read. Its false positives get in the way of the work the agent exists to do. And its verdicts are a matter of odds, which is what this design aims to remove.

Beyond Postgres: object grants and row access policies

Many agents also reach SaaS systems that use discretionary access, where owners grant access one object at a time. These systems have no row policy to generate. The same rule still holds. Grant the session identity the specific objects the task needs, not a broad integration account.

  • A common pattern runs the agent as an integration user that can see many records. A tighter pattern grants the session identity a narrow permission set for the task, then revokes it. P0’s Salesforce integration lets agents request permission sets this way.
  • Google Drive. Access is granted per file or folder. A session identity can get the folders that Alice’s request needs, instead of a service account that reads every shared drive. P0’s Google Drive integration works at that level.
  • Snowflake does support row-level security, through row access policies. These usually check CURRENT_ROLE(). The pattern from the last section carries over directly.

Object grants are coarser than rows. The guarantee has the same shape. The identity the system sees is scoped to one user’s task, and the agent can’t widen it.

What this doesn’t solve

RLS pinned to the user is needed to stop IDOR every time. Securing an agent takes more.

  • Leaking the user’s own data. Pinning stops the agent from reading Bob’s rows for Alice. But an agent under attack can still send Alice’s own rows to the attacker in its output. That calls for limits on outbound channels, one leg of the lethal trifecta.
  • Incorrect A policy error is enforced just as strictly as a correct rule. Policies need tests, like any other code.
  • Covert channels. The Postgres docs warn that foreign key checks can leak data across row-level limits. Schema design still matters.
  • Spliced delegation chains. A thread on the IETF OAuth list describes “delegation chain splicing.” A compromised middle hop mixes subject and actor tokens from different flows. The auth server then signs a chain that never happened. Three steps narrow this risk: keying policy on the outermost actor, using may_act, and the stricter actor chain profiles described above.

Questions to ask of your agent stack

Four questions show where an agentic app stands on IDOR:

  1. Which database role does each agent use? Can that role skip RLS?
  2. Can anything sent over the connection change the identity the policy reads?
  3. Where does the user’s identity stop traveling on its way to the database?
  4. If another agent got a user’s token, what could it read?

The larger lesson is that agentic applications can’t depend on agents to enforce their own access boundaries. The model can decide how to accomplish a task, but it shouldn’t decide what data it’s authorized to access.

That decision needs to come from verified identity, explicit policy and controls enforced independently of the agent, ideally in the system holding the data.

The more autonomy we give agents, the more important that separation becomes.