Blog | G5 Cyber Security

Fix ASP.NET Response Splitting

TL;DR

An HTTP response splitting attack lets attackers inject malicious content into web responses by manipulating data sent to the server. This guide shows you how to protect your ASP.NET applications.

What is an HTTP Response Splitting Attack?

Imagine a website takes some text you type and includes it in its reply (the ‘response’). If the website doesn’t handle that text carefully, an attacker could sneak extra instructions into the response, like telling your browser to display a fake login box. This is HTTP Response Splitting.

How to Fix It

  1. Understand Where User Input Goes
  • Encode Output Properly
  • The key is to make sure any user input included in the response is treated as *data*, not as instructions. Use ASP.NET’s built-in encoding methods.

    string userInput = Request.QueryString["name"];
    string encodedInput = HttpUtility.HtmlEncode(userInput);
    Response.Write("Hello, " + encodedInput); // Safe!
  • For URLs in Redirects: Use HttpUtility.UrlEncode() to encode the URL parameters.
  • string redirectURL = Request.QueryString["url"];
    string encodedURL = HttpUtility.UrlEncode(redirectURL);
    Response.Redirect("/somepage?url=" + encodedURL); // Safe!
  • Avoid Direct Header Manipulation
  • Directly setting response headers with user input is very risky. If possible, avoid it altogether.

    string customHeaderValue = Request.QueryString["headerValue"];
    string encodedHeaderValue = HttpUtility.HtmlEncode(customHeaderValue);
    Response.Headers["X-Custom-Header"] = encodedHeaderValue; // Still risky, but better
  • Use Safe Methods Where Available
  • Some ASP.NET methods are designed to be safer.

  • Content Security Policy (CSP)
  • While not a fix for the vulnerability itself, CSP can help mitigate the impact if an attack succeeds by controlling what resources the browser is allowed to load.

  • Testing
  • Always test your application after making changes!

    Exit mobile version