Installation
composer require moox/firewall
php artisan vendor:publish --tag="firewall-config"
Configure .env with:
MOOX_FIREWALL_ENABLED=true
MOOX_FIREWALL_WHITELIST=192.168.1.1,10.0.0.5
First Use Case
MOOX_FIREWALL_WHITELIST (e.g., 127.0.0.1,192.168.x.x).MOOX_FIREWALL_BACKDOOR=true and set a token in config/firewall.php under backdoor.token.config/firewall.php (adjust whitelist, backdoor, and UI settings).enabled=true (check app/Http/Kernel.php for Moox\Firewall\Http\Middleware\Firewall)./firewall/backdoor (protected by token).IP Whitelisting
config('firewall.whitelist')).moox/firewall's built-in CIDR notation (e.g., 192.168.1.0/24).// In a controller/middleware
if (!auth()->check() && !Firewall::isWhitelisted()) {
abort(403);
}
Backdoor Usage
.env).config/firewall.php or a migration:
// Example: Add a token column to a `firewall_tokens` table
Schema::create('firewall_tokens', function (Blueprint $table) {
$table->id();
$table->string('token')->unique();
$table->timestamps();
});
// In a service provider
Firewall::backdoor(function () {
event(new FirewallBackdoorAccessed());
});
Filament Integration
// app/Policies/FilamentPolicy.php
public function viewAny(User $user) {
return Firewall::isWhitelisted() || $user->isAdmin();
}
geoip2/geoip2 to block regions:
use GeoIp2\Database\Reader;
$reader = new Reader(file_get_contents('GeoLite2-City.mmdb'));
$record = $reader->city(request()->ip());
if ($record->country->isoCode === 'RU') {
abort(403, 'Access denied from this region.');
}
laravel-rate-limiting to throttle backdoor attempts:
RateLimiter::for('firewall-backdoor')->limit(5)->perMinute();
Misconfigured Whitelists
127.0.0.1 in the whitelist for local testing. Use a secondary admin IP as a backup.Backdoor Token Exposure
config/firewall.php.// config/firewall.php
'backdoor' => [
'token' => env('FIREWALL_BACKDOOR_TOKEN'),
]
Caching Headaches
php artisan config:clear
Proxy/Load Balancer IPs
X-Forwarded-For header:
// In FirewallServiceProvider
$ip = request()->ip() ?? request()->header('X-Forwarded-For');
dd(request()->ip(), request()->server('REMOTE_ADDR'));
app/Http/Middleware/TrustProxies.php (for testing):
protected $proxies = '*'; // Bypass all firewall checks
// app/Http/Middleware/Firewall.php
public function handle($request, Closure $next) {
if (!$this->isWhitelisted($request->ip())) {
Log::warning('Firewall blocked IP: ' . $request->ip());
abort(403);
}
return $next($request);
}
Custom Block Page Override the default block view by publishing assets:
php artisan vendor:publish --tag="firewall-views"
Then modify resources/views/vendor/firewall/block.blade.php.
Dynamic Whitelists Fetch IPs from an external source (e.g., database):
// config/firewall.php
'whitelist' => function () {
return WhitelistedIp::where('active', true)->pluck('ip')->toArray();
},
Multi-Tenant Support Scope whitelists to tenants:
// In a middleware
$tenantIp = Tenant::find(request()->tenant)->whitelistedIps;
if (!in_array(request()->ip(), $tenantIp)) {
abort(403);
}
API-Specific Rules Apply firewall logic to API routes only:
Route::middleware(['firewall', 'api'])->group(function () {
// API routes here
});
Environment-Specific Configs:
Use firewall.php overrides for staging/prod:
// config/firewall.php
'whitelist' => env('MOOX_FIREWALL_WHITELIST', []),
Then set MOOX_FIREWALL_WHITELIST per environment.
Health Check Endpoint:
Add a /health endpoint to verify firewall status:
Route::get('/health', function () {
return [
'firewall' => Firewall::isWhitelisted() ? 'active' : 'blocked',
];
});
CI/CD Integration: Temporarily disable the firewall in CI by setting:
MOOX_FIREWALL_ENABLED=false
in your .env.ci file.
How can I help you explore Laravel packages today?