Skip to content

Commit 3f73f48

Browse files
Merge pull request #2 from devopselvis/copilot/fix-763d5b85-9b58-4de7-9faf-90c6a3774dca
Create Java application with intentional vulnerabilities and CodeQL autobuild workflow
2 parents ffdb6a8 + c9d90bc commit 3f73f48

File tree

10 files changed

+712
-1
lines changed

10 files changed

+712
-1
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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@v3
37+
with:
38+
languages: ${{ matrix.language }}
39+
queries: +security-and-quality
40+
build-mode: none
41+
dependency-caching: true
42+
43+
# Autobuild attempts to build any compiled languages (Java, C#, Go, etc.)
44+
# If this step fails, remove it and run the build manually instead
45+
#- name: Autobuild
46+
# uses: github/codeql-action/autobuild@v3
47+
48+
- name: Perform CodeQL Analysis
49+
uses: github/codeql-action/analyze@v3
50+
with:
51+
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: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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 - Test2</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+
<!-- 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+
27+
<dependency>
28+
<groupId>com.google.guava</groupId>
29+
<artifactId>guava</artifactId>
30+
<version>32.1.1-jre</version>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>org.slf4j</groupId>
35+
<artifactId>slf4j-api</artifactId>
36+
<version>2.0.7</version>
37+
</dependency>
38+
39+
<dependency>
40+
<groupId>org.apache.commons</groupId>
41+
<artifactId>commons-lang3</artifactId>
42+
<version>3.12.0</version>
43+
</dependency>
44+
45+
<!-- Database connectivity for SQL injection demos -->
46+
<dependency>
47+
<groupId>mysql</groupId>
48+
<artifactId>mysql-connector-java</artifactId>
49+
<version>8.0.33</version>
50+
</dependency>
51+
52+
<!-- Web framework for HTTP vulnerabilities -->
53+
<dependency>
54+
<groupId>org.springframework</groupId>
55+
<artifactId>spring-web</artifactId>
56+
<version>5.3.21</version>
57+
</dependency>
58+
59+
<!-- JSON processing -->
60+
<dependency>
61+
<groupId>com.fasterxml.jackson.core</groupId>
62+
<artifactId>jackson-databind</artifactId>
63+
<version>2.13.3</version>
64+
</dependency>
65+
66+
<!-- Testing -->
67+
<dependency>
68+
<groupId>junit</groupId>
69+
<artifactId>junit</artifactId>
70+
<version>4.13.2</version>
71+
<scope>test</scope>
72+
</dependency>
73+
</dependencies>
74+
75+
<build>
76+
<plugins>
77+
<plugin>
78+
<groupId>org.apache.maven.plugins</groupId>
79+
<artifactId>maven-compiler-plugin</artifactId>
80+
<version>3.8.1</version>
81+
<configuration>
82+
<source>11</source>
83+
<target>11</target>
84+
</configuration>
85+
</plugin>
86+
87+
<plugin>
88+
<groupId>org.apache.maven.plugins</groupId>
89+
<artifactId>maven-surefire-plugin</artifactId>
90+
<version>3.0.0-M7</version>
91+
</plugin>
92+
</plugins>
93+
</build>
94+
</project>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
import com.example.ldap.LdapAuth;
7+
8+
/**
9+
* Main application class demonstrating various Java vulnerabilities
10+
* that should be detected by CodeQL scanning.
11+
*/
12+
public class VulnerableApplication {
13+
14+
public static void main(String[] args) {
15+
System.out.println("Starting Vulnerable Application...");
16+
17+
// Demonstrate various vulnerable components
18+
UserDatabase userDb = new UserDatabase();
19+
CryptoUtils crypto = new CryptoUtils();
20+
FileController fileController = new FileController();
21+
LdapAuth ldapAuth = new LdapAuth();
22+
23+
// Example usage that would trigger vulnerabilities
24+
String userInput = args.length > 0 ? args[0] : "admin";
25+
String password = args.length > 1 ? args[1] : "password123";
26+
27+
// SQL Injection vulnerability
28+
userDb.authenticateUser(userInput, password);
29+
userDb.deleteUser(userInput);
30+
31+
// Weak cryptography
32+
String token = crypto.generateToken();
33+
System.out.println("Generated token: " + token);
34+
35+
// Path traversal vulnerability
36+
String filename = args.length > 2 ? args[2] : "../../etc/passwd";
37+
fileController.readFile(filename);
38+
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+
48+
System.out.println("Application completed.");
49+
}
50+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
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+
}
90+
}

0 commit comments

Comments
 (0)