-
Notifications
You must be signed in to change notification settings - Fork 521
Expand file tree
/
Copy pathtainted-sql-string.java
More file actions
222 lines (197 loc) · 8.24 KB
/
tainted-sql-string.java
File metadata and controls
222 lines (197 loc) · 8.24 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
package com.r2c.tests;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.boot.autoconfigure.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
@RestController
@EnableAutoConfiguration
public class TestController {
private static final Logger LOGGER = LoggerFactory.getLogger(TestController.class);
@RequestMapping(value = "/test1", method = RequestMethod.POST, produces = "plain/text")
ResultSet test1(@RequestBody String name) {
// ruleid: tainted-sql-string
String sql = "SELECT * FROM table WHERE name = " + name + ";";
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:8080", "guest", "password");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.execute(sql);
return rs;
}
@RequestMapping(value = "/test2", method = RequestMethod.POST, produces = "plain/text")
ResultSet test2(@RequestBody String name) {
// ruleid: tainted-sql-string
String sql = String.format("SELECT * FROM table WHERE name = %s;", name);
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:8080", "guest", "password");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.execute(sql);
return rs;
}
@RequestMapping(value = "/test3", method = RequestMethod.POST, produces = "plain/text")
ResultSet test3(@RequestBody String name) {
String sql = "SELECT * FROM table WHERE name = ";
// ruleid: tainted-sql-string
sql.concat(name + ";");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:8080", "guest", "password");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.execute(sql);
return rs;
}
@RequestMapping(value = "/test4", method = RequestMethod.POST, produces = "plain/text")
ResultSet test4(@RequestBody String name) {
StringBuilder sql = new StringBuilder("SELECT * FROM table WHERE name = ");
// ruleid: tainted-sql-string
sql.append(name + ";");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:8080", "guest", "password");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.execute(sql.toString());
return rs;
}
@RequestMapping(value = "/test5", method = RequestMethod.POST, produces = "plain/text")
ResultSet test5(@RequestBody String name) {
String sql = "SELECT * FROM table WHERE name = ";
// ruleid: tainted-sql-string
sql += name + ";";
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:8080", "guest", "password");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.execute(sql);
return rs;
}
@RequestMapping(value = "/test5ok", method = RequestMethod.POST, produces = "plain/text")
ResultSet test5ok(@RequestBody String name) {
try {
// ok: tainted-sql-string
throw new Exception(String.format("Update request from %s to %s isn't allowed",
name, bar
));
}
catch (NullPointerException e) {
System.out.println("Caught inside fun().");
throw e; // rethrowing the exception
}
}
@RequestMapping(value = "/ok1", method = RequestMethod.POST, produces = "plain/text")
ResultSet ok1(@RequestBody String name) {
// ok: tainted-sql-string
String sql = "SELECT * FROM table WHERE name = 'everyone';";
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:8080", "guest", "password");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.execute(sql);
return rs;
}
@RequestMapping(value = "/ok2", method = RequestMethod.POST, produces = "plain/text")
ResultSet ok2(@RequestBody String name) {
String sql = "SELECT * FROM table WHERE name = 'everyone';";
// ok: tainted-sql-string
System.out.println(String.format("Got request from %s", name));
// ok: tainted-sql-string
System.out.println("select noise for tests using tainted name:" + name);
// ok: tainted-sql-string
Logger.debug("Create noise for tests using tainted name:" + name);
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:8080", "guest", "password");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.execute(sql);
return rs;
}
@RequestMapping(value = "/testok3", method = RequestMethod.POST, produces = "plain/text")
ResultSet ok3(@RequestBody Integer name) {
String sql = "SELECT * FROM table WHERE name = ";
// ok: tainted-sql-string
sql += name + ";";
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:8080", "guest", "password");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.execute(sql);
return rs;
}
@RequestMapping(value = "/testok4", method = RequestMethod.POST, produces = "plain/text")
ResultSet ok4(@RequestBody Boolean name) {
String sql = "SELECT * FROM table WHERE name = ";
// ok: tainted-sql-string
sql += name + ";";
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:8080", "guest", "password");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.execute(sql);
return rs;
}
@RequestMapping(value = "/testok5", method = RequestMethod.POST, produces = "plain/text")
ResultSet ok5(@RequestBody String name) {
String sql = "SELECT * FROM table WHERE name = ";
// ok: tainted-sql-string
sql += (name.substring(2,3) != "hello".substring(2,3)) + ";";
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:8080", "guest", "password");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.execute(sql);
return rs;
}
@RequestMapping(value = "/testok6", method = RequestMethod.POST, produces = "plain/text")
ResultSet ok6(@RequestBody String name) {
String sql = "SELECT * FROM table WHERE name = ";
// ok: tainted-sql-string
sql += ("hello".substring(2,3) == name.substring(2,3)) + ";";
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:8080", "guest", "password");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.execute(sql);
return rs;
}
}
class Bar {
int x;
public int getX() {
return x;
}
}
class Foo {
List<Bar> bars;
public List<Bar> getBars(String name) {
return bars;
}
}
class Test {
@RequestMapping(value = "/testok6", method = RequestMethod.POST, produces = "plain/text")
public ResultSet ok7(@RequestBody String name, Foo foo) {
var v = foo.getBars(name).get(0).getX();
String sql = "SELECT * FROM table WHERE name = ";
// ruleid: deepok: tainted-sql-string
sql += v + ";";
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:8080", "guest", "password");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.execute(sql);
return rs;
}
}
@Getter
@Setter
public class SiteModel {
private List<PrefixSiteIds> prefixes;
public List<PrefixSiteIds> getPrefixes(String name) {
return prefixes;
}
}
@Getter
@Setter
public class PrefixSiteIds {
public SiteIds sites;
}
@Getter
@Setter
public class SiteIds {
public Set<Integer> ids = new HashSet<>();
}
class Test2 {
@RequestMapping(value = "/testok8", method = RequestMethod.POST, produces = "plain/text")
public ResultSet ok8(@RequestBody String name, SiteModel sitemodel) {
var v = sitemodel.getPrefixes(name).sites.ids.get(0);
String sql = "SELECT * FROM table WHERE name = ";
// ruleid: deepok: tainted-sql-string
sql += v + ";";
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:8080", "guest", "password");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.execute(sql);
return rs;
}
}