Log Forging (CRLF)
Easy About 12 min Free, no account needed
Stop user input from forging extra log lines with embedded newlines.
The brief
The bug in plain terms: This is log forging (also called log injection), where data a user controls gets written straight into your log file without being neutralized first. Because log entries are separated by line breaks, an attacker who slips a newline character (
\ror\n) into their input can close off your real entry and write whole fake log lines of their own, planting forged events to frame someone, hide their tracks, or mislead anyone reading the logs. The trap: stripping out obvious bad words or HTML does nothing here, since the dangerous payload is the invisible carriage return and line feed characters themselves, so the fix has to neutralize those line breaks specifically (while leaving normal text untouched).
LogLine(user, action) formats an audit line:
[INFO] user=<user> action=<action>The values go in raw, so a value containing a newline (\n or \r) can forge
additional log lines, injecting a fake [INFO] entry, for instance.
Fix LogLine so user input cannot break onto a new line, without changing
ordinary values. Replace carriage returns and line feeds in both values with
a visible escape: \r becomes the two characters \r, \n becomes the two
characters \n (backslash + letter).
Starter code
using System;
public class Solution
{
// Formats an audit log line. User values are written verbatim.
public string LogLine(string user, string action)
{
return $"[INFO] user={user} action={action}";
}
}How it is graded
Functional tests and adversarial payloads both have to pass before the solution is accepted.
Related guides
What it practices
- CRLF injection
- Stop embedded newlines from forging log lines or email headers.
Related katas
- Email Header Injection Easy Pro Reject recipient addresses that could break out of an email header line and forge new headers.
- Comment Renderer (HTML Injection) Easy Free Fix a stored-XSS hole in a comment renderer without breaking its output.
- Filename Sanitizer Easy Free Reduce an uploaded filename to a safe, single path segment.
- Mass Assignment Guard Easy Free Decide which submitted fields may be applied to a record using an allowlist, not a blocklist.