通过在请求中添加新参数来跨站点脚本问题

时间:2017-02-01 13:28:43

标签: php laravel security xss

1 个答案:

答案 0 :(得分:0)

您可以将此类用作中间件

class XSSProtection
{
    /**
     * The following method loops through all request input and strips out all tags from
     * the request. This to ensure that users are unable to set ANY HTML within the form
     * submissions, but also cleans up input.
     *
     * @param Request $request
     * @param callable $next
     * @return mixed
     */
    public function handle(Request $request, \Closure $next)
    {
        if (!in_array(strtolower($request->method()), ['put', 'post'])) {
            return $next($request);
        }

        $input = $request->all();

        array_walk_recursive($input, function(&$input) {
            $input = strip_tags($input);
        });

        $request->merge($input);

        return $next($request);
    }
}

参考:https://gist.github.com/kirkbushell/5d40fdd7f7b364716742