From fd19d90a8326449311b89747f27feaefb305d8fd Mon Sep 17 00:00:00 2001 From: Tomohiro Murota Date: Sat, 21 May 2022 18:46:13 +0900 Subject: [PATCH 1/5] fix: Ignore non-HTML responses in storePreviousURL --- system/CodeIgniter.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/system/CodeIgniter.php b/system/CodeIgniter.php index 18e8d9eaaf2e..d040e6f01787 100644 --- a/system/CodeIgniter.php +++ b/system/CodeIgniter.php @@ -1031,6 +1031,11 @@ public function storePreviousURL($uri) return; } + // Ignore non-HTML responses + if (strpos($this->response->getHeaderLine('Content-Type'), 'text/html') === false) { + return; + } + // This is mainly needed during testing... if (is_string($uri)) { $uri = new URI($uri); From 753f9e68ea7bf3fd9c3243833c1f27d94300fe04 Mon Sep 17 00:00:00 2001 From: Tomohiro Murota Date: Mon, 30 May 2022 11:08:37 +0900 Subject: [PATCH 2/5] test: add test for storePreviousURL in case of non-HTML response --- tests/system/CodeIgniterTest.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/system/CodeIgniterTest.php b/tests/system/CodeIgniterTest.php index 8245d3bf18df..29d511d2a9d3 100644 --- a/tests/system/CodeIgniterTest.php +++ b/tests/system/CodeIgniterTest.php @@ -424,6 +424,30 @@ public function testNotStoresPreviousURL() $this->assertArrayNotHasKey('_ci_previous_url', $_SESSION); } + public function testNotStoresPreviousURLByCheckingContentType() + { + $_SERVER['argv'] = ['index.php', 'image']; + $_SERVER['argc'] = 2; + + $_SERVER['REQUEST_URI'] = '/image'; + + // Inject mock router. + $routes = Services::routes(); + $routes->add('image', static function () { + $response = Services::response(); + + return $response->setContentType('image/jpeg', ''); + }); + $router = Services::router($routes, Services::request()); + Services::injectMock('router', $router); + + ob_start(); + $this->codeigniter->useSafeOutput(true)->run(); + ob_get_clean(); + + $this->assertArrayNotHasKey('_ci_previous_url', $_SESSION); + } + /** * The method after all test, reset Servces:: config * Can't use static::tearDownAfterClass. This will cause a buffer exception From 3ca42268d7e3c9f265133abcc7c09e7c3b3a9871 Mon Sep 17 00:00:00 2001 From: Tomohiro Murota Date: Mon, 30 May 2022 12:29:09 +0900 Subject: [PATCH 3/5] docs: add user guide about breaking changes of previous_url --- user_guide_src/source/changelogs/v4.2.0.rst | 1 + user_guide_src/source/installation/upgrade_420.rst | 1 + 2 files changed, 2 insertions(+) diff --git a/user_guide_src/source/changelogs/v4.2.0.rst b/user_guide_src/source/changelogs/v4.2.0.rst index 077cf64a1ac8..26bb67446c9a 100644 --- a/user_guide_src/source/changelogs/v4.2.0.rst +++ b/user_guide_src/source/changelogs/v4.2.0.rst @@ -32,6 +32,7 @@ Behavior Changes - To prevent unexpected access from the web browser, if a controller is added to a cli route (``$routes->cli()``), all methods of that controller are no longer accessible via auto-routing. - There is a possible backward compatibility break for those users extending the History Collector and they should probably update ``History::setFiles()`` method. - The :php:func:`dot_array_search`'s unexpected behavior has been fixed. Now ``dot_array_search('foo.bar.baz', ['foo' => ['bar' => 23]])`` returns ``null``. The previous versions returned ``23``. +- The ``CodeIgniter::storePreviousURL()`` has been changed to store only the URLs whose Content-Type was ``text/html``. It also affects the behavior of ``previous_url()`` and ``redirect()->back()``. Enhancements ************ diff --git a/user_guide_src/source/installation/upgrade_420.rst b/user_guide_src/source/installation/upgrade_420.rst index 01336530c9f9..75774357fd1c 100644 --- a/user_guide_src/source/installation/upgrade_420.rst +++ b/user_guide_src/source/installation/upgrade_420.rst @@ -42,6 +42,7 @@ Breaking Changes **************** - The ``system/bootstrap.php`` file no longer returns a ``CodeIgniter`` instance, and does not load the ``.env`` file (now handled in ``index.php`` and ``spark``). If you have code that expects these behaviors it will no longer work and must be modified. This has been changed to make `Preloading `_ easier to implement. +- ``previous_url()`` has been changed to return only the URLs whose Content-Type was ``text/html``. Accordingly, the behavior of ``redirect()->back()`` has been changed. Breaking Enhancements ********************* From 4d3584ef67a82c962eaa85b37f581bc1f604e08b Mon Sep 17 00:00:00 2001 From: tearoom6 Date: Tue, 31 May 2022 09:51:06 +0900 Subject: [PATCH 4/5] docs: add links in the user guide Co-authored-by: kenjis --- user_guide_src/source/changelogs/v4.2.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/user_guide_src/source/changelogs/v4.2.0.rst b/user_guide_src/source/changelogs/v4.2.0.rst index 26bb67446c9a..32df0dbdf3d3 100644 --- a/user_guide_src/source/changelogs/v4.2.0.rst +++ b/user_guide_src/source/changelogs/v4.2.0.rst @@ -32,7 +32,7 @@ Behavior Changes - To prevent unexpected access from the web browser, if a controller is added to a cli route (``$routes->cli()``), all methods of that controller are no longer accessible via auto-routing. - There is a possible backward compatibility break for those users extending the History Collector and they should probably update ``History::setFiles()`` method. - The :php:func:`dot_array_search`'s unexpected behavior has been fixed. Now ``dot_array_search('foo.bar.baz', ['foo' => ['bar' => 23]])`` returns ``null``. The previous versions returned ``23``. -- The ``CodeIgniter::storePreviousURL()`` has been changed to store only the URLs whose Content-Type was ``text/html``. It also affects the behavior of ``previous_url()`` and ``redirect()->back()``. +- The ``CodeIgniter::storePreviousURL()`` has been changed to store only the URLs whose Content-Type was ``text/html``. It also affects the behavior of :php:func:`previous_url` and :php:func:`redirect()->back() `. Enhancements ************ From a86e392b0814e580330935ce7e23fa224a45bb15 Mon Sep 17 00:00:00 2001 From: Tomohiro Murota Date: Tue, 31 May 2022 10:00:24 +0900 Subject: [PATCH 5/5] docs: revert previous_url in the upgrade note --- user_guide_src/source/installation/upgrade_420.rst | 1 - 1 file changed, 1 deletion(-) diff --git a/user_guide_src/source/installation/upgrade_420.rst b/user_guide_src/source/installation/upgrade_420.rst index 75774357fd1c..01336530c9f9 100644 --- a/user_guide_src/source/installation/upgrade_420.rst +++ b/user_guide_src/source/installation/upgrade_420.rst @@ -42,7 +42,6 @@ Breaking Changes **************** - The ``system/bootstrap.php`` file no longer returns a ``CodeIgniter`` instance, and does not load the ``.env`` file (now handled in ``index.php`` and ``spark``). If you have code that expects these behaviors it will no longer work and must be modified. This has been changed to make `Preloading `_ easier to implement. -- ``previous_url()`` has been changed to return only the URLs whose Content-Type was ``text/html``. Accordingly, the behavior of ``redirect()->back()`` has been changed. Breaking Enhancements *********************