Blog | G5 Cyber Security

PDF Upload Security Risks

TL;DR

Yes, an inaccessible (or seemingly harmless) uploaded PDF can harm a server. PDFs can contain malicious code that exploits vulnerabilities in PDF viewers or the server itself. Proper validation and sanitisation are crucial.

How a PDF Can Be Harmful

Even if you don’t display the PDF directly in a browser, simply storing it can be risky. Here’s how:

Steps to Protect Your Server

  1. Input Validation: Always validate the file extension and MIME type before accepting an upload.
    • Don’t rely solely on the client-side check (it can be bypassed).
    • Use server-side checks. For example, in PHP:
  2. File Size Limits: Restrict the maximum allowed PDF file size to prevent DoS attacks.
    • Configure this in your web server settings (e.g., Apache, Nginx) or application code.
  3. Sanitisation/Scanning: This is the most important step.
    • Virus Scanning: Use a reputable antivirus scanner to scan uploaded PDFs before storing them. ClamAV is a popular open-source option.
      clamscan /path/to/uploaded/pdf_file.pdf
    • PDF Parsing and Validation Libraries: Use libraries specifically designed for PDF parsing to identify potentially malicious content.
      • PDFiD: A Python tool that identifies features within a PDF file, helping detect suspicious elements.
      • peepdf: Another Python library for analyzing PDFs; it can help find JavaScript and other embedded objects.
      • Ghostscript: While powerful, be cautious when using Ghostscript directly as it has had security vulnerabilities in the past. Use a wrapper or carefully control its input.
  4. Sandboxing/Isolation: If you need to process PDFs server-side (e.g., for indexing), do so within a sandboxed environment.
    • Containers (Docker) or virtual machines can provide isolation.
  5. Regular Updates: Keep your PDF viewers, libraries, and operating system up to date with the latest security patches.
  6. Content Security Policy (CSP): If you display PDFs in a browser, use CSP headers to restrict JavaScript execution.
    • Example header:
      Content-Security-Policy: script-src 'self'
  7. Storage Location and Permissions: Store uploaded PDFs in a dedicated directory with limited permissions. Prevent direct execution of scripts from that directory.

Important Considerations

Exit mobile version