mirror of
https://github.com/FOSSBilling/FOSSBilling.git
synced 2026-12-04 22:24:42 +09:00
Reenable verify_peer and verify_host for external services (#3395)
* Reenable verify_peer and verify_host for external services * Reenable verify_peer and verify_host for external services * Add a `tls_verify` option for server managers * Move normalizeBoolean to the Tools class * Make `secure` enabled by default * Address Copilot comment
This commit is contained in:
1 parent
739af86e6d
commit
649e41e950
15 files changed
+146
-26
No files matched your search
@@ -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'] ?? '';
|
||||
|
||||
@@ -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, [
|
||||
|
||||
@@ -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, [
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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,
|
||||
]);
|
||||
|
||||
|
||||
@@ -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.
|
||||
]);
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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');
|
||||
|
||||
@@ -241,13 +241,28 @@
|
||||
<label class="form-label col-3 col-form-label">{{ 'Use secure connection'|trans }}:</label>
|
||||
<div class="col">
|
||||
<div class="form-check form-check-inline">
|
||||
<input class="form-check-input" id="radioSecureYes" type="radio" name="secure" value="1">
|
||||
<input class="form-check-input" id="radioSecureYes" type="radio" name="secure" value="1" checked>
|
||||
<label class="form-check-label" for="radioSecureYes">{{ 'Yes'|trans }}</label>
|
||||
</div>
|
||||
<div class="form-check form-check-inline">
|
||||
<input class="form-check-input" id="radioSecureNo" type="radio" name="secure" value="0" checked>
|
||||
<input class="form-check-input" id="radioSecureNo" type="radio" name="secure" value="0">
|
||||
<label class="form-check-label" for="radioSecureNo">{{ 'No'|trans }}</label>
|
||||
</div>
|
||||
<span class="text-muted">{{ 'Controls whether HTTPS or HTTP is used for API requests.'|trans }}</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="mb-3 row">
|
||||
<label class="form-label col-3 col-form-label">{{ 'Verify TLS certificate'|trans }}:</label>
|
||||
<div class="col">
|
||||
<div class="form-check form-check-inline">
|
||||
<input class="form-check-input" id="radioTlsVerifyCreateYes" type="radio" name="tls_verify" value="1" checked>
|
||||
<label class="form-check-label" for="radioTlsVerifyCreateYes">{{ 'Yes'|trans }}</label>
|
||||
</div>
|
||||
<div class="form-check form-check-inline">
|
||||
<input class="form-check-input" id="radioTlsVerifyCreateNo" type="radio" name="tls_verify" value="0">
|
||||
<label class="form-check-label" for="radioTlsVerifyCreateNo">{{ 'No'|trans }}</label>
|
||||
</div>
|
||||
<span class="text-muted">{{ 'Recommended: keep enabled unless your server uses a self-signed or otherwise invalid certificate.'|trans }}</span>
|
||||
</div>
|
||||
</div>
|
||||
<input type="submit" value="{{ 'Add server'|trans }}" class="btn btn-primary">
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
{% set tlsVerify = server.config.tls_verify is defined ? server.config.tls_verify : true %}
|
||||
<div class="card">
|
||||
<div class="card-body">
|
||||
<h3>{{ 'Server Management'|trans }}</h3>
|
||||
@@ -102,6 +103,21 @@
|
||||
<input class="form-check-input" id="radioSecureConnectionNo" type="radio" name="secure" value="0"{% if not server.secure %} checked{% endif %}>
|
||||
<label class="form-check-label" for="radioSecureConnectionNo">{{ 'No'|trans }}</label>
|
||||
</div>
|
||||
<span class="text-muted">{{ 'Controls whether HTTPS or HTTP is used for API requests.'|trans }}</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="mb-3 row">
|
||||
<label class="form-label col-3 col-form-label">{{ 'Verify TLS certificate'|trans }}:</label>
|
||||
<div class="col">
|
||||
<div class="form-check form-check-inline">
|
||||
<input class="form-check-input" id="radioTlsVerifyYes" type="radio" name="tls_verify" value="1"{% if tlsVerify %} checked{% endif %}>
|
||||
<label class="form-check-label" for="radioTlsVerifyYes">{{ 'Yes'|trans }}</label>
|
||||
</div>
|
||||
<div class="form-check form-check-inline">
|
||||
<input class="form-check-input" id="radioTlsVerifyNo" type="radio" name="tls_verify" value="0"{% if not tlsVerify %} checked{% endif %}>
|
||||
<label class="form-check-label" for="radioTlsVerifyNo">{{ 'No'|trans }}</label>
|
||||
</div>
|
||||
<span class="text-muted">{{ 'Recommended: keep enabled unless your server uses a self-signed or otherwise invalid certificate.'|trans }}</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -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'));
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user