I am developing using CodeIgniter framework.
I have an image upload folder which permission is set to 755. My host is using suPHP so site users are able to upload to it.
I just noticed that when user uploads an image, the IMAGE file permission is 666.
I am naming the images using usernames so it would be quite easy for someone to guess that someone with username ‘hello’ would have an image stored in that directory named ‘hello.jpg’ or ‘hello.png’
Is this unsafe? Could a malicious visitor do something like erase or replace a member’s image from that image upload directory?
Do I have to name the image something random and then store that random image name into that user’s db table row so I know which image name matches which user?
You should always validate any user input of course, but you could in this case simply check that the current user’s username matches the name being used as the filename (assuming you authenticate the users prior to allowing them to upload), and ensure they have no means to specify the filename via anything they input.
In short, authenticate, and name your files using known user info’, server-side data only.