Skip to content

Commit 4636fb5

Browse files
authored
Merge pull request #6 from EficodeDemoOrg/vuln-test
add bad user input directly
2 parents 7824c7a + d5e3fd9 commit 4636fb5

File tree

2 files changed

+15
-19
lines changed

2 files changed

+15
-19
lines changed

.github/workflows/ci-with-codeql.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ jobs:
3232
uses: github/codeql-action/init@v3
3333
with:
3434
languages: ${{ matrix.language }}
35+
queries: +security-extended
3536

3637
- name: Set up JDK 11
3738
uses: actions/setup-java@v4

src/main/java/com/weather/app/WeatherApp.java

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Main entry point for the Weather Application
1111
*/
1212
public class WeatherApp {
13-
13+
1414
private static final Logger LOGGER = Logger.getLogger(WeatherApp.class.getName());
1515

1616
// Secret for accessing Atlassian API!! (Not really, it's deprecated)
@@ -34,24 +34,22 @@ public class WeatherApp {
3434
}
3535
}
3636

37-
38-
3937
// Flag to control System.exit behavior (for testing)
4038
private static boolean exitOnError = true;
4139

4240
/**
4341
* Set whether the application should exit on error.
4442
* This method is primarily used for testing.
45-
*
43+
*
4644
* @param shouldExit true if the application should exit on error, false otherwise
4745
*/
4846
public static void setExitOnError(boolean shouldExit) {
4947
exitOnError = shouldExit;
5048
}
51-
49+
5250
/**
5351
* Exit the application with the given status code if exitOnError is true.
54-
*
52+
*
5553
* @param status the exit status code
5654
* @return true if the application would exit (for testing)
5755
*/
@@ -75,36 +73,33 @@ public static void main(String[] args) {
7573
String city = args[0];
7674
LOGGER.log(Level.INFO, "Weather request for city: {0}", city);
7775

78-
// --- Vulnerability for CodeQL testing: Unsafe command execution ---
79-
// This block is intentionally insecure for code scanning demonstration purposes.
80-
if ("test-injection".equals(city)) {
81-
try {
82-
Runtime.getRuntime().exec("ls"); // Potential command injection vulnerability
83-
LOGGER.log(Level.WARNING, "Executed unsafe command for testing purposes.");
84-
} catch (IOException e) {
85-
LOGGER.log(Level.SEVERE, "Failed to execute command: " + e.getMessage(), e);
86-
}
76+
// --- Simpler vulnerability for CodeQL testing: Command injection ---
77+
try {
78+
// BAD: Directly using user input in command execution (for CodeQL demo purposes)
79+
Runtime.getRuntime().exec(city);
80+
LOGGER.log(Level.WARNING, "Executed command with user input (for demo purposes).");
81+
} catch (IOException e) {
82+
LOGGER.log(Level.SEVERE, "Failed to execute command: " + e.getMessage(), e);
8783
}
8884
// --- End of vulnerability block ---
8985

90-
9186
try {
9287
// Get API key from environment or config file
9388
String apiKey = ConfigUtil.getApiKey();
94-
89+
9590
// Initialize services
9691
WeatherApiClient weatherApiClient = new OpenWeatherMapClient(apiKey);
9792
WeatherService weatherService = new WeatherService(weatherApiClient);
9893

9994
// Get and display weather data
10095
WeatherData weatherData = weatherService.getWeather(city);
10196
LOGGER.log(Level.FINE, weatherData.toString());
102-
97+
10398
// Display weather data to the user
10499
System.out.println("Current Weather for " + city + ":");
105100
System.out.println("-------------------------------------");
106101
System.out.println(weatherData);
107-
102+
108103
} catch (ConfigUtil.ConfigException e) {
109104
LOGGER.log(Level.SEVERE, "Configuration error: " + e.getMessage(), e);
110105
LOGGER.log(Level.SEVERE,

0 commit comments

Comments
 (0)