-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathInsecure.java
More file actions
112 lines (96 loc) · 4.09 KB
/
Insecure.java
File metadata and controls
112 lines (96 loc) · 4.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package demo.security.servlet;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Insecure {
public void badFunction(HttpServletRequest request) throws IOException {
String obj = request.getParameter("data");
ObjectMapper mapper = new ObjectMapper();
mapper.enableDefaultTyping();
String val = mapper.readValue(obj, String.class);
File tempDir;
tempDir = File.createTempFile("", ".");
tempDir.delete();
tempDir.mkdir();
Files.exists(Paths.get("/tmp/", obj));
}
public String taintedSQL(HttpServletRequest request, Connection connection) throws Exception {
String user = request.getParameter("user");
String query = "SELECT userid FROM users WHERE username = '" + user + "'";
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(query);
return resultSet.getString(0);
}
public String taintedSQLByUsername(HttpServletRequest request, Connection connection) throws SQLException {
String username = request.getParameter("username");
String query = "SELECT * FROM users WHERE username = ?";
try (PreparedStatement statement = connection.prepareStatement(query)) {
statement.setString(1, username);
try (ResultSet resultSet = statement.executeQuery()) {
return resultSet.getString(1);
}
}
}
public String taintedSQLByEmail(HttpServletRequest request, Connection connection) throws SQLException {
String email = request.getParameter("email");
String query = "SELECT * FROM users WHERE email = ?";
try (PreparedStatement statement = connection.prepareStatement(query)) {
statement.setString(1, email);
try (ResultSet resultSet = statement.executeQuery()) {
return resultSet.getString(1);
}
}
}
public String hotspotSQL(Connection connection, String user) throws Exception {
Statement statement = null;
statement = connection.createStatement();
ResultSet rs = statement.executeQuery("select userid from users WHERE username=" + user);
return rs.getString(0);
}
// --------------------------------------------------------------------------
// Custom sources, sanitizer and sinks example
// See file s3649JavaSqlInjectionConfig.json in root directory
// --------------------------------------------------------------------------
public String getInput(String name) {
// Empty (fake) source
// To be a real source this should normally return something from an input
// that can be user manipulated e.g. an HTTP request, a cmd line parameter, a form input...
return "Hello World and " + name;
}
public void storeData(String input) {
// Empty (fake) sink
// To be a real sink this should normally build an SQL query from the input parameter
}
public void verifyData(String input) {
// Empty (fake) sanitizer (sic)
// To be a real sanitizer this should normally examine the input and sanitize it
// for any attempt of user manipulation (eg escaping characters, quoting strings etc...)
}
public void processParam(String input) {
// Empty method just for testing
}
public void doSomething() {
String myInput = getInput("Olivier"); // Get data from a source
processParam(myInput);
storeData(myInput); // store data w/o sanitizing --> Injection vulnerability
}
public void doSomethingSanitized() {
String myInput = getInput("Cameron"); // Get data from a source
verifyData(myInput); // Sanitize data
processParam(myInput);
storeData(myInput); // store data after sanitizing --> No injection vulnerability
}
}