All courses

Endpoints that authenticate but don't authorize

Spot new endpoints or queries that check who the caller is but not whether they may access this record (insecure direct object references). Explain why agents copying a nearby handler often miss ownership checks that are enforced elsewhere in the codebase.

Builds on: Trust boundaries and untrusted input, What the agent cannot see

Read it card by card Start the whole course

Login required, but whose invoice?

You asked the agent to add a PDF download next to the existing invoice page. It copied the view handler, kept `requireLogin`, wrote a test and reports green. A week later a customer finds that editing the number in `/exports/invoices/1042/pdf` downloads another company's invoice. Every request was logged in. None was checked for whether that caller may see *that* invoice. This bug is an insecure direct object reference (IDOR).

Who you are vs. what you may touch

**Authentication** answers *who is calling*. **Authorization** answers *may this caller access this record*. Codebases often enforce the second away from the handler: router middleware, a scoped query helper, a policy class. The agent copies the handler it opened. The guard lived in a file it never read. Its test logs in as the owner, so it's green. Later cost: any logged-in user reads or changes others' data by editing an id.

Checklist for any id from the request

For each new endpoint, query or job that takes an id: 1. Where is the record loaded? Does the query filter by the caller's own `owner_id`/`org_id`, taken from the session and not from the request? Or is there an explicit check after loading? 2. If the check lives elsewhere, confirm the new route actually passes through it. 3. Nested ids: does the child belong to the parent? 4. Writes, deletes and lists too. 5. Ask for a test where another user gets 404.

Where the guard really lived

The view handler the agent copied loads `findFirst({ where: { id } })`, with no owner in sight. It looked unguarded and worked, so the agent mirrored it. The real guard was in `routes/index.ts`: `app.use("/invoices", requireLogin, ownInvoice, invoices)`. The agent added its route to the exports router, mounted as `app.use("/exports", requireLogin, exports)`. Its summary, "mirrors the existing invoice view", was true of the code and false of the protection.

The tell: login guard, id-only lookup

Tell: the route has `requireLogin`, and the query filters on `req.params.id` alone. Nothing ties the invoice to `req.user`. BETTER: `where: { id: req.params.id, orgId: req.user.orgId }`. Another org's id then finds nothing and gets 404 rather than 403, which also hides that the invoice exists. Later cost: anyone with an account can walk the ids and download every invoice, with no error and nothing odd in the logs.
// TypeScript
--- a/src/routes/exports.ts
+++ b/src/routes/exports.ts
@@ -12,0 +13,8 @@
+r.get("/invoices/:id/pdf", requireLogin,
+  async (req, res) => {
+    const inv = await db.invoice.findFirst({
+      where: { id: req.params.id },
+    });
+    if (!inv) return res.sendStatus(404);
+    res.type("pdf").send(await toPdf(inv));
+  });

Every id needs a link to the caller

A login check says who is calling, not which records they may touch. For every id that comes from the request, find the line that ties the record to the caller, using the caller's identity from the session. If that line lives outside the handler, confirm the agent's new route actually goes through it.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.