Blog | G5 Cyber Security

Web App File Access: A Security Risk?

TL;DR

Yes! Letting a web application move, copy or delete files outside its designated server root directory is a major security risk. It can lead to data breaches, system compromise and denial of service.

Why it’s dangerous

Web applications should only have access to the files they absolutely need to function. Allowing them broader file system access opens up several attack vectors:

How it happens

This vulnerability often arises from poorly written or configured applications that:

How to fix it

  1. Restrict File Access: Configure your web server (e.g., Apache, Nginx) and application framework to limit file system access to a specific directory – the web root. This is the most important step.
  2. Input Validation: Always validate any user-supplied input that relates to filenames or paths. Use whitelisting (allowing only known good characters/paths) instead of blacklisting (blocking known bad ones). For example:
    // Example in PHP - very basic, improve for production
    $allowed_chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0-9._-';
    if (preg_match('/^[a-zA-Z0-9._-]+$/', $_POST['filename'])) {
      // Safe filename
    } else {
      // Invalid filename - reject the request
    }
    
  3. Use Absolute Paths: Always use absolute paths when working with files. This prevents attackers from using relative paths to escape the intended directory.
    // Example in PHP
    $base_dir = '/var/www/mywebapp/uploads/'; // Your web root
    $filepath = $base_dir . $_POST['filename'];
    
  4. Implement Permissions Checks: Ensure the application verifies that the user has permission to access or modify the requested files. Use appropriate file system permissions (e.g., using chmod and chown on Linux).
  5. Principle of Least Privilege: Run your web application with the minimum necessary privileges. Avoid running it as root if possible.
  6. Regular Security Audits: Regularly review your code and configuration for potential vulnerabilities, including file access issues. Consider using automated security scanning tools.
  7. Web Application Firewall (WAF): A WAF can help detect and block malicious requests that attempt to exploit file system vulnerabilities.

Example Scenario

Imagine a web application allows users to upload files. If the application doesn’t properly validate the filename, an attacker could upload a file named ../../../../etc/passwd. If the application then attempts to move this file, it could overwrite critical system configuration files.

cyber security best practice

Treat all user input as potentially malicious and implement robust validation and sanitization measures. Restricting file access is a fundamental cyber security principle for web applications.

Exit mobile version