-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclasswork12.js
More file actions
70 lines (59 loc) · 2.09 KB
/
Copy pathclasswork12.js
File metadata and controls
70 lines (59 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// ==========================================
// FILE: classwork12.js - Role Escalation
// ==========================================
const express = require('express');
const router = express.Router();
const jwt = require('jsonwebtoken');
const { SECRET_KEY } = require('./config');
// Login endpoint - returns JWT with role
router.post('/login', (req, res) => {
const { username, password } = req.body;
if (username === 'john' && password === 'user123') {
const token = jwt.sign(
{ username: 'john', role: 'user', userId: 1001 },
SECRET_KEY,
{ expiresIn: '1h' }
);
// VULNERABILITY: Response contains role that client can manipulate
res.json({
success: true,
token: token,
user: {
username: 'john',
role: 'user', // Client can intercept and change to 'admin'
userId: 1001
},
message: 'Intercept this response and change role to admin!'
});
} else {
res.status(401).json({ error: 'Invalid credentials' });
}
});
// Protected endpoint - VULNERABLE (trusts client-provided role)
router.get('/admin-panel', (req, res) => {
const authHeader = req.headers['authorization'];
if (!authHeader) {
return res.status(401).json({ error: 'No authorization header' });
}
try {
const token = authHeader.split(' ')[1];
const decoded = jwt.verify(token, SECRET_KEY);
// VULNERABILITY: Gets role from query parameter (client-controlled!)
const clientRole = req.query.role;
if (clientRole === 'admin') {
return res.json({
flag: 'FLAG{role_escalation_response_manipulation_success}',
message: 'You escalated privileges by manipulating the response!',
adminSecret: 'ADMIN_SECRET_KEY_12345',
warning: 'Always verify roles SERVER-SIDE, never trust client data!'
});
}
res.json({
message: 'Access denied. User role detected.',
hint: 'Intercept the /login response and change role from "user" to "admin", then add ?role=admin to this request'
});
} catch (err) {
res.status(401).json({ error: 'Invalid token' });
}
});
module.exports = router;