File tree 2 files changed +30
-3
lines changed
2 files changed +30
-3
lines changed Original file line number Diff line number Diff line change @@ -26,19 +26,29 @@ class Store implements StoreInterface
26
26
{
27
27
protected $ root ;
28
28
private $ keyCache ;
29
- private $ locks ;
29
+ private $ locks = [];
30
+ private $ options ;
30
31
31
32
/**
33
+ * Constructor.
34
+ *
35
+ * The available options are:
36
+ *
37
+ * * private_headers Set of response headers that should not be stored
38
+ * when a response is cached. (default: Set-Cookie)
39
+ *
32
40
* @throws \RuntimeException
33
41
*/
34
- public function __construct (string $ root )
42
+ public function __construct (string $ root, array $ options = [] )
35
43
{
36
44
$ this ->root = $ root ;
37
45
if (!file_exists ($ this ->root ) && !@mkdir ($ this ->root , 0777 , true ) && !is_dir ($ this ->root )) {
38
46
throw new \RuntimeException (sprintf ('Unable to create the store directory (%s). ' , $ this ->root ));
39
47
}
40
48
$ this ->keyCache = new \SplObjectStorage ();
41
- $ this ->locks = [];
49
+ $ this ->options = array_merge ([
50
+ 'private_headers ' => ['Set-Cookie ' ],
51
+ ], $ options );
42
52
}
43
53
44
54
/**
@@ -215,6 +225,10 @@ public function write(Request $request, Response $response)
215
225
$ headers = $ this ->persistResponse ($ response );
216
226
unset($ headers ['age ' ]);
217
227
228
+ foreach ($ this ->options ['private_headers ' ] as $ h ) {
229
+ unset($ headers [strtolower ($ h )]);
230
+ }
231
+
218
232
array_unshift ($ entries , [$ storedEnv , $ headers ]);
219
233
220
234
if (!$ this ->save ($ key , serialize ($ entries ))) {
Original file line number Diff line number Diff line change 12
12
namespace Symfony \Component \HttpKernel \Tests \HttpCache ;
13
13
14
14
use PHPUnit \Framework \TestCase ;
15
+ use Symfony \Component \HttpFoundation \Cookie ;
15
16
use Symfony \Component \HttpFoundation \Request ;
16
17
use Symfony \Component \HttpFoundation \Response ;
18
+ use Symfony \Component \HttpKernel \HttpCache \HttpCache ;
17
19
use Symfony \Component \HttpKernel \HttpCache \Store ;
18
20
19
21
class StoreTest extends TestCase
@@ -317,6 +319,17 @@ public function testPurgeHttpAndHttps()
317
319
$ this ->assertEmpty ($ this ->getStoreMetadata ($ requestHttps ));
318
320
}
319
321
322
+ public function testDoesNotStorePrivateHeaders ()
323
+ {
324
+ $ request = Request::create ('https://example.com/foo ' );
325
+ $ response = new Response ('foo ' );
326
+ $ response ->headers ->setCookie (Cookie::fromString ('foo=bar ' ));
327
+
328
+ $ this ->store ->write ($ request , $ response );
329
+ $ this ->assertArrayNotHasKey ('set-cookie ' , $ this ->getStoreMetadata ($ request )[0 ][1 ]);
330
+ $ this ->assertNotEmpty ($ response ->headers ->getCookies ());
331
+ }
332
+
320
333
protected function storeSimpleEntry ($ path = null , $ headers = [])
321
334
{
322
335
if (null === $ path ) {
You can’t perform that action at this time.
0 commit comments