CWE-616: Incomplete Identification of Uploaded File Variables (PHP)

Learn about CWE-616 (Incomplete Identification of Uploaded File Variables (PHP)), its security impact, exploitation methods, and prevention guidelines.

What is Incomplete Identification of Uploaded File Variables (PHP)?

• Overview: This vulnerability occurs when a PHP application uses outdated methods for processing uploaded files by relying on certain global variables (like $varname, $varname_size, $varname_name, $varname_type). These variables can be manipulated by attackers to process unauthorized files.

• Exploitation Methods:

  • Attackers can exploit this vulnerability by overwriting these global variables through POST requests or cookies.
  • Common attack patterns include providing malicious values such as file paths to sensitive files (e.g., "/etc/passwd") to gain unauthorized access or read arbitrary files.

• Security Impact:

  • Direct consequences include unauthorized file access and processing, potentially exposing sensitive data.
  • This can lead to further system compromise if attackers gain access to critical configuration files or user data.
  • The business impact may include data breaches, loss of customer trust, and potential legal liabilities.

• Prevention Guidelines:

  • Use the newer PHP superglobals like $_FILES to handle file uploads securely, as they are less prone to manipulation.
  • Validate and sanitize all user inputs meticulously to prevent overwriting of the intended file variables.
  • Implement robust security practices such as input validation, output encoding, and employing secure coding standards.
  • Utilize security-focused frameworks and tools that help manage file uploads safely, such as Laravel or Symfony, which provide built-in protections against these kinds of vulnerabilities.
Corgea can automatically detect and fix Incomplete Identification of Uploaded File Variables (PHP) in your codebase. [Try Corgea free today](https://corgea.app).

Technical Details

Likelihood of Exploit: Not specified

Affected Languages: PHP

Affected Technologies: Not specified

Vulnerable Code Example

// Vulnerable code using outdated global variables for file uploads
// These variables can be manipulated by an attacker through query parameters
if (isset(\$file) && \$file_size > 0) { 
    // Potentially malicious user can overwrite \$file, \$file_size, \$file_name, \$file_type
    move_uploaded_file(\$file, "/uploads/" . \$file_name); 
    echo "File uploaded successfully.";
} else {
    echo "No file uploaded.";
}

How to fix Incomplete Identification of Uploaded File Variables (PHP)?

The vulnerability arises because the code relies on global variables for file uploads, which can be manipulated by an attacker. The solution is to use PHP's \$_FILES superglobal array, which securely handles file uploads and cannot be tampered with via query parameters. This array provides a structured and secure way to access file data, ensuring that only legitimate uploaded files are processed.

Specific Fixes:

  1. Use \$_FILES superglobal instead of outdated global variables.
  2. Perform checks using the \$_FILES array to ensure the file was uploaded via HTTP POST.
  3. Validate file types and sizes before processing the upload.

Fixed Code Example

// Fixed code using \$_FILES superglobal for secure file upload handling
if (isset(\$_FILES['file']) && \$_FILES['file']['error'] === UPLOAD_ERR_OK) {
    // Ensure the file was uploaded without errors
    \$uploadedFile = \$_FILES['file'];
    \$filename = basename(\$uploadedFile['name']);
    \$destination = "/uploads/" . \$filename;

    // Validate the file type and size
    \$allowedTypes = ['image/jpeg', 'image/png', 'application/pdf'];
    if (in_array(\$uploadedFile['type'], \$allowedTypes) && \$uploadedFile['size'] <= 2000000) {
        move_uploaded_file(\$uploadedFile['tmp_name'], \$destination);
        echo "File uploaded successfully.";
    } else {
        echo "Invalid file type or size.";
    }
} else {
    echo "No file uploaded or upload error.";
}

Additional Security Best Practices:

  • Always validate the file type and size to reject potentially harmful files.
  • Use a secure directory for file storage, and ensure the directory is not directly web-accessible.
  • Implement server-side validation to prevent client-side tampering.
  • Consider using unique file names to avoid overwriting existing files, such as appending a timestamp or a unique ID.
Corgea Logo

Find this vulnerability and fix it with Corgea

Scan your codebase for CWE-616: Incomplete Identification of Uploaded File Variables (PHP) and get remediation guidance

Start for free and no credit card needed.