To follow this tutorial, you will need the following:
- Python 3.7+ installed on your machine
- Basic knowledge of Python and FastAPI
- A text editor or lightweight IDE such as Visual Studio Code
Why FastAPI?
FastAPI is a modern web framework for Python that enables you to build APIs quickly. It’s built on standard Python type hints, making it easier to work with and maintain. With built-in support for asynchronous request handling, it’s also ideal for high-performance applications.
Combined with Verisys Antivirus API, FastAPI helps you build secure file upload endpoints, crucial for any web or mobile app handling potentially unsafe files.
Project Scaffold/Setup
The first step is to create a Python environment, ready with the libraries we will use for the FastAPI project.
-
Open a terminal or command prompt, and navigate to the directory where you want to store the project
-
Create and activate a virtual environment
-
Install FastAPI, Uvicorn and
python-multipart
:1
pip install fastapi uvicorn python-multipart
-
For this tutorial, we’ll also use the
requests
library to send uploaded files to Verisys Antivirus API for malware scanning:1
pip install requests
Creating the FastAPI App
Let’s start by setting up a simple FastAPI app that will accept file uploads. Create a file main.py
with this content:
|
|
Starting the FastAPI App
To run the API using the Uvicorn web server, execute the following from the same folder as main.py
:
|
|
The --reload
flag allows the server to automatically reload as we make changes to the code, which is helpful during development.
Once Uvicorn starts, you’ll see output indicating that the app is running, along with the URL where it’s accessible. By default, it will run at:
|
|
Testing the API
To test the basic file upload functionality, you can simply use curl
, or you could a tool like Postman or Insomnia.
Here’s an example using curl, with a file testfile.png
(substitute for any file you want to upload):
|
|
You should see a result similar to:
|
|
Integrating Verisys Antivirus API
Uploading files is a start, but ensuring that uploaded files are free of malware is critical, especially when accepting files from end users. Verisys Antivirus API allows us to scan files before processing them further.
Verisys Antivirus API is a language-agnostic REST API that allows you to easily add malware scanning to mobile apps, web apps and backend processing.
By scanning user-generated content and file uploads, Verisys Antivirus API can stop dangerous malware at the edge, before it reaches your servers, applications - or end users.
Here’s how to integrate Verisys Antivirus API to scan uploaded files:
-
Get your Verisys API Key: Before we start, ensure you have your Verisys Antivirus API key. If you don’t have one, visit Verisys Antivirus API to get started or request a trial.
-
Send the file to Verisys Antivirus API: Once the file has been received, send it to the Verisys API for scanning.
Update main.py
with this content:
|
|
In this modified version, after the file is uploaded, it’s passed to the scan_file function, which sends the file to Verisys Antivirus API for scanning. The response from Verisys is returned as part of the result, indicating whether the file is safe or malicious.
Testing the Completed API
To test the file upload and scanning functionality, as before you can use curl
or another tool of your preference.
Here’s an example using curl, with an EICAR Anti-Virus Test File eicar.png
(substitute for any file you want to upload):
|
|
Depending on the file you upload, you should see the malware scan result:
|
|
Your upload endpoint could use the scan result to determine what to do next - for example, to prevent uploading files that contain malware.
Note that while in this example the file masqueraded as a PNG image file, Verisys Antivirus API detected that it was actually a plain text file!
Handling File Uploads Securely
Here are a couple of additional tips to ensure your file upload system is secure:
-
Limit File Size: Ensure you’re not accepting excessively large files, which could cause performance issues or even DoS attacks. FastAPI allows you to define custom file size limits.
-
Restrict File Types: Only accept specific file types (e.g., PDFs, images) to prevent the upload of executable or malicious files.
Checking the file extension and Content-Type
header are basic steps towards securing file uploads. However, both of these can easily be spoofed.
By scanning the actual file content, Verisys Antivirus API can identify 50+ different file formats for you, while also scanning files for malware.
Why Choose Verisys Antivirus API?
Verisys Antivirus API is specifically designed to ensure secure file uploads in your applications. Here’s why it’s a great fit for your project:
- Powerful Malware Detection: Verisys detects a wide range of threats, ensuring that no malicious files slip through.
- Easy Integration: The API is simple to integrate with any web framework, including FastAPI.
- Content Type Detection: Alongside malware scanning, the API also scans the file content to determine the real file type.
Conclusion
Handling file uploads is essential for many applications, but steps must be taken to ensure security. By using FastAPI and Verisys Antivirus API, you can create a secure file upload endpoint that scans files for malware before processing them further. Whether you’re building a web app or an API for file storage, this approach ensures that your infrastructure - and your users - remain safe from harmful files.
If you haven’t yet, sign up for Verisys Antivirus API today and start protecting your application against malware!