Skip to content

Commit 955069f

Browse files
authored
Merge pull request #101 from clue-labs/urldecode
Fix decoding special characters from database connection URI
2 parents db9466b + fc33803 commit 955069f

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

src/Factory.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,9 @@ public function createConnection($uri)
170170

171171
$connection = new Connection($stream, $executor);
172172
$command = $executor->enqueue(new AuthenticateCommand(
173-
isset($parts['user']) ? $parts['user'] : 'root',
174-
isset($parts['pass']) ? $parts['pass'] : '',
175-
isset($parts['path']) ? ltrim($parts['path'], '/') : ''
173+
isset($parts['user']) ? rawurldecode($parts['user']) : 'root',
174+
isset($parts['pass']) ? rawurldecode($parts['pass']) : '',
175+
isset($parts['path']) ? rawurldecode(ltrim($parts['path'], '/')) : ''
176176
));
177177
$parser->start();
178178

tests/FactoryTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,40 @@ public function testConnectWillUseGivenHostAndGivenPort()
3131
$factory->createConnection('127.0.0.1:1234');
3232
}
3333

34+
public function testConnectWillUseGivenUserInfoAsDatabaseCredentialsAfterUrldecoding()
35+
{
36+
$connection = $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->setMethods(array('write'))->getMock();
37+
$connection->expects($this->once())->method('write')->with($this->stringContains("user!\0"));
38+
39+
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
40+
$connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
41+
$connector->expects($this->once())->method('connect')->with('127.0.0.1:3306')->willReturn(\React\Promise\resolve($connection));
42+
43+
$factory = new Factory($loop, $connector);
44+
$promise = $factory->createConnection('user%[email protected]');
45+
46+
$promise->then($this->expectCallableNever(), $this->expectCallableNever());
47+
48+
$connection->emit('data', array("\x33\0\0\0" . "\x0a" . "mysql\0" . str_repeat("\0", 44)));
49+
}
50+
51+
public function testConnectWillUseGivenPathAsDatabaseNameAfterUrldecoding()
52+
{
53+
$connection = $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->setMethods(array('write'))->getMock();
54+
$connection->expects($this->once())->method('write')->with($this->stringContains("test database\0"));
55+
56+
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
57+
$connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
58+
$connector->expects($this->once())->method('connect')->with('127.0.0.1:3306')->willReturn(\React\Promise\resolve($connection));
59+
60+
$factory = new Factory($loop, $connector);
61+
$promise = $factory->createConnection('127.0.0.1/test%20database');
62+
63+
$promise->then($this->expectCallableNever(), $this->expectCallableNever());
64+
65+
$connection->emit('data', array("\x33\0\0\0" . "\x0a" . "mysql\0" . str_repeat("\0", 44)));
66+
}
67+
3468
public function testConnectWithInvalidUriWillRejectWithoutConnecting()
3569
{
3670
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();

0 commit comments

Comments
 (0)