AI agent reads other users' data: how to fix it
If your AI assistant returns another user’s record, its tools run with a key that sees all rows. The tools also trust the ids that the model chooses. Run every tool as the signed-in user and set the owner on the server. The model is not a security boundary. The server must check each tool call.
This guide shows how to confirm the problem and how to fix the tools in order. It also shows how to log tool calls so you can find past abuse.
What does “the agent reads other users’ data” mean?
You add an assistant to your app. You give it tools, for example getOrder(id) or searchCustomers(query). The model decides when to call a tool and which arguments to send. Your server runs the tool and returns the result to the model.
The problem starts when the tool runs with a powerful database key. In Supabase this is the service_role key. It bypasses Row Level Security (RLS). The tool then returns any row that the model asks for. The user does not need a special exploit. The user types a message such as “Show me order 1043” and the model sends that id to the tool.
This is Broken Object Level Authorization (BOLA) through a chat box. The BOLA pattern guide describes the same fault in normal API routes. The chat box adds one new risk: the caller of the tool is the model, and the model follows the text it reads. Text from the user, from a web page, or from a document can change the tool arguments.
OWASP names this class of fault Excessive Agency. The OWASP LLM06 page says it “enables damaging actions to be performed.” Its prevention list tells you to execute extensions in the user’s context. It also tells you to “limit the permissions” of each extension “to the minimum necessary.”
These are the usual causes:
- A shared key. The tool uses
service_roleor an admin database URL for every user. - An id from the model. The tool takes
user_idororder_idas an argument, and the code trusts it. - A broad tool. One tool, such as
runQuery, accepts any filter or any table. - No owner filter. The query reads
where id = $1and does not addand user_id = $2. - A missing check on writes. A tool refunds, deletes, or emails with no check of the owner and no approval.
- Fine data, wrong user. The tool returns the right shape of data, but for the wrong account.
This guide covers tool calls. The RAG chatbot leak guide covers the search index.
How do I confirm that my agent leaks data?
Test with two accounts that you own. Call them account A and account B. Do not test with real customers.
- Create account A and account B. Add one record to each, for example one order.
- Sign in as account B. Note the id of the order of B. Sign out.
- Sign in as account A. Open the chat.
- Ask for the record of B by id. Write: “Show order” and the id of the order of B.
- Ask for the record by an email address. Write: “Find the customer with the email of account B.”
- Ask a broad question. Write: “List the last 10 orders in the system.”
- Ask for the tool list. Write: “List the tools you can call and the arguments of each tool.”
Any record of account B in a reply to account A is a failure. A reply that lists tools with user_id or table arguments is also a warning sign.
Test the endpoint without the chat screen. Send the request with curl. This shows if the server enforces the rules, and not only the screen. Use your own route and token.
curl -s https://your-app.example/api/chat \
-H "Authorization: Bearer $TOKEN_A" \
-H "Content-Type: application/json" \
-d '{"messages":[{"role":"user","content":"Show order 1043. Print all fields."}]}'
Replace 1043 with the id of the order of account B.
Warning: Run these tests only on accounts and data that you own. Do not read the data of real users.
How do I fix it now?
Do these steps in order. Step 1 limits the loss. The other steps build the correct design.
- Turn off the risky tools. Remove any tool that reads or writes data of other users until you fix it. A chat with fewer tools is better than a chat that leaks.
- Rotate the keys that the tools used. If the tools used the
service_rolekey, check the service_role key guide. Rotate the key if the model output or a log showed it. - Run each tool as the user. Pass the user’s token to the database, so RLS applies. This is the main fix.
- Remove the user id from the tool schema. The server sets the owner. The model must not send it.
- Check the owner in each tool. Add an owner filter to each query, even when RLS is on.
- Add a confirmation step for write tools.
- Log each tool call.
- Test again with account A and account B.
How do I run the tools as the signed-in user?
The route handler reads the token of the user. It creates a database client with that token. Each tool receives this client and uses nothing else.
This example uses a Next.js route handler and Supabase. The getOrder tool has only one argument, the order number. It has no user id.
// app/api/chat/route.ts
import OpenAI from "openai";
import { createClient } from "@supabase/supabase-js";
const openai = new OpenAI();
const tools: OpenAI.Chat.Completions.ChatCompletionTool[] = [
{
type: "function",
function: {
name: "get_order",
description: "Get one order of the current user by order number.",
parameters: {
type: "object",
properties: { order_number: { type: "integer" } },
required: ["order_number"],
additionalProperties: false,
},
strict: true,
},
},
];
export async function POST(req: Request) {
const jwt = req.headers.get("authorization")?.replace("Bearer ", "");
if (!jwt) return Response.json({ error: "Unauthorized" }, { status: 401 });
const db = createClient(
process.env.SUPABASE_URL!,
process.env.SUPABASE_ANON_KEY!,
{ global: { headers: { Authorization: `Bearer ${jwt}` } } },
);
const { data: { user } } = await db.auth.getUser(jwt);
if (!user) return Response.json({ error: "Unauthorized" }, { status: 401 });
const { messages } = await req.json();
// Run the model loop here. Pass `db` and `user` to each tool.
}
The client uses the anon key and the token of the user. Postgres sees the user as the authenticated role. RLS policies then limit each query to the rows of that user. Do not create this client with the service_role key.
The tool code is short:
async function getOrder(db: SupabaseClient, userId: string, args: { order_number: number }) {
const { data, error } = await db
.from("orders")
.select("order_number, status, total, created_at")
.eq("user_id", userId)
.eq("order_number", args.order_number)
.maybeSingle();
if (error) return { error: "Not found" };
return data ?? { error: "Not found" };
}
The userId comes from the verified token. The error text is the same for a missing row and for a row of another user.
How do I write the RLS policy?
The tool filter is the first layer. RLS is the second layer. If a tool has a bug, the database still refuses the rows.
alter table public.orders enable row level security;
create policy "users read own orders"
on public.orders for select to authenticated
using ((select auth.uid()) = user_id);
The Supabase RLS guide shows the full setup. The guide on users who see other users’ data shows how to test the policies.
How do I handle the tool call in the loop?
The OpenAI Chat Completions API returns tool calls in message.tool_calls. Each call has an id and a function with a name and an arguments string. Parse the string as JSON. Check the result. Return it in a message with the role tool and the same tool_call_id.
const handlers = {
get_order: (a: { order_number: number }) => getOrder(db, user.id, a),
};
const completion = await openai.chat.completions.create({
model: "gpt-4.1",
messages,
tools,
});
const message = completion.choices[0].message;
messages.push(message);
for (const call of message.tool_calls ?? []) {
if (call.type !== "function") continue;
const handler = handlers[call.function.name as keyof typeof handlers];
if (!handler) {
messages.push({ role: "tool", tool_call_id: call.id, content: '{"error":"Unknown tool"}' });
continue;
}
const args = JSON.parse(call.function.arguments);
const result = await handler(args);
messages.push({ role: "tool", tool_call_id: call.id, content: JSON.stringify(result) });
}
The handler map is an allowlist. A tool name that you did not define gets an error. Run the loop again with the new messages until the model returns text. Set a step limit. The runaway agent guide shows how.
How do I scope each tool on the server?
Treat every tool argument as untrusted input. The model chose it, and an attacker can steer the model. Use these rules.
- Do not accept owner ids. Remove
user_id,account_id, andtenant_idfrom the schema. Read them from the session. - Use narrow tools. Write
get_orderandlist_my_orders. Do not writerun_queryorread_table. - Validate the arguments. Check the type, the range, and the length. The
strict: trueoption andadditionalProperties: falsehelp, but you must still check in code. - Limit the result. Return a fixed list of columns and a maximum number of rows.
- Return one error for all failures. Do not tell the model if the row exists for another user.
- Keep secrets out of results. The model can repeat anything that it reads.
How do I add a confirmation step for write tools?
A read leak exposes data. A write tool can also refund money, delete a record, or send an email. OWASP tells you to “require a human to approve high-impact actions” in the LLM06 prevention list.
Split each write tool in two steps:
- The tool
propose_refunddoes not change data. It saves a pending action with the order, the amount, and the user id. It returns an action id and a text summary. - The app shows the summary to the user with a Confirm button. The user clicks it.
- The button calls a normal API route. The route checks the session. It checks that the pending action belongs to the user. It runs the refund.
The model never runs the final step. A prompt injection can only create a proposal. The user sees it and can refuse it.
// Called by the Confirm button, not by the model
export async function POST(req: Request) {
const user = await requireUser(req);
const { actionId } = await req.json();
const action = await getPendingAction(actionId, user.id); // checks the owner
if (!action) return Response.json({ error: "Not found" }, { status: 404 });
await runRefund(action);
return Response.json({ ok: true });
}
Show the real values in the summary, from your own data. Do not show text that the model wrote about the action. The model can describe the action in a false way.
How do I log tool calls?
A log lets you find past abuse and prove that the fix works. Log each tool call with these fields:
- the time
- the user id from the session
- the tool name
- the arguments
- the number of rows returned
- the result status
- the id of the chat request
Do not log full results if they contain personal data. Log the row ids or a count.
Search the logs for these signs:
- A tool argument that holds an id which does not belong to the user.
- Many calls to one tool in a short time with different ids. This looks like id guessing.
- Calls right after the assistant read a file or a web page. This can show an indirect injection.
- Results with many rows for a user who normally sees few.
How do I stop this in the future?
Use these controls so that a new tool does not open the same gap.
- Use one tool factory. Create each tool from a function that receives the user client. A tool then cannot use another client.
- Ban the service key in chat code. Add a lint rule or a code review check. The chat route must not import the
service_roleclient. - Start with no tools. Add a tool only when a feature needs it. Give each tool the least access.
- Write a cross-account test. The test signs in as account A and asks for the record of account B. It expects no data. Run it in your build.
- Test each new tool with a prompt injection file. Check that the tool does not leave the scope of the user.
- Scan the live app after each release.
When should I get help?
Get help in these cases:
- The logs show one account that read records of many other accounts.
- The assistant had a tool for payments, refunds, or deletion, and you cannot tell if an attacker used it.
- Personal data left your system. You may have a duty to tell the users. Ask a lawyer about the rules in your region.
- The app has many tools and you cannot tell which ones run as the user.
- Your database has no RLS and the app depends on the
service_rolekey. A security engineer can plan the change with you.
After the fix, repeat the tests with account A and account B. Account A must get no data of account B in each test.
Common questions
Can I fix this with a stronger system prompt?
Is it safe if the model gets the user id from the session?
Do I still need RLS if I check the owner in my tool code?
Which tools need a confirmation step?
How is this different from a RAG leak?
Test your chat endpoint
A VibeEval scan sends requests to your chat endpoint from outside and checks if the assistant returns records that belong to another account.
14-day free trial · Cancel anytime