How To Create Middleware For XSS Protection In Laravel 8

Websolutionstuff | Dec-22-2021 | Categories : Laravel PHP

In this tutorial we will see how to create middleware for xss protection in laravel 8. 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 8 we are use middleware for prevent xss attack on website security. It's very necessory protection from xss attack or any other cyber attack on website.

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.

What are the types of XSS attacks?

There are three main types of XSS attacks. These are:

  • 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.

 

 

So, let's see how to create middleware for xss protection in laravel 8.

Step 1 : Create Middleware

In this step, We have to create custom middleware for xss prevention in laravel. So, copy below command and run on 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 bellow 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 use 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
Laravel 9 Image Upload Example
Laravel 9 Image Upload Example

In this tutorial, I will explain the laravel 9 image upload example. image or file upload is the most common task in web...

Read More

Feb-28-2022

How To Use OpenAI In Laravel 8/9
How To Use OpenAI In Laravel 8...

In this article, we will explore the integration of OpenAI into Laravel versions 8, 9, and 10. Our focus will be on unde...

Read More

Feb-06-2023

Laravel 8 Database Seeder Example
Laravel 8 Database Seeder Exam...

In this article, we will see the laravel 8 database seeder example. As we all know laravel framework provides...

Read More

Oct-23-2020

How To Drop Foreign Key In Laravel 10 Migration
How To Drop Foreign Key In Lar...

In this article, we will explore the process of removing foreign key constraints in Laravel 10 migrations. We will delve...

Read More

Apr-28-2023