What if a tiny missed check lets anyone change someone else's data?
Why Resource ownership checks in Express? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine building a web app where users can edit their own posts. You manually check user IDs against post owners in every route handler.
Manually checking ownership everywhere leads to repeated code, missed checks, and security holes if you forget one place.
Resource ownership checks centralize this logic, so Express middleware automatically verifies ownership before allowing edits.
app.put('/posts/:id', (req, res) => { if(req.user.id !== post.ownerId) return res.status(403).send('Forbidden'); /* update post */ })
app.put('/posts/:id', checkOwnership, (req, res) => { /* update post safely */ })This lets you protect resources consistently and securely without repeating code everywhere.
On a blog site, only the author can edit or delete their posts, enforced automatically by ownership checks.
Manual ownership checks are repetitive and risky.
Middleware centralizes and automates these checks.
This improves security and keeps code clean.
Practice
Solution
Step 1: Understand resource ownership
Resource ownership means a resource belongs to a specific user.Step 2: Purpose of ownership checks
Ownership checks prevent unauthorized users from accessing or changing resources they don't own.Final Answer:
To ensure only the owner can access or modify their resource -> Option BQuick Check:
Ownership check = restrict access to owner [OK]
- Thinking ownership checks speed up queries
- Allowing all users to edit resources
- Confusing ownership with logging
req.params.id and owner ID in resource.ownerId?Solution
Step 1: Check user ID equality
We comparereq.user.idwithresource.ownerIdusing strict equality to confirm ownership.Step 2: Respond with 403 if not owner
If IDs don't match, respond with 403 Forbidden to block access.Final Answer:
if (req.user.id === resource.ownerId) { next(); } else { res.status(403).send('Forbidden'); } -> Option CQuick Check:
Strict equality + 403 Forbidden = correct ownership check [OK]
- Using == instead of ===
- Sending wrong status codes like 404 or 401
- Comparing whole user object instead of user ID
req.user.id is '123' and resource.ownerId is '456'?
app.delete('/items/:id', (req, res) => {
const resource = {ownerId: '456'};
if (req.user.id === resource.ownerId) {
res.send('Deleted');
} else {
res.status(403).send('Forbidden');
}
});Solution
Step 1: Compare user ID and owner ID
Sincereq.user.id('123') does not equalresource.ownerId('456'), ownership check fails.Step 2: Return 403 Forbidden
The else block sends a 403 Forbidden response blocking deletion.Final Answer:
Response will be 403 Forbidden -> Option DQuick Check:
Non-matching IDs = 403 Forbidden [OK]
- Assuming deletion happens anyway
- Confusing 403 with 404
- Ignoring ownership check logic
function checkOwnership(req, res, next) {
const resource = {ownerId: '456'}; /* example */
if (req.user.id = resource.ownerId) {
next();
} else {
res.status(403).send('Forbidden');
}
}Solution
Step 1: Check the if condition syntax
The condition uses single equals (=), which assigns instead of compares, causing a bug.Step 2: Correct comparison operator
It should use strict equality (===) to comparereq.user.idandresource.ownerId.Final Answer:
Using assignment (=) instead of comparison (===) in the if condition -> Option AQuick Check:
Assignment in if condition = bug [OK]
- Confusing = with === in conditions
- Thinking next() needed in else block
- Wrong status code for forbidden access
post.ownerId. Which Express middleware correctly implements this ownership check and returns 403 if the user is not the owner?Solution
Step 1: Use middleware to check ownership before update
Middleware checks ifreq.user.idmatchespost.ownerIdand callsnext()if true.Step 2: Return 403 Forbidden if not owner
If IDs don't match, respond with 403 to block unauthorized edits.Final Answer:
app.put('/posts/:id', (req, res, next) => { if (req.user.id === post.ownerId) next(); else res.status(403).send('Forbidden'); }, (req, res) => { res.send('Post updated'); }); -> Option AQuick Check:
Middleware + strict equality + 403 Forbidden = correct pattern [OK]
- Using == instead of ===
- Sending wrong status codes like 404 or 401
- Not using middleware pattern for ownership check
