StoreTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpKernel\Tests\HttpCache;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\HttpFoundation\Cookie;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\HttpKernel\HttpCache\HttpCache;
  16. use Symfony\Component\HttpKernel\HttpCache\Store;
  17. class StoreTest extends TestCase
  18. {
  19. protected $request;
  20. protected $response;
  21. /**
  22. * @var Store
  23. */
  24. protected $store;
  25. protected function setUp(): void
  26. {
  27. $this->request = Request::create('/');
  28. $this->response = new Response('hello world', 200, []);
  29. HttpCacheTestCase::clearDirectory(sys_get_temp_dir().'/http_cache');
  30. $this->store = new Store(sys_get_temp_dir().'/http_cache');
  31. }
  32. protected function tearDown(): void
  33. {
  34. $this->store = null;
  35. $this->request = null;
  36. $this->response = null;
  37. HttpCacheTestCase::clearDirectory(sys_get_temp_dir().'/http_cache');
  38. }
  39. public function testReadsAnEmptyArrayWithReadWhenNothingCachedAtKey()
  40. {
  41. $this->assertEmpty($this->getStoreMetadata('/nothing'));
  42. }
  43. public function testUnlockFileThatDoesExist()
  44. {
  45. $this->storeSimpleEntry();
  46. $this->store->lock($this->request);
  47. $this->assertTrue($this->store->unlock($this->request));
  48. }
  49. public function testUnlockFileThatDoesNotExist()
  50. {
  51. $this->assertFalse($this->store->unlock($this->request));
  52. }
  53. public function testRemovesEntriesForKeyWithPurge()
  54. {
  55. $request = Request::create('/foo');
  56. $this->store->write($request, new Response('foo'));
  57. $metadata = $this->getStoreMetadata($request);
  58. $this->assertNotEmpty($metadata);
  59. $this->assertTrue($this->store->purge('/foo'));
  60. $this->assertEmpty($this->getStoreMetadata($request));
  61. // cached content should be kept after purging
  62. $path = $this->store->getPath($metadata[0][1]['x-content-digest'][0]);
  63. $this->assertTrue(is_file($path));
  64. $this->assertFalse($this->store->purge('/bar'));
  65. }
  66. public function testStoresACacheEntry()
  67. {
  68. $cacheKey = $this->storeSimpleEntry();
  69. $this->assertNotEmpty($this->getStoreMetadata($cacheKey));
  70. }
  71. public function testSetsTheXContentDigestResponseHeaderBeforeStoring()
  72. {
  73. $cacheKey = $this->storeSimpleEntry();
  74. $entries = $this->getStoreMetadata($cacheKey);
  75. [, $res] = $entries[0];
  76. $this->assertEquals('en9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08', $res['x-content-digest'][0]);
  77. }
  78. public function testDoesNotTrustXContentDigestFromUpstream()
  79. {
  80. $response = new Response('test', 200, ['X-Content-Digest' => 'untrusted-from-elsewhere']);
  81. $cacheKey = $this->store->write($this->request, $response);
  82. $entries = $this->getStoreMetadata($cacheKey);
  83. [, $res] = $entries[0];
  84. $this->assertEquals('en9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08', $res['x-content-digest'][0]);
  85. $this->assertEquals('en9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08', $response->headers->get('X-Content-Digest'));
  86. }
  87. public function testWritesResponseEvenIfXContentDigestIsPresent()
  88. {
  89. // Prime the store
  90. $this->store->write($this->request, new Response('test', 200, ['X-Content-Digest' => 'untrusted-from-elsewhere']));
  91. $response = $this->store->lookup($this->request);
  92. $this->assertNotNull($response);
  93. }
  94. public function testWritingARestoredResponseDoesNotCorruptCache()
  95. {
  96. /*
  97. * This covers the regression reported in https://github.com/symfony/symfony/issues/37174.
  98. *
  99. * A restored response does *not* load the body, but only keep the file path in a special X-Body-File
  100. * header. For reasons (?), the file path was also used as the restored response body.
  101. * It would be up to others (HttpCache...?) to honor this header and actually load the response content
  102. * from there.
  103. *
  104. * When a restored response was stored again, the Store itself would ignore the header. In the first
  105. * step, this would compute a new Content Digest based on the file path in the restored response body;
  106. * this is covered by "Checkpoint 1" below. But, since the X-Body-File header was left untouched (Checkpoint 2), downstream
  107. * code (HttpCache...) would not immediately notice.
  108. *
  109. * Only upon performing the lookup for a second time, we'd get a Response where the (wrong) Content Digest
  110. * is also reflected in the X-Body-File header, this time also producing wrong content when the downstream
  111. * evaluates it.
  112. */
  113. $this->store->write($this->request, $this->response);
  114. $digest = $this->response->headers->get('X-Content-Digest');
  115. $path = $this->getStorePath($digest);
  116. $response = $this->store->lookup($this->request);
  117. $this->store->write($this->request, $response);
  118. $this->assertEquals($digest, $response->headers->get('X-Content-Digest')); // Checkpoint 1
  119. $this->assertEquals($path, $response->headers->get('X-Body-File')); // Checkpoint 2
  120. $response = $this->store->lookup($this->request);
  121. $this->assertEquals($digest, $response->headers->get('X-Content-Digest'));
  122. $this->assertEquals($path, $response->headers->get('X-Body-File'));
  123. }
  124. public function testFindsAStoredEntryWithLookup()
  125. {
  126. $this->storeSimpleEntry();
  127. $response = $this->store->lookup($this->request);
  128. $this->assertNotNull($response);
  129. $this->assertInstanceOf(Response::class, $response);
  130. }
  131. public function testDoesNotFindAnEntryWithLookupWhenNoneExists()
  132. {
  133. $request = Request::create('/test', 'get', [], [], [], ['HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bar']);
  134. $this->assertNull($this->store->lookup($request));
  135. }
  136. public function testCanonizesUrlsForCacheKeys()
  137. {
  138. $this->storeSimpleEntry($path = '/test?x=y&p=q');
  139. $hitsReq = Request::create($path);
  140. $missReq = Request::create('/test?p=x');
  141. $this->assertNotNull($this->store->lookup($hitsReq));
  142. $this->assertNull($this->store->lookup($missReq));
  143. }
  144. public function testDoesNotFindAnEntryWithLookupWhenTheBodyDoesNotExist()
  145. {
  146. $this->storeSimpleEntry();
  147. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  148. $path = $this->getStorePath($this->response->headers->get('X-Content-Digest'));
  149. @unlink($path);
  150. $this->assertNull($this->store->lookup($this->request));
  151. }
  152. public function testRestoresResponseHeadersProperlyWithLookup()
  153. {
  154. $this->storeSimpleEntry();
  155. $response = $this->store->lookup($this->request);
  156. $this->assertEquals($response->headers->all(), array_merge(['content-length' => 4, 'x-body-file' => [$this->getStorePath($response->headers->get('X-Content-Digest'))]], $this->response->headers->all()));
  157. }
  158. public function testRestoresResponseContentFromEntityStoreWithLookup()
  159. {
  160. $this->storeSimpleEntry();
  161. $response = $this->store->lookup($this->request);
  162. $this->assertEquals($this->getStorePath('en'.hash('sha256', 'test')), $response->headers->get('X-Body-File'));
  163. }
  164. public function testInvalidatesMetaAndEntityStoreEntriesWithInvalidate()
  165. {
  166. $this->storeSimpleEntry();
  167. $this->store->invalidate($this->request);
  168. $response = $this->store->lookup($this->request);
  169. $this->assertInstanceOf(Response::class, $response);
  170. $this->assertFalse($response->isFresh());
  171. }
  172. public function testSucceedsQuietlyWhenInvalidateCalledWithNoMatchingEntries()
  173. {
  174. $req = Request::create('/test');
  175. $this->store->invalidate($req);
  176. $this->assertNull($this->store->lookup($this->request));
  177. }
  178. public function testDoesNotReturnEntriesThatVaryWithLookup()
  179. {
  180. $req1 = Request::create('/test', 'get', [], [], [], ['HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bar']);
  181. $req2 = Request::create('/test', 'get', [], [], [], ['HTTP_FOO' => 'Bling', 'HTTP_BAR' => 'Bam']);
  182. $res = new Response('test', 200, ['Vary' => 'Foo Bar']);
  183. $this->store->write($req1, $res);
  184. $this->assertNull($this->store->lookup($req2));
  185. }
  186. public function testDoesNotReturnEntriesThatSlightlyVaryWithLookup()
  187. {
  188. $req1 = Request::create('/test', 'get', [], [], [], ['HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bar']);
  189. $req2 = Request::create('/test', 'get', [], [], [], ['HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bam']);
  190. $res = new Response('test', 200, ['Vary' => ['Foo', 'Bar']]);
  191. $this->store->write($req1, $res);
  192. $this->assertNull($this->store->lookup($req2));
  193. }
  194. public function testStoresMultipleResponsesForEachVaryCombination()
  195. {
  196. $req1 = Request::create('/test', 'get', [], [], [], ['HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bar']);
  197. $res1 = new Response('test 1', 200, ['Vary' => 'Foo Bar']);
  198. $key = $this->store->write($req1, $res1);
  199. $req2 = Request::create('/test', 'get', [], [], [], ['HTTP_FOO' => 'Bling', 'HTTP_BAR' => 'Bam']);
  200. $res2 = new Response('test 2', 200, ['Vary' => 'Foo Bar']);
  201. $this->store->write($req2, $res2);
  202. $req3 = Request::create('/test', 'get', [], [], [], ['HTTP_FOO' => 'Baz', 'HTTP_BAR' => 'Boom']);
  203. $res3 = new Response('test 3', 200, ['Vary' => 'Foo Bar']);
  204. $this->store->write($req3, $res3);
  205. $this->assertEquals($this->getStorePath('en'.hash('sha256', 'test 3')), $this->store->lookup($req3)->headers->get('X-Body-File'));
  206. $this->assertEquals($this->getStorePath('en'.hash('sha256', 'test 2')), $this->store->lookup($req2)->headers->get('X-Body-File'));
  207. $this->assertEquals($this->getStorePath('en'.hash('sha256', 'test 1')), $this->store->lookup($req1)->headers->get('X-Body-File'));
  208. $this->assertCount(3, $this->getStoreMetadata($key));
  209. }
  210. public function testOverwritesNonVaryingResponseWithStore()
  211. {
  212. $req1 = Request::create('/test', 'get', [], [], [], ['HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bar']);
  213. $res1 = new Response('test 1', 200, ['Vary' => 'Foo Bar']);
  214. $this->store->write($req1, $res1);
  215. $this->assertEquals($this->getStorePath('en'.hash('sha256', 'test 1')), $this->store->lookup($req1)->headers->get('X-Body-File'));
  216. $req2 = Request::create('/test', 'get', [], [], [], ['HTTP_FOO' => 'Bling', 'HTTP_BAR' => 'Bam']);
  217. $res2 = new Response('test 2', 200, ['Vary' => 'Foo Bar']);
  218. $this->store->write($req2, $res2);
  219. $this->assertEquals($this->getStorePath('en'.hash('sha256', 'test 2')), $this->store->lookup($req2)->headers->get('X-Body-File'));
  220. $req3 = Request::create('/test', 'get', [], [], [], ['HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bar']);
  221. $res3 = new Response('test 3', 200, ['Vary' => 'Foo Bar']);
  222. $key = $this->store->write($req3, $res3);
  223. $this->assertEquals($this->getStorePath('en'.hash('sha256', 'test 3')), $this->store->lookup($req3)->headers->get('X-Body-File'));
  224. $this->assertCount(2, $this->getStoreMetadata($key));
  225. }
  226. public function testLocking()
  227. {
  228. $req = Request::create('/test', 'get', [], [], [], ['HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bar']);
  229. $this->assertTrue($this->store->lock($req));
  230. $this->store->lock($req);
  231. $this->assertTrue($this->store->isLocked($req));
  232. $this->store->unlock($req);
  233. $this->assertFalse($this->store->isLocked($req));
  234. }
  235. public function testPurgeHttps()
  236. {
  237. $request = Request::create('https://example.com/foo');
  238. $this->store->write($request, new Response('foo'));
  239. $this->assertNotEmpty($this->getStoreMetadata($request));
  240. $this->assertTrue($this->store->purge('https://example.com/foo'));
  241. $this->assertEmpty($this->getStoreMetadata($request));
  242. }
  243. public function testPurgeHttpAndHttps()
  244. {
  245. $requestHttp = Request::create('https://example.com/foo');
  246. $this->store->write($requestHttp, new Response('foo'));
  247. $requestHttps = Request::create('http://example.com/foo');
  248. $this->store->write($requestHttps, new Response('foo'));
  249. $this->assertNotEmpty($this->getStoreMetadata($requestHttp));
  250. $this->assertNotEmpty($this->getStoreMetadata($requestHttps));
  251. $this->assertTrue($this->store->purge('http://example.com/foo'));
  252. $this->assertEmpty($this->getStoreMetadata($requestHttp));
  253. $this->assertEmpty($this->getStoreMetadata($requestHttps));
  254. }
  255. public function testDoesNotStorePrivateHeaders()
  256. {
  257. $request = Request::create('https://example.com/foo');
  258. $response = new Response('foo');
  259. $response->headers->setCookie(Cookie::fromString('foo=bar'));
  260. $this->store->write($request, $response);
  261. $this->assertArrayNotHasKey('set-cookie', $this->getStoreMetadata($request)[0][1]);
  262. $this->assertNotEmpty($response->headers->getCookies());
  263. }
  264. public function testDiscardsInvalidBodyEval()
  265. {
  266. $request = Request::create('https://example.com/foo');
  267. $response = new Response('foo', 200, ['X-Body-Eval' => 'SSI']);
  268. $this->store->write($request, $response);
  269. $this->assertNull($this->store->lookup($request));
  270. $request = Request::create('https://example.com/foo');
  271. $content = str_repeat('a', 24).'b'.str_repeat('a', 24).'b';
  272. $response = new Response($content, 200, ['X-Body-Eval' => 'SSI']);
  273. $this->store->write($request, $response);
  274. $this->assertNull($this->store->lookup($request));
  275. }
  276. public function testLoadsBodyEval()
  277. {
  278. $request = Request::create('https://example.com/foo');
  279. $content = str_repeat('a', 24).'b'.str_repeat('a', 24);
  280. $response = new Response($content, 200, ['X-Body-Eval' => 'SSI']);
  281. $this->store->write($request, $response);
  282. $response = $this->store->lookup($request);
  283. $this->assertSame($content, $response->getContent());
  284. }
  285. protected function storeSimpleEntry($path = null, $headers = [])
  286. {
  287. if (null === $path) {
  288. $path = '/test';
  289. }
  290. $this->request = Request::create($path, 'get', [], [], [], $headers);
  291. $this->response = new Response('test', 200, ['Cache-Control' => 'max-age=420']);
  292. return $this->store->write($this->request, $this->response);
  293. }
  294. protected function getStoreMetadata($key)
  295. {
  296. $r = new \ReflectionObject($this->store);
  297. $m = $r->getMethod('getMetadata');
  298. $m->setAccessible(true);
  299. if ($key instanceof Request) {
  300. $m1 = $r->getMethod('getCacheKey');
  301. $m1->setAccessible(true);
  302. $key = $m1->invoke($this->store, $key);
  303. }
  304. return $m->invoke($this->store, $key);
  305. }
  306. protected function getStorePath($key)
  307. {
  308. $r = new \ReflectionObject($this->store);
  309. $m = $r->getMethod('getPath');
  310. $m->setAccessible(true);
  311. return $m->invoke($this->store, $key);
  312. }
  313. }