HTTP Response Splitting Attacks

Learn how to prevent HTTP Response Splitting attacks with real code examples and best practices. Protect your web applications from header manipulation and response injection vulnerabilities.

Tools recognizing this:

Fortify Checkmarx SonarQube Snyk Semgrep CodeQL

What is HTTP Response Splitting and How Does it Work?

HTTP Response Splitting is a web security vulnerability that occurs when an attacker can inject newline characters (CR/LF) into response headers. This allows the attacker to split the response into multiple responses, potentially leading to:

  • Cache poisoning attacks

  • Cross-site scripting (XSS)

  • Page defacement

  • Response header manipulation

  • Session hijacking

This guide covers HTTP Response Splitting attacks, examples, prevention methods, and how to protect your applications using real-world techniques.

One Simple HTTP Response Splitting Attack Example

Consider this example of setting a cookie with user input:

response.setHeader("Location", userInput + "/index.html");

An attacker could provide this input:

/page.html\r\nSet-Cookie: sessionId=hijacked

The resulting response headers become:

Location: /page.html Set-Cookie: sessionId=hijacked

This allows the attacker to inject their own response headers, potentially hijacking user sessions or performing other malicious actions.

HTTP Response Splitting Prevention Methods: How to Fix Your Code

The most effective way to fix HTTP Response Splitting is to properly validate and sanitize any user input that goes into response headers. This includes removing or encoding CR/LF characters and validating the input against an allowlist of acceptable values.

Code Samples

Vulnerable Code

String userInput = request.getParameter("page");
response.setHeader("Location", );

Fixed Code

String userInput = request.getParameter("page");
String sanitizedInput = userInput.replaceAll("[\r\n]", "");
response.setHeader("Location", sanitizedInput + "/index.html");

Fix Explanation

The vulnerable code directly includes user input in response headers. The fix removes all CR/LF characters from the input using replaceAll(). This prevents attackers from splitting the response with injected newlines.

Need more help in preventing HTTP Response Splitting?

Mobb supports fixing many forms of HTTP Response Splitting vulnerabilities, and can mitigate your issues in batch.

Start now for free at app.mobb.ai

We'd love your feedback!

We're excited to hear your thoughts and ideas about fixing vulnerabilities.

Book a meeting or Contact us if you have any corrections, questions or suggestions. Start now for free at https://app.mobb.ai

Last updated

Was this helpful?