-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Rust: Implement a new query for Log Injection #20221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: geoffw0 <40627776+geoffw0@users.noreply.github.com>
Co-authored-by: geoffw0 <40627776+geoffw0@users.noreply.github.com>
QHelp previews: rust/ql/src/queries/security/CWE-117/LogInjection.qhelpLog injectionIf unsanitized user input is written to a log entry, a malicious user may be able to forge new log entries. Forgery can occur if a user provides some input with characters that are interpreted when the log output is displayed. If the log is displayed as a plain text file, then new line characters can be used by a malicious user. If the log is displayed as HTML, then arbitrary HTML may be included to spoof log entries. RecommendationUser input should be suitably sanitized before it is logged. If the log entries are in plain text then line breaks should be removed from user input, using For log entries that will be displayed in HTML, user input should be HTML-encoded before being logged, to prevent forgery and other forms of HTML injection. ExampleIn the first example, a username, provided by the user via command line arguments, is logged using the use std::env;
use log::info;
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")).clone();
// BAD: log message constructed with unsanitized user input
info!("User login attempt: {}", username);
} In the second example, use std::env;
use log::info;
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")).clone();
// GOOD: log message constructed with sanitized user input
let sanitized_username = sanitize_for_logging(username.as_str());
info!("User login attempt: {}", sanitized_username);
} References
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot This looks great - amazing work!
There are a few things to clean up (see my other comments). Then I will try the query out for myself and get others involved in reviewing.
rust/ql/test/query-tests/security/CWE-117/LogInjection.expected
Outdated
Show resolved
Hide resolved
Co-authored-by: geoffw0 <40627776+geoffw0@users.noreply.github.com>
This PR implements a comprehensive CodeQL query for detecting log injection vulnerabilities in Rust code, following the patterns established by existing log injection queries for other languages.
Overview
Log injection occurs when user-controlled data is included in log output without proper sanitization, allowing malicious users to forge log entries. This is particularly dangerous when logs are processed by automated systems or displayed in web interfaces.
Implementation
The query follows the same architecture as the existing
rust/sql-injection
query and includes:Core Components
ActiveThreatModelSource
for sources and the existingsinkNode(this, "log-injection")
infrastructureExamples
Test Coverage
Example Detection
If a malicious user provides
Guest\n[INFO] Admin logged in\n
as input, the vulnerable code would create a forged log entry appearing to show an admin login.Query Details
rust/log-injection
The implementation leverages existing logging sink models in
rust/ql/lib/codeql/rust/frameworks/log.model.yml
which already define appropriate sinks for thelog
crate and standard I/O functions.Fixes github/codeql-team#4178.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.