A respected colleague insists that storing images on my server is insecure, especially if the file structure is easy to surmise (as we have image galleries created by the users, the naming scheme is easy to follow).
Instead he recommends storing the images above the root, and serving them using fread or fputthrough.
I cannot work out what the risks would be, or why they would be obviated when served through a script.
The overhead of such a script sounds ridiculous.
I do understand that images must be checked before being stored on the server, and to that end am using imagemagick to do a minor conversion and save to jpeg - which should get rid of any dross, as far as I would guess.
So, the questions to the great minds on SO:
Thanks all!
The typical way to serve up an image is to use an <img> tag and set the src attribute to point to a file on your web site. If you want to protect those images, you probably should use some form of password authentication. One method is HTTP Basic Authentication, which is covered in Recipe 8.10.
Put them in your web directory and secure them using . htaccess . If you want to authenticate via other means, then store the files in a directory that isn't web-accessible but is readable by the user php runs as.
I suspect that what your friend is referring to is not simply serving images but rather specifically serving user-supplied images. There are a number of security issues in serving user-supplied content. In the case of images, there are a number of ways to use an image upload to get the web server to execute code. A few of the more well-known ones include:
All of these examples lead to the ability to execute code as the web server. Although the functionality of the site requires the ability to upload files, storing them in directories that are not directly accessible by a URL makes it harder to exploit them. Similarly, using a script to serve them rather than the web server's MIME handlers insures that the images are treated as a stream of data rather than a potentially executable file.
Whether this much security is ridiculous depends on the number of users, nature of the data collected about them and the triviality of the web site. At the very least, an attacker would like to get the passwords of the users because of the tendency to synchronize them. A user on your site whose password is ABC123 will probably use that same password for email, social sites and possible banking and financial sites. Beyond passwords, if the data you collect about your users is personally identifiable or has other market value, or if you simply have a large number of users, then you have to assume that the site will be a target. That means, either be more careful serving user-supplied image files, validate them really well, or both.
I don't have a good answer to whether using imagemagick will address the security concerns. At the very least, be sure to use a current version because a quick search turned up a number of known vulnerabilities. Remember that the files which you need to worry about may blow up imagemagick, even if they do not exploit one of the known vulnerabilities, so be sure to have REALLY good error trapping.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With