Unrestricted File Upload is a high-risk web vulnerability that allows attackers to upload malicious files such as PHP shells, JavaScript payloads, or ransomware. In Symfony-based applications, improper file validation can make your entire server vulnerable to exploitation.
In this post, we’ll explore how unrestricted file uploads can be exploited in Symfony, how to prevent them using secure coding practices, and how you can scan your site for free with our free Website Security Scanner tool.
🔗 Also check out our Pentest Testing Blog for more cybersecurity tutorials.
An unrestricted file upload vulnerability occurs when a web application accepts files from users without properly validating:
File type
File extension
File size
MIME type
Upload destination
In Symfony apps, if file uploads aren't restricted correctly, attackers could upload backdoors or scripts and execute them on your server.
A hacker could upload a .php web shell and access it through a browser, giving them remote access to your system:
// Example of a malicious PHP web shell
<?php
if(isset($_REQUEST['cmd'])){
echo "<pre>";
$cmd = ($_REQUEST['cmd']);
system($cmd);
echo "</pre>";
die;
}
?>
Once this file is uploaded to your Symfony app and accessed via the browser, attackers can run any system command on your server.
Here’s how you can secure file uploads in Symfony with practical code examples:
Use Symfony’s built-in file validation using annotations or YAML/XML config to restrict MIME types and extensions.
use Symfony\Component\Validator\Constraints as Assert;
class UploadFile
{
/**
* @Assert\File(
* maxSize = "1024k",
* mimeTypes = {"image/jpeg", "image/png"},
* mimeTypesMessage = "Please upload a valid image file"
* )
*/
private $file;
}
Use PHP’s pathinfo() to reject unexpected extensions.
$allowedExt = ['jpg', 'png', 'pdf'];
$ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
if (!in_array(strtolower($ext), $allowedExt)) {
throw new \Exception('Invalid file type.');
}
To prevent execution of malicious files, always rename files on upload and store them outside the web root.
$newFileName = uniqid().'.'.$ext;
move_uploaded_file($_FILES['file']['tmp_name'], '/var/uploads/'.$newFileName);
📸 Screenshot of the webpage of our Website Vulnerability Scanner tool
Screenshot of the free tools webpage where you can access security assessment tools.
This tool checks for unrestricted file uploads and over 30+ critical vulnerabilities.
Here’s a simplified Symfony controller example:
// src/Controller/FileUploadController.php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class FileUploadController extends AbstractController
{
public function upload(Request $request): Response
{
$file = $request->files->get('upload_file');
if ($file && in_array($file->getClientMimeType(), ['image/jpeg', 'image/png'])) {
$filename = uniqid() . '.' . $file->guessExtension();
$file->move($this->getParameter('upload_directory'), $filename);
return new Response('File uploaded safely');
}
return new Response('Invalid file type', 400);
}
}
parameters:
upload_directory: '%kernel.project_dir%/var/uploads'
Insert this image here:
📸 Screenshot of a website vulnerability assessment report, generated using our tool to check Website Vulnerability
An Example of a vulnerability assessment report generated with our free tool, providing insights into possible vulnerabilities.
Need a professional audit? Our Web Application Penetration Testing Services at Pentest Testing Corp. can detect and patch file upload issues, XSS, SQLi, and more.
🔗 Visit: https://www.pentesttesting.com/web-app-penetration-testing-services/
OWASP Top 10 Coverage
Manual + Automated Testing
PDF Reports with CVSS Scores
Remediation Consultation
Before attackers do, test your site today using our Website Security Checker. No sign-up, instant vulnerability scan, and a downloadable report.
✔️ No login
✔️ Instant results
✔️ Detect 30+ issues including unrestricted file upload
For more tutorials, real-world hacking scenarios, and secure coding guides, visit:
🔗 Pentest Testing Blog
Unrestricted file upload is a dangerous but preventable issue in Symfony applications. With careful validation and secure file handling, you can shield your web app from being compromised. Combine secure coding with proactive scanning tools like ours for Website Security checks, and stay one step ahead of the attackers.