Skip to content

Commit

Permalink
Fix usage of config before its resolved
Browse files Browse the repository at this point in the history
  • Loading branch information
axlon committed Apr 1, 2024
1 parent 978ee88 commit 8e0eb64
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
29 changes: 20 additions & 9 deletions src/Illuminate/Http/Middleware/TrustHosts.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ class TrustHosts
*/
protected static $alwaysTrust;

/**
* Whether to trust subdomains of the application url.
*
* @var bool|null
*/
protected static $subdomains;

/**
* Create a new middleware instance.
*
Expand All @@ -39,9 +46,17 @@ public function __construct(Application $app)
*/
public function hosts()
{
return is_array(static::$alwaysTrust)
? static::$alwaysTrust
: [$this->allSubdomainsOfApplicationUrl()];
if (is_array(static::$alwaysTrust)) {
$hosts = static::$alwaysTrust;

if (static::$subdomains) {
$hosts[] = $this->allSubdomainsOfApplicationUrl();
}

return $hosts;
}

return [$this->allSubdomainsOfApplicationUrl()];
}

/**
Expand Down Expand Up @@ -69,13 +84,8 @@ public function handle(Request $request, $next)
*/
public static function at(array $hosts, bool $subdomains = true)
{
if ($subdomains) {
if ($host = parse_url(config('app.url'), PHP_URL_HOST)) {
$hosts[] = '^(.+\.)?'.preg_quote($host).'$';
}
}

static::$alwaysTrust = $hosts;
static::$subdomains = $subdomains;
}

/**
Expand Down Expand Up @@ -109,5 +119,6 @@ protected function allSubdomainsOfApplicationUrl()
public static function flushState()
{
static::$alwaysTrust = null;
static::$subdomains = null;
}
}
3 changes: 0 additions & 3 deletions tests/Foundation/Configuration/MiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,6 @@ protected function allSubdomainsOfApplicationUrl()
$configuration->trustHosts();
$this->assertEquals(['^(.+\.)?laravel\.test$'], $middleware->hosts());

app()['config'] = Mockery::mock(Repository::class);
app()['config']->shouldReceive('get')->with('app.url', null)->times(3)->andReturn('http://laravel.test');

$configuration->trustHosts(at: ['my.test']);
$this->assertEquals(['my.test', '^(.+\.)?laravel\.test$'], $middleware->hosts());

Expand Down

0 comments on commit 8e0eb64

Please sign in to comment.