Blog | G5 Cyber Security

Public Static Files: Security Risks & Fixes

TL;DR

Yes, having static files publicly available can create security risks. Attackers can exploit them to gain information about your system, deliver malicious content, or launch attacks. This guide explains the dangers and how to protect yourself.

Understanding the Risks

Static files (images, CSS, JavaScript, PDFs, etc.) are often overlooked in cyber security assessments, but they can be a significant vulnerability. Here’s why:

How to Mitigate Risks

Here’s how to improve the security of your static files:

1. Restrict Access

  1. Use a Web Server Configuration: Configure your web server (Apache, Nginx, IIS) to only serve static files from specific directories. Don’t make your entire filesystem accessible.
  2. Disable Directory Listing: This is crucial! Prevent attackers from browsing your file structure.
    • Apache: Add Options -Indexes to your directory configuration in your Apache config file (e.g., httpd.conf or a .htaccess file).
      <Directory /var/www/yourwebsite/static>
        Options -Indexes
        Require all granted
      </Directory>
    • Nginx: Use autoindex off; in your server block configuration.
      server {
          ... 
          location /static/ { 
              autoindex off;
              root /var/www/yourwebsite/static;
          }
      }

2. Content Security Policy (CSP)

Implement a CSP to control which resources the browser is allowed to load. This can prevent XSS attacks.

3. File Upload Security (If Applicable)

  1. Validate File Types: Only allow specific, necessary file types. Never trust the filename extension provided by the user.
    • Example (PHP):
      $allowed_types = ['jpg', 'jpeg', 'png', 'gif'];
      $file_extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
      if (!in_array($file_extension, $allowed_types)) {
        die('Invalid file type.');
      }
  2. Sanitize Filenames: Remove or replace potentially dangerous characters from filenames.
    $safe_filename = preg_replace('/[^a-zA-Z0-9._-]/', '_', $filename);
  3. Store Files Outside Web Root: Don’t store uploaded files directly in your web server’s document root. Serve them through a script that handles access control.
  4. Scan for Malware: Use an antivirus scanner to check uploaded files before storing them.

4. Regular Audits & Updates

  1. Regularly Scan Files: Check your static file directories for unexpected or modified files.
  2. Keep Software Updated: Ensure your web server, operating system, and any related software are up-to-date with the latest security patches.

5. Use a CDN (Content Delivery Network)

CDNs can help by offloading static file serving to their infrastructure, often including built-in security features like DDoS protection and malware scanning.

Exit mobile version