views:

67

answers:

3

We have an internal web application that acts as a repository to which users can upload files. These files can be any format, including HTML pages.

We have tested than in IE8, if you download an HTML file that contains some script that tries to access your cookies and, after downloading, you choose the "Open" option, the script executes and gets your cookie information with no problems at all.

Actually, that script could use XmlHttpRequest object to call the server and do some malicious operations within the session of the user who downloaded the file.

Is there any way to avoid this? We have tested that both Chrome and Firefox do not let this happen. How could this behaviour be avoided in any browser, including IE8?

+2  A: 

Don't allow the upload of arbritary content. It's exclusively a terrible idea.

One potential "solution" could be to only host the untrusted uploads on a domain that doesn't have any cookies and that the user doesn't associate any trust with in any way. This would be a "solution", but certainly not the ideal one.

Some more practical options could be an authorisation-based process, where each file goes through an automated review and then a manual confirmation of the automated cleaning/analysis phase.

All in all though, it's a very bad idea to allow the general public to do this.

Noon Silk
+1 for storing the uploads on a different domain and the general notion that this is always dangerous
Pekka
We will consider the option of downloading files from a different domain, and will keep an eye on the system to see if we are eventually forced to reject any dangerous upload. Thanks very much for your help.
antur123
+1  A: 

If you really need to have the users upload HTML files, you should make sure the HTML files in this directory are served with the mime type text/plain rather than text/html or similar.

This will prevent the opened files from executing scripts in the browser. If you're using apache, see the AddType directive.

Kristian J.
This is a good suggestion, and may do the trick. I'd still be concerned about serving the content from the same domain, but I think this should probably be done regardless.
Noon Silk
A: 

That's a really bad idea from a security point of view. Still, if you wish to do this, include HTTP response header Content-disposition: attachment It will force browser to download file instead of opening it. In Apache, it's done by adding Header set Content-disposition "attachment" to .htaccess file.

Note that it's a bad idea just to add Content-type: text/plain as mentioned in one of the answers, because it won't work for Internet Explorer. When IE receives file with text/plain content-type header, it turns on its MIME sniffer which tries to define file's real content-type (because some servers send all the files with text/plain). In case it meets HTML code inside a file, it will force the browser to serve file as text/html and render it.

p0deje