From 519eccd4d24e20931a0e4e6c4141e2b54f98f938 Mon Sep 17 00:00:00 2001 From: banghamid <62824084+banghamid@users.noreply.github.com> Date: Thu, 19 Nov 2026 21:11:43 +0700 Subject: [PATCH] fix(Helpers): strip port before evaluating subdomain in parse_subdomain() --- docs/changelogs/v4.7.7.md | 1 + system/Helpers/url_helper.php | 11 ++++++++ .../system/Helpers/URLHelper/MiscUrlTest.php | 25 ++++++++++++------- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/docs/changelogs/v4.7.7.md b/docs/changelogs/v4.7.7.md index 01419ee93694..121dae00153c 100644 --- a/docs/changelogs/v4.7.7.md +++ b/docs/changelogs/v4.7.7.md @@ -36,6 +36,7 @@ Release Date: Unreleased - **Debug Toolbar:** Fixed a bug where `History::setFiles()` emitted a PHP warning when attempting to delete a debugbar history file that had already been removed. - **Helpers:** Fixed a bug where `excerpt()` threw a `TypeError` when `$phrase` was not found in `$text` or when `$phrase` was `null` and `$radius` was an odd integer. When `$phrase` is not found, `excerpt()` now falls back to extracting an excerpt from the beginning of `$text`. - **Helpers:** Fixed a bug where `format_number()` with `$precision = 0` appended a trailing comma in locales using a comma as the decimal separator. +- **Helpers:** Fixed a bug where `parse_subdomain()` incorrectly treated hostnames with ports (such as IP addresses and two-part TLD domains) as having unexpected subdomains. - **Helpers:** Fixed bugs in `ArrayHelper::dotKeyExists()` where `'0'` path segments caused early loop termination, non-array intermediate values threw a `TypeError`, and paths with segments after a wildcard failed when the wildcard matched multiple elements. - **Helpers:** Fixed escaping of attribute values in HTML media helpers to prevent malformed HTML and attribute injection. - **HTTP:** Fixed a bug where query-string based routing truncated additional query parameters in `SiteURIFactory::parseRequestURI()` due to incorrect array index access. diff --git a/system/Helpers/url_helper.php b/system/Helpers/url_helper.php index c08187c6c40b..66adb4f4fd28 100644 --- a/system/Helpers/url_helper.php +++ b/system/Helpers/url_helper.php @@ -548,6 +548,17 @@ function parse_subdomain(?string $host = null): string { $host ??= service('request')->getUri()->getHost(); + // Handle IPv6 enclosed in brackets, with or without port: e.g. [::1] or [::1]:8080 + if (str_starts_with($host, '[')) { + return ''; + } + + // If host has a port (e.g. host:8080 or 192.168.1.1:8080), strip the port. + // IPv6 addresses without brackets have multiple colons and are handled by FILTER_VALIDATE_IP. + if (substr_count($host, ':') === 1) { + $host = explode(':', $host, 2)[0]; + } + // Handle localhost and IP addresses - they don't have subdomains if ($host === 'localhost' || filter_var($host, FILTER_VALIDATE_IP)) { return ''; diff --git a/tests/system/Helpers/URLHelper/MiscUrlTest.php b/tests/system/Helpers/URLHelper/MiscUrlTest.php index c8cefbda9c8b..a52004708326 100644 --- a/tests/system/Helpers/URLHelper/MiscUrlTest.php +++ b/tests/system/Helpers/URLHelper/MiscUrlTest.php @@ -1031,15 +1031,22 @@ public function testParseSubdomain(?string $host, string $expected, bool $useReq public static function provideParseSubdomain(): iterable { return [ - 'standard subdomain' => ['api.example.com', 'api', false], - 'multi-level subdomain' => ['admin.api.example.com', 'admin.api', false], - 'no subdomain (domain only)' => ['example.com', '', false], - 'localhost' => ['localhost', '', false], - 'ipv4' => ['127.0.0.1', '', false], - 'ipv6' => ['::1', '', false], - 'two-part tld no subdomain' => ['example.co.uk', '', false], - 'two-part tld with subdomain' => ['api.example.co.uk', 'api', false], - 'null uses request host' => [null, 'sub', true], + 'standard subdomain' => ['api.example.com', 'api', false], + 'multi-level subdomain' => ['admin.api.example.com', 'admin.api', false], + 'no subdomain (domain only)' => ['example.com', '', false], + 'localhost' => ['localhost', '', false], + 'ipv4' => ['127.0.0.1', '', false], + 'ipv6' => ['::1', '', false], + 'two-part tld no subdomain' => ['example.co.uk', '', false], + 'two-part tld with subdomain' => ['api.example.co.uk', 'api', false], + 'standard subdomain with port' => ['api.example.com:8080', 'api', false], + 'no subdomain with port' => ['example.com:8080', '', false], + 'localhost with port' => ['localhost:8080', '', false], + 'ipv4 with port' => ['192.168.1.1:8080', '', false], + 'ipv6 with port' => ['[::1]:8080', '', false], + 'two-part tld no subdomain with port' => ['example.co.uk:8080', '', false], + 'two-part tld with subdomain with port' => ['api.example.co.uk:8080', 'api', false], + 'null uses request host' => [null, 'sub', true], ]; } }