Skip to content

fix: correct getHostname() fallback logic in Email class #9587

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 31, 2025
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
12 changes: 8 additions & 4 deletions system/Email/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -2142,12 +2142,16 @@ protected function getSMTPData()
*/
protected function getHostname()
{
if (isset($_SERVER['SERVER_NAME'])) {
return $_SERVER['SERVER_NAME'];
$superglobals = service('superglobals');

$serverName = $superglobals->server('SERVER_NAME');
if (! in_array($serverName, [null, ''], true)) {
return $serverName;
}

if (isset($_SERVER['SERVER_ADDR'])) {
return '[' . $_SERVER['SERVER_ADDR'] . ']';
$serverAddr = $superglobals->server('SERVER_ADDR');
if (! in_array($serverAddr, [null, ''], true)) {
return '[' . $serverAddr . ']';
}

$hostname = gethostname();
Expand Down
51 changes: 51 additions & 0 deletions tests/system/Email/EmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,20 @@
use CodeIgniter\Events\Events;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\Mock\MockEmail;
use CodeIgniter\Test\ReflectionHelper;
use ErrorException;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use ReflectionException;

/**
* @internal
*/
#[Group('Others')]
final class EmailTest extends CIUnitTestCase
{
use ReflectionHelper;

public function testEmailValidation(): void
{
$config = config('Email');
Expand Down Expand Up @@ -215,4 +219,51 @@ public function testSetAttachmentCIDBufferString(): void
$email->archive['body'],
);
}

/**
* @throws ReflectionException
*/
public function testGetHostnameUsesServerName(): void
{
$email = $this->createMockEmail();

$superglobals = service('superglobals');
$superglobals->setServer('SERVER_NAME', 'example.test');

$getHostname = self::getPrivateMethodInvoker($email, 'getHostname');

$this->assertSame('example.test', $getHostname());
}

/**
* @throws ReflectionException
*/
public function testGetHostnameUsesServerAddr(): void
{
$email = $this->createMockEmail();

$superglobals = service('superglobals');
$superglobals->setServer('SERVER_NAME', '');
$superglobals->setServer('SERVER_ADDR', '192.168.1.10');

$getHostname = self::getPrivateMethodInvoker($email, 'getHostname');

$this->assertSame('[192.168.1.10]', $getHostname());
}

/**
* @throws ReflectionException
*/
public function testGetHostnameFallsBackToGethostnameFunction(): void
{
$email = $this->createMockEmail();

$superglobals = service('superglobals');
$superglobals->setServer('SERVER_NAME', '');
$superglobals->setServer('SERVER_ADDR', '');

$getHostname = self::getPrivateMethodInvoker($email, 'getHostname');

$this->assertSame(gethostname(), $getHostname());
}
}
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/v4.6.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Bugs Fixed

- **Cache:** Fixed a bug where a corrupted or unreadable cache file could cause an unhandled exception in ``FileHandler::getItem()``.
- **Database:** Fixed a bug where ``when()`` and ``whenNot()`` in ``ConditionalTrait`` incorrectly evaluated certain falsy values (such as ``[]``, ``0``, ``0.0``, and ``'0'``) as truthy, causing callbacks to be executed unexpectedly. These methods now cast the condition to a boolean using ``(bool)`` to ensure consistent behavior with PHP's native truthiness.
- **Email:** Fixed a bug where ``Email::getHostname()`` failed to use ``$_SERVER['SERVER_ADDR']`` when ``$_SERVER['SERVER_NAME']`` was not set.
- **Security:** Fixed a bug where the ``sanitize_filename()`` function from the Security helper would throw an error when used in CLI requests.
- **Session:** Fixed a bug where using the ``DatabaseHandler`` with an unsupported database driver (such as ``SQLSRV``, ``OCI8``, or ``SQLite3``) did not throw an appropriate error.

Expand Down
12 changes: 1 addition & 11 deletions utils/phpstan-baseline/codeigniter.superglobalAccess.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# total 83 errors
# total 79 errors

parameters:
ignoreErrors:
Expand Down Expand Up @@ -67,16 +67,6 @@ parameters:
count: 1
path: ../../system/Config/Services.php

-
message: '#^Accessing offset ''SERVER_ADDR'' directly on \$_SERVER is discouraged\.$#'
count: 2
path: ../../system/Email/Email.php

-
message: '#^Accessing offset ''SERVER_NAME'' directly on \$_SERVER is discouraged\.$#'
count: 2
path: ../../system/Email/Email.php

-
message: '#^Accessing offset ''HTTP_USER_AGENT'' directly on \$_SERVER is discouraged\.$#'
count: 2
Expand Down
2 changes: 1 addition & 1 deletion utils/phpstan-baseline/loader.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# total 3149 errors
# total 3145 errors
includes:
- argument.type.neon
- assign.propertyType.neon
Expand Down
Loading