]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
Force HTTPS if the original port is 443 behind a reverse proxy 1025/head
authorArthurHoaro <arthur@hoa.ro>
Fri, 17 Nov 2017 18:04:14 +0000 (19:04 +0100)
committerArthurHoaro <arthur@hoa.ro>
Sat, 2 Dec 2017 14:24:35 +0000 (15:24 +0100)
Fixes #1022

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

index 0083596643f510d4ea131fad9df25de215ff77ac..c6181df44d3938842067f02d589b9253839b2f3e 100644 (file)
@@ -302,6 +302,13 @@ function server_url($server)
                 $port = $server['HTTP_X_FORWARDED_PORT'];
             }
 
+            // This is a workaround for proxies that don't forward the scheme properly.
+            // Connecting over port 443 has to be in HTTPS.
+            // See https://github.com/shaarli/Shaarli/issues/1022
+            if ($port == '443') {
+                $scheme = 'https';
+            }
+
             if (($scheme == 'http' && $port != '80')
                 || ($scheme == 'https' && $port != '443')
             ) {
index dac02b3e77cab58cdf05a57fbecfa3cabae1247b..324b827acc11e9612c933fa89893dfd318f523a3 100644 (file)
@@ -186,4 +186,36 @@ class ServerUrlTest extends PHPUnit_Framework_TestCase
             )
         );
     }
+
+    /**
+     * Misconfigured server (see #1022): Proxy HTTP but 443
+     */
+    public function testHttpWithPort433()
+    {
+        $this->assertEquals(
+            'https://host.tld',
+            server_url(
+                array(
+                    'HTTPS' => 'Off',
+                    'SERVER_NAME' => 'host.tld',
+                    'SERVER_PORT' => '80',
+                    'HTTP_X_FORWARDED_PROTO' => 'http',
+                    'HTTP_X_FORWARDED_PORT' => '443'
+                )
+            )
+        );
+
+        $this->assertEquals(
+            'https://host.tld',
+            server_url(
+                array(
+                    'HTTPS' => 'Off',
+                    'SERVER_NAME' => 'host.tld',
+                    'SERVER_PORT' => '80',
+                    'HTTP_X_FORWARDED_PROTO' => 'https, http',
+                    'HTTP_X_FORWARDED_PORT' => '443, 80'
+                )
+            )
+        );
+    }
 }