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

Fix redis:// being rejected and username:password passing invalid auth to Redis #42

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
19 changes: 9 additions & 10 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ private function parseUrl($target)
}

$parts = parse_url($target);
if ($parts === false || !isset($parts['host']) || $parts['scheme'] !== 'tcp') {
$validSchemes = array('redis', 'tcp');
if ($parts === false || !isset($parts['host']) || !in_array($parts['scheme'], $validSchemes)) {
throw new InvalidArgumentException('Given URL can not be parsed');
}

Expand All @@ -110,16 +111,14 @@ private function parseUrl($target)
$parts['host'] = '127.0.0.1';
}

$auth = null;
if (isset($parts['user'])) {
$auth = $parts['user'];
}
// username:password@ (Redis doesn't support usernames)
if (isset($parts['pass'])) {
$auth .= ':' . $parts['pass'];
}
if ($auth !== null) {
$parts['auth'] = $auth;
}
$parts['auth'] = $parts['pass'];
}
// password@
else if (isset($parts['user'])) {
$parts['auth'] = $parts['user'];
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you fix the coding style here? E.g. } else if (...) { and fix the indentation.


if (isset($parts['path']) && $parts['path'] !== '') {
// skip first slash
Expand Down
11 changes: 10 additions & 1 deletion tests/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,20 @@ public function testWillWriteSelectCommandIfTargetContainsPath()
public function testWillWriteAuthCommandIfTargetContainsUserInfo()
{
$stream = $this->getMockBuilder('React\Stream\Stream')->disableOriginalConstructor()->getMock();
$stream->expects($this->once())->method('write')->with("*2\r\n$4\r\nauth\r\n$11\r\nhello:world\r\n");
$stream->expects($this->once())->method('write')->with("*2\r\n$4\r\nauth\r\n$5\r\nworld\r\n");

$this->connector->expects($this->once())->method('create')->willReturn(Promise\resolve($stream));
$this->factory->createClient('tcp://hello:[email protected]');
}

public function testWillWriteAuthCommandIfTargetContainsPasswordAsUser()
{
$stream = $this->getMockBuilder('React\Stream\Stream')->disableOriginalConstructor()->getMock();
$stream->expects($this->once())->method('write')->with("*2\r\n$4\r\nauth\r\n$5\r\nworld\r\n");

$this->connector->expects($this->once())->method('create')->willReturn(Promise\resolve($stream));
$this->factory->createClient('tcp://[email protected]');
}

public function testWillRejectIfConnectorRejects()
{
Expand Down