Authenticate every request that isn't meant to be public
app.use('/api', (req, res, next) => {
const token = req.headers.authorization?.split(' ')[1];
if (!isValidToken(token)) return res.status(401).json({ error: 'Unauthorized' });
next();
});
Rate limiting protects availability, not just data
const rateLimit = require('express-rate-limit');
app.use('/api', rateLimit({ windowMs: 15 * 60 * 1000, max: 100 }));
Even a fully public, unauthenticated endpoint needs a rate limit - otherwise one misbehaving client, malicious or not, can take the whole service down for everyone else.
Never trust a client-supplied ID
Every request for a specific record needs a server-side check that the authenticated user actually owns or is allowed to see it - a missing check here is the single most common API vulnerability we find in an audit.
Authentication vs authorization - a distinction worth keeping straight
Authentication answers "who is this?"; authorization answers "what are they allowed to do?" A shocking number of API security bugs come from nailing the first and forgetting the second - an endpoint correctly requires a valid token, but never checks whether the token's owner actually has permission to access the specific resource being requested. Testing "can user A read user B's data by changing an ID in the URL" should be a standing item in every API review.
Rate limiting protects more than just your servers
A login endpoint with no rate limit is an open invitation to credential-stuffing attacks, regardless of how strong your password hashing is. A sensible baseline is limiting by IP and by account simultaneously, with a shorter, stricter window on authentication and password-reset endpoints than on general API traffic:
// Express + express-rate-limit
const authLimiter = rateLimit({ windowMs: 15 * 60 * 1000, max: 5 });
app.post('/login', authLimiter, loginHandler);