Skip to content
This repository was archived by the owner on Dec 28, 2020. It is now read-only.

Commit 276c49d

Browse files
chrisguitarguydunglas
authored andcommitted
Use Zero Literally When Cookie Expiration is Zero (#37)
* Use Zero Literally When Cookie Expiration is Zero Otherwise add the cookie expiration to the current time. This means that folks using the `0` in their bundle config will have a cookie that lasts the length of the browser session. Everyone else will get a cookie that expiers $cookieExpire seconds in the future. Should fix #33 and address some of the concerns raised in #35 and the commit 5479263 * Add Tests to Verify the Cookie Expiration Behavior - One test to verify that a zero value sets the cookie with no expiration (a session cookie). - Another to test that an actual expiration is set to a time in the future.
1 parent 0e56b26 commit 276c49d

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-3
lines changed

EventListener/AngularCsrfCookieListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function onKernelResponse(FilterResponseEvent $event)
9898
$event->getResponse()->headers->setCookie(new Cookie(
9999
$this->cookieName,
100100
$this->angularCsrfTokenManager->getToken()->getValue(),
101-
time() + $this->cookieExpire,
101+
0 === $this->cookieExpire ? $this->cookieExpire : time() + $this->cookieExpire,
102102
$this->cookiePath,
103103
$this->cookieDomain,
104104
$this->cookieSecure,

spec/Dunglas/AngularCsrfBundle/EventListener/AngularCsrfCookieListenerSpec.php

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Dunglas\AngularCsrfBundle\Routing\RouteMatcherInterface;
1414
use PhpSpec\ObjectBehavior;
1515
use Prophecy\Argument;
16+
use Symfony\Component\HttpFoundation\Cookie;
1617
use Symfony\Component\HttpFoundation\Request;
1718
use Symfony\Component\HttpFoundation\Response;
1819
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
@@ -35,6 +36,8 @@ class AngularCsrfCookieListenerSpec extends ObjectBehavior
3536
private $routes = array('^/punk', '^/rock$');
3637
private $secureRequest;
3738
private $unsecureRequest;
39+
private $tokenManager;
40+
private $routeMatcher;
3841

3942
public function let(
4043
AngularCsrfTokenManager $tokenManager,
@@ -45,16 +48,18 @@ public function let(
4548
) {
4649
$token->getValue()->willReturn(self::TOKEN_VALUE);
4750
$tokenManager->getToken()->willReturn($token);
51+
$this->tokenManager = $tokenManager;
4852

4953
$this->secureRequest = $secureRequest;
5054
$this->unsecureRequest = $unsecureRequest;
5155

5256
$routeMatcher->match($this->secureRequest, $this->routes)->willReturn(true);
5357
$routeMatcher->match($this->unsecureRequest, $this->routes)->willReturn(false);
58+
$this->routeMatcher = $routeMatcher;
5459

5560
$this->beConstructedWith(
56-
$tokenManager,
57-
$routeMatcher,
61+
$this->tokenManager,
62+
$this->routeMatcher,
5863
$this->routes,
5964
self::COOKIE_NAME,
6065
self::COOKIE_EXPIRE,
@@ -100,4 +105,56 @@ public function it_does_not_set_cookie_when_it_does_not(FilterResponseEvent $eve
100105

101106
$this->onKernelResponse($event);
102107
}
108+
109+
public function it_sets_a_cookie_with_no_expiration_when_cookie_expire_is_zero(
110+
FilterResponseEvent $event,
111+
Response $response,
112+
ResponseHeaderBag $headers
113+
) {
114+
$headers->setCookie(Argument::allOf(
115+
Argument::type('Symfony\Component\HttpFoundation\Cookie'),
116+
Argument::that(function (Cookie $c) {
117+
return $c->getExpiresTime() === 0;
118+
})
119+
))->shouldBeCalled();
120+
$response->headers = $headers;
121+
122+
$event->getRequestType()->willReturn(HttpKernelInterface::MASTER_REQUEST)->shouldBeCalled();
123+
$event->getRequest()->willReturn($this->secureRequest)->shouldBeCalled();
124+
$event->getResponse()->willReturn($response)->shouldBeCalled();
125+
126+
$this->onKernelResponse($event);
127+
}
128+
129+
public function it_sets_a_cookie_with_expiration_in_the_future_when_expiration_when_required(
130+
CsrfToken $token,
131+
FilterResponseEvent $event,
132+
Response $response,
133+
ResponseHeaderBag $headers
134+
) {
135+
$headers->setCookie(Argument::allOf(
136+
Argument::type('Symfony\Component\HttpFoundation\Cookie'),
137+
Argument::that(function (Cookie $c) {
138+
return $c->getExpiresTime() > time();
139+
})
140+
))->shouldBeCalled();
141+
$response->headers = $headers;
142+
143+
$this->beConstructedWith(
144+
$this->tokenManager,
145+
$this->routeMatcher,
146+
$this->routes,
147+
self::COOKIE_NAME,
148+
3600,
149+
self::COOKIE_PATH,
150+
self::COOKIE_DOMAIN,
151+
self::COOKIE_SECURE
152+
);
153+
154+
$event->getRequestType()->willReturn(HttpKernelInterface::MASTER_REQUEST)->shouldBeCalled();
155+
$event->getRequest()->willReturn($this->secureRequest)->shouldBeCalled();
156+
$event->getResponse()->willReturn($response)->shouldBeCalled();
157+
158+
$this->onKernelResponse($event);
159+
}
103160
}

0 commit comments

Comments
 (0)