Browse Source

Fix Request::referer(true) returning scheme-relative URLs

chinpei215 8 years ago
parent
commit
dc55988f7d
2 changed files with 8 additions and 1 deletions
  1. 1 1
      src/Http/ServerRequest.php
  2. 7 0
      tests/TestCase/Http/ServerRequestTest.php

+ 1 - 1
src/Http/ServerRequest.php

@@ -576,7 +576,7 @@ class ServerRequest implements ArrayAccess, ServerRequestInterface
         if (!empty($ref) && !empty($base)) {
         if (!empty($ref) && !empty($base)) {
             if ($local && strpos($ref, $base) === 0) {
             if ($local && strpos($ref, $base) === 0) {
                 $ref = substr($ref, strlen($base));
                 $ref = substr($ref, strlen($base));
-                if (!strlen($ref)) {
+                if (!strlen($ref) || strpos($ref, '//') === 0) {
                     $ref = '/';
                     $ref = '/';
                 }
                 }
                 if ($ref[0] !== '/') {
                 if ($ref[0] !== '/') {

+ 7 - 0
tests/TestCase/Http/ServerRequestTest.php

@@ -723,6 +723,9 @@ class ServerRequestTest extends TestCase
         $result = $request->referer();
         $result = $request->referer();
         $this->assertSame('http://cakephp.org', $result);
         $this->assertSame('http://cakephp.org', $result);
 
 
+        $result = $request->referer(true);
+        $this->assertSame('/', $result);
+
         $request->env('HTTP_REFERER', '');
         $request->env('HTTP_REFERER', '');
         $result = $request->referer();
         $result = $request->referer();
         $this->assertSame('/', $result);
         $this->assertSame('/', $result);
@@ -731,6 +734,10 @@ class ServerRequestTest extends TestCase
         $result = $request->referer(true);
         $result = $request->referer(true);
         $this->assertSame('/some/path', $result);
         $this->assertSame('/some/path', $result);
 
 
+        $request->env('HTTP_REFERER', Configure::read('App.fullBaseUrl') . '///cakephp.org/');
+        $result = $request->referer(true);
+        $this->assertSame('/', $result); // Avoid returning scheme-relative URLs.
+
         $request->env('HTTP_REFERER', Configure::read('App.fullBaseUrl') . '/0');
         $request->env('HTTP_REFERER', Configure::read('App.fullBaseUrl') . '/0');
         $result = $request->referer(true);
         $result = $request->referer(true);
         $this->assertSame('/0', $result);
         $this->assertSame('/0', $result);