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 · 08/10
Medium
Setting the session cookie
Rate limiting and a fresh session ID on login are already there.
routes/session.ts
01 · Tap the vulnerable line
1
app.post("/login", rateLimit({ windowMs: 60_000, max: 10 }), async (req, res) => {
2
const user = await verifyCredentials(req.body.email, req.body.password);
3
if (!user) return res.status(401).send("Invalid email or password");
4
const sid = await sessions.create(user.id);
5
res.cookie("sid", sid, { maxAge: 30 * 24 * 3600 * 1000 });
6
res.redirect("/dashboard");
7
});
02 · What's the problem?
Insecure session cookie
Cross-site scripting (XSS)
Token not verified
No rate limiting
03 · How do you fix it?
Shorten maxAge to one day
Set httpOnly: true, secure: true, sameSite: "lax" (and a shorter lifetime)
Store the user ID in the cookie instead of a session ID
Check answer