-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathLogInjectionGood.rs
More file actions
28 lines (22 loc) · 921 Bytes
/
LogInjectionGood.rs
File metadata and controls
28 lines (22 loc) · 921 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
use std::env;
use log::{info, error};
fn sanitize_for_logging(input: &str) -> String {
// Remove newlines and carriage returns to prevent log injection
input.replace('\n', "").replace('\r', "")
}
fn main() {
env_logger::init();
// Get username from command line arguments
let args: Vec<String> = env::args().collect();
let username = args.get(1).unwrap_or(&String::from("Guest"));
// GOOD: log message constructed with sanitized user input
let sanitized_username = sanitize_for_logging(username);
info!("User login attempt: {}", sanitized_username);
// GOOD: another example with error logging
if username.is_empty() {
error!("Login failed for user: {}", sanitized_username);
}
// GOOD: formatted string with sanitized user input
let message = format!("Processing request for user: {}", sanitized_username);
info!("{}", message);
}