@@ -3,47 +3,47 @@ use log::{info, warn, error, debug, trace};
33
44fn main ( ) {
55 env_logger:: init ( ) ;
6-
6+
77 // Sources of user input
88 let args: Vec < String > = env:: args ( ) . collect ( ) ;
9- let username = args. get ( 1 ) . unwrap_or ( & String :: from ( "Guest" ) ) . clone ( ) ; // $ Source=commandargs
9+ let username = args. get ( 1 ) . unwrap_or ( & String :: from ( "Guest" ) ) . clone ( ) ; // $ MISSING: Source=commandargs
1010 let user_input = std:: env:: var ( "USER_INPUT" ) . unwrap_or ( "default" . to_string ( ) ) ; // $ Source=environment
11- let remote_data = reqwest:: blocking:: get ( "http://example.com/user" )
12- . unwrap ( ) . text ( ) . unwrap_or ( "remote_user" . to_string ( ) ) ; // $ Source=remote
13-
11+ let remote_data = reqwest:: blocking:: get ( "http://example.com/user" ) // $ Source=remote
12+ . unwrap ( ) . text ( ) . unwrap_or ( "remote_user" . to_string ( ) ) ;
13+
1414 // BAD: Direct logging of user input
15- info ! ( "User login: {}" , username) ; // $ Alert[rust/log-injection]
16- warn ! ( "Warning for user: {}" , user_input) ; // $ Alert[rust/log-injection]
17- error ! ( "Error processing: {}" , remote_data) ; // $ Alert[rust/log-injection]
18- debug ! ( "Debug info: {}" , username) ; // $ Alert[rust/log-injection]
19- trace ! ( "Trace data: {}" , user_input) ; // $ Alert[rust/log-injection]
20-
15+ info ! ( "User login: {}" , username) ; // $ MISSING: Alert[rust/log-injection]
16+ warn ! ( "Warning for user: {}" , user_input) ; // $ Alert[rust/log-injection]=environment
17+ error ! ( "Error processing: {}" , remote_data) ; // $ Alert[rust/log-injection]=remote
18+ debug ! ( "Debug info: {}" , username) ; // $ MISSING: Alert[rust/log-injection]
19+ trace ! ( "Trace data: {}" , user_input) ; // $ Alert[rust/log-injection]=environment
20+
2121 // BAD: Formatted strings with user input
2222 let formatted_msg = format ! ( "Processing user: {}" , username) ;
23- info ! ( "{}" , formatted_msg) ; // $ Alert[rust/log-injection]
24-
23+ info ! ( "{}" , formatted_msg) ; // $ MISSING: Alert[rust/log-injection]
24+
2525 // BAD: String concatenation with user input
2626 let concat_msg = "User activity: " . to_string ( ) + & username;
27- info ! ( "{}" , concat_msg) ; // $ Alert[rust/log-injection]
28-
27+ info ! ( "{}" , concat_msg) ; // $ MISSING: Alert[rust/log-injection]
28+
2929 // BAD: Complex formatting
30- info ! ( "User {} accessed resource at {}" , username, remote_data) ; // $ Alert[rust/log-injection]
31-
30+ info ! ( "User {} accessed resource at {}" , username, remote_data) ; // $ Alert[rust/log-injection]=remote
31+
3232 // GOOD: Sanitized input
3333 let sanitized_username = username. replace ( '\n' , "" ) . replace ( '\r' , "" ) ;
3434 info ! ( "Sanitized user login: {}" , sanitized_username) ;
35-
35+
3636 // GOOD: Constant strings
3737 info ! ( "System startup complete" ) ;
38-
38+
3939 // GOOD: Non-user-controlled data
4040 let system_time = std:: time:: SystemTime :: now ( ) ;
4141 info ! ( "Current time: {:?}" , system_time) ;
42-
42+
4343 // GOOD: Numeric data derived from user input (not directly logged)
4444 let user_id = username. len ( ) ;
4545 info ! ( "User ID length: {}" , user_id) ;
46-
46+
4747 // More complex test cases
4848 test_complex_scenarios ( & username, & user_input) ;
4949 test_indirect_flows ( & remote_data) ;
@@ -52,22 +52,22 @@ fn main() {
5252fn test_complex_scenarios ( username : & str , user_input : & str ) {
5353 // BAD: Indirect logging through variables
5454 let log_message = format ! ( "Activity for {}" , username) ;
55- info ! ( "{}" , log_message) ; // $ Alert[rust/log-injection]
56-
55+ info ! ( "{}" , log_message) ; // $ MISSING: Alert[rust/log-injection]
56+
5757 // BAD: Through function parameters
5858 log_user_activity ( username) ; // Function call - should be tracked
59-
59+
6060 // BAD: Through struct fields
6161 let user_info = UserInfo { name : username. to_string ( ) } ;
62- info ! ( "User info: {}" , user_info. name) ; // $ Alert[rust/log-injection]
63-
62+ info ! ( "User info: {}" , user_info. name) ; // $ MISSING: Alert[rust/log-injection]
63+
6464 // GOOD: After sanitization
6565 let clean_input = sanitize_input ( user_input) ;
6666 info ! ( "Clean input: {}" , clean_input) ;
6767}
6868
6969fn log_user_activity ( user : & str ) {
70- info ! ( "User activity: {}" , user) ; // $ Alert[rust/log-injection]
70+ info ! ( "User activity: {}" , user) ; // $ MISSING: Alert[rust/log-injection]
7171}
7272
7373fn sanitize_input ( input : & str ) -> String {
@@ -82,44 +82,44 @@ fn test_indirect_flows(data: &str) {
8282 // BAD: Flow through intermediate variables
8383 let temp_var = data;
8484 let another_var = temp_var;
85- info ! ( "Indirect flow: {}" , another_var) ; // $ Alert[rust/log-injection]
86-
85+ info ! ( "Indirect flow: {}" , another_var) ; // $ MISSING: Alert[rust/log-injection]
86+
8787 // BAD: Flow through collections
8888 let data_vec = vec ! [ data] ;
8989 if let Some ( item) = data_vec. first ( ) {
90- info ! ( "Vector item: {}" , item) ; // $ Alert[rust/log-injection]
90+ info ! ( "Vector item: {}" , item) ; // $ MISSING: Alert[rust/log-injection]
9191 }
92-
92+
9393 // BAD: Flow through Option/Result
9494 let optional_data = Some ( data) ;
9595 if let Some ( unwrapped) = optional_data {
96- info ! ( "Unwrapped data: {}" , unwrapped) ; // $ Alert[rust/log-injection]
96+ info ! ( "Unwrapped data: {}" , unwrapped) ; // $ MISSING: Alert[rust/log-injection]
9797 }
9898}
9999
100100// Additional test patterns for different logging scenarios
101101mod additional_tests {
102102 use log:: * ;
103-
103+
104104 pub fn test_macro_variations ( ) {
105105 let user_data = std:: env:: args ( ) . nth ( 1 ) . unwrap_or_default ( ) ; // $ Source=commandargs
106-
106+
107107 // BAD: Different log macro variations
108- info ! ( "Info: {}" , user_data) ; // $ Alert[rust/log-injection]
109- warn ! ( "Warning: {}" , user_data) ; // $ Alert[rust/log-injection]
110- error ! ( "Error: {}" , user_data) ; // $ Alert[rust/log-injection]
111- debug ! ( "Debug: {}" , user_data) ; // $ Alert[rust/log-injection]
112- trace ! ( "Trace: {}" , user_data) ; // $ Alert[rust/log-injection]
113-
108+ info ! ( "Info: {}" , user_data) ; // $ Alert[rust/log-injection]=commandargs
109+ warn ! ( "Warning: {}" , user_data) ; // $ Alert[rust/log-injection]=commandargs
110+ error ! ( "Error: {}" , user_data) ; // $ Alert[rust/log-injection]=commandargs
111+ debug ! ( "Debug: {}" , user_data) ; // $ Alert[rust/log-injection]=commandargs
112+ trace ! ( "Trace: {}" , user_data) ; // $ Alert[rust/log-injection]=commandargs
113+
114114 // BAD: Complex format strings
115- info ! ( "User {} did action {} at time {}" , user_data, "login" , "now" ) ; // $ Alert[rust/log-injection]
115+ info ! ( "User {} did action {} at time {}" , user_data, "login" , "now" ) ; // $ Alert[rust/log-injection]=commandargs
116116 }
117-
117+
118118 pub fn test_println_patterns ( ) {
119119 let user_data = std:: env:: var ( "USER" ) . unwrap_or_default ( ) ; // $ Source=environment
120-
120+
121121 // These might not be caught depending on model coverage, but are potential logging sinks
122- println ! ( "User: {}" , user_data) ;
123- eprintln ! ( "Error for user: {}" , user_data) ;
122+ println ! ( "User: {}" , user_data) ; // $ Alert[rust/log-injection]=environment
123+ eprintln ! ( "Error for user: {}" , user_data) ; // $ Alert[rust/log-injection]=environment
124124 }
125- }
125+ }
0 commit comments