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
59 changes: 59 additions & 0 deletions src/Uri/PortUtil.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

/**
* Copyright 2026 The Horde Project (http://www.horde.org/)
*
* See the enclosed file LICENSE for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @category Horde
* @copyright 2026 The Horde Project
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Core
*/

namespace Horde\Core\Uri;

/**
* Utility for IANA default port handling.
*/
class PortUtil
{
private const SCHEME_DEFAULTS = [
'http' => 80,
'https' => 443,
];

/**
* Whether a port is the IANA default for the given scheme.
*
* Returns true when the port can be omitted from a URI without
* changing its meaning (RFC 3986 section 3.2.3).
*/
public static function isIanaDefault(string $scheme, int|string|null $port): bool
{
if ($port === null || $port === '') {
return true;
}
$default = self::SCHEME_DEFAULTS[strtolower($scheme)] ?? null;
return $default !== null && (int) $port === $default;
}

/**
* Strip the port from a host:port string when it is the IANA default for the scheme.
*/
public static function stripDefaultPort(string $scheme, string $host): string
{
if (!str_contains($host, ':')) {
return $host;
}
$lastColon = strrpos($host, ':');
$port = substr($host, $lastColon + 1);
if (self::isIanaDefault($scheme, $port)) {
return substr($host, 0, $lastColon);
}
return $host;
}
}
4 changes: 1 addition & 3 deletions src/Uri/RouteUrlWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ public function absoluteUrlFor(string $routeName, array $params = []): ?string
? 'https'
: 'http';

// Strip default port to avoid redirect_uri mismatches with strict OIDC providers.
$defaultPort = $scheme === 'https' ? '443' : '80';
$host = preg_replace('/^(.+):' . $defaultPort . '$/', '$1', $host);
$host = PortUtil::stripDefaultPort($scheme, $host);

return $scheme . '://' . $host . $path;
}
Expand Down
66 changes: 66 additions & 0 deletions test/Unit/Uri/PortUtilTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

namespace Horde\Core\Test\Unit\Uri;

use Horde\Core\Uri\PortUtil;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

#[CoversClass(PortUtil::class)]
class PortUtilTest extends TestCase
{
/**
* @return array<string, array{string, int|string|null, bool}>
*/
public static function isIanaDefaultProvider(): array
{
return [
'https 443 is default' => ['https', 443, true],
'http 80 is default' => ['http', 80, true],
'https 80 is NOT default' => ['https', 80, false],
'http 443 is NOT default' => ['http', 443, false],
'https 8443 is NOT default' => ['https', 8443, false],
'http 8080 is NOT default' => ['http', 8080, false],
'null port is default' => ['https', null, true],
'empty string port is default' => ['http', '', true],
'string 443 on https' => ['https', '443', true],
'string 80 on http' => ['http', '80', true],
'string 443 on http' => ['http', '443', false],
'ftp 21 unknown scheme' => ['ftp', 21, false],
];
}

#[Test]
#[DataProvider('isIanaDefaultProvider')]
public function isIanaDefault(string $scheme, int|string|null $port, bool $expected): void
{
$this->assertSame($expected, PortUtil::isIanaDefault($scheme, $port));
}

/**
* @return array<string, array{string, string, string}>
*/
public static function stripDefaultPortProvider(): array
{
return [
'https host:443 stripped' => ['https', 'example.com:443', 'example.com'],
'http host:80 stripped' => ['http', 'example.com:80', 'example.com'],
'https host:80 kept' => ['https', 'example.com:80', 'example.com:80'],
'http host:443 kept' => ['http', 'example.com:443', 'example.com:443'],
'https host:8443 kept' => ['https', 'example.com:8443', 'example.com:8443'],
'no port unchanged' => ['https', 'example.com', 'example.com'],
'http host:8080 kept' => ['http', 'example.com:8080', 'example.com:8080'],
];
}

#[Test]
#[DataProvider('stripDefaultPortProvider')]
public function stripDefaultPort(string $scheme, string $host, string $expected): void
{
$this->assertSame($expected, PortUtil::stripDefaultPort($scheme, $host));
}
}
Loading