Last active
January 21, 2021 13:56
-
-
Save blood72/e66a6f6840f07516b5cdf7395349adff to your computer and use it in GitHub Desktop.
(Laravel) Allow request only if Referrer header is base same domain
This file contains hidden or 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
<?php | |
namespace App\Http\Middleware; | |
use Closure; | |
use Illuminate\Support\Facades\URL; | |
use Illuminate\Support\Str; | |
class RestrictFromReferer | |
{ | |
/** | |
* Handle an incoming request. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @param \Closure $next | |
* @param int $status | |
* @return mixed | |
*/ | |
public function handle($request, Closure $next, int $status = 403) | |
{ | |
$referer = $request->header('referer', ''); | |
if (! Str::endsWith(base_domain($referer), URL::domain())) { | |
abort($status); | |
} | |
return $next($request); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
base_domain()
URL::domain()