Skip to main content

Remote File Inclusion


Prevention of remote file inclusion attacks


  • Usage of Python 3 for developing applications and scripts
  • Usage of Django for building web applications in Python
  • Usage of urllib for making HTTP requests


Non compliant code

from django.http import HttpResponse
import urllib.request

def load_remote_file(request):
remote_file_url = request.GET.get('file_url')
remote_file = urllib.request.urlopen(remote_file_url).read()
return HttpResponse("File executed.")

In the above code, the function load_remote_file(request) is vulnerable to Remote File Inclusion (RFI). This function takes a URL as a parameter from the GET request (file_url) and then uses urllib.request.urlopen() to fetch the file from the remote location. The content of the file is then read and executed using the exec() function.

The vulnerability here is that an attacker can provide a URL of a malicious script as file_url, which the server will then download and execute. This can result in arbitrary code execution on the server, potentially leading to data theft, data manipulation, or a complete system compromise.

The exec() function is particularly dangerous as it executes the argument as Python code. This means that any Python code supplied by the attacker will be executed by the server, which poses a severe security risk.

In addition, there are no checks or sanitization on the file_url parameter, meaning that an attacker can supply any URL, including those pointing to malicious files. This lack of input validation further increases the risk of this vulnerability.


  • Validate the input: Ensure that the 'file_url' parameter is a valid URL and does not contain any malicious content.
  • Whitelist URLs: Only allow URLs from trusted sources to be loaded. This can be done by maintaining a list of trusted URLs and checking the 'file_url' against this list.
  • Remove the use of 'exec': The 'exec' function is dangerous as it can execute any Python code. Instead of using 'exec', find a safer way to process the file content.
  • Use a safer method to load files: Instead of loading and executing the file directly, consider downloading the file, scanning it for malicious content, and then processing it in a safe manner.
  • Implement error handling: If the file cannot be loaded or executed, the application should handle this gracefully and not crash or expose sensitive information.

Compliant code

from django.http import HttpResponse
import urllib.request
import os

TRUSTED_URLS = ['', '']

def validate_url(url):
# Check if the URL is in the list of trusted URLs
if url in TRUSTED_URLS:
return True
return False

def load_remote_file(request):
remote_file_url = request.GET.get('file_url')

# Validate the URL
if not validate_url(remote_file_url):
return HttpResponse("Invalid URL.")

# Download the file instead of executing it
remote_file = urllib.request.urlretrieve(remote_file_url, '/path/to/download/directory')
except Exception as e:
return HttpResponse("Failed to load the file.")

# Process the file safely here
# ...

return HttpResponse("File loaded successfully.")

The updated code includes several changes to address the Remote File Inclusion vulnerability:

  1. Validation of URLs: A function validate_url is added to check if the provided URL is in the list of trusted URLs. If the URL is not in the list, the function returns an HTTP response indicating that the URL is invalid.

  2. Removal of exec function: The exec function, which can execute any Python code, is removed. Instead of executing the file, the file is now downloaded using urllib.request.urlretrieve.

  3. Safe file processing: After the file is downloaded, it can be processed in a safe manner. This part is indicated by the comment # Process the file safely here.

  4. Error handling: If there is an error while downloading the file (for example, if the URL is not reachable), the application will return an HTTP response indicating that the file loading failed. This prevents the application from crashing and potentially exposing sensitive information.

  5. Trusted URLs: A list of trusted URLs is maintained. Only files from these URLs are allowed to be downloaded. This prevents an attacker from loading malicious files from untrusted sources.