-
Notifications
You must be signed in to change notification settings - Fork 725
Expand file tree
/
Copy pathSQLInjection.java
More file actions
329 lines (267 loc) · 10.5 KB
/
SQLInjection.java
File metadata and controls
329 lines (267 loc) · 10.5 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package checks;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.util.Locale;
import org.hibernate.Session;
import javax.persistence.EntityManager;
import javax.jdo.PersistenceManager;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.PreparedStatementCreatorFactory;
class SQLInjection {
private static final String CONSTANT = "SELECT * FROM TABLE";
public void method(String param, String param2, EntityManager entityManager, jakarta.persistence.EntityManager entityManager2) {
try {
Connection conn = DriverManager.getConnection("url", "user1", "password");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT Lname FROM Customers WHERE Snum = 2001");
rs = stmt.executeQuery("SELECT Lname FROM Customers WHERE Snum = "+param); // Noncompliant {{Make sure using a dynamically formatted SQL query is safe here.}} [[sc=30;ec=79]]
String query = "SELECT Lname FROM Customers WHERE Snum = "+param;
rs = stmt.executeQuery(query); // Noncompliant
boolean bool = false;
String query2 = "Select Lname ";
if(bool) {
query2 += "FROM Customers";
}else {
query2 += "FROM Providers";
}
query2 = query2 + " WHERE Snum =2001";
rs = stmt.executeQuery(query2);
//Prepared statement
PreparedStatement ps = conn.prepareStatement("SELECT Lname FROM Customers"+" WHERE Snum = 2001");
ps.executeQuery("SELECT Lname FROM Customers WHERE Snum = "+param); // Noncompliant
ps = conn.prepareStatement("SELECT Lname FROM Customers WHERE Snum = "+param); // Noncompliant
ps = conn.prepareStatement(query2);
final String queryNoConcatenation = bool ? param : param2;
conn.prepareStatement(queryNoConcatenation);
//Callable Statement
CallableStatement cs = conn.prepareCall("SELECT Lname FROM Customers WHERE Snum = 2001");
cs.executeQuery(query); // Noncompliant
cs = conn.prepareCall("SELECT Lname FROM Customers WHERE Snum = "+param2); // Noncompliant
cs = conn.prepareCall(query2);
cs = conn.prepareCall(CONSTANT);
cs = conn.prepareCall(foo());
String query3 = "SELECT * from table";
cs = conn.prepareCall(query3);
String s;
String tableName = "TableName";
String column = " column ";
String FROM = " FROM ";
if(true) {
s = "SELECT" +column+FROM +tableName;
} else {
s = "SELECT" +column+"FROM" +tableName;
}
cs = conn.prepareCall(s);
String request = foo() + " FROM table";
cs = conn.prepareCall(request); // Noncompliant
new A().prepareStatement(query);
A a = new A();
a.prepareStatement(query);
ps.executeQuery();
Session session = null;
session.createQuery("From Customer where id > ?");
session.createQuery(query); // Noncompliant
conn.prepareStatement(param);
conn.prepareStatement(sqlQuery + "plop"); // Noncompliant
String sql = "SELECT lastname, firstname FROM employee where uid = '" + param + "'";
entityManager.createNativeQuery(sql); // Noncompliant
entityManager2.createNativeQuery(sql); // Noncompliant
String concatenatedQuery0 = "SELECT * ";
concatenatedQuery0 += "FROM " + param;
rs = stmt.executeQuery(concatenatedQuery0); // Noncompliant
String concatenatedQuery = "SELECT * FROM ";
concatenatedQuery += param;
rs = stmt.executeQuery(concatenatedQuery); // Noncompliant
String concatenatedQuery2 = "SELECT * FROM ";
concatenatedQuery2 += param;
concatenatedQuery2 += "WHERE Snum =2";
rs = stmt.executeQuery(concatenatedQuery2); // Noncompliant
String concatenatedQuery3 = "SELECT * FROM ";
concatenatedQuery3 += "MYTABLE ";
concatenatedQuery3 += "WHERE Snum = 2";
rs = stmt.executeQuery(concatenatedQuery3); // Compliant
} catch (Exception e) {
}
}
void falseNegative(String param) throws SQLException {
Connection conn = DriverManager.getConnection("url", "user1", "password");
String s;
s = "SELECT FROM " + param;
conn.prepareStatement(s); // Compliant, false negative
String s2 = "SELECT";
s2 = s2 + param;
conn.prepareStatement(s2); // Compliant, true negative
String s3;
s3 = "SELECT";
s3 += param;
conn.prepareStatement(s3); // Noncompliant
}
void testSecondaryLocations(String param) throws SQLException {
Connection conn = DriverManager.getConnection("url", "user1", "password");
String query1 = "SELECT"; // secondary location
// ^^^^^^^^>
query1 += param; // secondary location
// ^^^^^>
conn.prepareStatement(query1); // Noncompliant
// ^^^^^^
boolean bool = false;
String query2 = "Select Lname ";
if(bool) {
query2 += "FROM Customers";
} else {
query2 += param;
}
query2 = query2 + " WHERE Snum =2001";
conn.prepareStatement(query2); // Noncompliant [[secondary=133,135,137,139]]
}
void foo(Connection conn, String param) throws SQLException {
Statement stmt = conn.createStatement();
String localVar = param;
String aliasParam;
aliasParam = param;
stmt.execute(aliasParam); // OK
stmt.execute(localVar); // OK
stmt.execute(param); // OK
String[] queries = {"test"};
stmt.execute(queries[0]); // OK
}
String foo() {
return "SELECT * ";
}
private String sqlQuery;
class A {
void prepareStatement(String s) {
}
}
private static void makeQuery(Connection p_con) {
try {
String query = null;
StringBuffer qryBuffer = new StringBuffer();
qryBuffer = new StringBuffer();
qryBuffer.append(" select abc from xyz ");
qryBuffer.append(" where bulubulu=?");
query = qryBuffer.toString();
p_con.prepareStatement(query); // Compliant
} catch (Exception e) {
System.out.println("makeQuery");
}
}
PersistenceManager pm;
void jdo(int id, String name) {
javax.jdo.Query q = pm.newQuery(Test.class, id + " > query_id "); // Noncompliant
q.setFilter("name == " + name); // Noncompliant
}
}
class Spring {
private JdbcTemplate jdbcTemplate;
private JdbcOperations jdbcOperations;
private PreparedStatementCreatorFactory preparedStatementCreatorFactory;
void test(String parameter) {
java.lang.String sqlInjection = "select count(*) from t_actor where column = " + parameter;
jdbcTemplate.queryForObject(sqlInjection, Integer.class); // Noncompliant
jdbcOperations.queryForObject(sqlInjection, Integer.class); // Noncompliant
new PreparedStatementCreatorFactory(sqlInjection); // Noncompliant
preparedStatementCreatorFactory.newPreparedStatementCreator(sqlInjection, new Object[] {}); // Noncompliant
}
}
class Test {
public void foo(String page, String projectUuid) {
String from = "from ResourceDBO r, ProjectDBO p where p.id = r.entityId and r.type = :entityType and r.mimeType in :mimeTypes";
if (projectUuid != null) {
from += " and p.uuid = :projectUuid";
}
String sortField = "lastUpdateTime";
boolean asc = false;
if (page != null) {
String countJql = "select count(*) " + from;
Session session = null;
session.createQuery(countJql); // Noncompliant
}
}
}
class SQLInjectionB {
private String user;
private JdbcTemplate tmpl = new JdbcTemplate();
private void foo() {
// field accessed with "this."
tmpl.batchUpdate(this.user); // Compliant
tmpl.queryForObject(this.user, String.class); // Compliant
// field accessed without "this."
tmpl.batchUpdate(user); // compliant
tmpl.queryForObject(user, String.class); // compliant
}
}
class SQLFormat {
private final Statement stmt;
public SQLFormat(Statement stmt) {
this.stmt = stmt;
}
public void formatInline(String input) throws SQLException {
this.stmt.execute(String.format("SELECT %s", input)); // Noncompliant
}
public void formatInlineConst() throws SQLException {
// Allow strings built with `format` if the arguments are constants.
this.stmt.execute(String.format("SELECT %s", "1"));
}
public void formatVar(String input) throws SQLException {
String query = String.format("SELECT %s", input);
this.stmt.execute(query); // Noncompliant
}
public void formatVarObject(Object input) throws SQLException {
String query = String.format("SELECT %s", input);
this.stmt.execute(query); // Noncompliant
}
public void formatVarConst() throws SQLException {
String query = String.format("SELECT %s", "1");
this.stmt.execute(query);
}
public void formatLocale(Locale locale, String input) throws SQLException {
String query = String.format(locale, "SELECT %s", input);
this.stmt.execute(query); // Noncompliant
}
public void formatLocaleConst(Locale locale) throws SQLException {
String query = String.format(locale, "SELECT %s", "1");
this.stmt.execute(query);
}
public void formatPrimitiveVar(int input) throws SQLException {
String query = String.format("SELECT %s", input);
this.stmt.execute(query);
}
public void formatPrimitiveConst() throws SQLException {
String query = String.format("SELECT %s", 1);
this.stmt.execute(query);
}
public void formatPrimitiveWrapperVar(Long input) throws SQLException {
String query = String.format("SELECT %s", input);
this.stmt.execute(query);
}
public void formatPrimitiveWrapperConst() throws SQLException {
String query = String.format("SELECT %s", Long.valueOf(4));
this.stmt.execute(query);
}
public void formatted(String input) throws SQLException {
String query = "SELECT %s".formatted(input);
this.stmt.execute(query); // Noncompliant
}
public void formattedConst() throws SQLException {
String query = "SELECT %s".formatted("1");
this.stmt.execute(query);
}
public void plusAssignment(String input) throws SQLException {
String query = "SELECT";
query += String.format("WHERE col = %c", input);
this.stmt.execute(query); // Noncompliant
}
public void plusAssignmentConst() throws SQLException {
// FP, but probably rare and not worth complicating the code to fix it.
String query = "SELECT";
query += String.format("WHERE col = \"%c\"", "value");
this.stmt.execute(query); // Noncompliant
}
}