Auth and RBAC Are in Place, So Why Can Users Read Each Other's Data?
Why RBAC passes and users still read each other's records: OWASP API1 BOLA, four ways to close the gap, a recommended default, and a two-user test for CI.
An API can verify every JWT, run a role check on every route, and still let user B open user A’s invoice by editing one path segment. The reason is structural: RBAC answers whether a role may perform invoice:read on the resource type Invoice, and it never answers whether this caller may read invoice 8812. That second question is object-level authorization, and its absence is the top entry in the OWASP API Security Top 10 (API1:2023, Broken Object Level Authorization, or BOLA, which most people still call IDOR). For a team with RBAC already shipped and a relational database underneath, the default I recommend is scoped queries behind a repository that requires a Principal, plus a test generated from the route table in CI that sends each request as a user from another tenant and, for data owned by one person, as a colleague from the same tenant. On shared-schema multi-tenant PostgreSQL, add Row-Level Security as the database backstop. The usual hardening steps leave that gap open; four methods close it, and a route-driven test keeps it closed.
Three Questions, Three OWASP Categories#
RBAC, JWT scopes and gateway route policies all answer the same question: may this caller use this function? OWASP files failures of that question under API5:2023, Broken Function Level Authorization (BFLA). The question that leaks data is a different one, and the property-level question is a third.
| Question | OWASP category | Typical control | Example failure |
|---|---|---|---|
| May this caller use this function? | API5:2023 BFLA | RBAC, JWT scopes, route guards | A viewer calls DELETE /admin/users/:id |
| May this caller touch this object? | API1:2023 BOLA | Ownership, tenant or relationship check | User B calls GET /invoices/8812, which belongs to A |
| May this caller read or write this field? | API3:2023 BOPLA | Output DTOs, write allowlists | A user sets role: "admin" through PATCH |
OWASP rates BOLA as easy to exploit, widespread, and easy to detect. The attacker changes an object ID in the path, the query string, a header or the body, and the API returns someone else’s record. Because the role check sits in the BFLA row, it passes every time: user B does hold invoice:read. The check that would have failed was never written.
The vulnerable shape is short enough to recognise on sight. This is a Hono handler with Prisma, but the same pattern appears in Express, Next.js route handlers and GraphQL resolvers:
// RBAC passes: user B has the "accountant" role, which grants invoice:read.
app.get('/invoices/:id', requirePermission('invoice:read'), async (c) => {
const invoice = await prisma.invoice.findUnique({
where: { id: c.req.param('id') }, // any invoice in the database
});
if (!invoice) return c.json({ error: 'Not found' }, 404);
return c.json(invoice);
});
The request passes three gates and reaches the database with no predicate that mentions the caller.
In a project I worked on, the list endpoint was scoped correctly: GET /invoices filtered by the caller’s tenant from the first commit. The detail endpoint and the PDF export were added later by different people, and both looked up the invoice by ID alone. The RBAC middleware was on all three routes. Nothing in code review flagged the difference, because the role check was visibly present and the missing predicate was invisible.
The Same Gap in Public Incidents#
The pattern is common enough that the incident write-ups read like variations on one template. The OWASP Top 10:2025 entry for broken access control (opens in new tab) states: “100% of the applications tested were found to have some form of broken access control.” That figure covers the whole category, not BOLA alone, and CWE-639 (authorization bypass through user-controlled key) is among the weaknesses it maps.
| Case | What was in place | What was missing |
|---|---|---|
| USPS Informed Visibility (2018) | A login was required to use the API | Any authenticated user could query other users’ account details; roughly 60 million accounts were exposed, per KrebsOnSecurity |
| Peloton (2021) | After the first fix, endpoints required authentication | Pen Test Partners documented that the same data stayed readable by every logged-in account, and anyone could self-register, until a later fix |
| First American Financial (2019) | Document links carried a valid record identifier | Changing the identifier in a link returned other customers’ documents; hundreds of millions of files were reachable |
| Optus (2022) | Access control was fixed on the main portal | The regulator alleges the same coding error was not fixed on the API domain, and the attack was a simple process of trial and error |
Two of these cases carry the lesson. Peloton shows that adding authentication to an endpoint does not close BOLA, since every attacker can become an authenticated user. Optus (as alleged by ACMA in its court filing, not yet a finding) shows the failure mode of per-endpoint checks: the fix landed on one code path and was missed on its twin.
Hardening Steps That Leave the Gap Open#
Several common responses reduce the ease of exploitation without adding the missing check. They are worth doing, but none of them is the control.
| Measure | What it does | Why BOLA stays open |
|---|---|---|
| UUIDs instead of sequential IDs | Slows enumeration | IDs leak through URLs, logs, shared links, referrers and other API responses. The OWASP IDOR cheat sheet frames complex identifiers as defence in depth and says access checks remain essential |
| Hiding the button in the UI | Improves UX | The API is still callable directly |
| API gateway authentication and route policies | Function-level: route plus token | The gateway does not know who owns invoice 8812 unless it calls the domain, at which point it is a handler check in a different place |
JWT scopes or claims (invoice:read) | Function-level (the BFLA row) | A scope says nothing about which invoice |
if (params.userId === jwt.sub) | Protects /users/:userId/* routes | OWASP’s API1 page says this covers only a small subset of cases and is not a sufficient solution |
| Rate limiting | Slows mass extraction | One leaked record is still a leak |
The OWASP API1 page does list random, unpredictable identifiers among its prevention items, next to the authorization check that every record lookup needs. Treat them as hardening that complements the object check.
Scoped Queries Behind a Repository#
The principle is to fetch within the caller’s scope, never to fetch and then check. The OWASP IDOR cheat sheet contrasts an insecure lookup that searches all projects with a scoped lookup that searches only the current user’s projects, and recommends method shapes like findByIdAndOwnerId(id, currentUserId). In SQL the scope is part of the lookup key on both reads and writes:
-- Read: tenant is part of the key, not a post-check
SELECT id, amount, status
FROM invoices
WHERE id = $1 AND tenant_id = $2;
-- Write: the scope is repeated in the write itself
UPDATE invoices SET status = $3
WHERE id = $1 AND tenant_id = $2
RETURNING id; -- zero rows => 404
The repository is what turns this from a convention into the only available path. There is no findById(id) without a Principal, and the Principal is built once from the verified token:
import type { PrismaClient } from '@prisma/client';
interface Principal {
tenantId: string;
userId: string;
roles: readonly string[];
}
// The repository accepts the client or a transaction client.
type Db = Pick<PrismaClient, 'invoice' | 'expenseReport'>;
// Same error for "does not exist" and "exists but is not yours".
export class NotFoundError extends Error {}
// The only way handlers reach invoice data.
export function invoiceRepo(db: Db, p: Principal) {
const scope = { tenantId: p.tenantId };
return {
findById: (id: string) => db.invoice.findFirst({ where: { id, ...scope } }),
list: (opts: { take: number; cursor?: string }) =>
db.invoice.findMany({
where: scope,
take: opts.take,
...(opts.cursor ? { skip: 1, cursor: { id: opts.cursor } } : {}),
orderBy: { id: 'asc' },
}),
async markPaid(id: string) {
const { count } = await db.invoice.updateMany({
where: { id, ...scope },
data: { status: 'PAID' },
});
if (count === 0) throw new NotFoundError();
},
};
}
// Tenant context comes from the verified token, never from a client header.
app.use('*', async (c, next) => {
const claims = c.get('jwtPayload');
c.set('principal', { tenantId: claims.tenant_id, userId: claims.sub, roles: claims.roles ?? [] });
await next();
});
Two details carry most of the value. First, tenant_id is a mandatory predicate, and the tenant comes from the token or session. A client-supplied X-Tenant-Id header is acceptable only if it is validated against the user’s memberships before it reaches the repository. Second, the write path uses updateMany with the scope and checks the count, so there is no window between a check and an unscoped update({ where: { id } }). Centralizing Authorization with a Service Layer explains the generic not-found error that this repository relies on, so I will not repeat the enumeration argument here.
Tenant isolation is only the outer scope. Invoices in this example are shared inside a tenant: every accountant of tenant A may read every invoice of tenant A. Plenty of data is private to one person inside the tenant: an expense report, a payslip, a direct message. A tenant predicate alone lets a colleague open those by ID, and that same-tenant variant is the one a cross-tenant test never sees. Owned data gets an ownership predicate on top of the tenant, and a role may widen it back to the tenant:
// Expense reports are private to their owner inside the tenant.
// An approver role widens the scope to the whole tenant; the tenant predicate always stays.
export function expenseReportRepo(db: Db, p: Principal) {
const scope = p.roles.includes('expense-approver')
? { tenantId: p.tenantId }
: { tenantId: p.tenantId, ownerId: p.userId };
return {
findById: (id: string) => db.expenseReport.findFirst({ where: { id, ...scope } }),
list: (opts: { take: number }) =>
db.expenseReport.findMany({ where: scope, take: opts.take, orderBy: { id: 'asc' } }),
};
}
This is where RBAC and object-level checks meet. The role decides how wide the scope is, and the scope still goes into the query.
The repository needs a fence, because the raw client is still importable. An ESLint no-restricted-imports rule that forbids the Prisma client import outside src/repositories/ is enough to make the unscoped path show up in review as a lint failure. The snippets here target Prisma ORM 7 with the legacy @prisma/client package; with the prisma-client generator, the rule has to name the generated output path instead.
Background jobs and admin tools then need an explicit system principal. Without rules it becomes the new back door, so three rules come with it. A system principal still names one tenant; a job that spans tenants loops over them and builds one principal per tenant. It carries only the roles the job declares, so the repository applies the same scope logic as for a user. Creating one writes an audit record with the job, the reason and, for admin tools, the person who asked:
interface AuditLog {
write(entry: Record<string, string>): Promise<void>;
}
// Only src/jobs/ and src/admin/ may import this; the same no-restricted-imports rule fences it.
export async function systemPrincipal(
audit: AuditLog,
input: { tenantId: string; job: string; reason: string; roles: string[]; requestedBy?: string },
): Promise<Principal> {
await audit.write({
type: 'system-principal',
tenantId: input.tenantId,
job: input.job,
reason: input.reason,
roles: input.roles.join(','),
requestedBy: input.requestedBy ?? input.job,
});
return { tenantId: input.tenantId, userId: `system:${input.job}`, roles: input.roles };
}
A job that needs all tenants in one query, such as a billing export, gets its own reviewed repository method. The Principal type offers no way to leave the tenant blank.
The trade-off is that this method models ownership and tenancy well and nothing else. Sharing, teams and delegated access need either richer scope objects or a relationship engine feeding the same repository, which is where the override section below comes in. Attribute-Based Access Control in TypeScript covers the can() and policy-builder approach for attribute-based rules, so I link rather than rebuild it. What the repository buys in return is that the scoped lookup is the only shape it offers, so no handler has to remember it. A lint rule is a review signal, not a compiler boundary. Raw SQL, a second client and separate services still bypass it, which is the gap the next section covers.
Row-Level Security as the Database Backstop#
The repository covers code that goes through it. Raw SQL in a migration script, a reporting job, or a new service written by someone who never saw the lint rule does not. For shared-schema multi-tenant SaaS on PostgreSQL, Row-Level Security makes the database refuse rows outside the tenant even when the application forgets the predicate. Multi-Tenant Authorization in TypeScript describes RLS as a safety net for row-level isolation in one sentence; what follows is the operational detail behind that sentence.
-- The app connects as a role that does not own the tables and cannot bypass RLS.
CREATE ROLE app_user LOGIN NOBYPASSRLS;
GRANT SELECT, INSERT, UPDATE, DELETE ON invoices TO app_user;
ALTER TABLE invoices ENABLE ROW LEVEL SECURITY;
ALTER TABLE invoices FORCE ROW LEVEL SECURITY; -- the table owner is subject to policies too
CREATE POLICY tenant_isolation ON invoices
USING (tenant_id = nullif(current_setting('app.tenant_id', true), '')::uuid)
WITH CHECK (tenant_id = nullif(current_setting('app.tenant_id', true), '')::uuid);
The application sets the tenant per transaction, so the setting dies at COMMIT or ROLLBACK and cannot bleed into the next request on a pooled connection:
import type { Pool, PoolClient } from 'pg';
export async function withTenant<T>(
pool: Pool,
tenantId: string,
fn: (client: PoolClient) => Promise<T>,
): Promise<T> {
const client = await pool.connect();
try {
await client.query('BEGIN');
// is_local = true: the value applies only to the current transaction
await client.query(`SELECT set_config('app.tenant_id', $1, true)`, [tenantId]);
const result = await fn(client);
await client.query('COMMIT');
return result;
} catch (err) {
await client.query('ROLLBACK');
throw err;
} finally {
client.release();
}
}
Four facts from the PostgreSQL documentation explain most of the surprises people hit with this setup. When RLS is enabled on a table with no policy, a default-deny policy applies and no rows are visible. Superusers and roles with BYPASSRLS always bypass row security. Table owners bypass it too unless FORCE ROW LEVEL SECURITY is set. That is the usual reason RLS appears to work in psql but not in the application, or the other way round: migrations tend to run as the owner. Finally, referential-integrity checks (primary key, unique, foreign key) always bypass row security, and the docs warn that this can leak existence through a covert channel.
The pooling caveat matters in practice. PgBouncer’s feature matrix marks session-level SET and RESET as unsupported in transaction pooling mode, so a SET app.tenant_id outside a transaction can land on whichever connection the pooler hands out next. A transaction-local set_config(..., true) inside a transaction is the safe form, and it is also the form Prisma’s official RLS client-extension example uses. That example is explicitly demo-only. Its README notes that the extension wraps every query in its own batch transaction, so explicit $transaction() calls may not work as intended. The pg helper above avoids the extension. When Prisma is the data layer, the same shape is one interactive transaction per request, and the repository runs on the transaction client:
import type { PrismaClient } from '@prisma/client';
// One interactive transaction per request: set_config and every query share one connection.
export function withTenantPrisma<T>(
prisma: PrismaClient,
tenantId: string,
fn: (tx: Db) => Promise<T>,
): Promise<T> {
return prisma.$transaction(async (tx) => {
await tx.$executeRaw`SELECT set_config('app.tenant_id', ${tenantId}, true)`;
return fn(tx);
});
}
// In a handler: invoiceRepo(tx, principal) instead of invoiceRepo(prisma, principal).
This is why the repository takes Db, a Pick of the client: the plain client and the transaction client both fit. Three limits come with the wrapper. Every query inside the callback has to go through tx; a call on the outer client takes another pooled connection, where app.tenant_id is unset and the policy returns no rows. Prisma ORM 7 caps an interactive transaction at 5 seconds by default (timeout) and waits up to 2 seconds to acquire one (maxWait), so a slow export or a handler that calls an external service needs explicit options or a separate path. And the transaction holds a connection for the whole request; the Prisma docs warn that long-open transactions hurt database performance and can cause deadlocks.
The nullif(..., '') wrapper handles an undocumented quirk. A thread on the pgsql-general list reports that after a custom setting has been assigned with SET LOCAL, current_setting returns an empty string once the transaction ends. Running the same sequence on PostgreSQL 17.10 shows the same empty string after both commit and rollback. Tom Lane’s explanation on the list is that a custom setting exists once it has been assigned, and its reset value is the empty string. He also calls the pattern an unsupported use of a mechanism meant for extension settings, so treat the nullif as required rather than optional. A bare ''::uuid cast then raises an error, which fails closed but is noisy. With nullif, the comparison against NULL simply matches no rows, which is the quieter fail-closed outcome.
On performance, Supabase’s RLS guidance applies to any PostgreSQL deployment. Index the columns the policy filters on. Wrap function calls inside the policy in (select ...) so they evaluate once per statement instead of once per row; this is only valid when the function’s result does not depend on the row. Keep joins out of policies; use IN or ANY over a precomputed set. The right number for your workload comes from EXPLAIN ANALYZE before and after enabling the policy, measured on your own tables.
The policy above isolates tenants and nothing finer. Ownership inside a tenant, such as the expense-report scope, stays a repository job unless the application also sets a user ID per transaction and the policy compares against it.
The trade-off is operational: two database roles (a migration role that owns tables and a runtime role that does not), a transaction per request, and debugging sessions where “no rows” is indistinguishable from “no data”. Everything above assumes an application server in front of the database, where RLS is the backstop and the repository is the primary control. In architectures where clients query the database directly, as with Supabase, RLS is the primary boundary and the policies carry the full authorization model.
This setup guards against a forgotten predicate. It does not guard against SQL injection. Any role can call set_config, so injected SQL that runs on the same connection can switch app.tenant_id to another tenant. Parameterised queries stay a separate, required control.
Choosing Between the Four Methods#
The four methods differ most on one axis: what happens when someone forgets.
| Method | Survives a forgotten endpoint? | List endpoints | Sharing and relationships | Operational cost | Main failure mode |
|---|---|---|---|---|---|
| Handler check (fetch, then compare) | No | Manual per list | Ad hoc | None | Opt-in; one missed route leaks |
| Scoped repository | Yes, if the raw client is fenced off | Native (WHERE scope) | Limited to ownership and tenancy | Low | Escape hatches: raw SQL, admin scripts, jobs |
| PostgreSQL RLS | Yes, at the database | Native | Awkward beyond tenant or owner columns | Medium (roles, pooling, migrations) | Owner or superuser bypass; session SET with pooling; slow policies |
| ReBAC or policy engine | Only where it is wired in | Needs a list-objects or filter API | Strong | High (extra service, data sync) | Dual-write drift; per-item check loops |
When to Override the Default#
Handler-level checks are the method most teams already have in a few places, and they work for the endpoint you remembered:
app.get('/invoices/:id', requirePermission('invoice:read'), async (c) => {
const { tenantId } = c.get('principal');
const invoice = await prisma.invoice.findUnique({ where: { id: c.req.param('id') } });
if (!invoice || invoice.tenantId !== tenantId) {
return c.json({ error: 'Not found' }, 404);
}
return c.json(invoice);
});
They are acceptable as a transitional step while routes migrate to the repository, and for nothing longer. Every new route (export, bulk, duplicate, webhook replay, GraphQL resolver) needs a person to remember the comparison, and the check on read says nothing about the write that follows in a different function. OWASP’s own third example scenario for API1 is a GraphQL delete mutation that takes a document ID with no permission check, which is the same shape.
The strongest version of the handler approach is a central policy point that every route must call. A route builder refuses to register a route without an authorize(resource) hook, so a forgotten check fails at startup. That solves the forgetting problem for HTTP routes, and it is a sound design. Three gaps remain, and they are why the repository stays the default. First, the hook decides before the handler fetches. It either loads the object itself, which is fetch-then-check plus a second query that can drift from the handler’s, or it decides from the ID alone, which works only when an engine holds the relationship. Second, a list endpoint still needs a filter inside its query, and a yes-or-no answer for the route does not produce one. Third, the hook guards routes, while jobs, nested GraphQL resolvers and a write in another function reach data by other paths. The repository puts the predicate into the query that returns the data, so check and fetch are one statement on every path that reads or writes. The two combine well: the route hook owns function-level decisions and engine calls, and the repository owns the data scope.
Relationship and policy engines are the override for a different problem: access that comes from sharing, nested folders or org-to-team-to-project inheritance instead of a single owner_id or tenant_id column. The SpiceDB and Auth0 FGA comparison and the Cedar, Rego and OpenFGA comparison on this site cover the engines, the dual-write problem and the policy languages in depth. The angle those posts leave open is list endpoints. A per-object check() protects GET /invoices/:id; GET /invoices needs a filter, and a loop of checks over every candidate row is the wrong tool. OpenFGA’s ListObjects returns the objects of a type that a user has a relationship with, and its documentation warns that performance varies drastically with model complexity and tuple count. OPA’s partial evaluation turns a policy into residual conditions that can become a data filter. Either way, the ID set or the filter still has to be pushed into the query, so in practice the engine feeds the repository. The detail lookup checks first and fetches second. That is the one place the fetch-within-scope rule bends: the relationship lives in the engine, so the engine answers first, and the fetch still carries the tenant scope. The list method pushes the engine’s answer into the query:
findById: async (id: string) => {
const { allowed } = await fga.check({
user: `user:${p.userId}`,
relation: 'viewer',
object: `invoice:${id}`,
});
return allowed ? db.invoice.findFirst({ where: { id, ...scope } }) : null;
},
list: async (opts: { take: number }) => {
const { objects } = await fga.listObjects({
user: `user:${p.userId}`,
relation: 'viewer',
type: 'invoice',
});
const allowedIds = objects.map((o) => o.slice('invoice:'.length));
return db.invoice.findMany({
where: { id: { in: allowedIds }, ...scope },
take: opts.take,
orderBy: { id: 'asc' },
});
},
This shape has a size limit. OpenFGA’s search-with-permissions guide recommends building the ID list first only when a user can see a small number of objects, around 1,000. The server returns at most listObjectsMaxResults objects (default 1,000) found within listObjectsDeadline (default 3 seconds), so a user who can see more gets a partial list. Past that size, the same guide describes a local index built from OpenFGA’s changes endpoint, intersected with the database search.
The trade-off is a network hop per check, a second source of truth for relationships, and consistency questions that the SpiceDB post already works through. Reach for an engine when the relationship graph is the product, and keep the repository chokepoint either way.
The Two-User Test in CI#
OWASP’s prevention list for API1 ends with an instruction to write tests that evaluate the authorization mechanism and to refuse deployments that make them fail. The IDOR cheat sheet adds the method: create multiple accounts with different authorization scopes, and verify that access is denied regardless of whether the identifier is predictable. The harness below does both. It seeds tenant A with an owner (A1) and a colleague (A2), and tenant B with one user. Every ID-bearing route faces the user from tenant B. Routes for owner-scoped data also face A2, which is the same-tenant case a cross-tenant test never exercises. A status code alone proves little on PATCH and DELETE, because a handler can change the row and still answer 404. Each case therefore also checks that the response does not echo A1’s data and that A1’s row is exactly as seeded. The route table is the single source of truth. A second test fails when the router has a parameterised route the table does not cover. That guards path parameters against the Optus failure mode, a fix that lands on one route and misses its twin. It proves coverage, not that each expectation in the table is right; that part stays a review job.
import { describe, expect, it } from 'vitest';
import { app } from '../src/app';
import { fixtures } from './fixtures'; // seeds owner A1 and colleague A2 in tenant A, user B in tenant B
import { requestSchemas } from '../src/request-schemas';
const idRoutes = [
{ method: 'GET', pattern: '/invoices/:id', resource: 'invoice' },
{ method: 'PATCH', pattern: '/invoices/:id', resource: 'invoice', body: { status: 'PAID' } },
{ method: 'DELETE', pattern: '/invoices/:id', resource: 'invoice' },
{ method: 'GET', pattern: '/invoices/:id/pdf', resource: 'invoice' },
{ method: 'GET', pattern: '/expense-reports/:id', resource: 'expenseReport', ownerScoped: true },
{ method: 'PATCH', pattern: '/expense-reports/:id', resource: 'expenseReport', ownerScoped: true, body: { amount: 1 } },
] as const;
// Every route faces user B from tenant B; owner-scoped routes also face A2, A1's colleague.
const cases = idRoutes.flatMap((route) => [
{ ...route, attacker: 'tenantB' as const },
...('ownerScoped' in route ? [{ ...route, attacker: 'colleagueA' as const }] : []),
]);
describe.each(cases)('$attacker -> $method $pattern', (route) => {
it('cannot reach an object owned by A1', async () => {
const victim = fixtures.victim(route.resource);
const res = await app.request(route.pattern.replace(':id', victim.id), {
method: route.method,
headers: {
Authorization: `Bearer ${fixtures.tokens[route.attacker]}`,
'Content-Type': 'application/json',
},
body: 'body' in route ? JSON.stringify(route.body) : undefined,
});
expect(res.status).toBe(404);
// A 404 alone proves little on writes: the response must not echo A1's data,
// and A1's row must be exactly as seeded.
expect(await res.text()).not.toContain(victim.id);
expect(await victim.read()).toEqual(victim.seeded);
});
});
it('every parameterised route has a case', () => {
const registered = app.routes
.filter((r) => r.method !== 'ALL' && r.path.includes(':'))
.map((r) => `${r.method} ${r.path}`);
const covered = idRoutes.map((r) => `${r.method} ${r.pattern}`);
expect(new Set(registered)).toEqual(new Set(covered));
});
Hono exposes app.routes directly, and each middleware registered with app.use('*', ...) appears there with method ALL, which the filter drops. The Set comparison also absorbs the duplicate entries Hono records when a route has per-route middleware. On Express the same inventory needs a walk over the router stack, and the rest of the harness is unchanged apart from swapping app.request for Supertest.
The path is only one of the four places OWASP lists for an object ID. POST /exports with an invoiceId in the body, or GET /invoices?customerId=..., has no : in its path, so the route filter above never sees it. When every route validates its query and body through one schema registry, the same inventory extends to those fields:
import { z } from 'zod'; // Zod 4
// Query and body schemas live in one registry; each route's validator reads from it.
export const requestSchemas = {
'POST /exports': {
body: z.object({ invoiceId: z.uuid(), format: z.enum(['pdf', 'csv']) }),
},
'GET /invoices': {
query: z.object({ customerId: z.uuid().optional(), cursor: z.string().optional() }),
},
} satisfies Record<string, { query?: z.ZodObject; body?: z.ZodObject }>;
// IDs that arrive in the query string or the body.
const idFields = [
{ route: 'POST /exports', in: 'body', field: 'invoiceId', resource: 'invoice', status: 404, extra: { format: 'pdf' } },
{ route: 'GET /invoices', in: 'query', field: 'customerId', resource: 'customer', status: 200 },
] as const;
const looksLikeId = (key: string) => key === 'id' || key.endsWith('Id');
it('every ID-shaped query or body field has a case', () => {
const declared = Object.entries(requestSchemas).flatMap(([route, parts]) =>
Object.entries(parts).flatMap(([where, schema]) =>
Object.keys(schema.shape)
.filter(looksLikeId)
.map((field) => `${route} ${where} ${field}`),
),
);
const covered = idFields.map((f) => `${f.route} ${f.in} ${f.field}`);
expect(new Set(declared)).toEqual(new Set(covered));
});
describe.each(idFields)('tenantB -> $route ($field in $in)', (f) => {
it('gets nothing that belongs to tenant A', async () => {
const victim = fixtures.victim(f.resource);
const [method, path] = f.route.split(' ');
const payload = { ...('extra' in f ? f.extra : {}), [f.field]: victim.id };
const res = await app.request(
f.in === 'query' ? `${path}?${new URLSearchParams(payload)}` : path,
{
method,
headers: {
Authorization: `Bearer ${fixtures.tokens.tenantB}`,
'Content-Type': 'application/json',
},
body: f.in === 'body' ? JSON.stringify(payload) : undefined,
},
);
expect(res.status).toBe(f.status); // 404 for a lookup, 200 with an empty list for a filter
expect(await res.text()).not.toContain(victim.id);
});
});
Each field case carries its own status, because a foreign ID in a filter is a valid query that returns an empty list. The looksLikeId rule is a naming convention, and the guard is only as strong as the registry: a handler that reads c.req.query() without a registered schema stays invisible to it. An object ID in a header stays a review item.
Three further checks belong in the same pipeline when RLS is in place. The first connects as app_user with app.tenant_id set to tenant B and asserts that SELECT count(*) FROM invoices WHERE tenant_id = '<A>' returns zero. The second runs the same count with app.tenant_id never set, and again after a committed transaction, and expects zero both times on the PostgreSQL version you run. The third is a catalog query that fails when any table with a tenant_id column is missing either flag:
SELECT c.relname
FROM pg_class c
JOIN pg_namespace n ON n.oid = c.relnamespace
JOIN pg_attribute a ON a.attrelid = c.oid
AND a.attname = 'tenant_id' AND NOT a.attisdropped
WHERE n.nspname = 'public'
AND c.relkind = 'r'
AND NOT (c.relrowsecurity AND c.relforcerowsecurity);
-- expected: zero rows
These three prove that the policies work. They do not prove that the deployed application connects as app_user, and connecting as the table owner is the first RLS mistake in the list below. A startup check on the runtime pool covers that:
import type { Pool } from 'pg';
// Runs once at startup on the runtime pool. The process refuses to serve if RLS would not apply.
export async function assertRlsApplies(pool: Pool): Promise<void> {
const { rows } = await pool.query<{
role: string;
rolsuper: boolean;
rolbypassrls: boolean;
rls_active: boolean;
}>(`
SELECT current_user AS role, r.rolsuper, r.rolbypassrls,
row_security_active('invoices') AS rls_active
FROM pg_roles r
WHERE r.rolname = current_user`);
const me = rows[0];
if (!me || me.rolsuper || me.rolbypassrls || !me.rls_active) {
throw new Error(`RLS does not apply to role ${me?.role ?? 'unknown'} on invoices`);
}
}
row_security_active reports whether row-level security is active for the table in the context of the current user, so it also catches a table owner connecting without FORCE. The role attributes catch superusers and BYPASSRLS roles by name, which makes the error message easier to act on.
For manual and dynamic testing, Autorize is an open-source Burp Suite extension. It replays a higher-privileged user’s traffic with a lower-privileged or unauthenticated session and flags responses that match; its flags still need a human to confirm. ID fuzzing follows the same idea by hand: harvest identifiers from user A’s session and substitute them into user B’s requests in the path, query string, body and headers, since OWASP lists all four as BOLA vectors.
Static analysis is weak here for a structural reason. Conventional SAST follows tainted data from a source to a sink, and a missing AND tenant_id = $2 has neither; the scanner has no business context that says user B should not see invoice 8812. Several vendors now claim that AI-assisted analysis finds this class of logic flaw, SonarSource’s Hunter Agent among them. Those claims are vendor-sourced and not independently benchmarked, so treat such tools as an extra signal and keep the two-user test as the control.
Mistakes That Reopen the Gap#
- Scoping the read but not the write. The ownership comparison runs on
findUnique, and theupdate({ where: { id } })three lines later has no scope. Repeat the scope in everyUPDATEandDELETE, or useupdateManywith a count check as in the repository above. - Tenant ID taken from the client. An
X-Tenant-Idheader or atenantIdfield in the body accepted at face value. Derive it from the verified token or session, or validate it against the user’s memberships first. - RLS enabled, but the application connects as the table owner. The policies silently do not apply. Set
FORCE ROW LEVEL SECURITY, run the application under a separate non-ownerNOBYPASSRLSrole, and let the startup check refuse anything else. - Session-level
SET app.tenant_idbehind a transaction-mode pooler. The setting lands on the wrong connection. Useset_config(..., true)inside an explicit transaction. - Casting
current_setting(...)withoutnullif(..., ''). The empty-string state turns into a cast error after the first transaction ends. - 403 for “not yours” and 404 for “does not exist”. The difference confirms existence to an attacker. Return the same response for both.
- Testing with one tenant, with the admin account, or only across tenants. Multi-Tenant Authorization in TypeScript lists untested cross-tenant scenarios as a pitfall. A colleague in the same tenant is the case a cross-tenant test misses; the harness above runs both.
- Per-item
check()loops on list endpoints. Push a filter into the query instead, whether that filter is a scope, a list-objects result or a partially evaluated policy. - GraphQL nested resolvers. The parent query is scoped, and the
node(id:)or nestedinvoice(id:)resolver is not. Resolvers call the same repository. - Declaring victory after BOLA. The user may own the object and still must not set
roleor readinternalNotes. That is API3 BOPLA, and Advanced ABAC: Field-Level Permissions and DB Integration covers it.
The scoped repository with a two-user CI test (one user from another tenant, one colleague from the same tenant) holds as the default for any team with RBAC already shipped, a relational database, and access rules that reduce to ownership or tenancy. On shared-schema multi-tenant PostgreSQL, RLS with FORCE, a non-owner role and transaction-local set_config belongs underneath it. Reach for a relationship or policy engine when sharing and nested relationships define access, and even then route its output through the same repository. If the codebase currently relies on handler checks, the first step is the route-inventory test, because it tells you which endpoints are unscoped before any refactor begins.
References#
- OWASP API1:2023 Broken Object Level Authorization (opens in new tab) - Definition, attack vectors, example scenarios and prevention list for BOLA, including why comparing the session user ID with the path parameter is not sufficient
- OWASP Insecure Direct Object Reference Prevention Cheat Sheet (opens in new tab) - The scoped-lookup pattern, complex identifiers as defence in depth, and multi-account testing guidance
- OWASP Top 10:2025 A01 Broken Access Control (opens in new tab) - Prevalence data for broken access control in web applications and its mapping to CWE-639
- PostgreSQL Documentation: Row Security Policies (opens in new tab) - Official reference for RLS semantics: default deny, BYPASSRLS, table-owner bypass, FORCE ROW LEVEL SECURITY and referential-integrity covert channels
- PostgreSQL Documentation: System Administration Functions (opens in new tab) - Reference for
set_configwith transaction-local scope andcurrent_settingwithmissing_ok - PostgreSQL Documentation: System Information Functions (opens in new tab) - Reference for
current_userandrow_security_active, used in the startup check - PostgreSQL Documentation: pg_roles (opens in new tab) - The
rolsuperandrolbypassrlsrole attributes - pgsql-general: SET LOCAL variable doesn’t become undefined after commit (opens in new tab) - Mailing-list thread showing that custom settings revert to an empty string after a transaction ends
- pgsql-general: Tom Lane on RESET, NULL and empty-string settings (opens in new tab) - Explanation of why a custom setting’s reset value is the empty string once it has been assigned, and why the pattern is unsupported
- PgBouncer Features (opens in new tab) - Feature matrix showing that session-level SET and RESET are unsupported in transaction pooling mode
- Prisma Client Extensions: Row-Level Security example (opens in new tab) - Official example of setting a transaction-local PostgreSQL variable from Prisma, with its documented caveats
- Prisma ORM 7: Transactions and batch queries (opens in new tab) - Interactive transactions, their default
maxWaitandtimeoutvalues, and the warning against keeping transactions open - Supabase: RLS Performance and Best Practices (opens in new tab) - Guidance on indexing policy columns and avoiding per-row function evaluation in RLS policies
- OpenFGA: Perform a List Objects Request (opens in new tab) - How a relationship-based engine answers which objects a user can see, and the performance caveats for list endpoints
- OpenFGA: Search with Permissions (opens in new tab) - When to list object IDs first and when to build a local index, by how many objects a user can reach
- OpenFGA: Relationship Queries (opens in new tab) - The
listObjectsMaxResultsandlistObjectsDeadlinedefaults that bound a ListObjects response - Open Policy Agent: Evaluating a Data Filter Policy (opens in new tab) - Using partial evaluation to turn authorization policies into query filters
- PortSwigger/autorize on GitHub (opens in new tab) - Burp Suite extension that replays requests with a lower-privileged session to detect authorization gaps
- KrebsOnSecurity: USPS Site Exposed Data on 60 Million Users (opens in new tab) - An authenticated API that let any logged-in user query other users’ account data
- Pen Test Partners: Tour de Peloton, Exposed User Data (opens in new tab) - Primary disclosure showing that adding authentication left user data readable by every logged-in account
- SecurityWeek: First American Financial Exposed Millions of Sensitive Documents (opens in new tab) - A link parameter change that exposed other customers’ documents
- CSO Online: Optus breach occurred due to a coding error, alleges ACMA (opens in new tab) - The regulator’s account of an access-control error fixed on one domain and missed on the API domain
- SonarSource: SonarQube Hunter Agent detects logical flaws (opens in new tab) - A vendor’s case for AI-assisted detection of broken access control, as the counterpoint to the traditional SAST blind spot
Related posts
Build SaaS authorization with AWS Cognito and Verified Permissions, covering Cedar policies, multi-tenant patterns, JWT flow, and cost in TypeScript.
authorization · aws · authentication +4
A vendor-neutral evaluation of AWS Verified Permissions, Cedar, OpenFGA, SpiceDB, Cerbos, and OPA: deployment, consistency, cost drivers, and a decision tree.
authorization · security · architecture +4
A deep comparison of Cedar, Rego, OpenFGA DSL, and Cerbos YAML/CEL policy languages: syntax, performance, formal verification, tooling, and TypeScript integration.
authorization · security · architecture +2
SpiceDB vs Auth0 FGA (OpenFGA): when ZedTokens, self-hosting or AuthZed's managed SpiceDB beat the default, plus list queries, outbox sync and pricing.
authorization · security · architecture +3
Authentication vs authorization, common permission pitfalls, the fail-closed principle, and the goals every permission system should meet.
typescript · nextjs · authorization +2