10
10
* Main entry point for the Weather Application
11
11
*/
12
12
public class WeatherApp {
13
-
13
+
14
14
private static final Logger LOGGER = Logger .getLogger (WeatherApp .class .getName ());
15
15
16
16
// Secret for accessing Atlassian API!! (Not really, it's deprecated)
@@ -34,24 +34,22 @@ public class WeatherApp {
34
34
}
35
35
}
36
36
37
-
38
-
39
37
// Flag to control System.exit behavior (for testing)
40
38
private static boolean exitOnError = true ;
41
39
42
40
/**
43
41
* Set whether the application should exit on error.
44
42
* This method is primarily used for testing.
45
- *
43
+ *
46
44
* @param shouldExit true if the application should exit on error, false otherwise
47
45
*/
48
46
public static void setExitOnError (boolean shouldExit ) {
49
47
exitOnError = shouldExit ;
50
48
}
51
-
49
+
52
50
/**
53
51
* Exit the application with the given status code if exitOnError is true.
54
- *
52
+ *
55
53
* @param status the exit status code
56
54
* @return true if the application would exit (for testing)
57
55
*/
@@ -75,36 +73,33 @@ public static void main(String[] args) {
75
73
String city = args [0 ];
76
74
LOGGER .log (Level .INFO , "Weather request for city: {0}" , city );
77
75
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 );
87
83
}
88
84
// --- End of vulnerability block ---
89
85
90
-
91
86
try {
92
87
// Get API key from environment or config file
93
88
String apiKey = ConfigUtil .getApiKey ();
94
-
89
+
95
90
// Initialize services
96
91
WeatherApiClient weatherApiClient = new OpenWeatherMapClient (apiKey );
97
92
WeatherService weatherService = new WeatherService (weatherApiClient );
98
93
99
94
// Get and display weather data
100
95
WeatherData weatherData = weatherService .getWeather (city );
101
96
LOGGER .log (Level .FINE , weatherData .toString ());
102
-
97
+
103
98
// Display weather data to the user
104
99
System .out .println ("Current Weather for " + city + ":" );
105
100
System .out .println ("-------------------------------------" );
106
101
System .out .println (weatherData );
107
-
102
+
108
103
} catch (ConfigUtil .ConfigException e ) {
109
104
LOGGER .log (Level .SEVERE , "Configuration error: " + e .getMessage (), e );
110
105
LOGGER .log (Level .SEVERE ,
0 commit comments