Skip to content
This repository has been archived by the owner on Dec 2, 2021. It is now read-only.

Commit

Permalink
Merge pull request #57 from zerkms/COOKIE_LOCALHOST_EXCEPTION
Browse files Browse the repository at this point in the history
Added exception for `localhost` domain
  • Loading branch information
scheb authored Jul 26, 2016
2 parents b72ba5c + 3fc9738 commit 514d431
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
8 changes: 7 additions & 1 deletion Security/TwoFactor/Trusted/TrustedCookieManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,14 @@ public function createTrustedCookie(Request $request, $user)
// Add token to user entity
$this->trustedComputerManager->addTrustedComputer($user, $token, $validUntil);

$domain = null;
$requestHost = $request->getHost();
if ($requestHost !== 'localhost') {
$domain = '.' . $requestHost;
}

// Create cookie
return new Cookie($this->cookieName, $tokenList, $validUntil, '/', '.' . $request->getHost(), $this->cookieSecure);
return new Cookie($this->cookieName, $tokenList, $validUntil, '/', $domain, $this->cookieSecure);
}

/**
Expand Down
14 changes: 14 additions & 0 deletions Tests/Security/TwoFactor/Trusted/TrustedCookieManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Scheb\TwoFactorBundle\Security\TwoFactor\Trusted\TrustedCookieManager;
use Symfony\Component\HttpFoundation\Cookie;
use Scheb\TwoFactorBundle\Tests\TestCase;
use Symfony\Component\HttpFoundation\Request;

class TrustedCookieManagerTest extends TestCase
{
Expand Down Expand Up @@ -186,6 +187,19 @@ public function createTrustedCookie_newTrustedToken_persistUserEntity()

$this->cookieManager->createTrustedCookie($request, $user);
}

/**
* @test
*/
public function createTrustedCookie_localhostSkippedInCookie()
{
$request = Request::create('');
$user = $this->createMock('Scheb\TwoFactorBundle\Model\TrustedComputerInterface');

$cookie = $this->cookieManager->createTrustedCookie($request, $user);

$this->assertNull($cookie->getDomain());
}
}

/**
Expand Down

0 comments on commit 514d431

Please sign in to comment.