Compare commits

...

11 Commits

Author SHA1 Message Date
vladvildanov 7e6ea7f6ab Revert change 2026-09-15 10:31:43 +03:00
vladvildanov 67bc88caac Fixed CRLF command smuggling and node misrouting in AbstractAggregateConnection::write() and CommandInterface::deserializeCommand() 2026-09-15 10:28:14 +03:00
vladvildanov 182eb7d4ad Revert "Fixed CRLF command injection / smuggling in AbstractAggregateConnection::write()"
This reverts commit a4720b03ee.
2026-09-15 10:07:45 +03:00
vladvildanov a4720b03ee Fixed CRLF command injection / smuggling in AbstractAggregateConnection::write() 2026-09-15 09:56:58 +03:00
Vladyslav Vildanov 37490865cc Fixed Stream::write()/read() leaving a dead connection when a host error handler throws exception (#1726) 2026-09-14 11:32:59 -07:00
Vladyslav Vildanov 3a5b55e46e Deprecated CommandInterface::deserializeCommand() (CVE GHSA-w6f5-v2h6-g786) (#1724) 2026-09-11 07:56:58 -07:00
Till Krüss 401abc4315 fix changelog typo 2026-09-10 15:31:07 -07:00
Lazizbek Ergashev e7b89c14b7 Fixed client_info connection parameter being ignored (#1722)
* Fixed `client_info` connection parameter being ignored
* Documented the default value of the `client_info` parameter
2026-09-10 15:24:31 -07:00
dependabot[bot] 3a8c350f3d Bump the github-actions group with 2 updates (#1720) 2026-09-01 16:46:54 -04:00
Sergey Sannikov 675951360f Fix RESP3 double parsing returning positive INF for -inf (#1716)
parseDouble() returned positive INF for the RESP3 payload ',-inf',
inverting the sign. The value is reachable with protocol=3, for
example ZSCORE on a member whose score is -inf.

The existing infinity test asserted only is_infinite(), which cannot
detect the sign inversion, so it now checks exact values.

NaN payloads keep their current behavior; that is addressed
separately.
2026-08-27 16:46:39 +03:00
Till Krüss 0795d69d9e bump dev version 2026-08-14 16:09:00 -07:00
22 changed files with 643 additions and 60 deletions
+1 -1
View File
@@ -13,7 +13,7 @@ jobs:
- name: Checkout
uses: actions/checkout@v7
- name: Check Spelling
uses: rojopolis/spellcheck-github-actions@0.63.0
uses: rojopolis/spellcheck-github-actions@0.66.0
with:
config_path: .github/spellcheck-settings.yml
task_name: Markdown
+5 -5
View File
@@ -85,13 +85,13 @@ jobs:
uses: actions/checkout@v7
- name: Start Redis standalone image
uses: hoverkraft-tech/compose-action@v3.0.0
uses: hoverkraft-tech/compose-action@v3.1.0
with:
compose-file: .github/docker-compose.yml
services: ${{ env.DOCKER_SERVICE }}
- name: Start Redis unprotected image
uses: hoverkraft-tech/compose-action@v3.0.0
uses: hoverkraft-tech/compose-action@v3.1.0
if: ${{ matrix.redis > '4.0' }}
with:
compose-file: .github/docker-compose.yml
@@ -99,7 +99,7 @@ jobs:
- name: Start Redis stack image
id: stack_infra
uses: hoverkraft-tech/compose-action@v3.0.0
uses: hoverkraft-tech/compose-action@v3.1.0
if: ${{ matrix.redis >= '7.2' && matrix.redis < '8.0' }}
with:
compose-file: .github/docker-compose.yml
@@ -107,7 +107,7 @@ jobs:
- name: Start Redis cluster image
id: cluster_infra
uses: hoverkraft-tech/compose-action@v3.0.0
uses: hoverkraft-tech/compose-action@v3.1.0
if: ${{ matrix.redis > '4.0' }}
with:
compose-file: .github/docker-compose.yml
@@ -115,7 +115,7 @@ jobs:
- name: Start Redis sentinels image
id: sentinel_infra
uses: hoverkraft-tech/compose-action@v3.0.0
uses: hoverkraft-tech/compose-action@v3.1.0
if: ${{ matrix.redis > '4.0' }}
with:
compose-file: .github/docker-compose.yml
+10
View File
@@ -1,5 +1,15 @@
## Changelog
## Unreleased
### Added
### Changed
- Deprecated `CommandInterface::deserializeCommand()` (CVE GHSA-w6f5-v2h6-g786)
### Fixed
- Fixed RESP3 double parsing returning positive `INF` for `-inf` payloads (#1716)
- Fixed `client_info` connection parameter being ignored (#1722)
- Fixed `Stream::write()`/`read()` leaving a dead connection when a host error handler throws exception (#1725)
- Fixed CRLF command smuggling and node misrouting in `AbstractAggregateConnection::write()` and `CommandInterface::deserializeCommand()` (CVE GHSA-w6f5-v2h6-g786)
## v3.6.0 (2026-08-14)
### Added
- Added support for new TS commands + Indonesian language support integration test (#1695)
+1 -1
View File
@@ -1 +1 @@
3.6.0
3.6.1-dev
+1 -1
View File
@@ -56,7 +56,7 @@ use Traversable;
*/
class Client implements ClientInterface, IteratorAggregate
{
public const VERSION = '3.6.0';
public const VERSION = '3.6.1-dev';
/** @var OptionsInterface */
private $options;
+51 -14
View File
@@ -158,15 +158,14 @@ abstract class Command implements CommandInterface
/**
* {@inheritDoc}
*
* @deprecated Not binary-safe; see CommandInterface::deserializeCommand().
* Scheduled for removal in the next major.
*/
public static function deserializeCommand(string $serializedCommand): CommandInterface
{
if ($serializedCommand[0] !== '*') {
throw new UnexpectedValueException('Invalid serializing format');
}
$commandArray = explode("\r\n", $serializedCommand);
$commandId = $commandArray[2];
$items = self::parseMultibulk($serializedCommand);
$commandId = $items[0];
$classPath = __NAMESPACE__ . '\Redis\\';
// Check if given command is a module command.
@@ -186,15 +185,53 @@ abstract class Command implements CommandInterface
}
$command = new $classPath();
$arguments = [];
for ($i = 4, $iMax = count($commandArray); $i < $iMax; $i++) {
$arguments[] = $commandArray[$i];
++$i;
}
$command->setArguments($arguments);
$command->setArguments(array_slice($items, 1));
return $command;
}
/**
* Parses a RESP multibulk buffer into its individual bulk-string values
* (command ID followed by its arguments), walking each string by its own
* declared byte length instead of splitting the buffer on "\r\n" -- which
* a bulk string's payload may legitimately contain (see GHSA-w6f5-v2h6-g786).
*
* @param string $buffer
* @return string[]
*/
private static function parseMultibulk(string $buffer): array
{
if ($buffer[0] !== '*') {
throw new UnexpectedValueException('Invalid serializing format');
}
$lineEnd = strpos($buffer, "\r\n");
if ($lineEnd === false) {
throw new UnexpectedValueException('Invalid serializing format');
}
$count = (int) substr($buffer, 1, $lineEnd - 1);
$offset = $lineEnd + 2;
$items = [];
for ($i = 0; $i < $count; ++$i) {
if (($buffer[$offset] ?? '') !== '$') {
throw new UnexpectedValueException('Invalid serializing format');
}
$lineEnd = strpos($buffer, "\r\n", $offset);
if ($lineEnd === false) {
throw new UnexpectedValueException('Invalid serializing format');
}
$bulkLen = (int) substr($buffer, $offset + 1, $lineEnd - $offset - 1);
$dataStart = $lineEnd + 2;
$items[] = substr($buffer, $dataStart, $bulkLen);
$offset = $dataStart + $bulkLen + 2;
}
return $items;
}
}
+6
View File
@@ -98,6 +98,12 @@ interface CommandInterface
*
* @param string $serializedCommand
* @return static
*
* @deprecated Not binary-safe: it re-parses on "\r\n" and ignores RESP bulk-length
* prefixes, so any argument containing "\r\n" is corrupted, and it
* instantiates a command class from the parsed input. Never call it on
* untrusted or serialized data (see CVE GHSA-w6f5-v2h6-g786). Scheduled
* for removal in the next major.
*/
public static function deserializeCommand(string $serializedCommand): CommandInterface;
}
+54 -14
View File
@@ -153,14 +153,16 @@ final class RawCommand implements CommandInterface
return $buffer;
}
/**
* {@inheritDoc}
*
* @deprecated Not binary-safe; see CommandInterface::deserializeCommand().
* Scheduled for removal in the next major.
*/
public static function deserializeCommand(string $serializedCommand): CommandInterface
{
if ($serializedCommand[0] !== '*') {
throw new UnexpectedValueException('Invalid serializing format');
}
$commandArray = explode("\r\n", $serializedCommand);
$commandId = $commandArray[2];
$items = self::parseMultibulk($serializedCommand);
$commandId = $items[0];
$classPath = __NAMESPACE__ . '\Redis\\';
// Check if given command is a module command.
@@ -180,15 +182,53 @@ final class RawCommand implements CommandInterface
}
$command = new $classPath();
$arguments = [];
for ($i = 4, $iMax = count($commandArray); $i < $iMax; $i++) {
$arguments[] = $commandArray[$i];
++$i;
}
$command->setArguments($arguments);
$command->setArguments(array_slice($items, 1));
return $command;
}
/**
* Parses a RESP multibulk buffer into its individual bulk-string values
* (command ID followed by its arguments), walking each string by its own
* declared byte length instead of splitting the buffer on "\r\n" -- which
* a bulk string's payload may legitimately contain (see GHSA-w6f5-v2h6-g786).
*
* @param string $buffer
* @return string[]
*/
private static function parseMultibulk(string $buffer): array
{
if ($buffer[0] !== '*') {
throw new UnexpectedValueException('Invalid serializing format');
}
$lineEnd = strpos($buffer, "\r\n");
if ($lineEnd === false) {
throw new UnexpectedValueException('Invalid serializing format');
}
$count = (int) substr($buffer, 1, $lineEnd - 1);
$offset = $lineEnd + 2;
$items = [];
for ($i = 0; $i < $count; ++$i) {
if (($buffer[$offset] ?? '') !== '$') {
throw new UnexpectedValueException('Invalid serializing format');
}
$lineEnd = strpos($buffer, "\r\n", $offset);
if ($lineEnd === false) {
throw new UnexpectedValueException('Invalid serializing format');
}
$bulkLen = (int) substr($buffer, $offset + 1, $lineEnd - $offset - 1);
$dataStart = $lineEnd + 2;
$items[] = substr($buffer, $dataStart, $bulkLen);
$offset = $dataStart + $bulkLen + 2;
}
return $items;
}
}
+17 -9
View File
@@ -77,17 +77,25 @@ abstract class AbstractAggregateConnection implements AggregateConnectionInterfa
*/
public function write(string $buffer): void
{
$rawCommands = [];
$explodedBuffer = explode("\r\n", trim($buffer));
$offset = 0;
$length = strlen($buffer);
while (!empty($explodedBuffer)) {
$argsLen = (int) explode('*', $explodedBuffer[0])[1];
$cmdLen = ($argsLen * 2) + 1;
$rawCommands[] = array_splice($explodedBuffer, 0, $cmdLen);
}
while ($offset < $length) {
$start = $offset;
$lineEnd = strpos($buffer, "\r\n", $offset);
$argsCount = (int) substr($buffer, $offset + 1, $lineEnd - $offset - 1);
$offset = $lineEnd + 2;
foreach ($rawCommands as $command) {
$command = implode("\r\n", $command) . "\r\n";
// Advance by each bulk string's own declared byte length rather than
// splitting on literal "\r\n", which a bulk string's value may legitimately
// contain (see GHSA-w6f5-v2h6-g786).
for ($i = 0; $i < $argsCount; ++$i) {
$lineEnd = strpos($buffer, "\r\n", $offset);
$bulkLen = (int) substr($buffer, $offset + 1, $lineEnd - $offset - 1);
$offset = $lineEnd + 2 + $bulkLen + 2;
}
$command = substr($buffer, $start, $offset - $start);
$commandObj = Command::deserializeCommand($command);
$this->getConnectionByCommand($commandObj)->write($command);
}
+8 -6
View File
@@ -208,13 +208,15 @@ class Factory implements FactoryInterface
);
}
$connection->addConnectCommand(
new RawCommand('CLIENT', ['SETINFO', 'LIB-NAME', $this->buildLibraryName()])
);
if ($parameters->client_info ?? true) {
$connection->addConnectCommand(
new RawCommand('CLIENT', ['SETINFO', 'LIB-NAME', $this->buildLibraryName()])
);
$connection->addConnectCommand(
new RawCommand('CLIENT', ['SETINFO', 'LIB-VER', Client::VERSION])
);
$connection->addConnectCommand(
new RawCommand('CLIENT', ['SETINFO', 'LIB-VER', Client::VERSION])
);
}
if (isset($parameters->database) && strlen($parameters->database)) {
$connection->addConnectCommand(
+1 -1
View File
@@ -36,7 +36,7 @@ use Predis\Retry\Retry;
* @property string $database Database index (see the SELECT command).
* @property bool $async_connect Performs the connect() operation asynchronously.
* @property bool $tcp_nodelay Toggles the Nagle's algorithm for coalescing.
* @property bool $client_info Whether to set LIB-NAME and LIB-VER when connecting.
* @property bool $client_info Whether to set LIB-NAME and LIB-VER when connecting, enabled by default.
* @property Retry $retry Retry configuration
* @property bool $cache (Relay only) Whether to use in-memory caching.
* @property string $serializer (Relay only) Serializer used for data serialization.
+7 -3
View File
@@ -205,7 +205,10 @@ class Stream implements StreamInterface
throw new RuntimeException('Cannot write to a non-writable stream');
}
$result = fwrite($this->stream, $string);
// Suppressed: some error handlers (Laravel, Symfony, Laminas) convert engine
// notices/warnings into thrown exceptions, which would otherwise bypass the
// return-value handling below and leave a dead connection looking "connected".
$result = @fwrite($this->stream, $string);
if ($result === false || $result === 0) {
$metadata = $this->getMetadata();
@@ -265,10 +268,11 @@ class Stream implements StreamInterface
return '';
}
// Suppressed: see the note in write() above.
if ($length === -1) {
$string = fgets($this->stream);
$string = @fgets($this->stream);
} else {
$string = fread($this->stream, $length);
$string = @fread($this->stream, $length);
}
if (false === $string) {
@@ -63,10 +63,14 @@ class Resp3Strategy extends Resp2Strategy
*/
protected function parseDouble(string $string): float
{
if ($string === 'inf' || $string === '-inf') {
if ($string === 'inf') {
return INF;
}
if ($string === '-inf') {
return -INF;
}
return (float) $string;
}
+11
View File
@@ -281,6 +281,17 @@ class CommandTest extends PredisTestCase
TOPKQUERY::class,
['key'],
],
// Regression cases for GHSA-w6f5-v2h6-g786 (CWE-93): a "\r\n" embedded
// in a bulk string's own byte payload must not be mistaken for a RESP
// line boundary.
'GET with CRLF embedded in the value' => [
GET::class,
["value\r\n*1\r\n\$4\r\nEVIL"],
],
'ZADD with CRLF embedded in the key' => [
ZADD::class,
["key\r\n*1\r\n\$4\r\nEVIL", 'value', 'key1', 'value1'],
],
];
}
}
+11
View File
@@ -238,6 +238,17 @@ class RawCommandTest extends PredisTestCase
TOPKQUERY::class,
['key'],
],
// Regression cases for GHSA-w6f5-v2h6-g786 (CWE-93): a "\r\n" embedded
// in a bulk string's own byte payload must not be mistaken for a RESP
// line boundary.
'GET with CRLF embedded in the value' => [
GET::class,
["value\r\n*1\r\n\$4\r\nEVIL"],
],
'ZADD with CRLF embedded in the key' => [
ZADD::class,
["key\r\n*1\r\n\$4\r\nEVIL", 'value', 'key1', 'value1'],
],
];
}
}
@@ -14,6 +14,7 @@ namespace Predis\Connection\Cluster;
use Predis\Command\CommandInterface;
use Predis\Command\Redis\GET;
use Predis\Command\Redis\SET;
use Predis\Connection\Parameters;
use PredisTestCase;
@@ -516,4 +517,73 @@ class PredisClusterTest extends PredisTestCase
$cluster->write($command1->serializeCommand() . $command2->serializeCommand() . $command3->serializeCommand());
}
/**
* Regression guard for GHSA-w6f5-v2h6-g786 (CWE-93): a CRLF embedded in a
* bulk string's own value must not be mistaken for a command boundary
* (splitting one command into a smuggled extra command), and must not
* corrupt the argument list used to pick the target node.
*
* @group disconnected
*/
public function testWriteHandlesCRLFEmbeddedInBulkStringValue(): void
{
$command = new SET();
$command->setArguments(['victim-key', "PAD\r\n*1\r\n\$7\r\nFLUSHDB"]);
$connection1 = $this->getMockConnection('tcp://127.0.0.1:7001');
$connection2 = $this->getMockConnection('tcp://127.0.0.1:7002');
$cluster = new PredisCluster(new Parameters());
$cluster->add($connection1);
$cluster->add($connection2);
$expectedConnection = $cluster->getConnectionByCommand($command);
$otherConnection = $expectedConnection === $connection1 ? $connection2 : $connection1;
$expectedConnection
->expects($this->once())
->method('write')
->with($command->serializeCommand());
$otherConnection
->expects($this->never())
->method('write');
$cluster->write($command->serializeCommand());
}
/**
* Regression guard for GHSA-w6f5-v2h6-g786 (CWE-93): a CRLF embedded in a
* bulk string KEY must not corrupt the argument list used to pick the
* target node, which would silently route the command to the wrong node.
*
* @group disconnected
*/
public function testWriteHandlesCRLFEmbeddedInBulkStringKey(): void
{
$command = new SET();
$command->setArguments(["victim\r\n*1\r\n\$4\r\nEVIL", 'somevalue']);
$connection1 = $this->getMockConnection('tcp://127.0.0.1:7001');
$connection2 = $this->getMockConnection('tcp://127.0.0.1:7002');
$cluster = new PredisCluster(new Parameters());
$cluster->add($connection1);
$cluster->add($connection2);
$expectedConnection = $cluster->getConnectionByCommand($command);
$otherConnection = $expectedConnection === $connection1 ? $connection2 : $connection1;
$expectedConnection
->expects($this->once())
->method('write')
->with($command->serializeCommand());
$otherConnection
->expects($this->never())
->method('write');
$cluster->write($command->serializeCommand());
}
}
@@ -1676,4 +1676,77 @@ class RedisClusterTest extends PredisTestCase
$cluster->write($command1->serializeCommand() . $command2->serializeCommand() . $command3->serializeCommand());
}
/**
* Regression guard for GHSA-w6f5-v2h6-g786 (CWE-93): a CRLF embedded in a
* bulk string's own value must not be mistaken for a command boundary
* (splitting one command into a smuggled extra command), and must not
* corrupt the argument list used to pick the target node.
*
* @group disconnected
*/
public function testWriteHandlesCRLFEmbeddedInBulkStringValue(): void
{
$command = new Command\Redis\SET();
$command->setArguments(['victim-key', "PAD\r\n*1\r\n\$7\r\nFLUSHDB"]);
$factory = $this->getMockBuilder(FactoryInterface::class)->getMock();
$connection1 = $this->getMockConnection('tcp://127.0.0.1:7001');
$connection2 = $this->getMockConnection('tcp://127.0.0.1:7002');
$connection3 = $this->getMockConnection('tcp://127.0.0.1:7003');
$cluster = new RedisCluster($factory, new Parameters());
$cluster->add($connection1);
$cluster->add($connection2);
$cluster->add($connection3);
$expectedConnection = $cluster->getConnectionByCommand($command);
foreach ([$connection1, $connection2, $connection3] as $connection) {
if ($connection === $expectedConnection) {
$connection->expects($this->once())->method('write')->with($command->serializeCommand());
} else {
$connection->expects($this->never())->method('write');
}
}
$cluster->write($command->serializeCommand());
}
/**
* Regression guard for GHSA-w6f5-v2h6-g786 (CWE-93): a CRLF embedded in a
* bulk string KEY must not corrupt the argument list used to pick the
* target node, which would silently route the command to the wrong node.
*
* @group disconnected
*/
public function testWriteHandlesCRLFEmbeddedInBulkStringKey(): void
{
$command = new Command\Redis\SET();
$command->setArguments(["victim\r\n*1\r\n\$4\r\nEVIL", 'somevalue']);
$factory = $this->getMockBuilder(FactoryInterface::class)->getMock();
$connection1 = $this->getMockConnection('tcp://127.0.0.1:7001');
$connection2 = $this->getMockConnection('tcp://127.0.0.1:7002');
$connection3 = $this->getMockConnection('tcp://127.0.0.1:7003');
$cluster = new RedisCluster($factory, new Parameters());
$cluster->add($connection1);
$cluster->add($connection2);
$cluster->add($connection3);
$expectedConnection = $cluster->getConnectionByCommand($command);
foreach ([$connection1, $connection2, $connection3] as $connection) {
if ($connection === $expectedConnection) {
$connection->expects($this->once())->method('write')->with($command->serializeCommand());
} else {
$connection->expects($this->never())->method('write');
}
}
$cluster->write($command->serializeCommand());
}
}
+18
View File
@@ -585,6 +585,24 @@ class FactoryTest extends PredisTestCase
$this->assertSame(['SETINFO', 'LIB-VER', Client::VERSION], $initCommands[2]->getArguments());
}
/**
* @group disconnected
* @return void
*/
public function testDoesNotSetClientNameAndVersionOnConnectionWithClientInfoDisabled(): void
{
$parameters = ['client_info' => false];
$factory = new Factory();
$connection = $factory->create($parameters);
$initCommands = $connection->getInitCommands();
$this->assertCount(1, $initCommands);
$this->assertInstanceOf(RawCommand::class, $initCommands[0]);
$this->assertSame('HELLO', $initCommands[0]->getId());
$this->assertSame([2, 'SETNAME', 'predis'], $initCommands[0]->getArguments());
}
/**
* @group disconnected
*/
@@ -1496,6 +1496,69 @@ repl_backlog_histlen:12978
$replication->write($command1->serializeCommand() . $command2->serializeCommand() . $command3->serializeCommand());
}
/**
* Regression guard for GHSA-w6f5-v2h6-g786 (CWE-93): a CRLF embedded in a
* bulk string's own value must not be mistaken for a command boundary
* (splitting one command into a smuggled extra command).
*
* @group disconnected
*/
public function testWriteHandlesCRLFEmbeddedInBulkStringValue(): void
{
$command = new Command\Redis\SET();
$command->setArguments(['victim-key', "PAD\r\n*1\r\n\$7\r\nFLUSHDB"]);
$master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
$slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
$slave1
->expects($this->never())
->method('write');
$master
->expects($this->once())
->method('write')
->with($command->serializeCommand());
$replication = new MasterSlaveReplication();
$replication->add($master);
$replication->add($slave1);
$replication->write($command->serializeCommand());
}
/**
* Regression guard for GHSA-w6f5-v2h6-g786 (CWE-93): a CRLF embedded in a
* bulk string KEY must not corrupt the argument list used to pick the
* target connection, which would silently route the command to the
* wrong node.
*
* @group disconnected
*/
public function testWriteHandlesCRLFEmbeddedInBulkStringKey(): void
{
$command = new Command\Redis\SET();
$command->setArguments(["victim\r\n*1\r\n\$4\r\nEVIL", 'somevalue']);
$master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
$slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
$slave1
->expects($this->never())
->method('write');
$master
->expects($this->once())
->method('write')
->with($command->serializeCommand());
$replication = new MasterSlaveReplication();
$replication->add($master);
$replication->add($slave1);
$replication->write($command->serializeCommand());
}
/**
* @medium
* @group disconnected
@@ -2020,6 +2020,85 @@ class SentinelReplicationTest extends PredisTestCase
$replication->write($command1->serializeCommand() . $command2->serializeCommand() . $command3->serializeCommand());
}
/**
* Regression guard for GHSA-w6f5-v2h6-g786 (CWE-93): a CRLF embedded in a
* bulk string's own value must not be mistaken for a command boundary
* (splitting one command into a smuggled extra command).
*
* @group disconnected
*/
public function testWriteHandlesCRLFEmbeddedInBulkStringValue(): void
{
$command = new Command\Redis\SET();
$command->setArguments(['victim-key', "PAD\r\n*1\r\n\$7\r\nFLUSHDB"]);
$sentinel = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?role=sentinel');
$master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
$slave = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
$strategy = new Replication\ReplicationStrategy();
$factory = new Connection\Factory();
$master
->expects($this->once())
->method('isConnected')
->willReturn(true);
$slave
->expects($this->never())
->method('write');
$master
->expects($this->once())
->method('write')
->with($command->serializeCommand());
$replication = new SentinelReplication('svc', [$sentinel], $factory, $strategy);
$replication->add($master);
$replication->add($slave);
$replication->write($command->serializeCommand());
}
/**
* Regression guard for GHSA-w6f5-v2h6-g786 (CWE-93): a CRLF embedded in a
* bulk string KEY must not corrupt the argument list used to pick the
* target connection, which would silently route the command to the
* wrong node.
*
* @group disconnected
*/
public function testWriteHandlesCRLFEmbeddedInBulkStringKey(): void
{
$command = new Command\Redis\SET();
$command->setArguments(["victim\r\n*1\r\n\$4\r\nEVIL", 'somevalue']);
$sentinel = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?role=sentinel');
$master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
$slave = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
$strategy = new Replication\ReplicationStrategy();
$factory = new Connection\Factory();
$master
->expects($this->once())
->method('isConnected')
->willReturn(true);
$slave
->expects($this->never())
->method('write');
$master
->expects($this->once())
->method('write')
->with($command->serializeCommand());
$replication = new SentinelReplication('svc', [$sentinel], $factory, $strategy);
$replication->add($master);
$replication->add($slave);
$replication->write($command->serializeCommand());
}
public function connectionsProvider(): array
{
return [
@@ -12,6 +12,7 @@
namespace Predis\Connection\Resource;
use ErrorException;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use RuntimeException;
@@ -431,6 +432,99 @@ class StreamTest extends TestCase
$stream->write('');
}
/**
* @return void
*/
public function testWriteSuppressesEngineWarningUnderThrowingErrorHandler(): void
{
$this->registerEngineWarningWrapper();
$this->installThrowingErrorHandler();
$stream = new Stream(fopen('predis-test-engine-warning://x', 'r+'));
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Unable to write to stream');
$stream->write('data');
}
/**
* @return void
*/
public function testReadSuppressesEngineWarningUnderThrowingErrorHandler(): void
{
$this->registerEngineWarningWrapper();
$this->installThrowingErrorHandler();
$stream = new Stream(fopen('predis-test-engine-warning://x', 'r+'));
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Unable to read from stream');
// Use the fgets() path (length = -1): PHP 7.2's fread() coerces a
// user stream wrapper's `false` return into an empty string instead
// of preserving it, which would make this assertion PHP-version
// dependent; fgets() doesn't have that quirk.
$stream->read(-1);
}
/**
* Mimics error handlers installed by Laravel, Symfony and Laminas, which
* convert engine notices/warnings into thrown exceptions unless the call
* site suppressed them with `@` (see GH-1725). The runner's own ambient
* error_reporting() level is irrelevant to what we want to assert here,
* so it's pinned to a known, fully-enabled value for the duration of the
* test rather than trusted as-is.
*
* @return void
*/
private function installThrowingErrorHandler(): void
{
$this->originalErrorReporting = error_reporting(E_ALL);
set_error_handler(static function ($level, $message, $file = '', $line = 0) {
if (error_reporting() & $level) {
throw new ErrorException($message, 0, $level, $file, $line);
}
return false;
});
$this->registeredErrorHandler = true;
}
/**
* @var bool
*/
private $registeredErrorHandler = false;
/**
* @var int|null
*/
private $originalErrorReporting;
/**
* @return void
*/
protected function tearDown(): void
{
if ($this->registeredErrorHandler) {
restore_error_handler();
error_reporting($this->originalErrorReporting);
$this->registeredErrorHandler = false;
}
}
/**
* @return void
*/
private function registerEngineWarningWrapper(): void
{
if (!in_array('predis-test-engine-warning', stream_get_wrappers(), true)) {
stream_wrapper_register('predis-test-engine-warning', EngineWarningStreamWrapperFixture::class);
}
}
public function writableModeProvider(): array
{
return [
@@ -478,3 +572,56 @@ class StreamTest extends TestCase
];
}
}
/**
* Stream wrapper fixture that raises an engine-style warning from
* stream_write()/stream_read(), used to verify that Stream::write()/read()
* suppress it instead of letting a host-installed error handler turn it
* into an uncaught exception (see GH-1725).
*/
class EngineWarningStreamWrapperFixture
{
/**
* @var resource
*/
public $context;
public function stream_open($path, $mode, $options, &$openedPath): bool
{
return true;
}
/**
* @return int|bool
*/
public function stream_write(string $data)
{
trigger_error('fwrite(): synthetic broken pipe', E_USER_WARNING);
return false;
}
/**
* @return string|bool
*/
public function stream_read(int $count)
{
trigger_error('fread(): synthetic broken pipe', E_USER_WARNING);
return false;
}
public function stream_eof(): bool
{
return false;
}
public function stream_stat()
{
return [];
}
public function stream_close(): void
{
}
}
@@ -60,11 +60,11 @@ class Resp3StrategyTest extends PredisTestCase
* @param string $data
* @return void
*/
public function testParseDataReturnsFloatInfinityOnInfinityOrNegativeInfinity(string $data): void
public function testParseDataReturnsFloatInfinityOnInfinityOrNegativeInfinity(string $data, float $expectedValue): void
{
$actualResponse = $this->strategy->parseData($data);
$this->assertInfinite($actualResponse);
$this->assertSame($expectedValue, $actualResponse);
}
/**
@@ -166,8 +166,8 @@ class Resp3StrategyTest extends PredisTestCase
public function infinityProvider(): array
{
return [
'positive infinity' => [",inf\r\n"],
'negative infinity' => [",-inf\r\n"],
'positive infinity' => [",inf\r\n", INF],
'negative infinity' => [",-inf\r\n", -INF],
];
}