Skip to content

Commit ff76fcf

Browse files
authored
fix(Helpers): strip port before evaluating subdomain in parse_subdomain() (#10794)
1 parent e4d791d commit ff76fcf

3 files changed

Lines changed: 28 additions & 9 deletions

File tree

‎docs/changelogs/v4.7.7.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Release Date: Unreleased
3636
- **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.
3737
- **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`.
3838
- **Helpers:** Fixed a bug where `format_number()` with `$precision = 0` appended a trailing comma in locales using a comma as the decimal separator.
39+
- **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.
3940
- **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.
4041
- **Helpers:** Fixed escaping of attribute values in HTML media helpers to prevent malformed HTML and attribute injection.
4142
- **HTTP:** Fixed a bug where query-string based routing truncated additional query parameters in `SiteURIFactory::parseRequestURI()` due to incorrect array index access.

‎system/Helpers/url_helper.php‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,17 @@ function parse_subdomain(?string $host = null): string
548548
{
549549
$host ??= service('request')->getUri()->getHost();
550550

551+
// Handle IPv6 enclosed in brackets, with or without port: e.g. [::1] or [::1]:8080
552+
if (str_starts_with($host, '[')) {
553+
return '';
554+
}
555+
556+
// If host has a port (e.g. host:8080 or 192.168.1.1:8080), strip the port.
557+
// IPv6 addresses without brackets have multiple colons and are handled by FILTER_VALIDATE_IP.
558+
if (substr_count($host, ':') === 1) {
559+
$host = explode(':', $host, 2)[0];
560+
}
561+
551562
// Handle localhost and IP addresses - they don't have subdomains
552563
if ($host === 'localhost' || filter_var($host, FILTER_VALIDATE_IP)) {
553564
return '';

‎tests/system/Helpers/URLHelper/MiscUrlTest.php‎

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,15 +1031,22 @@ public function testParseSubdomain(?string $host, string $expected, bool $useReq
10311031
public static function provideParseSubdomain(): iterable
10321032
{
10331033
return [
1034-
'standard subdomain' => ['api.example.com', 'api', false],
1035-
'multi-level subdomain' => ['admin.api.example.com', 'admin.api', false],
1036-
'no subdomain (domain only)' => ['example.com', '', false],
1037-
'localhost' => ['localhost', '', false],
1038-
'ipv4' => ['127.0.0.1', '', false],
1039-
'ipv6' => ['::1', '', false],
1040-
'two-part tld no subdomain' => ['example.co.uk', '', false],
1041-
'two-part tld with subdomain' => ['api.example.co.uk', 'api', false],
1042-
'null uses request host' => [null, 'sub', true],
1034+
'standard subdomain' => ['api.example.com', 'api', false],
1035+
'multi-level subdomain' => ['admin.api.example.com', 'admin.api', false],
1036+
'no subdomain (domain only)' => ['example.com', '', false],
1037+
'localhost' => ['localhost', '', false],
1038+
'ipv4' => ['127.0.0.1', '', false],
1039+
'ipv6' => ['::1', '', false],
1040+
'two-part tld no subdomain' => ['example.co.uk', '', false],
1041+
'two-part tld with subdomain' => ['api.example.co.uk', 'api', false],
1042+
'standard subdomain with port' => ['api.example.com:8080', 'api', false],
1043+
'no subdomain with port' => ['example.com:8080', '', false],
1044+
'localhost with port' => ['localhost:8080', '', false],
1045+
'ipv4 with port' => ['192.168.1.1:8080', '', false],
1046+
'ipv6 with port' => ['[::1]:8080', '', false],
1047+
'two-part tld no subdomain with port' => ['example.co.uk:8080', '', false],
1048+
'two-part tld with subdomain with port' => ['api.example.co.uk:8080', 'api', false],
1049+
'null uses request host' => [null, 'sub', true],
10431050
];
10441051
}
10451052
}

0 commit comments

Comments
 (0)