pratik-dabhi/laravel-firewall
Laravel firewall middleware for blocking abusive traffic by IP rules such as allow/deny lists and access restrictions. Helps protect routes and applications from unwanted requests with simple configuration and integration into a Laravel app.
Installation
composer require pratik-dabhi/laravel-firewall
php artisan vendor:publish --provider="PratikDabhi\Firewall\FirewallServiceProvider" --tag="config"
config/firewall.php) with sensible defaults.Basic Configuration
Edit config/firewall.php to define:
'rules' => [
'allow' => ['192.168.1.1', '10.0.0.0/8'],
'deny' => ['1.2.3.4', '172.16.0.0/12'],
],
geoip.enabled = true and configure MaxMind DB paths.rate_limits (e.g., max_attempts: 100, decay_minutes: 1).First Use Case: Block an IP
use PratikDabhi\Firewall\Facades\Firewall;
// Block a single IP
Firewall::deny('1.2.3.4');
// Check if an IP is allowed
if (Firewall::isAllowed(request()->ip())) {
// Proceed
} else {
abort(403, 'Access denied.');
}
Middleware Integration Use the provided middleware to auto-block requests:
// In Kernel.php
protected $middleware = [
\PratikDabhi\Firewall\Middleware\Firewall::class,
];
Dynamic Rule Management
Firewall::allow('192.168.1.100'); // Whitelist an IP
Firewall::denyCidr('172.16.0.0/16'); // Block a subnet
cache_rules = true in config to avoid reloading rules on every request.GeoIP Blocking
config/firewall.php:
'geoip' => [
'enabled' => true,
'database_path' => database_path('GeoLite2-Country.mmdb'),
'blocked_countries' => ['RU', 'CN'], // ISO country codes
],
Rate Limiting
config/firewall.php:
'rate_limits' => [
'api' => [
'max_attempts' => 60,
'decay_minutes' => 1,
],
'admin' => [
'max_attempts' => 10,
'decay_minutes' => 5,
],
],
Route::middleware(['firewall.rate_limit:api'])->group(function () {
// Rate-limited routes
});
Attack Logging
storage/logs/firewall.log (enabled by default).'logging' => [
'enabled' => true,
'log_blocked_attempts' => true,
'log_allowed_attempts' => false,
],
2001:db8::/32).GeoIP Database Updates
# Example: Download and replace the DB monthly
wget -O database_path/GeoLite2-Country.mmdb https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-Country&license_key=YOUR_KEY&suffix=tar.gz
tar -xzf GeoLite2-Country.tar.gz -C database_path
spatie/laravel-geoip for easier GeoIP management if needed.Rate Limiting Scope
// Override the rate limiter in a service provider
Firewall::extend('user_rate_limit', function ($request) {
return RateLimiter::for('user_' . $request->user()->id)->...
});
CIDR Parsing Errors
192.168.1) will throw exceptions. Validate inputs:
use PratikDabhi\Firewall\Support\Cidr;
if (!Cidr::isValid('192.168.1')) {
throw new \InvalidArgumentException('Invalid CIDR');
}
Performance with Large Rule Sets
cache_rules = false) if rules change frequently.IP Spoofing
request()->ip(). For proxied environments (e.g., Cloudflare), use:
$ip = request()->ip() ?? request()->header('CF-Connecting-IP');
Logging Overhead
log_blocked_attempts = false) reduces I/O but loses audit trails.Check Firewall Status
// Dump rules for debugging
dd(Firewall::getRules());
// Check if an IP is blocked
dd(Firewall::isAllowed('1.2.3.4')); // Returns bool
Rate Limit Debugging
$attempts = Firewall::getRateLimitAttempts('api', request()->ip());
dd($attempts);
GeoIP Debugging
use PratikDabhi\Firewall\Facades\GeoIp;
dd(GeoIp::getCountryCode('8.8.8.8')); // Should return 'US'
Custom Rule Providers
PratikDabhi\Firewall\Contracts\RuleProvider to fetch rules from a database:
class DatabaseRuleProvider implements RuleProvider {
public function getAllowRules() { ... }
public function getDenyRules() { ... }
}
Firewall::setRuleProvider(new DatabaseRuleProvider());
Custom Rate Limit Strategies
Firewall::extend('sliding_window', function () {
return new SlidingWindowLimiter();
});
Event Listeners
Firewall::blocked(function ($request, $reason) {
// Log or notify admins
});
HTTP Responses
Firewall::setBlockResponse(function ($request, $reason) {
return response()->json(['error' => 'Blocked'], 403);
});
How can I help you explore Laravel packages today?