Skip to content

Commit

Permalink
Fix CakeRequest::referer(true) returning scheme-relative URLs
Browse files Browse the repository at this point in the history
Backport of #11503 (and #8795)
  • Loading branch information
chinpei215 committed Dec 4, 2017
1 parent 7fbeea4 commit 9f65402
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/Cake/Network/CakeRequest.php
Expand Up @@ -439,7 +439,7 @@ public function referer($local = false) {
if (!empty($ref) && !empty($base)) {
if ($local && strpos($ref, $base) === 0) {
$ref = substr($ref, strlen($base));
if (empty($ref)) {
if (!strlen($ref) || strpos($ref, '//') === 0) {
$ref = '/';
}
if ($ref[0] !== '/') {
Expand Down
15 changes: 15 additions & 0 deletions lib/Cake/Test/Case/Network/CakeRequestTest.php
Expand Up @@ -739,6 +739,9 @@ public function testReferer() {
$result = $request->referer();
$this->assertSame($result, 'https://cakephp.org');

$result = $request->referer(true);
$this->assertSame('/', $result);

$_SERVER['HTTP_REFERER'] = '';
$result = $request->referer();
$this->assertSame($result, '/');
Expand All @@ -751,6 +754,18 @@ public function testReferer() {
$result = $request->referer(true);
$this->assertSame($result, '/some/path');

$_SERVER['HTTP_REFERER'] = Configure::read('App.fullBaseUrl') . '///cakephp.org/';
$result = $request->referer(true);
$this->assertSame('/', $result); // Avoid returning scheme-relative URLs.

$_SERVER['HTTP_REFERER'] = Configure::read('App.fullBaseUrl') . '/0';
$result = $request->referer(true);
$this->assertSame('/0', $result);

$_SERVER['HTTP_REFERER'] = Configure::read('App.fullBaseUrl') . '/';
$result = $request->referer(true);
$this->assertSame('/', $result);

$_SERVER['HTTP_REFERER'] = Configure::read('App.fullBaseUrl') . '/some/path';
$result = $request->referer(false);
$this->assertSame($result, Configure::read('App.fullBaseUrl') . '/some/path');
Expand Down

0 comments on commit 9f65402

Please sign in to comment.