Skip to content
engineering workbench.by StudyPort Academy
All errors
logic · DEBUG LIBRARY

The handler checks login and skips ownership

Locate the failed assumption, reproduce it, then repair the cause.

What actually caused it

The route checks that a user is logged in, then loads the record by the id in the path and returns it. Those are different questions. Authentication answers who is calling. It does not answer whether this caller owns this id. An attacker with any valid session changes the id and receives another user's note, invoice, or message. The status is 200 because the session was valid and the document existed. Nothing compared note.owner with the caller. Tests that only sign in as the owner stay green, because the owner's own id really is theirs. This is broken level authorization, and it is common because the happy path looks finished as soon as the 401 for anonymous users is in place.

Minimal reproduction

Any logged-in user, any id

if (!req.query.user) return res.status(401).json({ error: 'login' });
res.json(notes[req.params.id]);

Ada's session plus Bea's id returns Bea's note.

The fix

After the session check, load the record and compare its owner with the authenticated caller. If they differ, return 403 and do not include the record in the body. A 404 used to hide existence is a different product choice. The requirement is that the other person's fields do not leave. Do the same compare on update and delete. An editor role is not a substitute when the rule is ownership: the editor can still be the wrong person for this id. Add a test that signs in as Ada and requests Bea's id, and expect 403. The test that only uses Ada's id will not catch the bug.

Login, then the owner

if (!req.query.user) return res.status(401).json({ error: 'login' });
if (note.owner !== req.query.user) return res.status(403).json({ error: 'forbidden' });
res.json(note);

A mismatched owner is refused and the note is not in the body.