Click here to Skip to main content
15,885,881 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to ensure my MVC4 app is secured against the slow HTTP POST Dos attacks. I have run into a brick wall trying to set Server.ScriptTimeout, so now I'm trying to terminate requests where the content length exceeds a maximum value. I never like just throwing a blanket exception, so what should I be throwing here:

C#
public abstract class BaseController: Controller
    {
        protected static int MaxContentLength = 10;
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (filterContext.HttpContext.Request.RequestType == "POST")
            {
                if (filterContext.HttpContext.Request.ContentLength > MaxContentLength)
                {
                    throw new what???
                }
            }
            base.OnActionExecuting(filterContext);
        }
    }
Posted
Updated 27-Nov-14 20:40pm
v2

1 solution

Well, you can create your own Exception that would inherit from the base Exception class. That would have the text and a name that would tell the user that this is bad - well, it will tell him what went wrong.

For creating customs, you can read this document[^].

It will be like this,

C#
using System;

// extends the Exception base...
public class FileTooLargeException : Exception {
   // default constructor
   public FileTooLargeException () {
      // default error message to show, 
      // such as, "Maximum file size is 5mb".
   }
   // you can create your own too, to accept parameters and conditions etc
}


.. then call it using that statement.

However, you can also pass a simple string to the Exception to remove the blank-ness of it as this one,

C#
throw new Exception("Maximum file size is 5mb.");


.. this one would be more like it. Did you try?

While I was out, I was working on this answer - you can throw an HttpException that is present inside the System.Web. Use this HttpException instead of that Exception in my second code block and it will be the HttpException[^] in your application.

Somehow, there is a web.config file (since you're working with ASP.NET) where you can yourself specify the maximum size for the uploaded file. If you specify the size there, it will help you to focus on the logic only and the ASP.NET will take care of the remaining stuff itself. It is to be written like this,

HTML
<configuration>
  <system.web>
    <httpRuntime maxRequestLength="xxx" />
  </system.web>
</configuration>


.. these are a few methods that you can use in this scenario. :-)
 
Share this answer
 
v3
Comments
Brady Kelly 28-Nov-14 3:15am    
I considered a custom exception, but I was wondering where such would fit into the HTTP pipeline and so on.
Afzaal Ahmad Zeeshan 28-Nov-14 3:45am    
Yes there is an HttpException that you can raise in ASP.NET too. :-) See my updated answer.
Brady Kelly 28-Nov-14 3:55am    
Thanks. I am aware of the maxRequestLength setting, but the site is hosted on Azure, and I'm uncertain as to whether something like a machine.config there might have precedence.
Afzaal Ahmad Zeeshan 28-Nov-14 3:58am    
It might be - I am saying might be because I have not yet worked in Azure environment and so giving a wrong information might not be a good idea.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900