Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can XSS be avoided in HTML downloads?

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?

like image 823
German Latorre Avatar asked Oct 13 '10 10:10

German Latorre


3 Answers

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.

like image 166
Noon Silk Avatar answered Oct 22 '22 15:10

Noon Silk


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.

like image 5
p0deje Avatar answered Oct 22 '22 15:10

p0deje


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.

like image 1
Kristian J. Avatar answered Oct 22 '22 15:10

Kristian J.