14four/laravel-force-host
Middleware for Laravel that forces a single canonical host for all requests. Automatically redirects to your configured domain (and optionally scheme), preventing duplicate URLs across www/non-www and improving SEO, cache consistency, and security.
Host header to bypass security controls, perform SSRF, or route traffic maliciously). This is critical for APIs, multi-tenant apps, or systems exposed to untrusted networks (e.g., public-facing Laravel apps, microservices).trustedProxies, force_https, or middleware like App\Http\Middleware\TrustProxies).app/Http/Kernel.php (middleware stack).trustedProxies or VerifyCsrfToken middleware..env (FORCE_HOST=example.com), enabling environment-specific policies (e.g., dev/staging/prod).X-Forwarded-Host is present (configurable via trustedProxies).| Risk Area | Assessment | Mitigation Strategy |
|---|---|---|
| False Positives | Overly strict host enforcement may break multi-tenant or subdomain setups. | Test with FORCE_HOST=*.example.com or whitelist patterns (e.g., regex). |
| Performance | Minimal overhead (single regex check), but adds middleware latency. | Benchmark in staging; negligible for most apps (<0.1ms). |
| Proxy Misconfiguration | If trustedProxies misconfigured, X-Forwarded-Host may be ignored. |
Validate proxy headers in middleware; log warnings for mismatches. |
| Edge Cases | Wildcard subdomains or dynamic hosts (e.g., *.user-*.example.com). |
Extend package to support callback validation (e.g., ForceHost::validate($host)). |
X-Forwarded-* headers configured?trustedProxies or VerifyCsrfToken? How does this package complement them?Host headers can be tampered with at the CDN layer.Installation:
composer require 14four/laravel-force-host
Configuration:
app/Http/Kernel.php:
protected $middleware = [
\App\Http\Middleware\ForceHost::class, // Add to global middleware
// ... other middleware
];
if (app()->environment('production')) {
$middleware[] = \App\Http\Middleware\ForceHost::class;
}
Environment Setup:
FORCE_HOST in .env:
FORCE_HOST=api.example.com
FORCE_HOST=*.example.com
Testing:
public function test_host_validation()
{
$request = new Request(['Host' => 'evil.com']);
$middleware = new ForceHost();
$middleware->handle($request, function () {})->then(function () {
$this->fail('Should have blocked invalid host');
});
}
actingAs() or Http::fake() to simulate spoofed hosts.403 responses for invalid hosts.| Component | Compatibility Notes |
|---|---|
| Laravel Version | Tested on Laravel 5.5+ (PHP 7.2+). No breaking changes expected for newer versions. |
| PHP Version | Requires PHP 7.2+ (for named arguments in middleware). |
| Middleware Order | Must run before routing middleware (e.g., SubdomainMiddleware). |
| Proxy Headers | Respects X-Forwarded-Host if trustedProxies is configured. |
| Custom Hosts | Extendable via ForceHost::extend() for dynamic validation logic. |
Phase 1: Sandbox Testing
FORCE_HOST set to a single domain.Phase 2: Gradual Rollout
FORCE_HOST=*.staging.example.com.Phase 3: Production Hardening
app()->environment('production')).Phase 4: Monitoring & Alerting
.env updates suffice for most changes).trustedProxies is correctly configured for X-Forwarded-Host.FORCE_HOST=*.example.com or implement custom validation callbacks.Kernel.php..env for correct FORCE_HOST value.curl -H "Host: evil.com" http://your-app (should return 403).preg_match).| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Middleware Not Registered | No host validation. | CI/CD check for ForceHost in Kernel.php. |
Incorrect FORCE_HOST |
Overly permissive/blocking. | Validate .env via CI (e.g., regex pattern). |
| Proxy Misconfiguration | X-Forwarded-Host ignored. |
Log warnings if X-Forwarded-Host present but trustedProxies misconfigured. |
| Regex DoS | Malicious hosts |
How can I help you explore Laravel packages today?