Skip to content

Commit e8a825b

Browse files
authored
Merge pull request #434 from FriendsOfSymfony/anonymous-request-matcher
Use AnonymousRequestMatcher from FOSHttpCache
2 parents 218e9f4 + e645e38 commit e8a825b

File tree

15 files changed

+98
-125
lines changed

15 files changed

+98
-125
lines changed

.travis.yml

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,31 +31,13 @@ matrix:
3131
- php: 5.4
3232
env:
3333
- COMPOSER_FLAGS="--prefer-lowest"
34-
- SYMFONY_VERSION='2.3.*'
34+
- SYMFONY_VERSION='2.8.*'
3535
- php: 5.6
3636
env:
3737
- SYMFONY_VERSION='3.2.*'
3838
- PHPUNIT_FLAGS="--coverage-clover=coverage.clover"
3939
- COVERAGE=true
4040
- DOCCHECK=true
41-
- php: 5.6
42-
env: SYMFONY_VERSION='2.3.*'
43-
- php: 5.6
44-
env:
45-
- SYMFONY_VERSION='2.4.*'
46-
- FRAMEWORK_EXTRA_VERSION='~3.0'
47-
- php: 5.6
48-
env:
49-
- SYMFONY_VERSION='2.5.*'
50-
- FRAMEWORK_EXTRA_VERSION='~3.0'
51-
- php: 5.6
52-
env:
53-
- SYMFONY_VERSION='2.6.*'
54-
- FRAMEWORK_EXTRA_VERSION='~3.0'
55-
- php: 5.6
56-
env:
57-
- SYMFONY_VERSION='2.7.*'
58-
- FRAMEWORK_EXTRA_VERSION='~3.0'
5941
- php: 5.6
6042
env:
6143
- SYMFONY_VERSION='2.8.*'

CHANGELOG.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,38 @@
11
Changelog
22
=========
33

4+
1.3.13
5+
------
6+
7+
* Symfony HttpCache User Context: Move the AnonymousRequestMatcher to FOSHttpCache.
8+
9+
The recommended way to ignore cookie based sessions is to set `session_name_prefix` to
10+
false rather than omit the Cookie header from `user_identifier_headers`.
11+
12+
1.3.12
13+
------
14+
15+
* Prevent potential accidental caching on user context hash mismatch (particularly with symfony HttpCache).
16+
17+
1.3.11
18+
------
19+
20+
* #395 : Compatibility with SensioFrameworkExtraBundle 4.
21+
22+
1.3.10
23+
------
24+
25+
* Avoid calling deprecated method in Symfony 3.2.
26+
27+
1.3.9
28+
-----
29+
30+
* Fix configuration handling when only custom proxy client is configured.
31+
32+
1.3.8
33+
-----
34+
35+
* Do not sanity check hash on anonymous requests.
436

537
1.3.7
638
-----

DependencyInjection/Configuration.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,11 @@ private function addUserContextListenerSection(ArrayNodeDefinition $rootNode)
647647
->arrayNode('user_identifier_headers')
648648
->prototype('scalar')->end()
649649
->defaultValue(array('Cookie', 'Authorization'))
650-
->info('List of headers that contains the unique identifier for the user in the hash request.')
650+
->info('List of headers that contain the unique identifier for the user in the hash request.')
651+
->end()
652+
->scalarNode('session_name_prefix')
653+
->defaultValue(false)
654+
->info('Prefix for session cookies. Must match your PHP session configuration. Set to false to ignore the session in user context.')
651655
->end()
652656
->scalarNode('user_hash_header')
653657
->defaultValue('X-User-Context-Hash')

DependencyInjection/FOSHttpCacheExtension.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,12 @@ private function createRuleMatcher(ContainerBuilder $container, Reference $reque
186186

187187
private function loadUserContext(ContainerBuilder $container, XmlFileLoader $loader, array $config)
188188
{
189+
$configuredUserIdentifierHeaders = array_map('strtolower', $config['user_identifier_headers']);
190+
$completeUserIdentifierHeaders = $configuredUserIdentifierHeaders;
191+
if (false !== $config['session_name_prefix'] && !in_array('cookie', $completeUserIdentifierHeaders)) {
192+
$completeUserIdentifierHeaders[] = 'cookie';
193+
}
194+
189195
$loader->load('user_context.xml');
190196

191197
$container->getDefinition($this->getAlias().'.user_context.request_matcher')
@@ -194,16 +200,20 @@ private function loadUserContext(ContainerBuilder $container, XmlFileLoader $loa
194200

195201
$container->getDefinition($this->getAlias().'.event_listener.user_context')
196202
->replaceArgument(0, new Reference($config['match']['matcher_service']))
197-
->replaceArgument(2, $config['user_identifier_headers'])
203+
->replaceArgument(2, $completeUserIdentifierHeaders)
198204
->replaceArgument(3, $config['user_hash_header'])
199205
->replaceArgument(4, $config['hash_cache_ttl']);
200206

207+
$options = array(
208+
'user_identifier_headers' => $configuredUserIdentifierHeaders,
209+
'session_name_prefix' => $config['session_name_prefix'],
210+
);
201211
$container->getDefinition($this->getAlias().'.user_context.anonymous_request_matcher')
202-
->replaceArgument(0, $config['user_identifier_headers']);
212+
->replaceArgument(0, $options);
203213

204214
if ($config['logout_handler']['enabled']) {
205215
$container->getDefinition($this->getAlias().'.user_context.logout_handler')
206-
->replaceArgument(1, $config['user_identifier_headers'])
216+
->replaceArgument(1, $completeUserIdentifierHeaders)
207217
->replaceArgument(2, $config['match']['accept']);
208218
} else {
209219
$container->removeDefinition($this->getAlias().'.user_context.logout_handler');

EventListener/UserContextSubscriber.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public function onKernelResponse(FilterResponseEvent $event)
165165

166166
if ($request->headers->has($this->hashHeader)) {
167167
// hash has changed, session has most certainly changed, prevent setting incorrect cache
168-
if (!is_null($this->hash) && $this->hash !== $request->headers->get($this->hashHeader)) {
168+
if (null !== $this->hash && $this->hash !== $request->headers->get($this->hashHeader)) {
169169
$response->setCache([
170170
'max_age' => 0,
171171
's_maxage' => 0,

Resources/config/user_context.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
<argument />
4242
</service>
4343

44-
<service id="fos_http_cache.user_context.anonymous_request_matcher" class="FOS\HttpCacheBundle\UserContext\AnonymousRequestMatcher">
44+
<service id="fos_http_cache.user_context.anonymous_request_matcher" class="FOS\HttpCache\UserContext\AnonymousRequestMatcher">
4545
<argument type="collection" />
4646
</service>
4747
</services>

Resources/doc/reference/configuration/user-context.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,23 @@ for 15 minutes, configure:
174174
- Authorization
175175
hash_cache_ttl: 900
176176
177+
The ``Cookie`` header is automatically added to this list unless ``session_name_prefix``
178+
is set to ``false``.
179+
180+
``session_name_prefix``
181+
~~~~~~~~~~~~~~~~~~~~~~~
182+
183+
**type**: ``string`` **default**: ``PHPSESSID``
184+
185+
Defines which cookie is the session cookie. Normal cookies will be ignored in
186+
user context and only the session cookie is taken into account. It is
187+
recommended that you clean up the cookie header to avoid any other cookies in
188+
your requests.
189+
190+
If you set this configuration to ``false``, cookies are completely ignored. If
191+
you add the ``Cookie`` header to ``user_identifier_headers``, any cookie will
192+
make the request not anonymous.
193+
177194
``role_provider``
178195
~~~~~~~~~~~~~~~~~
179196

Tests/Functional/Security/Http/Logout/ContextInvalidationLogoutHandlerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ public function testLogout()
2323
'fos_http_cache.proxy_client.varnish',
2424
'\FOS\HttpCache\ProxyClient\Varnish'
2525
)
26-
->shouldReceive('ban')->once()->with(array('accept' => 'application/vnd.fos.user-context-hash', 'Cookie' => '.*test.*'))
27-
->shouldReceive('ban')->once()->with(array('accept' => 'application/vnd.fos.user-context-hash', 'Authorization' => '.*test.*'))
26+
->shouldReceive('ban')->once()->with(array('accept' => 'application/vnd.fos.user-context-hash', 'cookie' => '.*test.*'))
27+
->shouldReceive('ban')->once()->with(array('accept' => 'application/vnd.fos.user-context-hash', 'authorization' => '.*test.*'))
2828
->shouldReceive('flush')->once()
2929
;
3030

Tests/Resources/Fixtures/config/full.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
),
108108
'hash_cache_ttl' => 300,
109109
'user_identifier_headers' => array('Cookie', 'Authorization'),
110+
'session_name_prefix' => 'PHPSESSID',
110111
'user_hash_header' => 'FOS-User-Context-Hash',
111112
'role_provider' => true,
112113
),

Tests/Resources/Fixtures/config/full.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
</rule>
7676
</invalidation>
7777

78-
<user-context hash-cache-ttl="300" role-provider="true" user-hash-header="FOS-User-Context-Hash">
78+
<user-context hash-cache-ttl="300" role-provider="true" session-name-prefix="PHPSESSID" user-hash-header="FOS-User-Context-Hash">
7979
<match method="GET"/>
8080
<user-identifier-header>Cookie</user-identifier-header>
8181
<user-identifier-header>Authorization</user-identifier-header>

0 commit comments

Comments
 (0)