Skip to content

Commit e60ad47

Browse files
committed
allow to ignore cookie header
1 parent ea1b617 commit e60ad47

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed

doc/symfony-cache-configuration.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ options through the constructor:
152152
**default**: ``['Authorization', 'HTTP_AUTHORIZATION', 'PHP_AUTH_USER']``
153153

154154
* **session_name_prefix**: Prefix for session cookies. Must match your PHP session configuration.
155+
If cookies are not relevant in your application, you can set this to ``false`` to ignore any
156+
cookies. (**Only set this to ``false`` if you do not use sessions at all.**)
155157

156158
**default**: ``PHPSESSID``
157159

src/SymfonyCache/UserContextSubscriber.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ class UserContextSubscriber implements EventSubscriberInterface
5454
* - user_hash_method: HTTP Method used with the hash lookup request for user context hash generation.
5555
* - user_identifier_headers: List of request headers that authenticate a non-anonymous request.
5656
* - session_name_prefix: Prefix for session cookies. Must match your PHP session configuration.
57+
* To completely ignore the cookies header and consider requests with cookies
58+
* anonymous, pass false for this option.
5759
*
5860
* @param array $options Options to overwrite the default options
5961
*
@@ -81,7 +83,7 @@ public function __construct(array $options = array())
8183
$resolver->setAllowedTypes('user_hash_method', array('string'));
8284
// actually string[] but that is not supported by symfony < 3.4
8385
$resolver->setAllowedTypes('user_identifier_headers', array('array'));
84-
$resolver->setAllowedTypes('session_name_prefix', array('string'));
86+
$resolver->setAllowedTypes('session_name_prefix', array('string', 'boolean'));
8587
}
8688

8789
$this->options = $resolver->resolve($options);
@@ -141,6 +143,11 @@ public function preHandle(CacheEvent $event)
141143
*/
142144
protected function cleanupHashLookupRequest(Request $hashLookupRequest, Request $originalRequest)
143145
{
146+
if (!$this->options['session_name_prefix']) {
147+
$hashLookupRequest->headers->remove('Cookie');
148+
149+
return;
150+
}
144151
$sessionIds = array();
145152
foreach ($originalRequest->cookies as $name => $value) {
146153
if ($this->isSessionName($name)) {

src/UserContext/AnonymousRequestMatcher.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function __construct(array $options = array())
4141
) {
4242
// actually string[] but that is not supported by symfony < 3.4
4343
$resolver->setAllowedTypes('user_identifier_headers', array('array'));
44-
$resolver->setAllowedTypes('session_name_prefix', array('string'));
44+
$resolver->setAllowedTypes('session_name_prefix', array('string', 'boolean'));
4545
}
4646

4747
$this->options = $resolver->resolve($options);
@@ -58,9 +58,11 @@ public function matches(Request $request)
5858
}
5959
}
6060

61-
foreach ($request->cookies as $name => $value) {
62-
if (0 === strpos($name, $this->options['session_name_prefix'])) {
63-
return false;
61+
if ($this->options['session_name_prefix']) {
62+
foreach ($request->cookies as $name => $value) {
63+
if (0 === strpos($name, $this->options['session_name_prefix'])) {
64+
return false;
65+
}
6466
}
6567
}
6668

tests/Unit/SymfonyCache/UserContextSubscriberTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,34 @@ public function testUserHashUserWithSession($arg, $options)
178178
$this->assertSame($expectedContextHash, $request->headers->get($options['user_hash_header']));
179179
}
180180

181+
/**
182+
* When the session_name_prefix is set to false, the cookie header is completely ignored.
183+
*
184+
* This test does not have authentication headers and thus considers the request anonymous.
185+
*/
186+
public function testUserHashUserIgnoreCookies()
187+
{
188+
$userContextSubscriber = new UserContextSubscriber([
189+
'session_name_prefix' => false,
190+
]);
191+
192+
$sessionId1 = 'my_session_id';
193+
$cookies = array(
194+
'PHPSESSID' => $sessionId1,
195+
);
196+
$cookieString = "PHPSESSID=$sessionId1";
197+
$request = Request::create('/foo', 'GET', array(), $cookies, array(), array('Cookie' => $cookieString));
198+
199+
$event = new CacheEvent($this->kernel, $request);
200+
201+
$userContextSubscriber->preHandle($event);
202+
$response = $event->getResponse();
203+
204+
$this->assertNull($response);
205+
$this->assertTrue($request->headers->has('X-User-Context-Hash'));
206+
$this->assertSame('38015b703d82206ebc01d17a39c727e5', $request->headers->get('X-User-Context-Hash'));
207+
}
208+
181209
/**
182210
* @dataProvider provideConfigOptions
183211
*/

0 commit comments

Comments
 (0)