2

I have been gathering information on handling PDF files for web applications. Much of my research has been on displaying the files to the user.

What I want to learn here is how to properly handle the file on the server side. That is,

1. How to properly accept and store a new document.

Lets assume I can trust a new incoming PDF file. My current plan is to store the file outside of my web root. I will then store meta-data (title, date, doc type, location in system, etc) in a Database.

2. How to properly link a PDF file for access.

Whether the files are stored inside or outside of web-root, we do not want to use the full path of the document for an <a> tag or something of the like. We want to give as little information about the file system as possible. Lets assume my file is stored in /documents/*doctype*/*YYYY*/*YYYYMMDD*/*title*.pdf. I currently have two methods on the table for protecting the path.

A.) I can only provide a key or unique identifier for the document and when attempting to access I will look for that key along with the associated path in the database. Once the path is found the file file may be opened.

B.) I can encrypt the different pieces of the file path when output to the page and decrypt when a user attempts to view that PDF.

!!! Consideration: this may prevent my ability to open the files for viewing in browser provided sandbox environment. !!!

3. How to store potentially malicious PDF files.

Lets say I have an end user with ill intent that has decided they would like to attack my system using a PDF file. This user would be able to upload the file directly to my system without any characters being removed from the document. The file is only saved by the system and only meta-data is read. While this file is imported to my file system, will it be able to execute any malicious code? If the file is never read by the system, can it be considered safe until it is downloaded or viewed?

| improve this question | | | | |
  • What do you use in server: Java, PHP, .NET, Python, other? – mentallurg Feb 10 at 17:44
  • @mentallurg primarily PHP – FamousAv8er Feb 10 at 17:45
2

1. How to properly accept and store a new document.

I would recommend to store files in the database, not separately. A) File system is not transactional. That's why it is hard to keep it consistent with database. B) Creating of backups and restoring of particular versions from backups can lead to inconsistencies between file system and database: Database can contain references to files that don't exist, or there can be files that are not references in the database. Database will

2. How to properly link a PDF file for access.

Take your approach A). Use randomly generated unique IDs. They will not disclose your internal structure of the application (and hide the structure of your file system, if you stick to it).

3. How to store potentially malicious PDF files.

A) Check MIME type. There are many libraries that can do that for you. If the file is not PDF (not one of multiple PDF versions), then reject storing such file.

B) Set some size limit.

C) Use virus scanner (if your company or your provider has such option). Send file to virus scanner. Store it afterwards only if no virus was found.

| improve this answer | | | | |
  • Thank you for your answer. Out of curiosity if work was performed on the file in a language other than PHP (like Java) does that create additional attack vectors? – FamousAv8er Feb 10 at 18:10
  • 1
    Actually is not much important for this purpose if it is Java or PHP or .NET. The main risk is on the client side, when users download files from your server. If you accepted a malicious file, stored it and the user downloaded it, then user can have troubles. – mentallurg Feb 10 at 18:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.