views:

387

answers:

6

My website would like users to upload their photos...but how do I keep our server safe from harm? Allowing only JPGs should avoid virus trouble, but what if someone selects a 10Gb file - will that slow the whole website down?

We're using Classic ASP and IIS6 (sorry, but that's how it is, can't change that!). Previously we have used a DLL from a company called Persits to handle uploads. However, it would be helpful to other people if we extend this discussion to other languages/technologies too.

ASPs cannot detect the size of a file until it has finished uploading, so thats a pain. Or can I check content-length in the HTTP header before I start the transfer?

Q1. Are there any other ways someone could abuse the upload facility?
Q2. How can I limit the danger to keep the site running and the server safe?

Thank you.

A: 

There is a great component that uses Flash to upload files. Check it out

http://www.codeproject.com/KB/aspnet/FlashUpload.aspx

Daniel
A: 

This appears to enforce file upload size: http://www.aspupload.com/

I am not sure how it handles it.

Daniel A. White
Yeah, this is the Persits component Katy and I mention above. It doesnt check the file size until it has finished uploading which is a shame.
Magnus Smith
+2  A: 

In Persists, you can set the maximum filesize a user can upload:

Upload.SetMaxSize 100000, True

The "True" above shows that the file is to be rejected if over the Max size. If set to False then the file will be trucated.

See http://www.aspupload.com/object_upload.html#SetMaxSize

Katy
thanks, but does it upload the whole file first, before it looks athow big it is?
Magnus Smith
From the user manual:"A value set by SetMaxSize is applied to each uploaded file individually rather than an entire upload. Since AspUpload has no way of knowing in advance how many files there are in a POST and how large they are, it will always allow the upload process to go through, even if the very first file exceeds the specified limit"So the file will get uploaded, then if it fails it's truncated or deleted depending on the setting.
Katy
Hmm, thats a shame - it means a 10Gb file would still hog the server for ages, rather than being kicked out as soon as it went over 2Mb (say). Thank you for looking that up for me though!
Magnus Smith
+2  A: 

If you were using ASP.Net you can specify a maximum size of file in web.config (or machine.config), and ASP.Net will throw an error after the size is exceeded in the upload. That is to say, if you specify a limit of 4Mb, and someome tries to upload a 100Mb, .Net will complain as soon as it has uploaded more than 4Mb.

The property in question is maxRequestLength, which accorsing to MSDN "Specifies the limit for the input stream buffering threshold, in KB. This limit can be used to prevent denial of service attacks that are caused, for example, by users posting large files to the server."

For example.

<configuration>
  <system.web>
    <httpRuntime maxRequestLength="4000" ....

I am not sure if there is an equivalent in classic ASP though.

Tim C
A: 

I've just found an article on how to limit size using a setting called 'AspMaxRequestEntityAllowed' in IIS: http://www.banmanpro.com/support2/File_Upload_limits.asp

However, it doesn't work - my server already has that setting at 200k and yet we are currently uploading 1Mb files ok!

Magnus Smith
Is this because when uploading files the Content-Length header is not present in the http request?
Tim C
Assuming chunked transfer encoding bypasses this.
Magnus Smith
A: 

You can reject the oversized requests at the IIS level before they even get to your application by using Microsoft's UrlScan tool: http://technet.microsoft.com/en-us/security/cc242650.aspx

For IIS 6, it looks like you may not even need that. You should be able to set the MaxRequestEntityAllowed and ASPMaxRequestEntityAllowed metabase properties to your desired maximum value and the requests will be cut off at that point.

Chris Hynes
My server already has ASPMaxRequestEntityAllowed at 200k and yet we are currently uploading 1Mb files ok! It seems to be ignoring it.
Magnus Smith
Did you try setting both values? Or are the uploads using chunked transfer encoding? Also, I believe those settings only apply to ASP, not ASP.NET.
Chris Hynes
I can only assume we are using chunked transfer encoding then (though I don't actually know what this is, sorry!).
Magnus Smith