-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserDatabase.java
More file actions
90 lines (71 loc) · 3.05 KB
/
UserDatabase.java
File metadata and controls
90 lines (71 loc) · 3.05 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
81
82
83
84
85
86
87
88
89
90
package com.example.database;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
/**
* Database class with intentional SQL injection vulnerabilities
* to demonstrate CodeQL detection capabilities.
*/
public class UserDatabase {
private static final String DB_URL = "jdbc:mysql://localhost:3306/testdb";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "password";
/**
* VULNERABLE: SQL Injection vulnerability - user input directly concatenated
* This should trigger a high/critical CodeQL alert
*/
public boolean authenticateUser(String username, String password) {
try {
Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
Statement stmt = conn.createStatement();
// VULNERABILITY: Direct string concatenation leads to SQL injection
String query = "SELECT * FROM users WHERE username = '" + username +
"' AND password = '" + password + "'";
System.out.println("Executing query: " + query);
ResultSet rs = stmt.executeQuery(query);
boolean authenticated = rs.next();
rs.close();
stmt.close();
conn.close();
return authenticated;
} catch (Exception e) {
System.err.println("Database error: " + e.getMessage());
return false;
}
}
/**
* VULNERABLE: Another SQL injection point
*/
public void updateUserProfile(String userId, String email, String fullName) {
try {
Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
Statement stmt = conn.createStatement();
// VULNERABILITY: String concatenation in UPDATE statement
String updateQuery = "UPDATE users SET email = '" + email +
"', full_name = '" + fullName +
"' WHERE user_id = " + userId;
stmt.executeUpdate(updateQuery);
stmt.close();
conn.close();
} catch (Exception e) {
System.err.println("Update failed: " + e.getMessage());
}
}
/**
* VULNERABLE: Dynamic query construction - another SQL injection pattern
*/
public void deleteUser(String userIdParam) {
try {
Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
Statement stmt = conn.createStatement();
// VULNERABILITY: Direct concatenation in DELETE statement
String sql = "DELETE FROM users WHERE id = " + userIdParam;
stmt.executeUpdate(sql);
stmt.close();
conn.close();
} catch (Exception e) {
System.err.println("Delete failed: " + e.getMessage());
}
}
}