Skip to content

Commit

Permalink
[Security] AbstractRememberMeServices::encodeCookie() validates cooki…
Browse files Browse the repository at this point in the history
…e parts
  • Loading branch information
MacDada authored and fabpot committed May 21, 2015
1 parent 4d40852 commit 464c39a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
Expand Up @@ -268,9 +268,17 @@ protected function decodeCookie($rawCookie)
* @param array $cookieParts
*
* @return string
*
* @throws \InvalidArgumentException When $cookieParts contain the cookie delimiter. Extending class should either remove or escape it.
*/
protected function encodeCookie(array $cookieParts)
{
foreach ($cookieParts as $cookiePart) {
if (false !== strpos($cookiePart, self::COOKIE_DELIMITER)) {
throw new \InvalidArgumentException(sprintf('$cookieParts should not contain the cookie delimiter "%s"', self::COOKIE_DELIMITER));
}
}

return base64_encode(implode(self::COOKIE_DELIMITER, $cookieParts));
}

Expand Down
Expand Up @@ -119,8 +119,6 @@ protected function onLoginSuccess(Request $request, Response $response, TokenInt
* @param int $expires The Unix timestamp when the cookie expires
* @param string $password The encoded password
*
* @throws \RuntimeException if username contains invalid chars
*
* @return string
*/
protected function generateCookieValue($class, $username, $expires, $password)
Expand All @@ -141,8 +139,6 @@ protected function generateCookieValue($class, $username, $expires, $password)
* @param int $expires The Unix timestamp when the cookie expires
* @param string $password The encoded password
*
* @throws \RuntimeException when the private key is empty
*
* @return string
*/
protected function generateCookieHash($class, $username, $expires, $password)
Expand Down
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Http\RememberMe\AbstractRememberMeServices;

class AbstractRememberMeServicesTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -236,6 +237,30 @@ public function getPositiveRememberMeParameterValues()
);
}

public function testEncodeCookieAndDecodeCookieAreInvertible()
{
$cookieParts = array('aa', 'bb', 'cc');
$service = $this->getService();

$encoded = $this->callProtected($service, 'encodeCookie', array($cookieParts));
$this->assertInternalType('string', $encoded);

$decoded = $this->callProtected($service, 'decodeCookie', array($encoded));
$this->assertSame($cookieParts, $decoded);
}

/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage cookie delimiter
*/
public function testThereShouldBeNoCookieDelimiterInCookieParts()
{
$cookieParts = array('aa', 'b'.AbstractRememberMeServices::COOKIE_DELIMITER.'b', 'cc');
$service = $this->getService();

$this->callProtected($service, 'encodeCookie', array($cookieParts));
}

protected function getService($userProvider = null, $options = array(), $logger = null)
{
if (null === $userProvider) {
Expand All @@ -258,4 +283,13 @@ protected function getProvider()

return $provider;
}

private function callProtected($object, $method, array $args)
{
$reflection = new \ReflectionClass(get_class($object));
$reflectionMethod = $reflection->getMethod($method);
$reflectionMethod->setAccessible(true);

return $reflectionMethod->invokeArgs($object, $args);
}
}

0 comments on commit 464c39a

Please sign in to comment.