Topic 2 of 6 · 4 min read
Missing authorization and broken access control
Goal: Spot a missing ownership or role check in an AI-written endpoint and describe the request that exploits it.
Authentication asks "who are you?". Authorization asks "may you do this to that object?". AI-written endpoints often do the first and forget the second.
Check yourself · 1 of 3
A generated endpoint has a login middleware and returns an invoice by id from the URL. What is still missing?
Why it matters
Broken access control is the first item on the OWASP Top 10. It is also hard for a model to see, because the check is not in the task. "Add an endpoint to fetch an invoice by id" is complete without any ownership rule, so the generated code is complete too. It runs, it returns the invoice, and every logged-in user can read every invoice.
How to find it
For every endpoint in the diff, answer three questions:
- Is there a login check? A middleware or decorator that rejects anonymous callers.
- Is there an ownership check? Does the query or the code tie the object to the caller, for example
WHERE id = ? AND owner_id = ?. - Is there a role check? Admin, support and internal routes should name the role they need.
Then attack it by hand. Log in as user A, take the id of an object that belongs to user B, and send the request. If you get data back, the endpoint has an insecure direct object reference (IDOR). Changing a number in a URL is all the exploit needs.
Also check what the request body can set. If the handler copies the whole body into the record, a caller can send "role": "admin" or "ownerId": 1. This is called mass assignment.
Before and after
// Before: any logged-in user can read any invoice
app.get("/invoices/:id", requireLogin, async (req, res) => {
res.json(await db.invoice.findUnique({ where: { id: req.params.id } }));
});
// After: the row must belong to the caller
app.get("/invoices/:id", requireLogin, async (req, res) => {
const row = await db.invoice.findFirst({
where: { id: req.params.id, userId: req.user.id },
});
if (!row) return res.status(404).end();
res.json(row);
});
What goes wrong
- Hiding the button instead of the route. The agent removes the admin link from the UI. The API route is still open.
- Trusting an id from the client. A
userIdin the body or query replaces the id from the session. - Checking on read but not on write. The GET is scoped, the PUT and DELETE are not.
- Guessable ids are not a defense. Random ids slow an attacker. They do not replace the check.
Give the agent your authorization rule in the task, and keep a test that signs in as a second user.
Examples
The delete route that skipped the check
@app.delete("/api/projects/<int:pid>")
@login_required
def delete_project(pid):
Project.query.filter_by(id=pid).delete()
db.session.commit()
return "", 204
Any signed-in user can delete any project by guessing a number. The fix adds owner_id=current_user.id to the filter and returns 404 when no row matched.
The profile update that accepts a role
await User.update(req.body, { where: { id: req.user.id } });
Sending {"role":"admin"} in the body promotes the caller. Pick the allowed fields by name (name, avatarUrl) instead of passing the body through.
Key points
- Authentication proves identity; authorization decides whether that identity may touch this object.
- Every endpoint needs a login check, an ownership or tenant check, and a role check where relevant.
- Test by logging in as user A and requesting user B's object ids; any data back is an IDOR.
- Take the user id from the session, never from the request body or query string.
- Copying the whole request body into a record lets callers set fields such as role or owner (mass assignment).
Common mistakes
- Treating a login middleware as if it were an access control check.
- Reviewing only the read endpoint and missing that update and delete skip the ownership filter.
- Hiding admin features in the UI while leaving the API route unprotected.
- Reading the user id from request input when it is available from the verified session.
Check yourself · 2 of 3
In a profile update handler, which source should supply the id of the user being updated?
Check yourself · 3 of 3
An agent hides the admin link from non-admin users in the front end and calls the task done. What is the risk?
Practice items, not counted towards your mastery.
Start here
- CWE Top 25 Most Dangerous Software Weaknesses (MITRE) (opens in a new tab)DocsFree
The ranked list of weakness classes, including injection and broken access control, that generated code should be checked against.
- Do Users Write More Insecure Code with AI Assistants? (opens in a new tab)PaperFree
A user study showing assistant users wrote less secure code while feeling more sure of it.
- OWASP Top Ten (opens in a new tab)StandardFree
The standard awareness list of web application risks, from access control to misconfiguration.
More for this topic
- Authorization Cheat Sheet (OWASP) (opens in a new tab)ArticleFree
Deny by default, check on every request and test authorization; the rules to hold generated endpoints to.