Skip to content

Commit 0e00157

Browse files
committed
Fix hostname routing on non-standard ports
- Strip port from HTTP_HOST in RouteCollection constructor - Add test cases for hostname with port matching Fixes issue where hostname routing fails on custom ports (e.g., example.com:8080) because HTTP_HOST user-controlled port was not being removed during comparison. Co-authored-by: CodeIgniter Bot
1 parent 404dcf5 commit 0e00157

2 files changed

Lines changed: 87 additions & 1 deletion

File tree

‎system/Router/RouteCollection.php‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,21 @@ public function __construct(FileLocatorInterface $locator, Modules $moduleConfig
283283
$this->fileLocator = $locator;
284284
$this->moduleConfig = $moduleConfig;
285285

286-
$this->httpHost = service('request')->getServer('HTTP_HOST');
286+
// Remove port from HTTP_HOST (supports domains, IPv4, and IPv6, e.g., [::1]:8080)
287+
$httpHost = service('request')->getServer('HTTP_HOST');
288+
289+
if ($httpHost !== null) {
290+
$host = parse_url('http://' . $httpHost, PHP_URL_HOST);
291+
292+
$isValid = $host && (
293+
filter_var($host, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)
294+
|| filter_var(trim($host, '[]'), FILTER_VALIDATE_IP)
295+
);
296+
297+
$this->httpHost = $isValid ? strtolower($host) : null;
298+
} else {
299+
$this->httpHost = null;
300+
}
287301

288302
// Setup based on config file. Let routes file override.
289303
$this->defaultNamespace = rtrim($routing->defaultNamespace, '\\') . '\\';

‎tests/system/Router/RouteCollectionTest.php‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,78 @@ public function testHostnameOption(): void
579579
$this->assertSame($expected, $routes->getRoutes());
580580
}
581581

582+
#[DataProvider('provideHostnameOptionWithPortMatchesCorrectly')]
583+
public function testHostnameOptionWithPortMatchesCorrectly(
584+
string $incomingHost,
585+
string $routeHostname,
586+
bool $shouldMatch,
587+
): void {
588+
$superglobals = service('superglobals');
589+
$originalHost = $superglobals->server('HTTP_HOST');
590+
591+
try {
592+
$superglobals->setServer('HTTP_HOST', $incomingHost);
593+
594+
$routes = $this->getCollector();
595+
$routes->add('test-route', 'Controller::method', ['hostname' => $routeHostname]);
596+
597+
$expected = $shouldMatch ? ['test-route' => '\Controller::method'] : [];
598+
599+
$this->assertSame($expected, $routes->getRoutes());
600+
} finally {
601+
// Restore original HTTP_HOST or clear it if it wasn't set originally
602+
if ($originalHost !== null) {
603+
$superglobals->setServer('HTTP_HOST', $originalHost);
604+
} else {
605+
$superglobals->setServer('HTTP_HOST', []);
606+
}
607+
}
608+
}
609+
610+
public static function provideHostnameOptionWithPortMatchesCorrectly(): iterable
611+
{
612+
yield from self::provideHostnameWithPortCases();
613+
}
614+
615+
public static function provideHostnameWithPortCases(): iterable
616+
{
617+
yield 'domain with dev port' => [
618+
'example.com:8080',
619+
'example.com',
620+
true,
621+
];
622+
623+
yield 'domain with custom port' => [
624+
'example.com:3000',
625+
'example.com',
626+
true,
627+
];
628+
629+
yield 'case-insensitive domain with port' => [
630+
'EXAMPLE.COM:8080',
631+
'example.com',
632+
true,
633+
];
634+
635+
yield 'IPv6 address with port' => [
636+
'[::1]:8080',
637+
'[::1]',
638+
true,
639+
];
640+
641+
yield 'IPv6 address without port' => [
642+
'[::1]',
643+
'[::1]',
644+
true,
645+
];
646+
647+
yield 'mismatched domain with port should not match' => [
648+
'attacker.com:8080',
649+
'example.com',
650+
false,
651+
];
652+
}
653+
582654
public function testResourceScaffoldsCorrectly(): void
583655
{
584656
$routes = $this->getCollector();

0 commit comments

Comments
 (0)