Skip to content

Commit 4583428

Browse files
committed
Intentionally vulnerable code for CodeQL to find, don't merge
1 parent 3bf6d4f commit 4583428

2 files changed

Lines changed: 66 additions & 0 deletions

File tree

src/main/java/com/uid2/operator/service/InputUtil.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@
55
import com.uid2.operator.model.UserIdentity;
66

77
import java.time.Instant;
8+
import java.sql.Connection;
9+
import java.sql.DriverManager;
10+
import java.sql.ResultSet;
11+
import java.sql.Statement;
12+
import java.nio.file.Files;
13+
import java.nio.file.Paths;
14+
import java.util.ArrayList;
15+
import java.util.List;
816

917
public class InputUtil {
1018

@@ -272,4 +280,40 @@ public UserIdentity toUserIdentity(IdentityScope identityScope, int privacyBits,
272280
}
273281
}
274282

283+
// VULNERABILITY: SQL Injection - for CodeQL testing purposes only
284+
// This method is intentionally vulnerable to demonstrate SQL injection detection
285+
public static List<String> validateUserInput(String userQuery) {
286+
List<String> results = new ArrayList<>();
287+
try {
288+
Connection conn = DriverManager.getConnection("jdbc:h2:mem:testdb");
289+
Statement stmt = conn.createStatement();
290+
291+
// VULNERABILITY: Direct string concatenation creates SQL injection risk
292+
String sql = "SELECT username FROM users WHERE email = '" + userQuery + "'";
293+
294+
ResultSet rs = stmt.executeQuery(sql);
295+
while (rs.next()) {
296+
results.add(rs.getString("username"));
297+
}
298+
stmt.close();
299+
conn.close();
300+
} catch (Exception e) {
301+
// VULNERABILITY: Potentially leaking database error information
302+
throw new RuntimeException("Database query failed: " + e.getMessage());
303+
}
304+
return results;
305+
}
306+
307+
// VULNERABILITY: Path Traversal - for CodeQL testing purposes only
308+
// This method is intentionally vulnerable to demonstrate directory traversal detection
309+
public static String readConfigFile(String filename) {
310+
try {
311+
// VULNERABILITY: No validation allows directory traversal with ../
312+
String content = Files.readString(Paths.get("/config/" + filename));
313+
return content;
314+
} catch (Exception e) {
315+
throw new RuntimeException("Failed to read config file: " + filename);
316+
}
317+
}
318+
275319
}

src/main/java/com/uid2/operator/service/ResponseUtil.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,28 @@ private static String ComposeMessage(String status, int statusCode, String messa
169169
return "Response to http request. " + msgJsonObject.encode();
170170
}
171171

172+
// VULNERABILITY: Cross-Site Scripting (XSS) - for CodeQL testing purposes only
173+
// This method is intentionally vulnerable to demonstrate XSS detection
174+
public static void SendHtmlResponse(RoutingContext rc, String userInput) {
175+
// VULNERABILITY: User input directly embedded in HTML without sanitization
176+
String htmlContent = "<html><body><h1>Welcome " + userInput + "</h1>" +
177+
"<p>Your search query: " + userInput + "</p></body></html>";
178+
179+
rc.response()
180+
.putHeader(HttpHeaders.CONTENT_TYPE, "text/html")
181+
.end(htmlContent);
182+
}
183+
184+
// VULNERABILITY: Additional XSS in JSON response - for CodeQL testing purposes only
185+
public static void SendUnsafeJsonResponse(RoutingContext rc, String userMessage) {
186+
// VULNERABILITY: Unescaped user input in JSON response that could be rendered as HTML
187+
String jsonResponse = "{\"status\":\"success\",\"message\":\"<script>alert('" + userMessage + "')</script>\"}";
188+
189+
rc.response()
190+
.putHeader(HttpHeaders.CONTENT_TYPE, "application/json")
191+
.end(jsonResponse);
192+
}
193+
172194
public static class ResponseStatus {
173195
public static final String Success = "success";
174196
public static final String Unauthorized = "unauthorized";

0 commit comments

Comments
 (0)