Security Playbook
by Federico Tejedor Llorente
Learn
Learning path
Concepts
Breaches
Practice
Labs
Playground
Quiz
Work with me
🔥 0
⚡ 0
Playbooks
Playbooks
System Design
Security
●
Algorithms
Portfolio
↗
en
English
✓
Español
← All code reviews
Find the bug · 07/10
Medium
Admin-only middleware
Tokens are issued by your own auth service, signed with HS256.
middleware/auth.ts
01 · Tap the vulnerable line
1
import jwt from "jsonwebtoken";
2
3
export function requireAdmin(req, res, next) {
4
const token = req.headers.authorization?.replace("Bearer ", "");
5
const claims = jwt.decode(token);
6
if (!claims || claims.role !== "admin") return res.status(403).end();
7
req.user = claims;
8
next();
9
}
02 · What's the problem?
Insecure session cookie
Token not verified
Cross-site request forgery (CSRF)
Cross-site scripting (XSS)
03 · How do you fix it?
Also check claims.exp
Move the token to a cookie
Use jwt.verify(token, SECRET, { algorithms: ["HS256"] }) and trust only its result
Check answer