|
14 | 14 | import java.util.HashSet; |
15 | 15 | import java.util.List; |
16 | 16 | import java.util.Set; |
| 17 | +import java.util.function.Consumer; |
17 | 18 |
|
18 | 19 | class AllowListSanitizerWithJavaUtilList { |
19 | 20 | public static Connection connection; |
@@ -51,6 +52,7 @@ public static void main(String[] args) throws IOException, SQLException { |
51 | 52 | var x = new AllowListSanitizerWithJavaUtilList(); |
52 | 53 | x.testNonStaticFields(args); |
53 | 54 | testMultipleSources(args); |
| 55 | + testEscape(args); |
54 | 56 | } |
55 | 57 |
|
56 | 58 | private static void testStaticFields(String[] args) throws IOException, SQLException { |
@@ -229,11 +231,11 @@ private static void testLocal(String[] args) throws IOException, SQLException { |
229 | 231 | ResultSet results = connection.createStatement().executeQuery(query); |
230 | 232 | } |
231 | 233 | } |
232 | | - // BAD: an allowlist is used but it may contain a non-compile-time constant element |
| 234 | + // BAD: an allowlist is used but it contains a non-compile-time constant element |
233 | 235 | { |
234 | 236 | List<String> allowlist = new ArrayList<String>(); |
235 | 237 | allowlist.add("allowed1"); |
236 | | - possiblyMutate(allowlist); |
| 238 | + addNonConstantStringDirectly(allowlist); |
237 | 239 | if(allowlist.contains(tainted)){ |
238 | 240 | String query = "SELECT ITEM,PRICE FROM PRODUCT WHERE ITEM_CATEGORY='" |
239 | 241 | + tainted + "' ORDER BY PRICE"; |
@@ -278,8 +280,27 @@ private static void testMultipleSources(String[] args) throws IOException, SQLEx |
278 | 280 | } |
279 | 281 | } |
280 | 282 |
|
281 | | - private static void possiblyMutate(List<String> list) { |
| 283 | + private static void testEscape(String[] args) throws IOException, SQLException { |
| 284 | + String tainted = args[1]; |
| 285 | + boolean b = args[2] == "True"; |
| 286 | + { |
| 287 | + // BAD: an allowlist is used which contains constant strings |
| 288 | + List<String> allowlist = new ArrayList<String>(); |
| 289 | + addNonConstantStringViaLambda(e -> allowlist.add(e)); |
| 290 | + if(allowlist.contains(tainted)){ // missing result |
| 291 | + String query = "SELECT ITEM,PRICE FROM PRODUCT WHERE ITEM_CATEGORY='" |
| 292 | + + tainted + "' ORDER BY PRICE"; |
| 293 | + ResultSet results = connection.createStatement().executeQuery(query); |
| 294 | + } |
| 295 | + } |
| 296 | + } |
| 297 | + |
| 298 | + private static void addNonConstantStringDirectly(List<String> list) { |
282 | 299 | list.add(getNonConstantString()); |
283 | 300 | } |
284 | 301 |
|
| 302 | + private static void addNonConstantStringViaLambda(Consumer<String> adder) { |
| 303 | + adder.accept(getNonConstantString()); |
| 304 | + } |
| 305 | + |
285 | 306 | } |
0 commit comments