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

URL-encodes the userInfo component of an URI #253

Merged
merged 4 commits into from Dec 28, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 27 additions & 5 deletions src/Uri.php
Expand Up @@ -437,9 +437,9 @@ public function withScheme($scheme)

public function withUserInfo($user, $password = null)
{
$info = $user;
if ($password != '') {
$info .= ':' . $password;
$info = $this->filterUserInfoComponent($user);
if ($password !== null) {
$info .= ':' . $this->filterUserInfoComponent($password);
}

if ($this->userInfo === $info) {
Expand Down Expand Up @@ -537,7 +537,9 @@ private function applyParts(array $parts)
$this->scheme = isset($parts['scheme'])
? $this->filterScheme($parts['scheme'])
: '';
$this->userInfo = isset($parts['user']) ? $parts['user'] : '';
$this->userInfo = isset($parts['user'])
? $this->filterUserInfoComponent($parts['user'])
: '';
$this->host = isset($parts['host'])
? $this->filterHost($parts['host'])
: '';
Expand All @@ -554,7 +556,7 @@ private function applyParts(array $parts)
? $this->filterQueryAndFragment($parts['fragment'])
: '';
if (isset($parts['pass'])) {
$this->userInfo .= ':' . $parts['pass'];
$this->userInfo .= ':' . $this->filterUserInfoComponent($parts['pass']);
}

$this->removeDefaultPort();
Expand All @@ -576,6 +578,26 @@ private function filterScheme($scheme)
return strtolower($scheme);
}

/**
* @param string $component
*
* @return string
*
* @throws \InvalidArgumentException If the user info is invalid.
*/
private function filterUserInfoComponent($component)
cedx marked this conversation as resolved.
Show resolved Hide resolved
{
if (!is_string($component)) {
throw new \InvalidArgumentException('User info must be a string');
}

return preg_replace_callback(
'/(?:[^%' . self::$charUnreserved . self::$charSubDelims . ']+|%(?![A-Fa-f0-9]{2}))/',
[$this, 'rawurlencodeMatchZero'],
$component
);
}

/**
* @param string $host
*
Expand Down
11 changes: 11 additions & 0 deletions tests/UriTest.php
Expand Up @@ -680,6 +680,17 @@ public function testExtendingClassesInstantiates()
new ExtendedUriTest('http://h:9/')
);
}

public function testSpecialCharsOfUserInfo()
{
// The `userInfo` must always be URL-encoded.
$uri = (new Uri)->withUserInfo('foo@bar.com', 'pass#word');
$this->assertSame('foo%40bar.com:pass%23word', $uri->getUserInfo());

// The `userInfo` can already be URL-encoded: it should not be encoded twice.
$uri = (new Uri)->withUserInfo('foo%40bar.com', 'pass%23word');
$this->assertSame('foo%40bar.com:pass%23word', $uri->getUserInfo());
}
}

class ExtendedUriTest extends Uri
Expand Down