Skip to content

Trust Proxies ​

EdgeTrust supplies CIDR lists for Laravel's trusted proxy configuration. You must wire those lists into your application explicitly.

WARNING

EdgeTrust does not silently replace or rewrite your middleware stack. Installing the package alone does not change which proxies Laravel trusts until you configure TrustProxies to use EdgeTrust.

Supported package targets are Laravel 10+. The registration style differs by framework major; use the section that matches your app.

Laravel 11+ (withMiddleware) ​

Laravel 11 / 12 / 13 configure middleware from bootstrap/app.php.

php
<?php

use Hosni\EdgeTrust\Facades\EdgeTrust;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Middleware;
use Illuminate\Http\Request;

return Application::configure(basePath: dirname(__DIR__))
    ->withMiddleware(function (Middleware $middleware) {
        $middleware->trustProxies(
            at: EdgeTrust::proxies(),
            headers: Request::HEADER_X_FORWARDED_FOR
                | Request::HEADER_X_FORWARDED_HOST
                | Request::HEADER_X_FORWARDED_PORT
                | Request::HEADER_X_FORWARDED_PROTO
                | Request::HEADER_X_FORWARDED_PREFIX
                | Request::HEADER_X_FORWARDED_AWS_ELB,
        );
    })
    ->create();

Alternative — replace the middleware class ​

php
->withMiddleware(function (Middleware $middleware) {
    $middleware->replace(
        \Illuminate\Http\Middleware\TrustProxies::class,
        \Hosni\EdgeTrust\Http\Middleware\TrustProxies::class,
    );
})

Adjust forwarded headers to match your edge. Defaults also live in config/edge-trust.php under headers.

Laravel 10 (app/Http/Kernel.php) ​

Laravel 10 still uses the HTTP kernel middleware stack.

Replace the global middleware entry ​

php
// app/Http/Kernel.php

protected $middleware = [
    // \Illuminate\Http\Middleware\TrustProxies::class,
    \Hosni\EdgeTrust\Http\Middleware\TrustProxies::class,
    // ...
];

Or keep your app class and override proxies() ​

php
namespace App\Http\Middleware;

use Hosni\EdgeTrust\Facades\EdgeTrust;
use Illuminate\Http\Middleware\TrustProxies as Middleware;

class TrustProxies extends Middleware
{
    protected function proxies()
    {
        return EdgeTrust::proxies();
    }
}

Ensure that class remains registered in $middleware.

Laravel 9 and earlier ​

EdgeTrust's Composer constraints require Laravel 10+. Apps on Laravel 9 or below should upgrade the framework first.

If you are still on Laravel 9 while planning an upgrade, the wiring pattern matches Laravel 10: register TrustProxies in app/Http/Kernel.php and return EdgeTrust::proxies() from proxies(). Do not expect package support, tests, or security fixes on those majors.

Laravel 8 and earlier often used fideloper/proxy instead of Illuminate\Http\Middleware\TrustProxies. That stack is outside EdgeTrust's supported surface.

Package middleware behavior ​

Hosni\EdgeTrust\Http\Middleware\TrustProxies resolves proxies via ProxyManager on each request from cache / bundled archives — never a remote fetch on the request path.

Do not trust * ​

Avoid configuring Laravel to trust all proxies (*) when you sit behind a CDN. Prefer EdgeTrust's explicit CIDR lists for the providers you enable.

Order of operations ​

  1. Enable providers via EDGE_TRUST_PROVIDERS (or published config)
  2. Choose a mode
  3. Pass EdgeTrust::proxies() into TrustProxies, or register the package middleware
  4. Restrict origin network access — Origin firewall

Verifying client IPs ​

php
use Illuminate\Http\Request;

Route::get('/debug-ip', function (Request $request) {
    return [
        'ip' => $request->ip(),
        'ips' => $request->ips(),
    ];
});

With TrustProxies correctly configured and traffic arriving via your CDN, $request->ip() should reflect the end-user address (subject to header trust rules), not only the edge hop.

Released under the MIT License.