Skip to content

Commit fbbaa2e

Browse files
Add Java application with multiple vulnerability patterns and CodeQL workflow
Co-authored-by: mickeygousset <20031479+mickeygousset@users.noreply.github.com>
1 parent a12aebb commit fbbaa2e

File tree

18 files changed

+523
-0
lines changed

18 files changed

+523
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
name: "CodeQL Analysis"
2+
3+
on:
4+
push:
5+
branches: [ "main", "master" ]
6+
pull_request:
7+
branches: [ "main", "master" ]
8+
schedule:
9+
- cron: '15 2 * * 1' # Weekly on Mondays at 2:15 AM
10+
11+
jobs:
12+
analyze:
13+
name: Analyze Java Code
14+
runs-on: ubuntu-latest
15+
permissions:
16+
actions: read
17+
contents: read
18+
security-events: write
19+
20+
strategy:
21+
fail-fast: false
22+
matrix:
23+
language: [ 'java' ]
24+
25+
steps:
26+
- name: Checkout repository
27+
uses: actions/checkout@v4
28+
29+
- name: Set up JDK 11
30+
uses: actions/setup-java@v3
31+
with:
32+
java-version: '11'
33+
distribution: 'temurin'
34+
35+
- name: Initialize CodeQL
36+
uses: github/codeql-action/init@v2
37+
with:
38+
languages: ${{ matrix.language }}
39+
queries: +security-and-quality
40+
41+
# Autobuild attempts to build any compiled languages (Java, C#, Go, etc.)
42+
# If this step fails, remove it and run the build manually instead
43+
- name: Autobuild
44+
uses: github/codeql-action/autobuild@v2
45+
46+
- name: Perform CodeQL Analysis
47+
uses: github/codeql-action/analyze@v2
48+
with:
49+
category: "/language:${{matrix.language}}"

pom.xml

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
5+
http://maven.apache.org/xsd/maven-4.0.0.xsd">
6+
<modelVersion>4.0.0</modelVersion>
7+
8+
<groupId>com.example</groupId>
9+
<artifactId>vulnerable-app</artifactId>
10+
<version>1.0.0</version>
11+
<packaging>jar</packaging>
12+
13+
<name>Vulnerable Java Application</name>
14+
<description>A simple Java application with intentional vulnerabilities for CodeQL scanning demonstration</description>
15+
16+
<properties>
17+
<maven.compiler.source>11</maven.compiler.source>
18+
<maven.compiler.target>11</maven.compiler.target>
19+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20+
</properties>
21+
22+
<dependencies>
23+
<!-- Database connectivity for SQL injection demos -->
24+
<dependency>
25+
<groupId>mysql</groupId>
26+
<artifactId>mysql-connector-java</artifactId>
27+
<version>8.0.33</version>
28+
</dependency>
29+
30+
<!-- Web framework for HTTP vulnerabilities -->
31+
<dependency>
32+
<groupId>org.springframework</groupId>
33+
<artifactId>spring-web</artifactId>
34+
<version>5.3.21</version>
35+
</dependency>
36+
37+
<!-- JSON processing -->
38+
<dependency>
39+
<groupId>com.fasterxml.jackson.core</groupId>
40+
<artifactId>jackson-databind</artifactId>
41+
<version>2.13.3</version>
42+
</dependency>
43+
44+
<!-- Testing -->
45+
<dependency>
46+
<groupId>junit</groupId>
47+
<artifactId>junit</artifactId>
48+
<version>4.13.2</version>
49+
<scope>test</scope>
50+
</dependency>
51+
</dependencies>
52+
53+
<build>
54+
<plugins>
55+
<plugin>
56+
<groupId>org.apache.maven.plugins</groupId>
57+
<artifactId>maven-compiler-plugin</artifactId>
58+
<version>3.8.1</version>
59+
<configuration>
60+
<source>11</source>
61+
<target>11</target>
62+
</configuration>
63+
</plugin>
64+
65+
<plugin>
66+
<groupId>org.apache.maven.plugins</groupId>
67+
<artifactId>maven-surefire-plugin</artifactId>
68+
<version>3.0.0-M7</version>
69+
</plugin>
70+
</plugins>
71+
</build>
72+
</project>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.example.app;
2+
3+
import com.example.database.UserDatabase;
4+
import com.example.security.CryptoUtils;
5+
import com.example.web.FileController;
6+
7+
/**
8+
* Main application class demonstrating various Java vulnerabilities
9+
* that should be detected by CodeQL scanning.
10+
*/
11+
public class VulnerableApplication {
12+
13+
public static void main(String[] args) {
14+
System.out.println("Starting Vulnerable Application...");
15+
16+
// Demonstrate various vulnerable components
17+
UserDatabase userDb = new UserDatabase();
18+
CryptoUtils crypto = new CryptoUtils();
19+
FileController fileController = new FileController();
20+
21+
// Example usage that would trigger vulnerabilities
22+
String userInput = args.length > 0 ? args[0] : "admin";
23+
String password = args.length > 1 ? args[1] : "password123";
24+
25+
// SQL Injection vulnerability
26+
userDb.authenticateUser(userInput, password);
27+
28+
// Weak cryptography
29+
String token = crypto.generateToken();
30+
System.out.println("Generated token: " + token);
31+
32+
// Path traversal vulnerability
33+
String filename = args.length > 2 ? args[2] : "../../etc/passwd";
34+
fileController.readFile(filename);
35+
36+
System.out.println("Application completed.");
37+
}
38+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.example.database;
2+
3+
import java.sql.Connection;
4+
import java.sql.DriverManager;
5+
import java.sql.ResultSet;
6+
import java.sql.Statement;
7+
8+
/**
9+
* Database class with intentional SQL injection vulnerabilities
10+
* to demonstrate CodeQL detection capabilities.
11+
*/
12+
public class UserDatabase {
13+
14+
private static final String DB_URL = "jdbc:mysql://localhost:3306/testdb";
15+
private static final String DB_USER = "root";
16+
private static final String DB_PASSWORD = "password";
17+
18+
/**
19+
* VULNERABLE: SQL Injection vulnerability - user input directly concatenated
20+
* This should trigger a high/critical CodeQL alert
21+
*/
22+
public boolean authenticateUser(String username, String password) {
23+
try {
24+
Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
25+
Statement stmt = conn.createStatement();
26+
27+
// VULNERABILITY: Direct string concatenation leads to SQL injection
28+
String query = "SELECT * FROM users WHERE username = '" + username +
29+
"' AND password = '" + password + "'";
30+
31+
System.out.println("Executing query: " + query);
32+
ResultSet rs = stmt.executeQuery(query);
33+
34+
boolean authenticated = rs.next();
35+
36+
rs.close();
37+
stmt.close();
38+
conn.close();
39+
40+
return authenticated;
41+
42+
} catch (Exception e) {
43+
System.err.println("Database error: " + e.getMessage());
44+
return false;
45+
}
46+
}
47+
48+
/**
49+
* VULNERABLE: Another SQL injection point
50+
*/
51+
public void updateUserProfile(String userId, String email, String fullName) {
52+
try {
53+
Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
54+
Statement stmt = conn.createStatement();
55+
56+
// VULNERABILITY: String concatenation in UPDATE statement
57+
String updateQuery = "UPDATE users SET email = '" + email +
58+
"', full_name = '" + fullName +
59+
"' WHERE user_id = " + userId;
60+
61+
stmt.executeUpdate(updateQuery);
62+
63+
stmt.close();
64+
conn.close();
65+
66+
} catch (Exception e) {
67+
System.err.println("Update failed: " + e.getMessage());
68+
}
69+
}
70+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.example.security;
2+
3+
import java.util.Random;
4+
import java.security.MessageDigest;
5+
6+
/**
7+
* Security utilities with intentional cryptographic vulnerabilities
8+
* to demonstrate CodeQL detection capabilities.
9+
*/
10+
public class CryptoUtils {
11+
12+
// VULNERABLE: Using weak random number generator
13+
private static final Random random = new Random();
14+
15+
/**
16+
* VULNERABLE: Uses weak random number generation for security tokens
17+
* This should trigger a CodeQL alert for insecure randomness
18+
*/
19+
public String generateToken() {
20+
StringBuilder token = new StringBuilder();
21+
22+
// VULNERABILITY: Using java.util.Random for security-sensitive operations
23+
for (int i = 0; i < 32; i++) {
24+
int randomChar = random.nextInt(36);
25+
if (randomChar < 10) {
26+
token.append((char) ('0' + randomChar));
27+
} else {
28+
token.append((char) ('a' + randomChar - 10));
29+
}
30+
}
31+
32+
return token.toString();
33+
}
34+
35+
/**
36+
* VULNERABLE: Uses weak random for session IDs
37+
*/
38+
public String generateSessionId() {
39+
// VULNERABILITY: Predictable session ID generation
40+
long sessionId = System.currentTimeMillis() + random.nextInt(1000);
41+
return Long.toString(sessionId);
42+
}
43+
44+
/**
45+
* VULNERABLE: Weak hash function usage
46+
*/
47+
public String hashPassword(String password) {
48+
try {
49+
// VULNERABILITY: Using MD5 for password hashing (weak algorithm)
50+
MessageDigest md = MessageDigest.getInstance("MD5");
51+
byte[] hash = md.digest(password.getBytes());
52+
53+
StringBuilder hexString = new StringBuilder();
54+
for (byte b : hash) {
55+
String hex = Integer.toHexString(0xff & b);
56+
if (hex.length() == 1) {
57+
hexString.append('0');
58+
}
59+
hexString.append(hex);
60+
}
61+
62+
return hexString.toString();
63+
64+
} catch (Exception e) {
65+
System.err.println("Hashing failed: " + e.getMessage());
66+
return password; // VULNERABILITY: Fallback to plaintext
67+
}
68+
}
69+
70+
/**
71+
* VULNERABLE: Hard-coded encryption key
72+
*/
73+
public String getEncryptionKey() {
74+
// VULNERABILITY: Hard-coded secret key
75+
return "MySecretKey123456789";
76+
}
77+
}

0 commit comments

Comments
 (0)