]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
Merge pull request #899 from smuth4/master
authorArthurHoaro <arthur@hoa.ro>
Thu, 13 Jul 2017 12:15:06 +0000 (14:15 +0200)
committerGitHub <noreply@github.com>
Thu, 13 Jul 2017 12:15:06 +0000 (14:15 +0200)
Respect HTTP_X_FORWARDED_HOST

application/HttpUtils.php
tests/HttpUtils/ServerUrlTest.php

index a81f90565fb3b79349981cadb049de3a2fe2d6aa..88a1efdb86382646648d9a26a2cbeab022f7ecdf 100644 (file)
@@ -311,7 +311,19 @@ function server_url($server)
             }
         }
 
-        return $scheme.'://'.$server['SERVER_NAME'].$port;
+        if (isset($server['HTTP_X_FORWARDED_HOST'])) {
+            // Keep forwarded host
+            if (strpos($server['HTTP_X_FORWARDED_HOST'], ',') !== false) {
+                $hosts = explode(',', $server['HTTP_X_FORWARDED_HOST']);
+                $host = trim($hosts[0]);
+            } else {
+                $host = $server['HTTP_X_FORWARDED_HOST'];
+            }
+        } else {
+            $host = $server['SERVER_NAME'];
+        }
+
+        return $scheme.'://'.$host.$port;
     }
 
     // SSL detection
index 7fdad6594961e5995d40f050fc3f90fbeded7f3d..dac02b3e77cab58cdf05a57fbecfa3cabae1247b 100644 (file)
@@ -38,6 +38,34 @@ class ServerUrlTest extends PHPUnit_Framework_TestCase
         );
     }
 
+    /**
+     * Detect a Proxy that sets Forwarded-Host
+     */
+    public function testHttpsProxyForwardedHost()
+    {
+        $this->assertEquals(
+            'https://host.tld:8080',
+            server_url(
+                array(
+                    'HTTP_X_FORWARDED_PROTO' => 'https',
+                    'HTTP_X_FORWARDED_PORT' => '8080',
+                    'HTTP_X_FORWARDED_HOST' => 'host.tld'
+                )
+            )
+        );
+
+        $this->assertEquals(
+            'https://host.tld:4974',
+            server_url(
+                array(
+                    'HTTP_X_FORWARDED_PROTO' => 'https, https',
+                    'HTTP_X_FORWARDED_PORT' => '4974, 80',
+                    'HTTP_X_FORWARDED_HOST' => 'host.tld, example.com'
+                )
+            )
+        );
+    }
+
     /**
      * Detect a Proxy with SSL enabled
      */