Performance: Resource ownership checks
MEDIUM IMPACT
This affects server response time and user interaction speed by adding authorization logic before resource access.
app.get('/resource/:id', async (req, res) => { const resource = await Resource.findOne({ _id: req.params.id, ownerId: req.user.id }).select('_id data'); if (!resource) return res.status(404).send('Not found or no access'); res.send(resource); });
app.get('/resource/:id', async (req, res) => { const resource = await Resource.findById(req.params.id); if (!resource) return res.status(404).send('Not found'); if (resource.ownerId.toString() !== req.user.id) return res.status(403).send('Forbidden'); res.send(resource); });
| Pattern | DB Query Cost | Data Transferred | Response Time Impact | Verdict |
|---|---|---|---|---|
| Fetch full resource then check ownership | High (full document fetch) | Large (all fields) | Slower (blocks response) | [X] Bad |
| Query with ownership filter and select fields | Low (filtered query) | Small (selected fields) | Faster (quick response) | [OK] Good |
| Middleware full fetch for ownership | High (full fetch every request) | Large | Slower (adds latency) | [X] Bad |
| Middleware existence check with ownership filter | Low (exists query) | Minimal | Faster (lightweight check) | [OK] Good |