Skip to content

Commit e0fe137

Browse files
committed
fix: prevent QUERY page caching
Skip PageCache for QUERY requests because request content is part of the QUERY semantics and the current cache key only includes method and URI. - document the routes->add() CSRF behavior change for QUERY - clarify CURLRequest::query() versus the query request option - document that PageCache does not cache QUERY responses - add focused PageCache tests for QUERY requests Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
1 parent bc90f0f commit e0fe137

6 files changed

Lines changed: 54 additions & 8 deletions

File tree

‎system/Filters/PageCache.php‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use CodeIgniter\Cache\ResponseCache;
1717
use CodeIgniter\HTTP\CLIRequest;
1818
use CodeIgniter\HTTP\IncomingRequest;
19+
use CodeIgniter\HTTP\Method;
1920
use CodeIgniter\HTTP\NonBufferedResponseInterface;
2021
use CodeIgniter\HTTP\RedirectResponse;
2122
use CodeIgniter\HTTP\RequestInterface;
@@ -53,6 +54,10 @@ public function before(RequestInterface $request, $arguments = null)
5354
{
5455
assert($request instanceof CLIRequest || $request instanceof IncomingRequest);
5556

57+
if ($request instanceof IncomingRequest && $request->getMethod() === Method::QUERY) {
58+
return null;
59+
}
60+
5661
$response = service('response');
5762

5863
return $this->pageCache->get($request, $response);
@@ -67,6 +72,10 @@ public function after(RequestInterface $request, ResponseInterface $response, $a
6772
{
6873
assert($request instanceof CLIRequest || $request instanceof IncomingRequest);
6974

75+
if ($request instanceof IncomingRequest && $request->getMethod() === Method::QUERY) {
76+
return null;
77+
}
78+
7079
if (
7180
! $response instanceof NonBufferedResponseInterface
7281
&& ! $response instanceof RedirectResponse

‎tests/system/Filters/PageCacheTest.php‎

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use CodeIgniter\HTTP\DownloadResponse;
1717
use CodeIgniter\HTTP\IncomingRequest;
18+
use CodeIgniter\HTTP\Method;
1819
use CodeIgniter\HTTP\RedirectResponse;
1920
use CodeIgniter\HTTP\Response;
2021
use CodeIgniter\HTTP\ResponseInterface;
@@ -31,14 +32,15 @@
3132
#[Group('Others')]
3233
final class PageCacheTest extends CIUnitTestCase
3334
{
34-
private function createRequest(): IncomingRequest
35+
private function createRequest(string $method = Method::GET): IncomingRequest
3536
{
3637
$superglobals = service('superglobals');
3738
$superglobals->setServer('REQUEST_URI', '/');
3839

3940
$siteUri = new SiteURI(new App());
4041

41-
return new IncomingRequest(new App(), $siteUri, null, new UserAgent());
42+
return (new IncomingRequest(new App(), $siteUri, null, new UserAgent()))
43+
->withMethod($method);
4244
}
4345

4446
public function testDefaultConfigCachesAllStatusCodes(): void
@@ -141,6 +143,39 @@ public function testCustomCacheStatusCodes(): void
141143
$this->assertNotInstanceOf(ResponseInterface::class, $result);
142144
}
143145

146+
public function testQueryRequestIsNotCached(): void
147+
{
148+
$config = new Cache();
149+
$filter = new PageCache($config);
150+
151+
$request = $this->createRequest(Method::QUERY);
152+
$response = new Response();
153+
$response->setStatusCode(200);
154+
$response->setBody('Success');
155+
156+
$result = $filter->after($request, $response);
157+
$this->assertNotInstanceOf(ResponseInterface::class, $result);
158+
}
159+
160+
public function testQueryRequestDoesNotReturnCachedResponse(): void
161+
{
162+
$filter = new PageCache(new Cache());
163+
$request = $this->createRequest(Method::QUERY);
164+
$key = service('responsecache')->generateCacheKey($request);
165+
166+
service('cache')->save($key, serialize([
167+
'headers' => [],
168+
'output' => 'Cached',
169+
'status' => 200,
170+
'reason' => 'OK',
171+
]), 60);
172+
173+
$result = $filter->before($request);
174+
$this->assertNull($result);
175+
176+
service('cache')->delete($key);
177+
}
178+
144179
public function testDownloadResponseNotCached(): void
145180
{
146181
$config = new Cache();

‎user_guide_src/source/changelogs/v4.8.0.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Behavior Changes
4040
- **Database:** The Postgre driver's ``$db->error()['code']`` previously always returned ``''``. It now returns the 5-character SQLSTATE string for query and transaction failures (e.g., ``'42P01'``), or ``'08006'`` for connection-level failures. Code that relied on ``$db->error()['code'] === ''`` will need updating.
4141
- **Filters:** HTTP method matching for method-based filters is now case-sensitive. The keys in ``Config\Filters::$methods`` must exactly match the request method
4242
(e.g., ``GET``, ``POST``). Lowercase method names (e.g., ``post``) will no longer match.
43+
- **HTTP:** Routes defined with ``$routes->add()`` now also match HTTP ``QUERY`` requests. If the route has CSRF protection, remember that CSRF verification does not protect safe methods such as ``GET`` and ``QUERY``.
4344
- **Testing:** Tests using the ``FeatureTestTrait`` must now use uppercase HTTP method names when performing a request when using the ``call()`` method directly
4445
(e.g., ``$this->call('GET', '/path')`` instead of ``$this->call('get', '/path')``). Additionally, setting method-based routes using ``withRoutes()`` must
4546
also use uppercase method names (e.g., ``$this->withRoutes([['GET', 'home', 'Home::index']])``).

‎user_guide_src/source/general/caching.rst‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ Valid options are:
9090
caching errors that should be temporary. For example, a cached 404 page would
9191
remain cached even after the resource is created, until the cache expires.
9292

93-
.. note:: Regardless of this setting, ``DownloadResponse`` and ``RedirectResponse``
94-
instances are never cached by the ``PageCache`` filter.
93+
.. note:: Regardless of this setting, ``DownloadResponse``, ``RedirectResponse``,
94+
and HTTP ``QUERY`` responses are never cached by the ``PageCache`` filter.
9595

9696
Enabling Caching
9797
================

‎user_guide_src/source/incoming/routing.rst‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -367,9 +367,9 @@ Routes with any HTTP verbs
367367
another, more appropriate method.
368368

369369
.. warning:: While the ``add()`` method seems to be convenient, it is recommended to always use the HTTP-verb-based
370-
routes, described above, as it is more secure. If you use the :doc:`CSRF protection </libraries/security>`, it does not protect **GET**
371-
requests. If the URI specified in the ``add()`` method is accessible by the GET method, the CSRF protection
372-
will not work.
370+
routes, described above, as it is more secure. If you use the :doc:`CSRF protection </libraries/security>`, it does not protect
371+
**GET** or **QUERY** requests. If the URI specified in the ``add()`` method is accessible by the GET or QUERY method,
372+
the CSRF protection will not work.
373373

374374
It is possible to define a route with any HTTP verbs.
375375
You can use the ``add()`` method:

‎user_guide_src/source/libraries/curlrequest.rst‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ each take the URL as the first parameter and an array of options as the second:
118118

119119
The ``query()`` shortcut sends an HTTP QUERY request. QUERY requests usually
120120
include query content, so use options such as ``json``, ``form_params``, or
121-
``body`` to send the expected body.
121+
``body`` to send the expected body. This is different from the ``query`` request
122+
option, which adds URL query variables to the request URI.
122123

123124
Base URI
124125
--------

0 commit comments

Comments
 (0)