|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the official PHP MCP SDK. |
| 5 | + * |
| 6 | + * A collaboration between Symfony and the PHP Foundation. |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Mcp\Tests\Unit\Server\Transport\Http\Middleware; |
| 13 | + |
| 14 | +use Mcp\Server\Transport\Http\Middleware\DnsRebindingProtectionMiddleware; |
| 15 | +use Nyholm\Psr7\Factory\Psr17Factory; |
| 16 | +use PHPUnit\Framework\Attributes\TestDox; |
| 17 | +use PHPUnit\Framework\TestCase; |
| 18 | +use Psr\Http\Message\ResponseFactoryInterface; |
| 19 | +use Psr\Http\Message\ResponseInterface; |
| 20 | +use Psr\Http\Message\ServerRequestInterface; |
| 21 | +use Psr\Http\Server\RequestHandlerInterface; |
| 22 | + |
| 23 | +class DnsRebindingProtectionMiddlewareTest extends TestCase |
| 24 | +{ |
| 25 | + private Psr17Factory $factory; |
| 26 | + private RequestHandlerInterface $handler; |
| 27 | + |
| 28 | + protected function setUp(): void |
| 29 | + { |
| 30 | + $this->factory = new Psr17Factory(); |
| 31 | + $this->handler = new class($this->factory) implements RequestHandlerInterface { |
| 32 | + public function __construct(private ResponseFactoryInterface $factory) |
| 33 | + { |
| 34 | + } |
| 35 | + |
| 36 | + public function handle(ServerRequestInterface $request): ResponseInterface |
| 37 | + { |
| 38 | + return $this->factory->createResponse(200); |
| 39 | + } |
| 40 | + }; |
| 41 | + } |
| 42 | + |
| 43 | + #[TestDox('allows request with localhost Host header')] |
| 44 | + public function testAllowsLocalhostHost(): void |
| 45 | + { |
| 46 | + $middleware = new DnsRebindingProtectionMiddleware(responseFactory: $this->factory); |
| 47 | + $request = $this->factory->createServerRequest('POST', 'http://localhost:8000/') |
| 48 | + ->withHeader('Host', 'localhost:8000'); |
| 49 | + |
| 50 | + $response = $middleware->process($request, $this->handler); |
| 51 | + |
| 52 | + $this->assertSame(200, $response->getStatusCode()); |
| 53 | + } |
| 54 | + |
| 55 | + #[TestDox('allows request with 127.0.0.1 Host header')] |
| 56 | + public function testAllows127001Host(): void |
| 57 | + { |
| 58 | + $middleware = new DnsRebindingProtectionMiddleware(responseFactory: $this->factory); |
| 59 | + $request = $this->factory->createServerRequest('POST', 'http://127.0.0.1/') |
| 60 | + ->withHeader('Host', '127.0.0.1:3000'); |
| 61 | + |
| 62 | + $response = $middleware->process($request, $this->handler); |
| 63 | + |
| 64 | + $this->assertSame(200, $response->getStatusCode()); |
| 65 | + } |
| 66 | + |
| 67 | + #[TestDox('allows request with [::1] Host header')] |
| 68 | + public function testAllowsIpv6LocalhostHost(): void |
| 69 | + { |
| 70 | + $middleware = new DnsRebindingProtectionMiddleware(responseFactory: $this->factory); |
| 71 | + $request = $this->factory->createServerRequest('POST', 'http://[::1]/') |
| 72 | + ->withHeader('Host', '[::1]:8000'); |
| 73 | + |
| 74 | + $response = $middleware->process($request, $this->handler); |
| 75 | + |
| 76 | + $this->assertSame(200, $response->getStatusCode()); |
| 77 | + } |
| 78 | + |
| 79 | + #[TestDox('allows request with no Host header')] |
| 80 | + public function testAllowsEmptyHost(): void |
| 81 | + { |
| 82 | + $middleware = new DnsRebindingProtectionMiddleware(responseFactory: $this->factory); |
| 83 | + $request = $this->factory->createServerRequest('POST', 'http://localhost/') |
| 84 | + ->withoutHeader('Host'); |
| 85 | + |
| 86 | + $response = $middleware->process($request, $this->handler); |
| 87 | + |
| 88 | + $this->assertSame(200, $response->getStatusCode()); |
| 89 | + } |
| 90 | + |
| 91 | + #[TestDox('rejects request with evil Host header')] |
| 92 | + public function testRejectsEvilHost(): void |
| 93 | + { |
| 94 | + $middleware = new DnsRebindingProtectionMiddleware(responseFactory: $this->factory); |
| 95 | + $request = $this->factory->createServerRequest('POST', 'http://evil.example.com/') |
| 96 | + ->withHeader('Host', 'evil.example.com'); |
| 97 | + |
| 98 | + $response = $middleware->process($request, $this->handler); |
| 99 | + |
| 100 | + $this->assertSame(403, $response->getStatusCode()); |
| 101 | + $this->assertStringContainsString('Host', (string) $response->getBody()); |
| 102 | + } |
| 103 | + |
| 104 | + #[TestDox('rejects request with evil Host header even with port')] |
| 105 | + public function testRejectsEvilHostWithPort(): void |
| 106 | + { |
| 107 | + $middleware = new DnsRebindingProtectionMiddleware(responseFactory: $this->factory); |
| 108 | + $request = $this->factory->createServerRequest('POST', 'http://evil.example.com:8000/') |
| 109 | + ->withHeader('Host', 'evil.example.com:8000'); |
| 110 | + |
| 111 | + $response = $middleware->process($request, $this->handler); |
| 112 | + |
| 113 | + $this->assertSame(403, $response->getStatusCode()); |
| 114 | + } |
| 115 | + |
| 116 | + #[TestDox('allows request with valid localhost Origin header')] |
| 117 | + public function testAllowsLocalhostOrigin(): void |
| 118 | + { |
| 119 | + $middleware = new DnsRebindingProtectionMiddleware(responseFactory: $this->factory); |
| 120 | + $request = $this->factory->createServerRequest('POST', 'http://localhost/') |
| 121 | + ->withHeader('Host', 'localhost:8000') |
| 122 | + ->withHeader('Origin', 'http://localhost:8000'); |
| 123 | + |
| 124 | + $response = $middleware->process($request, $this->handler); |
| 125 | + |
| 126 | + $this->assertSame(200, $response->getStatusCode()); |
| 127 | + } |
| 128 | + |
| 129 | + #[TestDox('rejects request with evil Origin header')] |
| 130 | + public function testRejectsEvilOrigin(): void |
| 131 | + { |
| 132 | + $middleware = new DnsRebindingProtectionMiddleware(responseFactory: $this->factory); |
| 133 | + $request = $this->factory->createServerRequest('POST', 'http://localhost/') |
| 134 | + ->withHeader('Host', 'localhost:8000') |
| 135 | + ->withHeader('Origin', 'http://evil.example.com'); |
| 136 | + |
| 137 | + $response = $middleware->process($request, $this->handler); |
| 138 | + |
| 139 | + $this->assertSame(403, $response->getStatusCode()); |
| 140 | + $this->assertStringContainsString('Origin', (string) $response->getBody()); |
| 141 | + } |
| 142 | + |
| 143 | + #[TestDox('rejects malformed Origin header')] |
| 144 | + public function testRejectsMalformedOrigin(): void |
| 145 | + { |
| 146 | + $middleware = new DnsRebindingProtectionMiddleware(responseFactory: $this->factory); |
| 147 | + $request = $this->factory->createServerRequest('POST', 'http://localhost/') |
| 148 | + ->withHeader('Host', 'localhost') |
| 149 | + ->withHeader('Origin', 'not-a-url'); |
| 150 | + |
| 151 | + $response = $middleware->process($request, $this->handler); |
| 152 | + |
| 153 | + $this->assertSame(403, $response->getStatusCode()); |
| 154 | + } |
| 155 | + |
| 156 | + #[TestDox('Host matching is case-insensitive')] |
| 157 | + public function testHostMatchingIsCaseInsensitive(): void |
| 158 | + { |
| 159 | + $middleware = new DnsRebindingProtectionMiddleware(responseFactory: $this->factory); |
| 160 | + $request = $this->factory->createServerRequest('POST', 'http://localhost/') |
| 161 | + ->withHeader('Host', 'LOCALHOST:8000'); |
| 162 | + |
| 163 | + $response = $middleware->process($request, $this->handler); |
| 164 | + |
| 165 | + $this->assertSame(200, $response->getStatusCode()); |
| 166 | + } |
| 167 | + |
| 168 | + #[TestDox('supports custom allowed hosts')] |
| 169 | + public function testCustomAllowedHosts(): void |
| 170 | + { |
| 171 | + $middleware = new DnsRebindingProtectionMiddleware( |
| 172 | + allowedHosts: ['myapp.local'], |
| 173 | + responseFactory: $this->factory, |
| 174 | + ); |
| 175 | + |
| 176 | + $allowed = $this->factory->createServerRequest('POST', 'http://myapp.local/') |
| 177 | + ->withHeader('Host', 'myapp.local:9000'); |
| 178 | + $this->assertSame(200, $middleware->process($allowed, $this->handler)->getStatusCode()); |
| 179 | + |
| 180 | + $rejected = $this->factory->createServerRequest('POST', 'http://localhost/') |
| 181 | + ->withHeader('Host', 'localhost'); |
| 182 | + $this->assertSame(403, $middleware->process($rejected, $this->handler)->getStatusCode()); |
| 183 | + } |
| 184 | + |
| 185 | + #[TestDox('allows request with no Origin header')] |
| 186 | + public function testAllowsEmptyOrigin(): void |
| 187 | + { |
| 188 | + $middleware = new DnsRebindingProtectionMiddleware(responseFactory: $this->factory); |
| 189 | + $request = $this->factory->createServerRequest('POST', 'http://localhost/') |
| 190 | + ->withHeader('Host', 'localhost'); |
| 191 | + |
| 192 | + $response = $middleware->process($request, $this->handler); |
| 193 | + |
| 194 | + $this->assertSame(200, $response->getStatusCode()); |
| 195 | + } |
| 196 | + |
| 197 | + #[TestDox('Host header check runs before Origin header check')] |
| 198 | + public function testHostCheckRunsBeforeOriginCheck(): void |
| 199 | + { |
| 200 | + $middleware = new DnsRebindingProtectionMiddleware(responseFactory: $this->factory); |
| 201 | + $request = $this->factory->createServerRequest('POST', 'http://evil.example.com/') |
| 202 | + ->withHeader('Host', 'evil.example.com') |
| 203 | + ->withHeader('Origin', 'http://evil.example.com'); |
| 204 | + |
| 205 | + $response = $middleware->process($request, $this->handler); |
| 206 | + |
| 207 | + $this->assertSame(403, $response->getStatusCode()); |
| 208 | + $this->assertStringContainsString('Host', (string) $response->getBody()); |
| 209 | + } |
| 210 | +} |
0 commit comments