Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added additional checking if response length is beyond a reasonable limit (10mb) #190

Merged
merged 1 commit into from
Nov 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/Connector/Exception/FailedToSendCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,30 @@ public static function fromEmptyResponseSize(Command $attemptedCommand, Connecti
));
}

public static function fromFailedResponseUnpack(Command $attemptedCommand, ConnectionAddress $connectionAddress) : self
{
return new self(sprintf(
'Response length could not be unpacked for %s (maybe invalid format?). Address was: %s',
get_class($attemptedCommand),
$connectionAddress->toString()
));
}

public static function fromTooLargeResponseLength(
int $responseLengthReturned,
int $responseLengthLimit,
Command $attemptedCommand,
ConnectionAddress $connectionAddress
) : self {
return new self(sprintf(
'Response length returned (%d) exceeded our limit for reading (%d) for %s. Address was: %s',
$responseLengthReturned,
$responseLengthLimit,
get_class($attemptedCommand),
$connectionAddress->toString()
));
}

/** @param resource $socketResource */
public static function readingResponseContentFromSocket(Command $attemptedCommand, $socketResource, ConnectionAddress $connectionAddress) : self
{
Expand Down
29 changes: 25 additions & 4 deletions src/Connector/SocketConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use const E_STRICT;
use const E_WARNING;
use const SOCK_STREAM;
use function array_key_exists;
use function is_array;
use function json_encode;
use function pack;
use function register_shutdown_function;
Expand All @@ -32,6 +34,8 @@
/** @internal */
final class SocketConnector implements Connector
{
private const MAXIMUM_RESPONSE_LENGTH_TO_READ = 10000000;

/** @var resource */
private $socket;

Expand Down Expand Up @@ -139,17 +143,34 @@ public function sendCommand(Command $message) : string
}

// Read the response back and drop it. Needed for socket liveness
$responseLength = @socket_read($this->socket, 4);
$responseLengthPacked = @socket_read($this->socket, 4);

if ($responseLength === false) {
if ($responseLengthPacked === false) {
throw Exception\FailedToSendCommand::readingResponseSizeFromSocket($message, $this->socket, $this->connectionAddress);
}

if ($responseLength === '') {
if ($responseLengthPacked === '') {
throw Exception\FailedToSendCommand::fromEmptyResponseSize($message, $this->connectionAddress);
}

$dataRead = @socket_read($this->socket, unpack('N', $responseLength)[1]);
$responseLengthUnpacked = unpack('Nlen', $responseLengthPacked);
asgrim marked this conversation as resolved.
Show resolved Hide resolved

if (! is_array($responseLengthUnpacked) || ! array_key_exists('len', $responseLengthUnpacked)) {
throw Exception\FailedToSendCommand::fromFailedResponseUnpack($message, $this->connectionAddress);
}

$responseLength = (int) $responseLengthUnpacked['len'];

if ($responseLength > self::MAXIMUM_RESPONSE_LENGTH_TO_READ) {
throw Exception\FailedToSendCommand::fromTooLargeResponseLength(
$responseLength,
self::MAXIMUM_RESPONSE_LENGTH_TO_READ,
$message,
$this->connectionAddress
);
}

$dataRead = @socket_read($this->socket, $responseLength);

if ($dataRead === false) {
throw Exception\FailedToSendCommand::readingResponseContentFromSocket($message, $this->socket, $this->connectionAddress);
Expand Down