@@ -3,47 +3,47 @@ use log::{info, warn, error, debug, trace};
3
3
4
4
fn main ( ) {
5
5
env_logger:: init ( ) ;
6
-
6
+
7
7
// Sources of user input
8
8
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
10
10
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
+
14
14
// 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
+
21
21
// BAD: Formatted strings with user input
22
22
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
+
25
25
// BAD: String concatenation with user input
26
26
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
+
29
29
// 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
+
32
32
// GOOD: Sanitized input
33
33
let sanitized_username = username. replace ( '\n' , "" ) . replace ( '\r' , "" ) ;
34
34
info ! ( "Sanitized user login: {}" , sanitized_username) ;
35
-
35
+
36
36
// GOOD: Constant strings
37
37
info ! ( "System startup complete" ) ;
38
-
38
+
39
39
// GOOD: Non-user-controlled data
40
40
let system_time = std:: time:: SystemTime :: now ( ) ;
41
41
info ! ( "Current time: {:?}" , system_time) ;
42
-
42
+
43
43
// GOOD: Numeric data derived from user input (not directly logged)
44
44
let user_id = username. len ( ) ;
45
45
info ! ( "User ID length: {}" , user_id) ;
46
-
46
+
47
47
// More complex test cases
48
48
test_complex_scenarios ( & username, & user_input) ;
49
49
test_indirect_flows ( & remote_data) ;
@@ -52,22 +52,22 @@ fn main() {
52
52
fn test_complex_scenarios ( username : & str , user_input : & str ) {
53
53
// BAD: Indirect logging through variables
54
54
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
+
57
57
// BAD: Through function parameters
58
58
log_user_activity ( username) ; // Function call - should be tracked
59
-
59
+
60
60
// BAD: Through struct fields
61
61
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
+
64
64
// GOOD: After sanitization
65
65
let clean_input = sanitize_input ( user_input) ;
66
66
info ! ( "Clean input: {}" , clean_input) ;
67
67
}
68
68
69
69
fn log_user_activity ( user : & str ) {
70
- info ! ( "User activity: {}" , user) ; // $ Alert[rust/log-injection]
70
+ info ! ( "User activity: {}" , user) ; // $ MISSING: Alert[rust/log-injection]
71
71
}
72
72
73
73
fn sanitize_input ( input : & str ) -> String {
@@ -82,44 +82,44 @@ fn test_indirect_flows(data: &str) {
82
82
// BAD: Flow through intermediate variables
83
83
let temp_var = data;
84
84
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
+
87
87
// BAD: Flow through collections
88
88
let data_vec = vec ! [ data] ;
89
89
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]
91
91
}
92
-
92
+
93
93
// BAD: Flow through Option/Result
94
94
let optional_data = Some ( data) ;
95
95
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]
97
97
}
98
98
}
99
99
100
100
// Additional test patterns for different logging scenarios
101
101
mod additional_tests {
102
102
use log:: * ;
103
-
103
+
104
104
pub fn test_macro_variations ( ) {
105
105
let user_data = std:: env:: args ( ) . nth ( 1 ) . unwrap_or_default ( ) ; // $ Source=commandargs
106
-
106
+
107
107
// 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
+
114
114
// 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
116
116
}
117
-
117
+
118
118
pub fn test_println_patterns ( ) {
119
119
let user_data = std:: env:: var ( "USER" ) . unwrap_or_default ( ) ; // $ Source=environment
120
-
120
+
121
121
// 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
124
124
}
125
- }
125
+ }
0 commit comments