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

[5.5] Attempt to fix AuthenticateSession/remember_me issue #19843

Merged
merged 2 commits into from
Jul 6, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/Illuminate/Auth/Recaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct($recaller)
*/
public function id()
{
return explode('|', $this->recaller, 2)[0];
return explode('|', $this->recaller, 3)[0];
}

/**
Expand All @@ -41,7 +41,17 @@ public function id()
*/
public function token()
{
return explode('|', $this->recaller, 2)[1];
return explode('|', $this->recaller, 3)[1];
}

/**
* Get the password from the recaller.
*
* @return string
*/
public function hash()
{
return explode('|', $this->recaller, 3)[2];
}

/**
Expand All @@ -51,7 +61,7 @@ public function token()
*/
public function valid()
{
return $this->properString() && $this->hasBothSegments();
return $this->properString() && $this->hasAllSegments();
}

/**
Expand All @@ -65,14 +75,14 @@ protected function properString()
}

/**
* Determine if the recaller has both segments.
* Determine if the recaller has all segments.
*
* @return bool
*/
protected function hasBothSegments()
protected function hasAllSegments()
{
$segments = explode('|', $this->recaller);

return count($segments) == 2 && trim($segments[0]) !== '' && trim($segments[1]) !== '';
return count($segments) == 3 && trim($segments[0]) !== '' && trim($segments[1]) !== '';
}
}
2 changes: 1 addition & 1 deletion src/Illuminate/Auth/SessionGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ protected function ensureRememberTokenIsSet(AuthenticatableContract $user)
protected function queueRecallerCookie(AuthenticatableContract $user)
{
$this->getCookieJar()->queue($this->createRecaller(
$user->getAuthIdentifier().'|'.$user->getRememberToken()
$user->getAuthIdentifier().'|'.$user->getRememberToken().'|'.$user->getAuthPassword()
Copy link
Contributor

Choose a reason for hiding this comment

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

Really sending password here? :/

Choose a reason for hiding this comment

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

I'm also having trouble understanding how this can be safe, please advise.

Copy link
Member Author

Choose a reason for hiding this comment

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

It's not the password, it's just the hash.

Choose a reason for hiding this comment

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

Thanks. In that case, this was what confused me in Authenticatable.php:

public function getAuthPassword()
{
    return $this->password;
}

));
}

Expand Down
8 changes: 6 additions & 2 deletions src/Illuminate/Session/Middleware/AuthenticateSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,12 @@ public function handle($request, Closure $next)
return $next($request);
}

if (! $request->session()->has('password_hash') && $this->auth->viaRemember()) {
$this->logout($request);
if ($this->auth->viaRemember()) {
$passwordHash = explode('|', $request->cookies->get($this->auth->getRecallerName()))[2];

if ($passwordHash != $request->user()->getAuthPassword()) {
$this->logout($request);
}
}

if (! $request->session()->has('password_hash')) {
Expand Down
6 changes: 4 additions & 2 deletions tests/Auth/AuthGuardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,12 +279,13 @@ public function testLoginMethodQueuesCookieWhenRemembering()
$guard = new \Illuminate\Auth\SessionGuard('default', $provider, $session, $request);
$guard->setCookieJar($cookie);
$foreverCookie = new \Symfony\Component\HttpFoundation\Cookie($guard->getRecallerName(), 'foo');
$cookie->shouldReceive('forever')->once()->with($guard->getRecallerName(), 'foo|recaller')->andReturn($foreverCookie);
$cookie->shouldReceive('forever')->once()->with($guard->getRecallerName(), 'foo|recaller|bar')->andReturn($foreverCookie);
$cookie->shouldReceive('queue')->once()->with($foreverCookie);
$guard->getSession()->shouldReceive('put')->once()->with($guard->getName(), 'foo');
$session->shouldReceive('migrate')->once();
$user = m::mock('Illuminate\Contracts\Auth\Authenticatable');
$user->shouldReceive('getAuthIdentifier')->andReturn('foo');
$user->shouldReceive('getAuthPassword')->andReturn('bar');
$user->shouldReceive('getRememberToken')->andReturn('recaller');
$user->shouldReceive('setRememberToken')->never();
$provider->shouldReceive('updateRememberToken')->never();
Expand All @@ -303,6 +304,7 @@ public function testLoginMethodCreatesRememberTokenIfOneDoesntExist()
$session->shouldReceive('migrate')->once();
$user = m::mock('Illuminate\Contracts\Auth\Authenticatable');
$user->shouldReceive('getAuthIdentifier')->andReturn('foo');
$user->shouldReceive('getAuthPassword')->andReturn('foo');
$user->shouldReceive('getRememberToken')->andReturn(null);
$user->shouldReceive('setRememberToken')->once();
$provider->shouldReceive('updateRememberToken')->once();
Expand Down Expand Up @@ -360,7 +362,7 @@ public function testUserUsesRememberCookieIfItExists()
{
$guard = $this->getGuard();
list($session, $provider, $request, $cookie) = $this->getMocks();
$request = \Symfony\Component\HttpFoundation\Request::create('/', 'GET', [], [$guard->getRecallerName() => 'id|recaller']);
$request = \Symfony\Component\HttpFoundation\Request::create('/', 'GET', [], [$guard->getRecallerName() => 'id|recaller|baz']);
$guard = new \Illuminate\Auth\SessionGuard('default', $provider, $session, $request);
$guard->getSession()->shouldReceive('get')->once()->with($guard->getName())->andReturn(null);
$user = m::mock('Illuminate\Contracts\Auth\Authenticatable');
Expand Down