DEV Community

Cover image for Prevent Unrestricted File Upload in Symfony
Pentest Testing Corp
Pentest Testing Corp

Posted on

Prevent Unrestricted File Upload in Symfony

🚨 Unrestricted File Upload in Symfony β€” How to Fix It Securely

File upload functionality is a common feature in modern web applications, especially those built with Symfony. However, unrestricted file uploads can introduce severe vulnerabilities, allowing attackers to upload malicious scripts, execute remote code, or compromise the server.

In this blog post, we’ll cover:

  • What is Unrestricted File Upload in Symfony?
  • Real-world attack scenarios
  • How to secure file uploads in Symfony with code examples
  • Free tool to test your site
  • Links to detailed service and blog resources

Prevent Unrestricted File Upload in Symfony

πŸ” Don't forget to test your own website now using our Free Website Security Scanner!


πŸ’‘ What is Unrestricted File Upload?

An unrestricted file upload vulnerability allows a user to upload files to the server without proper validation. In Symfony applications, if you don’t validate the file's type, size, or location, a user might upload:

  • Malicious PHP scripts
  • Web shells
  • Executables or dangerous payloads

Such files can be accessed and executed, leading to Remote Code Execution (RCE) or full system compromise.


πŸ”“ Real-World Exploit Example

Let’s assume an insecure Symfony controller:

// src/Controller/UploadController.php

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class UploadController extends AbstractController
{
    public function upload(Request $request): Response
    {
        $file = $request->files->get('upload_file');
        $file->move($this->getParameter('upload_directory'), $file->getClientOriginalName());

        return new Response('File uploaded!');
    }
}
Enter fullscreen mode Exit fullscreen mode

🚩 Problems:

  • No MIME type validation
  • No file extension check
  • Uses original filename (can be exploited)
  • No size restriction

An attacker could upload shell.php, access it via URL, and run commands on your server!


πŸ›‘οΈ How to Prevent Unrestricted File Upload in Symfony

Here’s how to fix the above vulnerability step by step.

βœ… Validate File MIME Type & Extension

use Symfony\Component\HttpFoundation\File\Exception\FileException;

public function secureUpload(Request $request): Response
{
    $file = $request->files->get('upload_file');

    $allowedMimeTypes = ['image/jpeg', 'image/png', 'application/pdf'];
    $allowedExtensions = ['jpg', 'jpeg', 'png', 'pdf'];

    $extension = $file->guessExtension();
    $mimeType = $file->getMimeType();

    if (!in_array($mimeType, $allowedMimeTypes) || !in_array($extension, $allowedExtensions)) {
        return new Response('Invalid file type!', 400);
    }

    $safeFilename = uniqid() . '.' . $extension;

    try {
        $file->move($this->getParameter('upload_directory'), $safeFilename);
    } catch (FileException $e) {
        return new Response('Upload failed.', 500);
    }

    return new Response('File securely uploaded.');
}
Enter fullscreen mode Exit fullscreen mode

βœ… Set File Size Limit

Configure upload_max_filesize and post_max_size in your php.ini, and limit size in your Symfony form type as well.


πŸ”¬ Test for This Vulnerability Using Our Free Tool

You can easily check if your site is vulnerable to unrestricted file upload using our Free Website Vulnerability Scanner.

πŸ–ΌοΈ Screenshot of the website vulnerability scanner

Screenshot of the free tools webpage where you can access security assessment tools.Screenshot of the free tools webpage where you can access security assessment tools.

Just enter your website URL, and our tool will scan for common file upload issues and much more.


πŸ“„ Sample Report Screenshot

πŸ–ΌοΈ Screenshot of a website vulnerability assessment report showing file upload issues detected by the tool to check Website Vulnerability.

An Example of a vulnerability assessment report generated with our free tool, providing insights into possible vulnerabilities.An Example of a vulnerability assessment report generated with our free tool, providing insights into possible vulnerabilities.

This gives you instant, actionable insights into your website's security posture.


πŸ“š Learn More on Our Cybersecurity Blog

Stay ahead of hackers by reading our expert posts, security how-tos, and case studies.

πŸ”— Visit our blog: Pentest Testing Blog


πŸ›‘οΈ Need a Professional Security Audit?

If you're developing a Symfony app, especially for production, a professional security review is non-negotiable.

We offer an in-depth Web App Penetration Testing Service which includes:

  • OWASP Top 10 testing (including file upload flaws)
  • Business logic flaws
  • Source code review
  • Custom exploitation reports

πŸ“ž Get in touch today to secure your Symfony app before it's too late.


βœ… Final Thoughts

Unrestricted file uploads are among the easiest yet most dangerous vulnerabilities to exploit. Symfony developers must:

  • Validate MIME types and extensions
  • Rename files securely
  • Restrict file size
  • Prevent direct access to uploaded content

Make your Symfony applications resilient from day oneβ€”and don’t forget to scan your site with our free security checker.

Top comments (0)

OSZAR »