Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Ignore non-HTML responses in storePreviousURL #6012

Merged
merged 5 commits into from
May 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions system/CodeIgniter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
24 changes: 24 additions & 0 deletions tests/system/CodeIgniterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/v4.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 :php:func:`previous_url` and :php:func:`redirect()->back() <redirect>`.

Enhancements
************
Expand Down