From e19f7748bce7581f1fe7c33fd66bfe505a41fb10 Mon Sep 17 00:00:00 2001
From: Tim van Dijen <tvdijen@gmail.com>
Date: Thu, 10 May 2018 20:00:20 +0200
Subject: [PATCH] Fix edge-case #594 with integer SERVER_PORT

---
 lib/SimpleSAML/Utils/HTTP.php           | 3 +++
 tests/lib/SimpleSAML/Utils/HTTPTest.php | 8 ++++++++
 2 files changed, 11 insertions(+)

diff --git a/lib/SimpleSAML/Utils/HTTP.php b/lib/SimpleSAML/Utils/HTTP.php
index 06f4e0b37..7233c9cc1 100644
--- a/lib/SimpleSAML/Utils/HTTP.php
+++ b/lib/SimpleSAML/Utils/HTTP.php
@@ -110,6 +110,9 @@ class HTTP
     {
         $default_port = self::getServerHTTPS() ? '443' : '80';
         $port = isset($_SERVER['SERVER_PORT']) ? $_SERVER['SERVER_PORT'] : $default_port;
+
+        // Take care of edge-case where SERVER_PORT is an integer
+        $port = strval($port);
         
         if ($port !== $default_port) {
             return ':'.$port;
diff --git a/tests/lib/SimpleSAML/Utils/HTTPTest.php b/tests/lib/SimpleSAML/Utils/HTTPTest.php
index 2abf7e9d4..d5708322b 100644
--- a/tests/lib/SimpleSAML/Utils/HTTPTest.php
+++ b/tests/lib/SimpleSAML/Utils/HTTPTest.php
@@ -366,6 +366,10 @@ class HTTPTest extends TestCase
         $_SERVER['SERVER_PORT'] = '80';
         $this->assertEquals(HTTP::getServerPort(), '');
 
+        // Test HTTP + standard integer port
+        $_SERVER['SERVER_PORT'] = 80;
+        $this->assertEquals(HTTP::getServerPort(), '');
+
         // Test HTTP + without port
         unset($_SERVER['SERVER_PORT']);
         $this->assertEquals(HTTP::getServerPort(), '');
@@ -375,6 +379,10 @@ class HTTPTest extends TestCase
         $_SERVER['SERVER_PORT'] = '3030';
         $this->assertEquals(HTTP::getServerPort(), ':3030');
 
+        // Test HTTPS + non-standard integer port
+        $_SERVER['SERVER_PORT'] = 3030;
+        $this->assertEquals(HTTP::getServerPort(), ':3030');
+
         // Test HTTPS + standard port
         $_SERVER['SERVER_PORT'] = '443';
         $this->assertEquals(HTTP::getServerPort(), '');
-- 
GitLab