|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Unit; |
| 4 | + |
| 5 | +use App\Http\Middleware\TrustProxies; |
| 6 | +use Illuminate\Http\Request; |
| 7 | +use Tests\TestCase; |
| 8 | + |
| 9 | +class TrustedProxiesTest extends TestCase |
| 10 | +{ |
| 11 | + protected function tearDown(): void |
| 12 | + { |
| 13 | + // These are process-wide statics on the Symfony/Illuminate classes, not |
| 14 | + // part of the Laravel application container, so they survive the |
| 15 | + // per-test application reset and must be cleared manually. |
| 16 | + Request::setTrustedProxies([], -1); |
| 17 | + TrustProxies::flushState(); |
| 18 | + |
| 19 | + parent::tearDown(); |
| 20 | + } |
| 21 | + |
| 22 | + private function makeSpoofedRequest(string $remoteAddr, string $forwardedFor): Request |
| 23 | + { |
| 24 | + $request = Request::create('http://example.com/test', 'GET', [], [], [], [ |
| 25 | + 'REMOTE_ADDR' => $remoteAddr, |
| 26 | + ]); |
| 27 | + $request->headers->set('X-Forwarded-For', $forwardedFor); |
| 28 | + |
| 29 | + return $request; |
| 30 | + } |
| 31 | + |
| 32 | + public function test_untrusted_direct_client_cannot_spoof_its_ip_by_default() |
| 33 | + { |
| 34 | + config([ |
| 35 | + 'trustedproxy.proxies' => '', |
| 36 | + 'trustedproxy.headers' => '', |
| 37 | + ]); |
| 38 | + |
| 39 | + $request = $this->makeSpoofedRequest('203.0.113.9', '1.1.1.1'); |
| 40 | + |
| 41 | + $resolvedIp = (new TrustProxies())->handle($request, fn ($req) => $req->ip()); |
| 42 | + |
| 43 | + $this->assertSame('203.0.113.9', $resolvedIp); |
| 44 | + } |
| 45 | + |
| 46 | + public function test_a_configured_trusted_proxy_can_report_the_real_client_ip() |
| 47 | + { |
| 48 | + config([ |
| 49 | + 'trustedproxy.proxies' => '10.0.0.5', |
| 50 | + 'trustedproxy.headers' => 'HEADER_X_FORWARDED_FOR', |
| 51 | + ]); |
| 52 | + |
| 53 | + $request = $this->makeSpoofedRequest('10.0.0.5', '203.0.113.42'); |
| 54 | + |
| 55 | + $resolvedIp = (new TrustProxies())->handle($request, fn ($req) => $req->ip()); |
| 56 | + |
| 57 | + $this->assertSame('203.0.113.42', $resolvedIp); |
| 58 | + } |
| 59 | + |
| 60 | + public function test_an_untrusted_client_cannot_spoof_its_ip_by_impersonating_a_trusted_proxy() |
| 61 | + { |
| 62 | + config([ |
| 63 | + 'trustedproxy.proxies' => '10.0.0.5', |
| 64 | + 'trustedproxy.headers' => 'HEADER_X_FORWARDED_FOR', |
| 65 | + ]); |
| 66 | + |
| 67 | + $request = $this->makeSpoofedRequest('203.0.113.9', '1.1.1.1'); |
| 68 | + |
| 69 | + $resolvedIp = (new TrustProxies())->handle($request, fn ($req) => $req->ip()); |
| 70 | + |
| 71 | + $this->assertSame('203.0.113.9', $resolvedIp); |
| 72 | + } |
| 73 | + |
| 74 | + public function test_an_invalid_header_name_in_config_is_ignored_without_breaking_valid_ones() |
| 75 | + { |
| 76 | + config([ |
| 77 | + 'trustedproxy.proxies' => '10.0.0.5', |
| 78 | + 'trustedproxy.headers' => 'NOT_A_REAL_HEADER,HEADER_X_FORWARDED_FOR', |
| 79 | + ]); |
| 80 | + |
| 81 | + $request = $this->makeSpoofedRequest('10.0.0.5', '203.0.113.42'); |
| 82 | + |
| 83 | + $resolvedIp = (new TrustProxies())->handle($request, fn ($req) => $req->ip()); |
| 84 | + |
| 85 | + $this->assertSame('203.0.113.42', $resolvedIp); |
| 86 | + } |
| 87 | + |
| 88 | + public function test_a_trusted_proxy_cannot_forward_headers_when_no_headers_are_configured() |
| 89 | + { |
| 90 | + config([ |
| 91 | + 'trustedproxy.proxies' => '10.0.0.5', |
| 92 | + 'trustedproxy.headers' => '', |
| 93 | + ]); |
| 94 | + |
| 95 | + // The request genuinely comes from the trusted proxy, but since |
| 96 | + // APP_TRUSTED_HEADERS is unset, X-Forwarded-For must still be |
| 97 | + // ignored - it must NOT fall back to trusting the framework's |
| 98 | + // default header set. |
| 99 | + $request = $this->makeSpoofedRequest('10.0.0.5', '203.0.113.42'); |
| 100 | + |
| 101 | + $resolvedIp = (new TrustProxies())->handle($request, fn ($req) => $req->ip()); |
| 102 | + |
| 103 | + $this->assertSame('10.0.0.5', $resolvedIp); |
| 104 | + } |
| 105 | + |
| 106 | + public function test_a_trusted_proxy_cannot_forward_headers_when_every_configured_header_is_invalid() |
| 107 | + { |
| 108 | + config([ |
| 109 | + 'trustedproxy.proxies' => '10.0.0.5', |
| 110 | + 'trustedproxy.headers' => 'NOT_A_REAL_HEADER,ALSO_NOT_REAL', |
| 111 | + ]); |
| 112 | + |
| 113 | + $request = $this->makeSpoofedRequest('10.0.0.5', '203.0.113.42'); |
| 114 | + |
| 115 | + $resolvedIp = (new TrustProxies())->handle($request, fn ($req) => $req->ip()); |
| 116 | + |
| 117 | + $this->assertSame('10.0.0.5', $resolvedIp); |
| 118 | + } |
| 119 | +} |
0 commit comments