Skip to content
Katabench
Try free

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 (\r or \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:

Text
[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

C#
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.

C# secure coding exercises →

Related guides

What it practices

CRLF injection
Stop embedded newlines from forging log lines or email headers.

Related katas