Trusted Hosts

Use TrustedHostMiddleware to limit which hostnames your application accepts. Atom checks the request host before routing and returns 400 Bad Request if the host is malformed or outside the configured list. This gives code that builds absolute URLs, such as password recovery, a validated host to work with.

Registration

Add the middleware to the application's existing middlewares() hook.

app/Application.php (excerpt)
use Atom\Http\MiddlewareRegistry;
use Atom\Http\TrustedHostMiddleware;

protected function middlewares(MiddlewareRegistry $middlewares): void
{
    $middlewares->add(TrustedHostMiddleware::class);
}

Configuration

Set TRUSTED_HOSTS to the hostnames your application serves. Include a port where the application should accept only that port.

.env
TRUSTED_HOSTS=example.com,www.example.com,localhost:8080

Include only the hosts needed in each environment. A hostname without a port accepts any valid port. *.example.com matches subdomains, and example.com needs its own entry. An empty list disables Trusted Hosts validation.

Registration order

When the app runs behind a reverse proxy and uses both middleware classes, add TrustedProxyMiddleware first. It normalizes the forwarded host from a configured proxy, then TrustedHostMiddleware checks that effective host against the allowlist.

app/Application.php (excerpt)
use Atom\Http\TrustedHostMiddleware;
use Atom\Http\TrustedProxyMiddleware;

// Inside middlewares().
$middlewares
    ->add(TrustedProxyMiddleware::class)
    ->add(TrustedHostMiddleware::class);

See Trusted Proxies for how to configure the proxy addresses you control.