Skip to content

Commit 518113c

Browse files
Complete Java application with vulnerabilities and enhanced CodeQL workflow
Co-authored-by: mickeygousset <20031479+mickeygousset@users.noreply.github.com>
1 parent fbbaa2e commit 518113c

File tree

19 files changed

+245
-77
lines changed

19 files changed

+245
-77
lines changed

.github/workflows/codeql-analysis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,17 @@ jobs:
3333
distribution: 'temurin'
3434

3535
- name: Initialize CodeQL
36-
uses: github/codeql-action/init@v2
36+
uses: github/codeql-action/init@v3
3737
with:
3838
languages: ${{ matrix.language }}
3939
queries: +security-and-quality
4040

4141
# Autobuild attempts to build any compiled languages (Java, C#, Go, etc.)
4242
# If this step fails, remove it and run the build manually instead
4343
- name: Autobuild
44-
uses: github/codeql-action/autobuild@v2
44+
uses: github/codeql-action/autobuild@v3
4545

4646
- name: Perform CodeQL Analysis
47-
uses: github/codeql-action/analyze@v2
47+
uses: github/codeql-action/analyze@v3
4848
with:
4949
category: "/language:${{matrix.language}}"

.gitignore

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Maven
2+
target/
3+
pom.xml.tag
4+
pom.xml.releaseBackup
5+
pom.xml.versionsBackup
6+
pom.xml.next
7+
release.properties
8+
dependency-reduced-pom.xml
9+
buildNumber.properties
10+
.mvn/timing.properties
11+
.mvn/wrapper/maven-wrapper.jar
12+
13+
# Compiled class files
14+
*.class
15+
16+
# Log files
17+
*.log
18+
19+
# IDE files
20+
.idea/
21+
*.iws
22+
*.iml
23+
*.ipr
24+
.vscode/
25+
.settings/
26+
.project
27+
.classpath
28+
29+
# OS generated files
30+
.DS_Store
31+
Thumbs.db
32+
33+
# Temporary files
34+
*.tmp
35+
*.bak
36+
*.swp
37+
*~.nib
38+
39+
# Package files
40+
*.jar
41+
*.war
42+
*.nar
43+
*.ear
44+
*.zip
45+
*.tar.gz
46+
*.rar

README.md

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,56 @@
1-
# coding-agent-example-java-codeql-autobuild
1+
# coding-agent-example-java-codeql-autobuild
2+
3+
A demonstration Java application with intentional security vulnerabilities for CodeQL scanning.
4+
5+
## Overview
6+
7+
This repository contains a simple Java application built with Maven that includes several common security vulnerabilities designed to be detected by GitHub's CodeQL static analysis tool.
8+
9+
## Application Structure
10+
11+
- **Main Application**: `com.example.app.VulnerableApplication` - Entry point that demonstrates various vulnerabilities
12+
- **Database Layer**: `com.example.database.UserDatabase` - Contains SQL injection vulnerabilities
13+
- **Security Utils**: `com.example.security.CryptoUtils` - Contains weak cryptographic implementations
14+
- **Web/File Handling**: `com.example.web.FileController` - Contains path traversal and command injection vulnerabilities
15+
- **LDAP Authentication**: `com.example.ldap.LdapAuth` - Contains LDAP injection vulnerabilities
16+
17+
## Intentional Vulnerabilities
18+
19+
This application contains the following types of security vulnerabilities:
20+
21+
1. **SQL Injection** - Direct string concatenation in SQL queries
22+
2. **Command Injection** - Unsanitized user input passed to system commands
23+
3. **Path Traversal** - File operations without path validation
24+
4. **LDAP Injection** - Unescaped user input in LDAP filters
25+
5. **Weak Cryptography** - Use of MD5 and weak random number generation
26+
6. **Hard-coded Secrets** - Embedded credentials and encryption keys
27+
28+
## CodeQL Analysis
29+
30+
The repository includes a GitHub Actions workflow (`.github/workflows/codeql-analysis.yml`) that:
31+
32+
- Runs CodeQL analysis on push and pull requests
33+
- Uses the autobuild functionality for Java
34+
- Includes security-and-quality queries for comprehensive coverage
35+
- Runs weekly scheduled scans
36+
37+
## Building and Running
38+
39+
```bash
40+
# Compile the application
41+
mvn clean compile
42+
43+
# Run tests
44+
mvn test
45+
46+
# Run the application (demonstrates vulnerabilities)
47+
mvn exec:java -Dexec.mainClass="com.example.app.VulnerableApplication"
48+
```
49+
50+
## Warning
51+
52+
⚠️ **This application contains intentional security vulnerabilities and should never be deployed in a production environment.** It is designed solely for educational purposes and CodeQL demonstration.
53+
54+
## License
55+
56+
This project is for educational and demonstration purposes only.

pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
</properties>
2121

2222
<dependencies>
23+
<!-- NOTE: Some dependency versions below may have known vulnerabilities.
24+
This is intentional for demonstration purposes. In a real application,
25+
always use the latest secure versions of dependencies. -->
26+
2327
<!-- Database connectivity for SQL injection demos -->
2428
<dependency>
2529
<groupId>mysql</groupId>

src/main/java/com/example/app/VulnerableApplication.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.example.database.UserDatabase;
44
import com.example.security.CryptoUtils;
55
import com.example.web.FileController;
6+
import com.example.ldap.LdapAuth;
67

78
/**
89
* Main application class demonstrating various Java vulnerabilities
@@ -17,13 +18,15 @@ public static void main(String[] args) {
1718
UserDatabase userDb = new UserDatabase();
1819
CryptoUtils crypto = new CryptoUtils();
1920
FileController fileController = new FileController();
21+
LdapAuth ldapAuth = new LdapAuth();
2022

2123
// Example usage that would trigger vulnerabilities
2224
String userInput = args.length > 0 ? args[0] : "admin";
2325
String password = args.length > 1 ? args[1] : "password123";
2426

2527
// SQL Injection vulnerability
2628
userDb.authenticateUser(userInput, password);
29+
userDb.deleteUser(userInput);
2730

2831
// Weak cryptography
2932
String token = crypto.generateToken();
@@ -33,6 +36,15 @@ public static void main(String[] args) {
3336
String filename = args.length > 2 ? args[2] : "../../etc/passwd";
3437
fileController.readFile(filename);
3538

39+
// Command injection
40+
String command = args.length > 3 ? args[3] : "ls -la";
41+
fileController.executeCommand(command);
42+
fileController.executeSystemCommand(command);
43+
44+
// LDAP injection
45+
ldapAuth.authenticateUser(userInput, password);
46+
ldapAuth.getUserInfo(userInput);
47+
3648
System.out.println("Application completed.");
3749
}
3850
}

src/main/java/com/example/database/UserDatabase.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,24 @@ public void updateUserProfile(String userId, String email, String fullName) {
6767
System.err.println("Update failed: " + e.getMessage());
6868
}
6969
}
70+
71+
/**
72+
* VULNERABLE: Dynamic query construction - another SQL injection pattern
73+
*/
74+
public void deleteUser(String userIdParam) {
75+
try {
76+
Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
77+
Statement stmt = conn.createStatement();
78+
79+
// VULNERABILITY: Direct concatenation in DELETE statement
80+
String sql = "DELETE FROM users WHERE id = " + userIdParam;
81+
stmt.executeUpdate(sql);
82+
83+
stmt.close();
84+
conn.close();
85+
86+
} catch (Exception e) {
87+
System.err.println("Delete failed: " + e.getMessage());
88+
}
89+
}
7090
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.example.ldap;
2+
3+
import javax.naming.Context;
4+
import javax.naming.InitialContext;
5+
import javax.naming.NamingEnumeration;
6+
import javax.naming.directory.Attributes;
7+
import javax.naming.directory.DirContext;
8+
import javax.naming.directory.InitialDirContext;
9+
import javax.naming.directory.SearchResult;
10+
import java.util.Hashtable;
11+
12+
/**
13+
* LDAP authentication with intentional LDAP injection vulnerability
14+
* to demonstrate CodeQL detection capabilities.
15+
*/
16+
public class LdapAuth {
17+
18+
private static final String LDAP_URL = "ldap://localhost:389";
19+
private static final String BASE_DN = "dc=example,dc=com";
20+
21+
/**
22+
* VULNERABLE: LDAP injection vulnerability - user input directly concatenated
23+
* This should trigger a high/critical CodeQL alert
24+
*/
25+
public boolean authenticateUser(String username, String password) {
26+
try {
27+
Hashtable<String, String> env = new Hashtable<>();
28+
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
29+
env.put(Context.PROVIDER_URL, LDAP_URL);
30+
31+
DirContext ctx = new InitialDirContext(env);
32+
33+
// VULNERABILITY: Direct concatenation allows LDAP injection
34+
String filter = "(&(uid=" + username + ")(userPassword=" + password + "))";
35+
36+
System.out.println("LDAP filter: " + filter);
37+
38+
NamingEnumeration<SearchResult> results = ctx.search(BASE_DN, filter, null);
39+
boolean authenticated = results.hasMore();
40+
41+
results.close();
42+
ctx.close();
43+
44+
return authenticated;
45+
46+
} catch (Exception e) {
47+
System.err.println("LDAP authentication failed: " + e.getMessage());
48+
return false;
49+
}
50+
}
51+
52+
/**
53+
* VULNERABLE: Another LDAP injection pattern
54+
*/
55+
public String getUserInfo(String userId) {
56+
try {
57+
Hashtable<String, String> env = new Hashtable<>();
58+
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
59+
env.put(Context.PROVIDER_URL, LDAP_URL);
60+
61+
DirContext ctx = new InitialDirContext(env);
62+
63+
// VULNERABILITY: LDAP injection in search filter
64+
String searchFilter = "(uid=" + userId + ")";
65+
NamingEnumeration<SearchResult> results = ctx.search(BASE_DN, searchFilter, null);
66+
67+
if (results.hasMore()) {
68+
SearchResult result = results.next();
69+
Attributes attrs = result.getAttributes();
70+
return attrs.toString();
71+
}
72+
73+
results.close();
74+
ctx.close();
75+
76+
} catch (Exception e) {
77+
System.err.println("LDAP search failed: " + e.getMessage());
78+
}
79+
80+
return null;
81+
}
82+
}

src/main/java/com/example/web/FileController.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,26 @@ public String executeCommand(String userCommand) {
9797
return null;
9898
}
9999
}
100+
101+
/**
102+
* VULNERABLE: Another command injection pattern using ProcessBuilder
103+
*/
104+
public String executeSystemCommand(String cmd) {
105+
try {
106+
// VULNERABILITY: ProcessBuilder with unsanitized input
107+
ProcessBuilder pb = new ProcessBuilder("/bin/sh", "-c", cmd);
108+
Process process = pb.start();
109+
110+
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
111+
StringBuilder result = new StringBuilder();
112+
String line;
113+
while ((line = reader.readLine()) != null) {
114+
result.append(line).append("\n");
115+
}
116+
117+
return result.toString();
118+
} catch (Exception e) {
119+
return "Error: " + e.getMessage();
120+
}
121+
}
100122
}
-1.91 KB
Binary file not shown.
-2.82 KB
Binary file not shown.

0 commit comments

Comments
 (0)