DEV Community

Cover image for Prevent Unvalidated Redirects in Laravel Securely
Pentest Testing Corp
Pentest Testing Corp

Posted on

Prevent Unvalidated Redirects in Laravel Securely

πŸ›‘ Introduction

Unvalidated redirects and forwards are serious security risks in Laravel applications. Attackers can exploit these vulnerabilities to redirect users to malicious websites or gain unauthorized access.

Preventing Unvalidated Redirects in Laravel Securely

In this guide, we’ll explore how to:

βœ… Identify unvalidated redirects and forwards in Laravel
βœ… Prevent them with secure coding practices
βœ… Test your application for security flaws


πŸ” What Are Unvalidated Redirects and Forwards?

Unvalidated redirects occur when an application accepts untrusted input for redirection without validation.

❌ Insecure Redirect Example in Laravel

use Illuminate\Http\Request;

public function redirectTo(Request $request)
{
    $url = $request->input('url');
    return redirect($url);
}
Enter fullscreen mode Exit fullscreen mode

🚨 Problem: If an attacker supplies a malicious URL, users could be redirected to a phishing site.

Similarly, forwards involve sending users to a new page within the application based on user input, potentially bypassing authentication.


⚠ Security Risks of Unvalidated Redirects

πŸ”΄ Phishing Attacks – Attackers can redirect users to fake login pages.
πŸ”΄ Session Hijacking – Malicious redirects can steal session cookies.
πŸ”΄ Bypassing Authorization – Forwarding users based on input could lead to privilege escalation.


βœ… How to Prevent Unvalidated Redirects in Laravel

1️⃣ Use a Whitelist for Allowed Redirects

Only allow redirects to specific, predefined URLs.

use Illuminate\Http\Request;

public function redirectTo(Request $request)
{
    $url = $request->input('url');
    $allowedUrls = [
        'https://trusted-site.com/home',
        'https://trusted-site.com/dashboard',
    ];

    if (in_array($url, $allowedUrls)) {
        return redirect($url);
    }

    return redirect('/default');
}
Enter fullscreen mode Exit fullscreen mode

2️⃣ Use Named Routes Instead of URLs

Laravel’s named routes help prevent malicious redirects.

use Illuminate\Http\Request;

public function redirectTo(Request $request)
{
    $routeName = $request->input('route');

    if (Route::has($routeName)) {
        return redirect()->route($routeName);
    }

    return redirect()->route('home');
}
Enter fullscreen mode Exit fullscreen mode

3️⃣ Avoid Using Untrusted Input

Never use raw user input in redirect functions. Instead, define redirect destinations explicitly.

public function redirectToDashboard()
{
    return redirect()->route('dashboard');
}
Enter fullscreen mode Exit fullscreen mode

πŸ›  Testing for Unvalidated Redirects

Regular security testing is crucial for Laravel applications. You can use the Free Website Security Scanner to scan your website for vulnerabilities.

πŸ“Έ Security Scanner Webpage Screenshot

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 for Website Security test.

Once the scan is complete, you will get a detailed vulnerability assessment report with security recommendations.

πŸ“œ Example: Vulnerability Report Screenshot

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 to check Website Vulnerability, providing insights into possible vulnerabilities.


🎯 Conclusion

Unvalidated redirects and forwards pose a major security risk in Laravel applications.

By following these best practices:

βœ” Validating user input
βœ” Using named routes
βœ” Conducting regular security scans

You can protect your users from malicious attacks.

For more security insights, visit the Pentest Testing Corp Blog. πŸš€


Top comments (0)