hyperf 和 laravel、lumen防止xss攻击中间件封装

hyper

f 防止xss攻击自定义中间件封装

<?php

declare(strict_types=1);

namespace AppMiddleware;

use PsrContainerContainerInterface;
use PsrHttpMessageResponseInterface;
use PsrHttpServerMiddlewareInterface;
use PsrHttpMessageServerRequestInterface;
use PsrHttpServerRequestHandlerInterface;

class XSSProtectionMiddleware implements MiddlewareInterface
{
    /**
     * @var ContainerInterface
     */
    protected $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        $input = $request->all();

        array_walk_recursive($input, function(&$input) {
            $input = (strip_tags(htmlspecialchars(htmlentities($input, ENT_QUOTES, 'UTF-8'))));
        });

        $request = $request->withQueryParams($input);

        return $handler->handle($request);
    }
}

laravel、lumen防止xss攻击自定义中间件封装

<?php

namespace AppHttpMiddleware;

use Closure;
use IlluminateHttpRequest;

class XSSProtectionMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  IlluminateHttpRequest  $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(htmlspecialchars(htmlentities($input, ENT_QUOTES, 'UTF-8'))));
        });

        $request->merge($input);

        return $next($request);
    }
}