Repository navigation
Expand file tree
/
Copy pathPreparedStatementAndResultSetCheckSample.java
More file actions
405 lines (324 loc) · 16 KB
/
Copy pathPreparedStatementAndResultSetCheckSample.java
File metadata and controls
405 lines (324 loc) · 16 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package checks;
import com.google.common.base.Strings;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;
abstract class PreparedStatementAndResultSetCheckSample {
private static final int INDEX_42 = 42;
private static final int INDEX_1 = 1;
abstract PreparedStatement getPreparedStatement();
abstract PreparedStatement getPreparedStatement(String s);
abstract int getIntValue();
abstract String getQuery();
void foo(Connection connection) throws SQLException {
PreparedStatement ps = connection.prepareStatement("SELECT fname, lname FROM employees where hireDate > ? and salary < ?");
ps.setDate(0, new Date(0)); // Noncompliant {{PreparedStatement indices start at 1.}}
// ^
ps.setDouble(3, 0.0); // Noncompliant {{This "PreparedStatement" only has 2 parameters.}}
// ^
ps.setString(getIntValue(), ""); // Compliant - first argument can not be evaluated
ps.setInt(1, 0); // Compliant
ResultSet rs = ps.executeQuery();
rs.getString(0); // Noncompliant {{ResultSet indices start at 1.}}
rs.getDate(0, new GregorianCalendar()); // Noncompliant {{ResultSet indices start at 1.}}
rs.getString(1); // Compliant
}
void bar(Connection connection) throws SQLException {
PreparedStatement ps = connection.prepareStatement("SELECT fname, lname FROM employees where hireDate > 1986");
ps.setDate(0, new Date(0)); // Noncompliant {{PreparedStatement indices start at 1.}}
ps.setDouble(3, 0.0); // Noncompliant {{This "PreparedStatement" has no parameters.}}
// ^
}
void dam(Connection connection, String query) throws SQLException {
PreparedStatement ps = connection.prepareStatement(query);
ps.setDate(0, new Date(0)); // Noncompliant {{PreparedStatement indices start at 1.}}
ps.setDouble(3, 0.0); // Compliant - Query of the preparedStatement is unknown
}
void cro(PreparedStatement ps) throws SQLException {
ps.setDate(0, new Date(0)); // Noncompliant {{PreparedStatement indices start at 1.}}
ps.setDouble(3, 0.0); // Compliant - Query of the preparedStatement is unknown
}
void elk() throws SQLException {
getPreparedStatement().setDate(0, new Date(0)); // Noncompliant {{PreparedStatement indices start at 1.}}
getPreparedStatement().setDouble(3, 0.0); // Compliant - Query of the preparedStatement is unknown
}
void gra() throws SQLException {
PreparedStatement ps = getPreparedStatement();
ps.setDate(0, new Date(0)); // Noncompliant {{PreparedStatement indices start at 1.}}
ps.setDouble(3, 0.0); // Compliant - Query of the preparedStatement is unknown
PreparedStatement ps2 = ps;
ps2.setDate(0, new Date(0)); // Noncompliant {{PreparedStatement indices start at 1.}}
ps2.setDouble(3, 0.0); // Compliant - Query of the preparedStatement is unknown
}
void hio(boolean test) throws SQLException {
PreparedStatement ps = getPreparedStatement("SELECT fname, lname FROM employees where hireDate > 1986");
if (test) {
ps = getPreparedStatement("SELECT fname, lname FROM employees where hireDate > ? and salary < ?");
ps.setDouble(1, 0.0); // Compliant - last assignment is used
ps.setDouble(2, 0.0); // Compliant
}
ps = getPreparedStatement("SELECT fname, lname FROM employees where hireDate > 1986");
PreparedStatement ps2 = getPreparedStatement("SELECT fname, lname FROM employees where hireDate > ? and salary < ?");
ps2.setDouble(1, 0.0); // Compliant
int a;
a = 2;
int[] b = new int[1];
b[0] = 3;
}
void false_negative(boolean test) throws SQLException {
PreparedStatement ps;
if (test) {
ps = getPreparedStatement("SELECT fname, lname FROM employees where hireDate > ?");
} else {
ps = getPreparedStatement("SELECT fname, lname FROM employees where hireDate > ? and salary < ?");
}
ps.setDouble(1, 0.0); // Compliant - last assignment is used
ps.setDouble(2, 0.0); // Compliant FALSE NEGATIVE - in then would have been applied, there would be no 2nd parameter (CFG?)
}
public void reproducer(Connection connection) throws SQLException {
String selectClause = ";";
selectClause = "SELECT anything FROM somewhere "
+ selectClause;
PreparedStatement statement = connection.prepareStatement(selectClause);
statement.setString(1, "anything"); // Noncompliant {{This "PreparedStatement" has no parameters.}}
}
public void updateCoffeeSales(HashMap<String, Integer> salesForWeek, Connection con, String param) throws SQLException {
String dbName = "doug";
PreparedStatement updateSales = null;
PreparedStatement updateTotal = null;
PreparedStatement other = null;
String updateString = "update " + dbName + ".COFFEES set SALES = ?";
String updateStatement =
"update " + dbName + ".COFFEES " +
"set TOTAL = TOTAL + ? " +
"where COF_NAME = ?";
try {
PreparedStatement ps = con.prepareStatement(updateStatement);
ps.setInt(1, 1); // Compliant
ps.setString(3, "three"); // Noncompliant
ps.setString(72, "boom"); // Noncompliant
ps = con.prepareStatement(updateStatement);
ps.setInt(1, 2); // Compliant
ps.setString(2, "three"); // Compliant
updateSales = con.prepareStatement(updateString);
updateTotal = con.prepareStatement(updateStatement);
other = con.prepareStatement("update " + dbName + ".COFFEES set SALES = ?");
for (Map.Entry<String, Integer> e : salesForWeek.entrySet()) {
updateSales.setInt(1, e.getValue().intValue()); // Compliant
updateSales.setString(2, e.getKey()); // Noncompliant
updateTotal.setInt(1, e.getValue().intValue()); // Compliant
updateTotal.setString(2, e.getKey()); // Compliant
other.setInt(2, getIntValue()); // Noncompliant
}
updateString = "update " + param + ".COFFEES set SALES = ?";
PreparedStatement testParam = con.prepareStatement(updateString);
testParam.setInt(3, 0); // Compliant - "param" is unknown and may itself contain placeholders
testParam = con.prepareStatement(param + " update");
testParam.setInt(3, 0); // Compliant - "param" is unknown and may itself contain placeholders
testParam = con.prepareStatement(param + param);
testParam.setInt(3, 0); // Compliant
String[] array = new String[]{""};
PreparedStatement qix = con.prepareStatement(array[0]);
qix.setString(3, ""); // Compliant
} catch(SQLException e) {
}
}
void fromConstant(Connection con) throws SQLException {
String sql = "select ... from ... where job_id = ?";
PreparedStatement ps = con.prepareStatement(sql);
ps.setInt(INDEX_42, 1); // Noncompliant
ps.setInt(INDEX_1, 1); // Compliant
}
private class IndirectInititalization {
Connection conn;
void foo() {
PreparedStatement ps = null;
try {
ps = prepareInsertRequest("SomeTable", 2);
ps.setString(1, "someValue");
ps.setInt(2, 4);
} catch (SQLException e) {
e.printStackTrace();
}
}
public PreparedStatement prepareInsertRequest(String tableName, int argsNum) throws SQLException {
return conn.prepareStatement("INSERT INTO " + tableName + " VALUES (?" + Strings.repeat(",?", argsNum - 1) + ")");
}
}
void plusEqualWithConstant(Connection conn) throws SQLException {
String q = "SELECT * FROM table WHERE name=?";
q += " AND c IS NOT NULL";
PreparedStatement ps = conn.prepareStatement(q);
ps.setString(1, "someValue"); // compliant, += with constant.
}
void plusEqualWithConstant2(Connection conn) throws SQLException {
String sql = "update User set";
sql += " user_password=?,";
sql += " user_name=?,";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, "someValue"); // compliant, += with constant.
ps.setString(2, "someValue"); // compliant, += with constant.
ps.setString(3, "someValue"); // Noncompliant
}
void plusEqualWithConstant3(Connection conn) throws SQLException {
String q = "SELECT * FROM table WHERE name=?";
q += q;
PreparedStatement ps = conn.prepareStatement(q);
ps.setString(2, "someValue"); // compliant, += with constant.
ps.setString(3, "someValue"); // Noncompliant
}
void concatenation(Connection conn) throws SQLException {
String sql = "update User set";
String sql2 = " user_password=?,";
String sql3 = " user_name=?,";
PreparedStatement ps = conn.prepareStatement(sql + sql2 + sql3);
ps.setString(1, "someValue"); // compliant, += with constant.
ps.setString(2, "someValue"); // compliant
ps.setString(3, "someValue"); // Noncompliant
}
void concatenation2(Connection con, String password, String name, int position) throws Exception {
String sql = " user_password=?,";
sql += " user_name=?,";
sql += " user_position=?,";
sql = "update User set" + sql + " WHERE user_id=?";
PreparedStatement statement = con.prepareStatement(sql);
statement.setString(1, password);
statement.setString(2, name); // compliant
statement.setInt(3, position); // compliant
statement.setInt(4, position); // compliant
statement.setInt(5, position); // Noncompliant
// ...
}
void concatenation3(Connection con, String password, String name, int position) throws Exception {
String firstPart = " user_password=?,";
firstPart += " user_name=?,";
String secondPart = " user_position=?,";
String bothParts = firstPart + secondPart;
PreparedStatement statement = con.prepareStatement(bothParts);
statement.setString(3, password); // compliant
statement.setString(4, password); // Noncompliant
}
static class SqlConstants {
static final String SELECT = "SELECT * FROM t WHERE a = ?";
static final String AND_CLAUSE = " AND b = ?";
}
private static final String COUNT_WRAPPER_BEGIN = "select count(*) from (";
private static final String COUNT_WRAPPER_END = ")";
void unknownMethodCallInConcatenation(Connection con) throws SQLException {
String sql = getQuery();
sql = COUNT_WRAPPER_BEGIN + sql + COUNT_WRAPPER_END;
PreparedStatement ps = con.prepareStatement(sql);
ps.setInt(1, 1); // Compliant - part of the query is unknown
}
void unknownMethodCallInPlusAssignment(Connection con) throws SQLException {
String sql = "select * from t where ";
sql += getQuery();
PreparedStatement ps = con.prepareStatement(sql);
ps.setInt(1, 1); // Compliant - part of the query is unknown
}
void ternaryInPlusAssignment(Connection con, boolean isRecall) throws SQLException {
String sql = "SELECT * FROM t WHERE ";
sql += isRecall ? "LENDER_REF_ID = ? AND SENDER_ID = ?" : "BORROWER_REF_ID = ? AND RECIPIENT_ID = ?";
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, "a"); // Compliant
ps.setLong(2, 1L); // Compliant
ps.setLong(3, 1L); // Noncompliant
}
void ternaryWithDifferentBranches(Connection con, boolean withImage) throws SQLException {
String sql = "INSERT INTO Product (a, b" + (withImage ? ", c" : "") + ") VALUES (?, ?" + (withImage ? ", ?" : "") + ")";
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, "a"); // Compliant
ps.setString(2, "b"); // Compliant
ps.setString(3, "c"); // Compliant - the third parameter exists when the condition holds
ps.setString(4, "d"); // Noncompliant
}
void ternaryWithNonConstantFragment(Connection con, String tenant, int maxTurns) throws SQLException {
String sql = "SELECT * FROM m WHERE m.tenant = ? AND m.session_id = ? "
+ (maxTurns > 0 ? "AND t.id IN (SELECT id FROM t WHERE tenant=? AND session=? LIMIT " + maxTurns + ") " : "")
+ "ORDER BY m.seq";
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, tenant); // Compliant
if (maxTurns > 0) {
ps.setString(3, tenant); // Compliant
ps.setString(4, tenant); // Compliant
}
// The whole query resolves: the int fragment contributes no placeholder and the branch taken contributes two.
ps.setString(5, tenant); // Noncompliant {{This "PreparedStatement" only has 4 parameters.}}
}
void ternaryWithUnresolvableBranch(Connection con, boolean cond) throws SQLException {
PreparedStatement ps = con.prepareStatement("SELECT * FROM t WHERE a = ? " + (cond ? getQuery() : ""));
ps.setString(5, "x"); // Compliant - one branch of the conditional is unknown, so the count is too
}
void assignedInBothBranches(Connection con, boolean hasPassword) throws SQLException {
String sql;
if (hasPassword) {
sql = "UPDATE Account SET email = ?, name = ?, password = ? WHERE id = ?";
} else {
sql = "UPDATE Account SET email = ?, name = ? WHERE id = ?";
}
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, "a"); // Compliant
if (hasPassword) {
// FALSE POSITIVE: only the closest assignment is resolved, while the branch taken here has 4 parameters.
// Can't be fixed without dataflow analysis.
ps.setString(4, "b"); // Noncompliant
}
}
void nonConstantNumericFragment(Connection con, int limit, Integer offset) throws SQLException {
String sql = "SELECT * FROM t WHERE id = ? LIMIT " + limit + " OFFSET " + offset;
PreparedStatement ps = con.prepareStatement(sql);
ps.setInt(1, 1); // Compliant
// A number concatenated into the query can not add a placeholder, so the count stays exact.
ps.setInt(2, 2); // Noncompliant
}
void nonConstantNumericFragmentFromMethod(Connection con) throws SQLException {
PreparedStatement ps = con.prepareStatement("SELECT * FROM t WHERE id = ? LIMIT " + getIntValue());
ps.setInt(2, 2); // Noncompliant
}
void boxedCharFragment(Connection con, Character separator) throws SQLException {
PreparedStatement ps = con.prepareStatement("SELECT * FROM t WHERE id = ? AND flag = " + separator);
ps.setInt(2, 2); // Compliant - the character may be '?'
ps.setInt(3, 2); // Noncompliant {{This "PreparedStatement" only has 2 parameters.}}
}
void nonConstantCharFragment(Connection con, char separator) throws SQLException {
PreparedStatement ps = con.prepareStatement("SELECT * FROM t WHERE id = ? AND flag = " + separator);
ps.setInt(2, 2); // Compliant - the character may be '?'
// A character adds at most one placeholder, so anything past that is still reportable.
ps.setInt(3, 2); // Noncompliant {{This "PreparedStatement" only has 2 parameters.}}
}
private static final char QUESTION_MARK = '?';
void charConstantFragment(Connection con) throws SQLException {
PreparedStatement ps = con.prepareStatement("SELECT * FROM t WHERE id = " + QUESTION_MARK);
ps.setInt(1, 1); // Compliant
}
void charLiteralIsThePlaceholder(Connection con) throws SQLException {
PreparedStatement ps = con.prepareStatement("SELECT * FROM t WHERE id = " + '?');
ps.setInt(1, 1); // Compliant
ps.setInt(2, 2); // Noncompliant {{This "PreparedStatement" only has 1 parameters.}}
}
void charLiteralIsNotAPlaceholder(Connection con) throws SQLException {
PreparedStatement ps = con.prepareStatement("SELECT * FROM t WHERE id = ? AND flag = " + 'y');
ps.setInt(1, 1); // Compliant
ps.setInt(2, 2); // Noncompliant {{This "PreparedStatement" only has 1 parameters.}}
}
void charLocalVariableFragment(Connection con) throws SQLException {
// the char-typed fragment is bounded at one placeholder
final char questionMark = '?';
PreparedStatement ps = con.prepareStatement("SELECT * FROM t WHERE id = " + questionMark);
ps.setInt(1, 1); // Compliant
ps.setInt(2, 2); // Noncompliant {{This "PreparedStatement" only has 1 parameters.}}
}
void constantsFromAnotherClass(Connection con) throws SQLException {
String sql = SqlConstants.SELECT;
sql += SqlConstants.AND_CLAUSE;
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, "a"); // Compliant
ps.setString(2, "b"); // Compliant
ps.setString(3, "c"); // Noncompliant
}
}