-
-
Save erowsika/d35b02bbc02194c73d4c26e46ba3cd0b to your computer and use it in GitHub Desktop.
Laravel 5 XSS protection middleware
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment