I read couple of blogs and came across these guidelines:
Firstly where to store the image:
- Amazon S3. The uploaded image can simply uploaded to Amazon and the URL stored in the DB.
- Save the files to the disk or disk server or cloud
Flaws in storing in a Database:
- Losing space on the database server.
- Slows down the database server by:
- Transmitting the entire picture in it’s query response to the webserver.
- Webserver sends this to the requesting browser.
- Large Bandwidth usage
- If the user is uploading the images to the server will effect hosting price.
- Database is not designed to work as file storage.
Where not to store:
- Not within the website structure where they can be accessed by a URL.
- Not in a username folder.
- Do not store all the image in one folder. Create different folders and limit the number of images that can be stored in a folder
Please read the below link to check how many files can be saved in a directory. Consider the fact that its an old answer and may differ from todays facts
https://stackoverflow.com/questions/466521/how-many-files-can-i-put-in-a-directory
Precautions while storing image:
- Avoid user provided image names.
- Resize the images.
- Open and check the files contents to be an image. Do not rely on the file extensions and mime type
Guideline while uploading images:
These are taken from acunetix.com
Define an .htaccess file that will only allow access to files with allowed extensions.
Do not place the .htaccess file in the same directory where the uploaded files will be stored, instead, place it in the parent directory. This way the .htaccess file can never be overwritten by an attacker.
A typical .htaccess which allows only GIF, JPG, JPEG, and PNG files should include the following (this should be adapted as necessary for specific requirements). The following will also prevent double extension attacks:
deny from all <
files ~ “^w+.(gif|jpe?g|png)$”>
order deny,allow
allow from all
</files>
If possible, upload the files in a directory outside the server root.
Prevent overwriting of existing files (to prevent the .htaccess overwrite attack).
Create a whitelist of accepted MIME-types (map extensions from these MIME-types).
Generate a random file name and add the previously generated extension.
Don’t rely on client-side validation only, since it is not enough. Ideally, both server-side and client-side validation should be implemented.
For Best Size please read the guidelines here:
https://flothemes.com/flothemes-image-sizes/
Good resources to read:
https://www.acunetix.com/websitesecurity/upload-forms-threat/
No comments:
Post a Comment