FAQ - Help and questions

How do I stop users from forging log lines with newlines in their input?

#security#logging#injectionpublished 0 replies0 views
ReplyWatch
Share
Hacker NewsRedditLinkedInE-mail

Short answer

A value written to a log as plain text can contain a newline and start a fake line that looks like one of yours. Write user-controlled values with the Debug format, which quotes the text and escapes control characters, or log in a structured format such as JSON, and cap the length of what you record.

Checked with rustc 1.93.0, Windows 11

Frequently asked question, written and answered by the Ruststudio team. Replies are open: corrections are welcome.

#1

Answer#

Log files are read by people and by tools that split on newlines. If a login name is written as it came, an attacker can submit a name that contains a line break followed by text shaped like a log entry, for example a successful login. Anyone reading the log later, or any tool that parses it, sees a line you never wrote. This is log injection (also called log forging).

The cheapest defence in Rust is the {:?} format for strings that came from outside: it surrounds the text with quotes and writes control characters as escapes such as \n.

src/main.rsrust
fn main() {
    let user_input = "bob\n2026-01-01T00:00:00Z INFO login ok user=admin";

    // WRONG: the newline lets the user write a line that looks like one of ours.
    println!("INFO login failed user={user_input}");

    // RIGHT: the Debug format quotes the value and escapes control characters.
    println!("INFO login failed user={user_input:?}");

    // Also cap the length of anything the user controls.
    let shown: String = user_input.chars().take(20).collect();
    println!("INFO login failed user={shown:?} (truncated)");
}
text
INFO login failed user=bob
2026-01-01T00:00:00Z INFO login ok user=admin
INFO login failed user="bob\n2026-01-01T00:00:00Z INFO login ok user=admin"
INFO login failed user="bob\n2026-01-01T00:00" (truncated)

The first line is the injected, plain write: the output has two lines, and the second one looks like a genuine entry. The second version shows one line with the newline visible as \n. The third also limits the length. (This is about integrity of the log. Keeping secrets out of logs is a different problem, covered in the thread on API keys and passwords.)

How to do it#

  1. Format every value that came from outside with {:?}, or use a logging library that escapes values in a structured format (JSON lines, key-value fields).
  2. Cap the length of logged user input, so one request cannot flood the log.
  3. Log events with fixed field names, so analysis tools can tell your fields from user text.
  4. Restrict who can read and write the log files, and ship them to a place the service cannot edit.
  5. In log viewers, remember that terminal escape sequences in a log can also affect what a person sees; escaping control characters covers this too.

Common mistakes#

  • Escaping only \n and forgetting \r and other control characters. {:?} handles them all.
  • Logging the full request body "for debugging" in production.
  • Treating the log as proof of what happened without protecting it from changes.

Does a structured logging crate make this unnecessary?#

A structured format with proper escaping (such as JSON) prevents the forged-line problem by construction, as long as you pass user data as field values and not as part of the message template. Check how your crate encodes control characters.

Should I log failed login names at all?#

Often yes, escaped and with limits, since they help to spot attacks. Never log passwords, including wrong ones that may be a typo of the real one.

Sources#

    QuoteReply
    Share
    Hacker NewsRedditLinkedInE-mail

    No replies yet