nasrulhazim
8/15/2018 - 7:54 AM

XSSProtection Middleware

XSSProtection Middleware

<?php

namespace App\Http\Middleware;

use Closure;

class XSSProtection
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (!in_array(strtolower($request->method()), ['put', 'post'])) {
            return $next($request);
        }

        $input = $request->all();

        array_walk_recursive($input, function (&$input) {
            if (is_string($input)) {
                $input = htmlspecialchars($input);
            }
        });

        $request->merge($input);

        return $next($request);
    }
}