Broken Access Control (IDOR): The #1 Silent Bug in AI-Generated SaaS Apps
How an innocent /api/project/[id] endpoint allows strangers to inspect, tamper with, and delete other users' work.
Table of Contents6 sections
In OWASP's Top 10 Web Application Security Risks, Broken Access Control consistently holds the #1 position. In AI-generated applications, it is not just common—it is nearly universal.
AI models like GPT-4, Claude 3.5 Sonnet, and specialized coding agents are trained to fulfill user requests efficiently. When you prompt: "Create an endpoint to download an invoice by ID", the model translates that prompt directly to:
SELECT * FROM invoices WHERE id = :id.
Notice what is missing? The user check.
The AI assumes that because the route has authentication, or because the frontend only presents links to the logged-in user's own invoices, the system is secure. This oversight is known as an Insecure Direct Object Reference (IDOR) or Broken Object-Level Authorization (BOLA).
The Anatomy of a Typical AI IDOR Bug
Imagine an invoice generation app built with Next.js and Drizzle ORM. The AI generates this route:
// app/api/invoices/[id]/route.ts
import { NextResponse } from "next/server";
import { db } from "@/server/db";
import { invoices } from "@/drizzle/schema";
import { eq } from "drizzle-orm";
import { auth } from "@/server/auth";
import { headers } from "next/headers";
export async function GET(
req: Request,
{ params }: { params: { id: string } }
) {
// The AI included authentication! Great, right?
const session = await auth.api.getSession({ headers: await headers() });
if (!session?.user) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
// FATAL BUG: It queries by ID alone.
const [invoice] = await db
.select()
.from(invoices)
.where(eq(invoices.id, params.id));
if (!invoice) {
return NextResponse.json({ error: "Not found" }, { status: 404 });
}
return NextResponse.json({ invoice });
}At first glance, this route appears secure. It checks if the visitor is logged in. It queries the database and handles 404 errors.
Here is the exploit:
- Alice signs up and creates Invoice
inv_101. - Bob signs up and creates Invoice
inv_102. - Bob is an authenticated user. He changes the URL in his browser from
/invoices/inv_102to/invoices/inv_101. - Because Bob has a valid
session.user, the authentication check passes. - The database returns Alice's invoice, containing her full name, billing address, line items, and financial transaction totals.
In less than 3 seconds, Bob has breached Alice's private financial data.
How to Test for IDOR in Your App
You don't need automated penetration testing suites to check for IDOR. Follow this manual verification process:
- Open Chrome and log in with Account 1.
- Open Chrome Incognito (or Firefox) and log in with Account 2.
- In Account 1, create a document, project, or invoice. Note its ID (from the URL or Network tab, e.g.
uuid-12345). - In Account 2, attempt three actions:
- Read: Navigate to
/api/documents/uuid-12345or view the page. - Update: Send a
PATCHorPOSTwith Account 2's session modifyinguuid-12345. - Delete: Send a
DELETErequest targetinguuid-12345.
If Account 2 can view, modify, or delete Account 1's record, you have a verified IDOR vulnerability.
Remediation: Tenant-Aware Queries
The fix is straightforward, but it must be applied consistently across every single read and mutation.
Never query an entity by its primary key alone when that entity belongs to a user or organization. Always include the tenant boundary in the WHERE clause:
// SECURE IMPLEMENTATION
export async function GET(
req: Request,
{ params }: { params: { id: string } }
) {
const session = await auth.api.getSession({ headers: await headers() });
if (!session?.user) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
// Query must verify BOTH the object ID AND the owner's user ID!
const [invoice] = await db
.select()
.from(invoices)
.where(
and(
eq(invoices.id, params.id),
eq(invoices.userId, session.user.id) // Enforce ownership!
)
);
if (!invoice) {
// Return a 404 rather than 403 to prevent enumeration attacks
return NextResponse.json({ error: "Invoice not found" }, { status: 404 });
}
return NextResponse.json({ invoice });
}What About Multi-Tenant Organizations?
If your app has teams or organizations (where multiple users share access to a project), checking eq(invoices.userId, session.user.id) is insufficient. You must verify membership:
// Multi-tenant organization verification
async function verifyOrgMembership(orgId: string, userId: string) {
const [membership] = await db
.select()
.from(organizationMembers)
.where(
and(
eq(organizationMembers.orgId, orgId),
eq(organizationMembers.userId, userId)
)
);
return Boolean(membership);
}Before executing any query on an organization's resource, verify that the current user belongs to that organization.
Route-by-Route IDOR Checklist
Before shipping your MVP, audit every API route and Server Action:
- ☐Does every
GETendpoint query withWHERE id = :id AND user_id = :userId? - ☐Does every
PATCH/PUTendpoint verify ownership before writing updates? - ☐Does every
DELETEendpoint enforce ownership? - ☐If using UUIDs, are they generated using cryptographically secure v4 or v7 UUIDs rather than sequential integers (
1, 2, 3)? - ☐Are 404 responses returned when an unauthorized user probes an existing ID, preventing resource enumeration?
Automated IDOR Auditing with vibesupport
Auditing dozens of routes manually is time-consuming and error-prone. vibesupport specializes in automated endpoint analysis and senior developer code audits to guarantee your data stays completely isolated.
Don't let a hidden bug derail your launch.
vibesupport scans your AI-built codebase or live website for exposed secrets, missing database RLS, and broken access controls—then pairs you with senior engineers to fix them directly in GitHub.