Skip to content

Commit

Permalink
Update DSN to support special chars for user and password
Browse files Browse the repository at this point in the history
Previously, DSN would not be able to parse special characters like "@"
or "/". This adds support by requiring the input to be url encoded and
then DSN would decode it after parsing.
  • Loading branch information
stnguyen90 committed Jul 31, 2023
1 parent 61c9b8f commit a820cd7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/DSN/DSN.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public function __construct(string $dsn)
}

$this->scheme = $parts['scheme'] ?? null;
$this->user = $parts['user'] ?? null;
$this->password = $parts['pass'] ?? null;
$this->user = isset($parts['user']) ? \urldecode($parts['user']) : null;
$this->password = isset($parts['pass']) ? \urldecode($parts['pass']) : null;
$this->host = $parts['host'] ?? null;
$this->port = $parts['port'] ?? null;
$this->path = $parts['path'] ?? null;
Expand Down Expand Up @@ -129,7 +129,7 @@ public function getPath(): ?string
}

/**
* Return the query string
* Return the raw query string
*
* @return ?string
*/
Expand Down
33 changes: 33 additions & 0 deletions tests/DSN/DSNTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,39 @@ public function testSuccess(): void
$this->assertEquals('region=us-east-1', $dsn->getQuery());
$this->assertEquals('us-east-1', $dsn->getParam('region'));
$this->assertEmpty($dsn->getParam('doesNotExist'));

$password = 'sl/sh+$@no:her';
$encoded = \urlencode($password);
$dsn = new DSN("sms://user:$encoded@localhost");
$this->assertEquals('sms', $dsn->getScheme());
$this->assertEquals('user', $dsn->getUser());
$this->assertEquals($password, $dsn->getPassword());
$this->assertEquals('localhost', $dsn->getHost());
$this->assertNull($dsn->getPort());
$this->assertEmpty($dsn->getPath());
$this->assertNull($dsn->getQuery());

$user = 'admin@example.com';
$encoded = \urlencode($user);
$dsn = new DSN("sms://$encoded@localhost");
$this->assertEquals('sms', $dsn->getScheme());
$this->assertEquals($user, $dsn->getUser());
$this->assertNull($dsn->getPassword());
$this->assertEquals('localhost', $dsn->getHost());
$this->assertNull($dsn->getPort());
$this->assertEmpty($dsn->getPath());
$this->assertNull($dsn->getQuery());

$value = 'I am 100% value=<complex>, "right"?!';
$encoded = \urlencode($value);
$dsn = new DSN("sms://localhost?value=$encoded");
$this->assertEquals('sms', $dsn->getScheme());
$this->assertNull($dsn->getUser());
$this->assertNull($dsn->getPassword());
$this->assertEquals('localhost', $dsn->getHost());
$this->assertNull($dsn->getPort());
$this->assertEmpty($dsn->getPath());
$this->assertEquals("value=$encoded", $dsn->getQuery());
}

public function testGetParam(): void
Expand Down

0 comments on commit a820cd7

Please sign in to comment.