From 92292649621f2aadc84ab94376244650a9f55696 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 18 May 2020 16:39:27 -0500 Subject: [PATCH] add trust hosts middleware --- src/Illuminate/Http/Middleware/TrustHosts.php | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/Illuminate/Http/Middleware/TrustHosts.php diff --git a/src/Illuminate/Http/Middleware/TrustHosts.php b/src/Illuminate/Http/Middleware/TrustHosts.php new file mode 100644 index 000000000000..8b563151adc0 --- /dev/null +++ b/src/Illuminate/Http/Middleware/TrustHosts.php @@ -0,0 +1,73 @@ +app = $app; + } + + /** + * Get the host patterns that should be trusted. + * + * @return array + */ + abstract public function hosts(); + + /** + * Handle the incoming request. + * + * @param \Illuminate\Http\Request $request + * @param callable $next + * @return \Illuminate\Http\Response + */ + public function handle(Request $request, $next) + { + if ($this->shouldSpecifyTrustedHosts()) { + Request::setTrustedHosts(array_filter($this->hosts())); + } + + return $next($request); + } + + /** + * Determine if the application should specify trusted hosts. + * + * @return bool + */ + protected function shouldSpecifyTrustedHosts() + { + return config('app.env') !== 'local' && + ! $this->app->runningUnitTests(); + } + + /** + * Get a regular expression matching the application URL and all of its subdomains. + * + * @return string|null + */ + protected function allSubdomainsOfApplicationUrl() + { + if ($host = parse_url($this->app['config']->get('app.url'), PHP_URL_HOST)) { + return '^(.+\.)?'.preg_quote($host).'$'; + } + } +}