Skip to content

Commit

Permalink
[1.x] Fix the issue for connecting if the SSL is not verified peer (#85)
Browse files Browse the repository at this point in the history
* Fix

* cleaning

* remove default context

* configure tls

* formatting

---------

Co-authored-by: Joe Dixon <[email protected]>
Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
3 people authored Mar 27, 2024
1 parent 9e2bd2a commit d1b72f4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
29 changes: 23 additions & 6 deletions src/Servers/Reverb/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,7 @@ public static function make(
default => throw new InvalidArgumentException("Unsupported protocol [{$protocol}]."),
};

if (empty($options['tls']) && $hostname && Certificate::exists($hostname)) {
[$certificate, $key] = Certificate::resolve($hostname);

$options['tls']['local_cert'] = $certificate;
$options['tls']['local_pk'] = $key;
}
$options['tls'] = static::configureTls($options['tls'] ?? [], $hostname);

$uri = empty($options['tls']) ? "{$host}:{$port}" : "tls://{$host}:{$port}";

Expand Down Expand Up @@ -109,4 +104,26 @@ protected static function pusherRoutes(): RouteCollection

return $routes;
}

/**
* Configure the TLS context for the server.
*
* @param array $context<string, mixed>
* @return array<string, mixed>
*/
protected static function configureTls(array $context, ?string $hostname): array
{
$context = array_filter($context, fn ($value) => $value !== null);

$usesTls = ($context['local_cert'] ?? false) || ($context['local_pk'] ?? false);

if (! $usesTls && $hostname && Certificate::exists($hostname)) {
[$certificate, $key] = Certificate::resolve($hostname);

$context['local_cert'] = $certificate;
$context['local_pk'] = $key;
}

return $context;
}
}
14 changes: 14 additions & 0 deletions tests/Unit/Servers/Reverb/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

it('can create a server using tls on the given host and port', function () {
$this->app->config->set('reverb.servers.reverb.options.tls.local_cert', '/path/to/cert.pem');
$this->app->config->set('reverb.servers.reverb.options.tls.verify_peer', false);
$server = Factory::make('127.0.0.1', '8002', options: $this->app->config->get('reverb.servers.reverb.options'));

$socket = (new ReflectionProperty($server, 'socket'))->getValue($server);
Expand All @@ -55,3 +56,16 @@

$server->stop();
});

it('can create a server without tls when context values are null', function () {
$this->app->config->set('reverb.servers.reverb.options.tls.local_cert', null);
$this->app->config->set('reverb.servers.reverb.options.tls.verify_peer', null);
$server = Factory::make('127.0.0.1', '8002', options: $this->app->config->get('reverb.servers.reverb.options'));

$socket = (new ReflectionProperty($server, 'socket'))->getValue($server);
$socketServer = (new ReflectionProperty($socket, 'server'))->getValue($socket);

expect($socketServer)->toBeInstanceOf(TcpServer::class);

$server->stop();
});

0 comments on commit d1b72f4

Please sign in to comment.