diff --git a/src/library/FOSSBilling/Tools.php b/src/library/FOSSBilling/Tools.php index fac6c9601..1106aa882 100644 --- a/src/library/FOSSBilling/Tools.php +++ b/src/library/FOSSBilling/Tools.php @@ -226,6 +226,28 @@ class Tools return is_countable($value) ? count($value) : 0; } + /** + * Normalizes mixed input into a boolean value. + */ + public static function normalizeBoolean(mixed $value, bool $default = false): bool + { + if (is_bool($value)) { + return $value; + } + + if (is_int($value) || is_float($value)) { + return (bool) $value; + } + + if (is_string($value)) { + $normalized = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); + + return $normalized ?? $default; + } + + return $default; + } + public static function isHTTPS(): bool { $protocol = $_SERVER['HTTPS'] ?? $_SERVER['HTTP_X_FORWARDED_PROTO'] ?? $_SERVER['REQUEST_SCHEME'] ?? ''; diff --git a/src/library/Payment/Adapter/PayPalEmail.php b/src/library/Payment/Adapter/PayPalEmail.php index a231071b9..96a50e18b 100644 --- a/src/library/Payment/Adapter/PayPalEmail.php +++ b/src/library/Payment/Adapter/PayPalEmail.php @@ -296,8 +296,6 @@ class Payment_Adapter_PayPalEmail extends Payment_AdapterAbstract implements FOS } $client = $this->getHttpClient()->withOptions([ - 'verify_peer' => false, - 'verify_host' => false, 'timeout' => 600, ]); $response = $client->request('POST', $url, [ diff --git a/src/library/Registrar/Adapter/Internetbs.php b/src/library/Registrar/Adapter/Internetbs.php index cdb531dcd..3241d46ae 100644 --- a/src/library/Registrar/Adapter/Internetbs.php +++ b/src/library/Registrar/Adapter/Internetbs.php @@ -328,10 +328,7 @@ class Registrar_Adapter_Internetbs extends Registrar_AdapterAbstract $params['apikey'] = $this->config['apikey']; $params['password'] = $this->config['password']; - $client = $this->getHttpClient()->withOptions([ - 'verify_peer' => false, - 'verify_host' => false, - ]); + $client = $this->getHttpClient(); try { $response = $client->request('POST', $this->_getApiUrl() . $command, [ diff --git a/src/library/Registrar/Adapter/Namecheap.php b/src/library/Registrar/Adapter/Namecheap.php index 1d2aed139..4c0cdeb6e 100644 --- a/src/library/Registrar/Adapter/Namecheap.php +++ b/src/library/Registrar/Adapter/Namecheap.php @@ -123,8 +123,6 @@ class Registrar_Adapter_Namecheap extends Registrar_AdapterAbstract $client = $this->getHttpClient()->withOptions([ 'timeout' => 60, - 'verify_peer' => 0, - 'verify_host' => 0, ]); $callUrl = $this->_getApiUrl(); diff --git a/src/library/Registrar/Adapter/Resellerclub.php b/src/library/Registrar/Adapter/Resellerclub.php index 39c5a432a..4099c3bfa 100644 --- a/src/library/Registrar/Adapter/Resellerclub.php +++ b/src/library/Registrar/Adapter/Resellerclub.php @@ -668,8 +668,6 @@ class Registrar_Adapter_Resellerclub extends Registrar_AdapterAbstract $client = $this->getHttpClient()->withOptions([ 'timeout' => 60, - 'verify_peer' => false, - 'verify_host' => false, ]); $callUrl = $this->_getApiUrl() . $url . '.' . $type; diff --git a/src/library/Server/Manager/CWP.php b/src/library/Server/Manager/CWP.php index a3297310e..5d63056ee 100644 --- a/src/library/Server/Manager/CWP.php +++ b/src/library/Server/Manager/CWP.php @@ -381,6 +381,8 @@ class Server_Manager_CWP extends Server_Manager */ private function request(string $func, array $data): mixed { + $verifyTls = \FOSSBilling\Tools::normalizeBoolean($this->_config['config']['tls_verify'] ?? true, true); + // Add the access hash to the data array $data['key'] = $this->_config['accesshash']; @@ -391,10 +393,10 @@ class Server_Manager_CWP extends Server_Manager // Construct the URL for the request $url = 'https://' . $host . ':' . $port . '/v1/' . $func; - // Get the HTTP client with options set to ignore SSL verification + // Get the HTTP client with TLS verification options from server configuration $client = $this->getHttpClient()->withOptions([ - 'verify_peer' => false, - 'verify_host' => false, + 'verify_peer' => $verifyTls, + 'verify_host' => $verifyTls, ]); // Make the request and convert the response to an array diff --git a/src/library/Server/Manager/Directadmin.php b/src/library/Server/Manager/Directadmin.php index a2bf7f5a2..60fd4a6c9 100644 --- a/src/library/Server/Manager/Directadmin.php +++ b/src/library/Server/Manager/Directadmin.php @@ -709,6 +709,8 @@ class Server_Manager_Directadmin extends Server_Manager */ private function request(string $command, array $fields = [], bool $post = true, string $asUser = ''): array { + $verifyTls = \FOSSBilling\Tools::normalizeBoolean($this->_config['config']['tls_verify'] ?? true, true); + // Get the host from the configuration $host = $this->_config['host']; @@ -728,8 +730,8 @@ class Server_Manager_Directadmin extends Server_Manager $httpClient = $this->getHttpClient()->withOptions([ 'auth_basic' => [$username, $this->_config['password']], 'timeout' => 60, - 'verify_host' => false, - 'verify_peer' => false, + 'verify_host' => $verifyTls, + 'verify_peer' => $verifyTls, ]); // Construct the URL for the request @@ -809,7 +811,7 @@ class Server_Manager_Directadmin extends Server_Manager parse_str($data, $response); // Log the parsed response data for debugging purposes - $this->getLog()->debug('Parsed Response: ' . print_r($response, 1)); + $this->getLog()->debug('Parsed Response: ' . print_r($response, true)); // Return the parsed response data return $response; diff --git a/src/library/Server/Manager/Hestia.php b/src/library/Server/Manager/Hestia.php index d9baf598b..c6cafd902 100644 --- a/src/library/Server/Manager/Hestia.php +++ b/src/library/Server/Manager/Hestia.php @@ -348,6 +348,7 @@ class Server_Manager_Hestia extends Server_Manager */ private function request($params): mixed { + $verifyTls = \FOSSBilling\Tools::normalizeBoolean($this->_config['config']['tls_verify'] ?? true, true); $host = 'https://' . $this->_config['host'] . ':' . $this->getPort() . '/api/'; // Set return code to yes @@ -365,8 +366,8 @@ class Server_Manager_Hestia extends Server_Manager // Send POST query $client = $this->getHttpClient()->withOptions([ - 'verify_peer' => false, - 'verify_host' => false, + 'verify_peer' => $verifyTls, + 'verify_host' => $verifyTls, 'timeout' => 30, ]); diff --git a/src/library/Server/Manager/Whm.php b/src/library/Server/Manager/Whm.php index 9aa297d93..a314ded46 100644 --- a/src/library/Server/Manager/Whm.php +++ b/src/library/Server/Manager/Whm.php @@ -578,10 +578,12 @@ class Server_Manager_Whm extends Server_Manager */ private function request(string $action, array $params = []): mixed { + $verifyTls = \FOSSBilling\Tools::normalizeBoolean($this->_config['config']['tls_verify'] ?? true, true); + // Create the HTTP client with the necessary options $client = $this->getHttpClient()->withOptions([ - 'verify_peer' => false, - 'verify_host' => false, + 'verify_peer' => $verifyTls, + 'verify_host' => $verifyTls, 'timeout' => 90, // Account creation can timeout if set too low - see #1086. ]); diff --git a/src/modules/Servicehosting/Api/Admin.php b/src/modules/Servicehosting/Api/Admin.php index 3f800208f..3dd096b47 100644 --- a/src/modules/Servicehosting/Api/Admin.php +++ b/src/modules/Servicehosting/Api/Admin.php @@ -12,6 +12,7 @@ declare(strict_types=1); namespace Box\Mod\Servicehosting\Api; +use FOSSBilling\Tools; use FOSSBilling\Validation\Api\RequiredParams; /** @@ -198,6 +199,7 @@ class Admin extends \Api_Abstract * @optional string $port - server API port * @optional string $passwordLength - password length for generated accounts * @optional bool $secure - flag to define whether to use secure connection (https) to server or not (http) + * @optional bool $tls_verify - flag to define whether to verify TLS certificates when calling server APIs * @optional bool $active - flag to enable/disable server * * @return int - server id @@ -213,6 +215,11 @@ class Admin extends \Api_Abstract { $service = $this->getService(); + $data['config'] = [ + 'userprefix' => $data['userprefix'] ?? null, + 'tls_verify' => Tools::normalizeBoolean($data['tls_verify'] ?? true, true), + ]; + return (int) $service->createServer($data['name'], $data['ip'], $data['manager'], $data); } @@ -268,6 +275,7 @@ class Admin extends \Api_Abstract * @optional string $port - server API port * @optional string $passwordLength - password length for generated accounts * @optional bool $secure - flag to define whether to use secure connection (https) to server or not (http) + * @optional bool $tls_verify - flag to define whether to verify TLS certificates when calling server APIs * @optional bool $active - flag to enable/disable server * * @throws \FOSSBilling\Exception @@ -278,9 +286,10 @@ class Admin extends \Api_Abstract $model = $this->di['db']->getExistingModelById('ServiceHostingServer', $data['id'], 'Server not found'); $service = $this->getService(); - $data['config'] = [ - 'userprefix' => $data['userprefix'] ?? null, - ]; + $existingConfig = json_decode($model->config ?? '', true) ?? []; + + $data['config'] = $existingConfig; + $data['config']['tls_verify'] = Tools::normalizeBoolean($data['tls_verify'] ?? ($existingConfig['tls_verify'] ?? true), true); return (bool) $service->updateServer($model, $data); } diff --git a/src/modules/Servicehosting/Service.php b/src/modules/Servicehosting/Service.php index 1e2e9aad7..f767c58c3 100644 --- a/src/modules/Servicehosting/Service.php +++ b/src/modules/Servicehosting/Service.php @@ -749,8 +749,9 @@ class Service implements InjectionAwareInterface $model->password = $data['password'] ?? null; $model->accesshash = $data['accesshash'] ?? null; $model->port = $data['port'] ?? null; + $model->config = isset($data['config']) ? json_encode($data['config']) : null; $model->passwordLength = is_numeric($data['passwordLength'] ?? '') ? intval($data['passwordLength']) : null; - $model->secure = $data['secure'] ?? 0; + $model->secure = $data['secure'] ?? 1; $model->created_at = date('Y-m-d H:i:s'); $model->updated_at = date('Y-m-d H:i:s'); diff --git a/src/modules/Servicehosting/templates/admin/mod_servicehosting_index.html.twig b/src/modules/Servicehosting/templates/admin/mod_servicehosting_index.html.twig index 0dab94295..e1ce15f8a 100644 --- a/src/modules/Servicehosting/templates/admin/mod_servicehosting_index.html.twig +++ b/src/modules/Servicehosting/templates/admin/mod_servicehosting_index.html.twig @@ -241,13 +241,28 @@
- +
- +
+ {{ 'Controls whether HTTPS or HTTP is used for API requests.'|trans }} +
+ +
+ +
+
+ + +
+
+ + +
+ {{ 'Recommended: keep enabled unless your server uses a self-signed or otherwise invalid certificate.'|trans }}
diff --git a/src/modules/Servicehosting/templates/admin/mod_servicehosting_server.html.twig b/src/modules/Servicehosting/templates/admin/mod_servicehosting_server.html.twig index 08e8b28ff..e25c0456d 100644 --- a/src/modules/Servicehosting/templates/admin/mod_servicehosting_server.html.twig +++ b/src/modules/Servicehosting/templates/admin/mod_servicehosting_server.html.twig @@ -21,6 +21,7 @@ {% endblock %} {% block content %} +{% set tlsVerify = server.config.tls_verify is defined ? server.config.tls_verify : true %}

{{ 'Server Management'|trans }}

@@ -102,6 +103,21 @@
+ {{ 'Controls whether HTTPS or HTTP is used for API requests.'|trans }} +
+ +
+ +
+
+ + +
+
+ + +
+ {{ 'Recommended: keep enabled unless your server uses a self-signed or otherwise invalid certificate.'|trans }}
diff --git a/tests-legacy/library/FOSSBilling/FOSSBilling_ToolsTest.php b/tests-legacy/library/FOSSBilling/FOSSBilling_ToolsTest.php index 605338add..84234097d 100644 --- a/tests-legacy/library/FOSSBilling/FOSSBilling_ToolsTest.php +++ b/tests-legacy/library/FOSSBilling/FOSSBilling_ToolsTest.php @@ -2,6 +2,8 @@ declare(strict_types=1); +use PHPUnit\Framework\Attributes\Group; + #[Group('Core')] final class FOSSBilling_ToolsTest extends PHPUnit\Framework\TestCase { @@ -77,4 +79,18 @@ final class FOSSBilling_ToolsTest extends PHPUnit\Framework\TestCase $result = FOSSBilling\Tools::sanitizeContent($input, true); $this->assertStringNotContainsString('onerror', $result); } + + public function testNormalizeBooleanHandlesBooleanStringValues(): void + { + $this->assertTrue(FOSSBilling\Tools::normalizeBoolean('true')); + $this->assertFalse(FOSSBilling\Tools::normalizeBoolean('false')); + $this->assertTrue(FOSSBilling\Tools::normalizeBoolean('1')); + $this->assertFalse(FOSSBilling\Tools::normalizeBoolean('0')); + } + + public function testNormalizeBooleanRespectsDefaultForUnknownString(): void + { + $this->assertTrue(FOSSBilling\Tools::normalizeBoolean('maybe', true)); + $this->assertFalse(FOSSBilling\Tools::normalizeBoolean('maybe')); + } } diff --git a/tests-legacy/modules/Servicehosting/Api/AdminTest.php b/tests-legacy/modules/Servicehosting/Api/AdminTest.php index 03f1b87e1..d893fb118 100644 --- a/tests-legacy/modules/Servicehosting/Api/AdminTest.php +++ b/tests-legacy/modules/Servicehosting/Api/AdminTest.php @@ -414,6 +414,49 @@ final class AdminTest extends \BBTestCase $this->assertTrue($result); } + public function testServerUpdatePreservesUnknownConfigKeys(): void + { + $data = [ + 'id' => 1, + 'tls_verify' => '0', + ]; + + $model = new \Model_ServiceHostingServer(); + $model->loadBean(new \DummyBean()); + $model->config = json_encode([ + 'userprefix' => 'oldprefix', + 'custom_key' => 'keep-me', + 'tls_verify' => true, + ]); + + $serviceMock = $this->createMock(\Box\Mod\Servicehosting\Service::class); + $serviceMock->expects($this->once()) + ->method('updateServer') + ->with( + $model, + $this->callback(function (array $payload): bool { + return isset($payload['config']) + && $payload['config']['custom_key'] === 'keep-me' + && $payload['config']['userprefix'] === 'oldprefix' + && $payload['config']['tls_verify'] === false; + }) + ) + ->willReturn(true); + + $dbMock = $this->createMock('\Box_Database'); + $dbMock->expects($this->once()) + ->method('getExistingModelById') + ->willReturn($model); + + $di = $this->getDi(); + $di['db'] = $dbMock; + $this->api->setDi($di); + $this->api->setService($serviceMock); + + $result = $this->api->server_update($data); + $this->assertTrue($result); + } + public function testServerTestConnection(): void { $data['id'] = 1;