-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathuserService.js
More file actions
80 lines (69 loc) · 2.39 KB
/
Copy pathuserService.js
File metadata and controls
80 lines (69 loc) · 2.39 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
71
72
73
74
75
76
77
78
79
80
// userService.js - Sample code with intentional bugs for practice
// Use this file to practice code review and debugging with GitHub Copilot CLI
//
// Try these commands:
// copilot --allow-all -p "Review @samples/buggy-code/js/userService.js for security issues"
// copilot --allow-all -p "Find all bugs in @samples/buggy-code/js/userService.js"
const db = require('./db');
// BUG 1: SQL Injection
// The userId is directly concatenated into the query string
async function getUser(userId) {
return db.query(`SELECT * FROM users WHERE id = ${userId}`);
}
// BUG 2: Race Condition
// Multiple requests can trigger parallel database calls before cache is set
let userCache = {};
async function getCachedUser(userId) {
if (!userCache[userId]) {
userCache[userId] = await getUser(userId);
}
return userCache[userId];
}
// BUG 3: SQL Injection + No Error Handling
// String interpolation in SQL and no try/catch
async function updateUser(userId, data) {
await db.query(`UPDATE users SET name = '${data.name}' WHERE id = ${userId}`);
return getUser(userId);
}
// BUG 4: Sensitive Data in Logs
// Password is logged in plain text
async function login(email, password) {
console.log(`Login attempt: ${email} / ${password}`);
const user = await db.query(`SELECT * FROM users WHERE email = '${email}'`);
if (user.password === password) {
return { success: true, user };
}
return { success: false };
}
// BUG 5: Weak Password Comparison
// Using == instead of === and comparing plain text passwords
async function verifyPassword(inputPassword, storedPassword) {
return inputPassword == storedPassword;
}
// BUG 6: No Input Validation
// Directly using user input without any validation
async function createUser(userData) {
const query = `INSERT INTO users (name, email, password) VALUES ('${userData.name}', '${userData.email}', '${userData.password}')`;
return db.query(query);
}
// BUG 7: Hardcoded Secret
// JWT secret should be in environment variables
const JWT_SECRET = 'super-secret-key-12345';
function generateToken(userId) {
return jwt.sign({ userId }, JWT_SECRET);
}
// BUG 8: Missing Authentication Check
// This endpoint should verify the user is authorized
async function deleteUser(userId) {
return db.query(`DELETE FROM users WHERE id = ${userId}`);
}
module.exports = {
getUser,
getCachedUser,
updateUser,
login,
verifyPassword,
createUser,
generateToken,
deleteUser
};