CRLF Injection
CRLF injection is an attack that slips carriage return (CR: \r) and line feed (LF: \n)
characters into input that an application writes into a line-based format, such as HTTP headers or
log files.
Many text-based protocols use a carriage return followed by a line feed (CRLF) to mark the end of a
line. HTTP is one of them: each header sits on its own line, and a blank line separates the headers
from the body. Suppose an application redirects users to a path taken from a query parameter, and
copies the parameter into the Location header unchecked. An attacker can include an encoded CRLF
(%0d%0a) in a link like this one:
/redirect?to=/home%0d%0aSet-Cookie:%20session=attacker-chosen-valueThe server's response then contains a header of the attacker's choosing:
HTTP/1.1 302 Found
Location: /home
Set-Cookie: session=attacker-chosen-valueThat one link sets the victim's session cookie to a value the attacker knows, which sets up a session fixation attack. With two CRLFs in a row, the attacker can end the headers and write a response body of their own, which is known as HTTP response splitting. The same trick against log files lets an attacker forge entries (see log injection).
Many modern web frameworks refuse to send header values that contain line breaks, which has made
response splitting rare. Still, strip or encode \r and \n in any input you write into headers,
logs, emails or other line-based formats.