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?