Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changelogs/v4.7.7.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 11 additions & 0 deletions system/Helpers/url_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '';
Expand Down
25 changes: 16 additions & 9 deletions tests/system/Helpers/URLHelper/MiscUrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
];
}
}
Loading