Skip to content

Commit

Permalink
Merge pull request #6012 from tearoom6/fix/previous_url_only_html
Browse files Browse the repository at this point in the history
  • Loading branch information
MGatner authored May 31, 2022
2 parents f012cc7 + a86e392 commit cae264a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
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

0 comments on commit cae264a

Please sign in to comment.