Secure file upload in PHP web applications

This is an old paper I wrote about vulnerabilities in file upload implementations in PHP web applications. The web site it used to live on no longer exists, so I thought I repost it here to have it close at hand. It is relevant to other web application technologies (Java, .NET, etc.) but all the examples are for PHP.

Introduction

Various web applications allow users to upload files. Web forums let users upload avatars. Photo galleries let users upload pictures. Social networking web sites may allow uploading pictures, videos, etc. Blog sites allow uploading avatars and/or pictures.

Providing file upload function without opening security holes proved to be quite a challenge in PHP web applications. The applications we have tested suffered from a variety of security problems, ranging from arbitrary file disclosure to remote arbitrary code execution. In this article I am going to point out various security holes occurring in file upload implementations and suggest a way to implement a secure file upload.

Secure File Upload in PHP Web Applications - download PDF

AttachmentSize
PDF icon php-file-upload.pdf360.21 KB

Comments

Worth mentioning that this implementation is still vulnerable to cross-origin attacks via Flash/PDF documents. See https://soroush.secproject.com/blog/2014/05/even-uploading-a-jpg-file-ca.... Safest thing to do would be to host the uploaded content on a separate domain (like Google/Facebook/GitHub do). You can also add the "Content-disposition: attachment" header to responses as well to help limit the impact (this will prevent both cross-site flash and JavaScript includes). File content inspection is also an option, but comes with its own set of pitfalls.