-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVulnerableLogin.java
More file actions
42 lines (36 loc) · 1.03 KB
/
Copy pathVulnerableLogin.java
File metadata and controls
42 lines (36 loc) · 1.03 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
/*
* SQL Injection Demonstration
*
* INTENTIONALLY VULNERABLE CODE
*
* This example demonstrates unsafe construction of SQL queries
* using untrusted user-controlled input.
*
* Educational and authorized laboratory use only.
*/
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class VulnerableLogin {
public static boolean authenticate(
Connection connection,
String username,
String password) throws SQLException {
/*
* VULNERABLE:
* User-controlled values are concatenated directly
* into the SQL statement.
*/
String query =
"SELECT * FROM users WHERE username='"
+ username
+ "' AND password='"
+ password
+ "'";
try (Statement statement = connection.createStatement();
ResultSet result = statement.executeQuery(query)) {
return result.next();
}
}
}