-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathTask1-vuln.js
More file actions
39 lines (34 loc) · 941 Bytes
/
Task1-vuln.js
File metadata and controls
39 lines (34 loc) · 941 Bytes
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
const express = require('express');
const app = express();
app.use(express.json());
// Mock database functions
const db = {
users: [
{ id: 1, role: 'admin' },
{ id: 2, role: 'user' }
],
async getUserById(userId) {
return this.users.find(user => user.id === userId);
},
async deleteProject(projectId) {
console.log(`Project ${projectId} deleted by admin.`);
}
};
//auth.js
async function isAdmin(userId) {
const user = await db.getUserById(userId);
return user && user.role === 'admin';
}
//routes.js
app.post('/project/:id/delete', async (req, res) => {
const userId = parseInt(req.body.userId, 10); // Convert string to integer
const projectId = req.params.id;
if (await isAdmin(userId)) {
await db.deleteProject(projectId);
return res.send('Project deleted');
}
return res.status(403).send('Not allowed');
});
app.listen(3000, () => {
console.log('Server started on port 3000');
});