Skip to content

Commit b547b22

Browse files
committed
fix: redis connect to protocol tls
1 parent 6e5a9cb commit b547b22

2 files changed

Lines changed: 35 additions & 10 deletions

File tree

system/Session/Handlers/RedisHandler.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,20 +102,21 @@ protected function setSavePath(): void
102102
throw SessionException::forEmptySavepath();
103103
}
104104

105-
if (preg_match('#(?:tcp://)?([^:?]+)(?:\:(\d+))?(\?.+)?#', $this->savePath, $matches)) {
106-
if (! isset($matches[3])) {
107-
$matches[3] = ''; // Just to avoid undefined index notices below
105+
if (preg_match('#(?:(^.*)://)?([^:?]+)(?:\:(\d+))?(\?.+)?#', $this->savePath, $matches)) {
106+
if (! isset($matches[4])) {
107+
$matches[4] = ''; // Just to avoid undefined index notices below
108108
}
109109

110110
$this->savePath = [
111-
'host' => $matches[1],
112-
'port' => empty($matches[2]) ? self::DEFAULT_PORT : $matches[2],
113-
'password' => preg_match('#auth=([^\s&]+)#', $matches[3], $match) ? $match[1] : null,
114-
'database' => preg_match('#database=(\d+)#', $matches[3], $match) ? (int) $match[1] : 0,
115-
'timeout' => preg_match('#timeout=(\d+\.\d+|\d+)#', $matches[3], $match) ? (float) $match[1] : 0.0,
111+
'protocol' => in_array($matches[1], ['tcp', 'tls'], true) ? $matches[1] : 'tcp',
112+
'host' => $matches[2],
113+
'port' => empty($matches[3]) ? self::DEFAULT_PORT : $matches[3],
114+
'password' => preg_match('#auth=([^\s&]+)#', $matches[4], $match) ? $match[1] : null,
115+
'database' => preg_match('#database=(\d+)#', $matches[4], $match) ? (int) $match[1] : 0,
116+
'timeout' => preg_match('#timeout=(\d+\.\d+|\d+)#', $matches[4], $match) ? (float) $match[1] : 0.0,
116117
];
117118

118-
preg_match('#prefix=([^\s&]+)#', $matches[3], $match) && $this->keyPrefix = $match[1];
119+
preg_match('#prefix=([^\s&]+)#', $matches[4], $match) && $this->keyPrefix = $match[1];
119120
} else {
120121
throw SessionException::forInvalidSavePathFormat($this->savePath);
121122
}
@@ -135,7 +136,7 @@ public function open($path, $name): bool
135136

136137
$redis = new Redis();
137138

138-
if (! $redis->connect($this->savePath['host'], ($this->savePath['host'][0] === '/' ? 0 : $this->savePath['port']), $this->savePath['timeout'])) {
139+
if (! $redis->connect($this->savePath['protocol'] . '://' . $this->savePath['host'], ($this->savePath['host'][0] === '/' ? 0 : $this->savePath['port']), $this->savePath['timeout'])) {
139140
$this->logger->error('Session: Unable to connect to Redis with the configured settings.');
140141
} elseif (isset($this->savePath['password']) && ! $redis->auth($this->savePath['password'])) {
141142
$this->logger->error('Session: Unable to authenticate to Redis instance.');

tests/system/Session/Handlers/Database/RedisHandlerTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,30 @@ protected function getInstance($options = [])
5454
return new RedisHandler(new AppConfig(), $this->userIpAddress);
5555
}
5656

57+
public function testSavePathTLSAuth()
58+
{
59+
$handler = $this->getInstance(
60+
['savePath' => 'tls://127.0.0.1:6379?auth=password&timeout=2.5']
61+
);
62+
63+
$savePath = $this->getPrivateProperty($handler, 'savePath');
64+
65+
$this->assertSame('password', $savePath['password']);
66+
$this->assertSame(2.5, $savePath['timeout']);
67+
}
68+
69+
public function testSavePathTCPAuth()
70+
{
71+
$handler = $this->getInstance(
72+
['savePath' => 'tcp://127.0.0.1:6379?auth=password&timeout=2.5']
73+
);
74+
75+
$savePath = $this->getPrivateProperty($handler, 'savePath');
76+
77+
$this->assertSame('password', $savePath['password']);
78+
$this->assertSame(2.5, $savePath['timeout']);
79+
}
80+
5781
public function testSavePathTimeoutFloat()
5882
{
5983
$handler = $this->getInstance(

0 commit comments

Comments
 (0)