From 8be8aff802b970c3f9cde5618becc71ed56d2f95 Mon Sep 17 00:00:00 2001 From: Marcel Pociot Date: Fri, 11 Jun 2021 15:16:52 +0200 Subject: [PATCH] Improve subdomain detection --- .../Controllers/TunnelMessageController.php | 6 +-- tests/Feature/Server/TunnelTest.php | 38 +++++++++++++++++++ 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/app/Server/Http/Controllers/TunnelMessageController.php b/app/Server/Http/Controllers/TunnelMessageController.php index a2635e5..c4b4fda 100644 --- a/app/Server/Http/Controllers/TunnelMessageController.php +++ b/app/Server/Http/Controllers/TunnelMessageController.php @@ -70,11 +70,9 @@ class TunnelMessageController extends Controller protected function detectSubdomain(Request $request): ?string { - if (substr_count($request->header('Host'), '.') === 1) { - return null; - } + $serverHost = $this->detectServerHost($request); - $subdomain = Str::before($request->header('Host'), '.'); + $subdomain = Str::before($request->header('Host'), '.' . $serverHost); return $subdomain === $request->header('Host') ? null : $subdomain; } diff --git a/tests/Feature/Server/TunnelTest.php b/tests/Feature/Server/TunnelTest.php index 0caefa3..20b2fdc 100644 --- a/tests/Feature/Server/TunnelTest.php +++ b/tests/Feature/Server/TunnelTest.php @@ -64,6 +64,17 @@ class TunnelTest extends TestCase ])); } + /** @test */ + public function it_returns_404_for_non_existing_clients_on_custom_hosts() + { + $this->expectException(ResponseException::class); + $this->expectExceptionMessage(404); + + $this->await($this->browser->get('http://127.0.0.1:8080/', [ + 'Host' => 'tunnel.share.beyondco.de', + ])); + } + /** @test */ public function it_sends_incoming_requests_to_the_connected_client() { @@ -91,6 +102,33 @@ class TunnelTest extends TestCase $this->assertSame('Hello World!', $response->getBody()->getContents()); } + /** @test */ + public function it_sends_incoming_requests_to_the_connected_client_on_custom_hosts() + { + $this->app['config']['expose.admin.validate_auth_tokens'] = false; + + $this->createTestHttpServer(); + + $this->app['config']['expose.admin.validate_auth_tokens'] = false; + + /** + * We create an expose client that connects to our server and shares + * the created test HTTP server. + */ + $client = $this->createClient(); + $this->await($client->connectToServer('127.0.0.1:8085', 'tunnel', 'share.beyondco.de')); + + /** + * Once the client is connected, we perform a GET request on the + * created tunnel. + */ + $response = $this->await($this->browser->get('http://127.0.0.1:8080/', [ + 'Host' => 'tunnel.share.beyondco.de', + ])); + + $this->assertSame('Hello World!', $response->getBody()->getContents()); + } + /** @test */ public function it_sends_incoming_requests_to_the_connected_client_via_tcp() {