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 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
16 changes: 7 additions & 9 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,15 +111,12 @@ private function parseUrl($target)
$parts['host'] = '127.0.0.1';
}

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

Choose a reason for hiding this comment

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

If we want to properly parse the URI, I suppose it makes sense to only support the "pass" component and ignore the "user" component entirely, so this block should probably be removed? 👍

We should also make sure to update the documentation and/or tests accordingly 👍

Copy link
Author

Choose a reason for hiding this comment

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

The reason it checks for user as the auth is that the parser is parsing the pass in pass@host as the user. I added a test for both cases and it would be a backwards incompatible change to remove support for pass@host. It doesn't seem like it adds anything to remove support for that.

}

if (isset($parts['path']) && $parts['path'] !== '') {
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