Skip to main content

Free 30-min security demo  — We'll scan your real code and show live findings, no commitment Book Now

Offensive360
Academy HTTP Response Splitting
Advanced · 20 min

HTTP Response Splitting

Learn how CRLF injection in HTTP headers enables cache poisoning, XSS, and session hijacking.

1 Header Injection via CRLF

HTTP headers are terminated by carriage return + line feed (CRLF: \r\n). When user input is placed in a response header without stripping CRLF characters, attackers can inject additional headers or even a second HTTP response body.

Vulnerable redirect (PHP):

$redirect = $_GET["url"];
header("Location: " . $redirect);

An attacker sends: https://example.com/%0d%0aSet-Cookie:%20session=evil

This produces the response headers:

Location: https://example.com/
Set-Cookie: session=evil

Cache poisoning: By injecting a full second response body, attackers can poison shared caches with malicious content served to all users. This is known as HTTP Response Splitting.

2 Prevention

The primary defense is to strip or reject CRLF characters from any user input that will be used in HTTP response headers.

Safe redirect (Node.js/Express):

function safeRedirect(res, url) {
  // Strip CRLF characters
  const safe = url.replace(/[\r\n]/g, "");
  // Additionally validate against an allowlist
  const allowed = ["https://example.com", "https://app.example.com"];
  if (!allowed.some(a => safe.startsWith(a))) {
    return res.status(400).send("Invalid redirect");
  }
  res.redirect(safe);
}

Framework protections:

  • Express.js: res.redirect() sanitizes the Location header since v4
  • Django: HttpResponseRedirect encodes the URL
  • Spring: Use UriComponentsBuilder to construct safe URIs

Defense checklist:

  • Strip \r and \n from all header values
  • Validate redirect URLs against an allowlist
  • Use framework redirect methods rather than raw header setting
  • Set appropriate Cache-Control headers

Knowledge Check

0/3 correct
Q1

What characters enable HTTP Response Splitting?

Q2

Which attack is enabled by HTTP Response Splitting against shared caches?

Q3

What is the most direct fix for CRLF injection in redirect headers?

Code Exercise

Sanitize Redirect URL

The redirect function below passes user input directly to the Location header. Fix it by stripping CRLF characters and validating against an allowlist.

javascript