-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Changes from 11 commits
d954b50
39ea507
d72efc5
2a19a17
49265b6
7b1aa23
9836592
4328ed8
9e4f59c
bc0d327
f05d815
265c2e3
e84135a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| /** | ||
| * Provides classes and predicates for reasoning about log injection | ||
| * vulnerabilities. | ||
| */ | ||
|
|
||
| import rust | ||
| private import codeql.rust.dataflow.DataFlow | ||
| private import codeql.rust.dataflow.FlowSink | ||
| private import codeql.rust.Concepts | ||
| private import codeql.util.Unit | ||
|
|
||
| /** | ||
| * Provides default sources, sinks and barriers for detecting log injection | ||
| * vulnerabilities, as well as extension points for adding your own. | ||
| */ | ||
| module LogInjection { | ||
| /** | ||
| * A data flow source for log injection vulnerabilities. | ||
| */ | ||
| abstract class Source extends DataFlow::Node { } | ||
|
|
||
| /** | ||
| * A data flow sink for log injection vulnerabilities. | ||
| */ | ||
| abstract class Sink extends QuerySink::Range { | ||
| override string getSinkType() { result = "LogInjection" } | ||
| } | ||
|
|
||
| /** | ||
| * A barrier for log injection vulnerabilities. | ||
| */ | ||
| abstract class Barrier extends DataFlow::Node { } | ||
|
|
||
| /** | ||
| * An active threat-model source, considered as a flow source. | ||
| */ | ||
| private class ActiveThreatModelSourceAsSource extends Source, ActiveThreatModelSource { } | ||
|
|
||
| /** | ||
| * A sink for log-injection from model data. | ||
| */ | ||
| private class ModelsAsDataSink extends Sink { | ||
| ModelsAsDataSink() { sinkNode(this, "log-injection") } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| <!DOCTYPE qhelp PUBLIC | ||
| "-//Semmle//qhelp//EN" | ||
| "qhelp.dtd"> | ||
| <qhelp> | ||
|
|
||
| <overview> | ||
|
|
||
| <p>If unsanitized user input is written to a log entry, a malicious user may be able to forge new log entries.</p> | ||
|
|
||
| <p>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.</p> | ||
| </overview> | ||
|
|
||
| <recommendation> | ||
| <p> | ||
| User input should be suitably sanitized before it is logged. | ||
| </p> | ||
| <p> | ||
| If the log entries are in plain text then line breaks should be removed from user input, using | ||
| <code>String::replace</code> or similar. Care should also be taken that user input is clearly marked | ||
| in log entries. | ||
| </p> | ||
| <p> | ||
| 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. | ||
| </p> | ||
|
|
||
| </recommendation> | ||
|
|
||
| <example> | ||
| <p>In the first example, a username, provided by the user via command line arguments, is logged using the <code>log</code> crate. | ||
| If a malicious user provides <code>Guest\n[INFO] User: Admin\n</code> as a username parameter, | ||
| the log entry will be split into multiple lines, where the second line will appear as <code>[INFO] User: Admin</code>, | ||
| potentially forging a legitimate admin login entry. | ||
| </p> | ||
| <sample src="LogInjectionBad.rs" /> | ||
|
|
||
| <p>In the second example, <code>String::replace</code> is used to ensure no line endings are present in the user input before logging.</p> | ||
| <sample src="LogInjectionGood.rs" /> | ||
| </example> | ||
|
|
||
| <references> | ||
| <li>OWASP: <a href="https://owasp.org/www-community/attacks/Log_Injection">Log Injection</a>.</li> | ||
| </references> | ||
| </qhelp> | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,41 @@ | ||||||
| /** | ||||||
| * @name Log injection | ||||||
| * @description Building log entries from user-controlled sources is vulnerable to | ||||||
| * insertion of forged log entries by a malicious user. | ||||||
| * @kind path-problem | ||||||
| * @problem.severity error | ||||||
| * @security-severity 2.6 | ||||||
|
||||||
| * @security-severity 2.6 | |
| * @security-severity 7.8 |
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.
I generated it with the cwe-scores tool. I appreciate that the number differs from other versions of the query, I haven't looked into why but I'm guessing its because something the tool bases its score on has changed since they were created.
Dismissed
Show dismissed
Hide dismissed
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| 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); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| 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); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| multipleCallTargets | ||
| | main.rs:9:43:9:63 | ...::from(...) | | ||
| | main.rs:44:19:44:32 | username.len() | |
Uh oh!
There was an error while loading. Please reload this page.