Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Calling Middleware::trustHosts() with arguments breaks entire application #50845

Closed
axlon opened this issue Mar 29, 2024 · 4 comments · Fixed by #50877
Closed

[11.x] Calling Middleware::trustHosts() with arguments breaks entire application #50845

axlon opened this issue Mar 29, 2024 · 4 comments · Fixed by #50877

Comments

@axlon
Copy link
Contributor

axlon commented Mar 29, 2024

Laravel Version

11.1

PHP Version

11.1.1

Database Driver & Version

n/a

Description

The new bootstrapping process breaks when calling $middleware->trustHosts(...) with any arguments while $subdomains is set to true. This is because doing so will cause the middleware to prepare its list of trusted domains, hen the middleware does this it calls the config() helper which hasn't yet been resolved at this point.

PS. I'm open to fixing this myself; I think the easiest way to fix this is to defer the configuration of the middleware to a booted callback, that way we can be sure that the config has been resolved.

PPS. If the above is agreeable, I'd like to also add the ability to call $middleware->trustHosts() with a callback. I initially ran into the problem described in this issue in my own code, because I was trying to register additional domains from my config (which cannot be done currently as I can only pass an array of already resolved domains)

Steps To Reproduce

I've made an example repo here which shows the problem.

Basically:

$middleware->trustHosts(); // Works
$middleware->trustHosts(subdomains: false); // Kinda works... still registers subdomains
$middleware->trustHosts([...], subdomains: true); // Error! Nothing works
$middleware->trustHosts([...], subdomains: false); // Works
@RudahXimenes
Copy link

I second this issue.

Is there any alternative to set TrustHosts while it is not fixed?

@gzwan
Copy link

gzwan commented Mar 30, 2024

Is there any alternative to set TrustHosts while it is not fixed?

@JurianArie
Copy link
Contributor

@RudahXimenes @gzwan

You can copy the middleware from 10.x https://github.com/laravel/laravel/blob/10.x/app/Http/Middleware/TrustHosts.php and replace the middleware in you bootstrap/app.php:

->withMiddleware(function (\Illuminate\Foundation\Configuration\Middleware $middleware) {
    $middleware->replace(\Illuminate\Http\Middleware\TrustHosts::class, \App\Http\Middleware\TrustHosts::class);
})

@RudahXimenes
Copy link

@RudahXimenes @gzwan

You can copy the middleware from 10.x https://github.com/laravel/laravel/blob/10.x/app/Http/Middleware/TrustHosts.php and replace the middleware in you bootstrap/app.php:

->withMiddleware(function (\Illuminate\Foundation\Configuration\Middleware $middleware) {
    $middleware->replace(\Illuminate\Http\Middleware\TrustHosts::class, \App\Http\Middleware\TrustHosts::class);
})

Thank you, buddy

I'll take a look and implement it this way!

amaralis added a commit to amaralis/laravel-docs that referenced this issue Aug 15, 2024
taylorotwell added a commit to laravel/docs that referenced this issue Aug 15, 2024
* Add trustHosts() documentation when hosts are fetched from configuration file

Document the fix from laravel/framework#50877, triggered by issue laravel/framework#50845

* Update requests.md

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants