what is the danger in permitting members to upload documents to your database?

Nemetona
Nemetona used Ask the Experts™
on
We permit members to upload certain file types; pdf, docs, xlms, jpg and some other image file types  from our website into a database table where they are stored as a longblob (MySQL) and the documents/images can be viewed / downloaded by the member as well.

What is the danger of them introducing some malevolent code that would affect our database or our server?  What steps could we take to mitigate this?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
into a database table where they are stored as a longblob (MySQL)
I have never understood why one would want to store documents in a DB Table. The OS file system is a perfectly good store and far simpler than extracting blobs from a database. Best practice is store the document in the file system and the path in the database.

The danger is what you would expect from allowing content from untrusted sources onto your network.
Here are some things to consider
1. Assume all incoming documents are suspect and treat accordingly. Scan all incoming documents for malware
2. Store the documents outside of the webroot to prevent direct access to the documents
3. Store the path to the document in the database.
4. Limit uploads only to the document types you allow
5. Do not trust the extension or the mime type in the header - test the document to ensure it is of the claimed type.

Author

Commented:
Could you provide any pointers on "test the document to ensure it is of the claimed type. "
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
What is your scripting environment.
In PHP you would use something like finfo_file()
Code might look something like this
$allowedtypes = array(
  'text/html',
  'image/gif',
  'application/vnd.ms-excel'
);
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mimetype = finfo_file($finfo, $filename);
$valid = false;
foreach($allowedtypes as $t) {
    if ($mimetype == $t) {
      $valid = true;
      break;
     }
}
if (!$valid) {
   echo "File type not allowed ....";
}

Open in new window

Author

Commented:
Thank you for that.  We are using javascript.
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
We are using javascript.
That is client side - what are you using to connect to MySQL?

Author

Commented:
java

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial