Selaa lähdekoodia

Fall back for pathless request URIs

Jose Tejera 3 kuukautta sitten
vanhempi
sitoutus
4545f44c51
2 muutettua tiedostoa jossa 26 lisäystä ja 1 poistoa
  1. 2 1
      lib/Request.php
  2. 24 0
      tst/RequestTest.php

+ 2 - 1
lib/Request.php

@@ -231,7 +231,8 @@ class Request
     public function getRequestUri()
     public function getRequestUri()
     {
     {
         $uri = array_key_exists('REQUEST_URI', $_SERVER) ? filter_var($_SERVER['REQUEST_URI'], FILTER_SANITIZE_URL) : '';
         $uri = array_key_exists('REQUEST_URI', $_SERVER) ? filter_var($_SERVER['REQUEST_URI'], FILTER_SANITIZE_URL) : '';
-        return empty($uri) ? '/' : parse_url($uri, PHP_URL_PATH);
+        $path = empty($uri) ? '' : parse_url($uri, PHP_URL_PATH);
+        return is_string($path) && $path !== '' ? $path : '/';
     }
     }
 
 
     /**
     /**

+ 24 - 0
tst/RequestTest.php

@@ -39,6 +39,30 @@ class RequestTest extends TestCase
         $this->assertEquals('view', $request->getOperation());
         $this->assertEquals('view', $request->getOperation());
     }
     }
 
 
+    /**
+     * @dataProvider provideRequestUris
+     */
+    public function testRequestUriValidation($uri, $expected)
+    {
+        $this->reset();
+        if ($uri !== null) {
+            $_SERVER['REQUEST_URI'] = $uri;
+        }
+        $request = new Request;
+        $this->assertSame($expected, $request->getRequestUri());
+    }
+
+    public function provideRequestUris()
+    {
+        return [
+            [null, '/'],
+            ['/', '/'],
+            ['/path/to/privatebin?query=value', '/path/to/privatebin'],
+            ['?query=value', '/'],
+            ['http://[', '/'],
+        ];
+    }
+
     public function testRead()
     public function testRead()
     {
     {
         $this->reset();
         $this->reset();