Laravel 9 Create Middleware For XSS Protection

Websolutionstuff | Apr-30-2022 | Categories : Laravel PHP

In this article, we will see laravel 9 create middleware for XSS protection. Cross-site scripting is a type of security vulnerability that can be found in some web applications. XSS attacks enable attackers to inject client-side scripts into web pages viewed by other users. In laravel 9 we use middleware for prevent xss attack on website security. It's very necessory protection from xss attack or any other cyberattack on the website.

So, let's see how to create middleware for XSS protection in laravel 9, laravel 9 XSS validation, laravel 9 create middleware, laravel 9 XSS protection, XSS protection laravel 9.

The XSS filter through we can remove the HTML tag from our input value and also it's very important to remove html tag for the security. Input sanitization is a security protocol for checking, filtering, and cleaning data inputs from app users.

Types of XSS attacks?

There are three main types of XSS attacks:

  • Reflected XSS, where the malicious script comes from the current HTTP request.
  • Stored XSS, where the malicious script comes from the website's database.
  • DOM-based XSS, where the vulnerability exists in client-side code rather than server-side code.
Step 1: Create Middleware

In this step, We have to create custom middleware for XSS prevention in laravel 9. So, copy the below command and run it on the terminal.

php artisan make:middleware XSS

 

 

Step 2: Register Middleware

Now, register middleware in app/http/kernel.php path.

class Kernel extends HttpKernel
{
    protected $routeMiddleware = [
        'XSS' => \App\Http\Middleware\XSS::class,
    ];
}

 

Step 3 : Add code In Middleware File

In this step, we can see new file in app/Http/Middleware/XSS.php and then just put the below code in our XSS.php file. You can directly use strip_tags() in any input filed of save data in controller.

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class XSS
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        $input = $request->all();
        array_walk_recursive($input, function(&$input) {
            $input = strip_tags($input);
        });
        $request->merge($input);
        return $next($request);
    }
}

 

 

Step 4 : Route

Now, we are using XSS middleware in our routes.php file

routes/web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Middleware\XSS;
use App\Http\Controllers\UserController;

Route::group(['middleware' => ['XSS']], function () {
    Route::get('xss_prevention', [UserController::class,'xssPrevention']);
    Route::post('xss_prevention_data_store', [UserController::class,'xssPreventionStore'])->name('xssPreventionStore');
});

 


You might also like :

Recommended Post
Featured Post
How To Create Stacked Bar Chart In Laravel 9 Using Highcharts
How To Create Stacked Bar Char...

In this article, we will see how to create a dynamic stacked bar chart in laravel 9 using highchart. Here, we will...

Read More

Dec-30-2022

Laravel 9 Two Factor Authentication Using Email
Laravel 9 Two Factor Authentic...

In this article, we will see laravel 9 two-factor authentication using email. Here, we will send the OTP code to em...

Read More

Dec-22-2022

Jquery appendTo And prependTo Example
Jquery appendTo And prependTo...

In this article we will see jquery appendTo() and prependTo example. The appendTo() method inserts HTML elements at...

Read More

Dec-13-2021

How To Run Python Script In Laravel 9
How To Run Python Script In La...

In this article, we will see how to run the python scripts in laravel 9. Python is a popular programming language.&...

Read More

May-07-2022