From 7ce18d59c208fb28baaef549280d09bf9b9dc903 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Blanco?= <ablanco@siu.edu.ar> Date: Wed, 2 Mar 2016 14:03:01 -0300 Subject: [PATCH] Fixes issue 337 --- .../Metadata/MetaDataStorageHandler.php | 6 +--- .../MetaDataStorageHandlerFlatFile.php | 4 +-- .../Metadata/MetaDataStorageHandlerPdo.php | 4 +-- .../Metadata/MetaDataStorageSource.php | 6 +--- lib/SimpleSAML/Utils/HTTP.php | 18 ++++++++-- tests/lib/SimpleSAML/Utils/HTTPTest.php | 35 +++++++++++++++++++ www/admin/hostnames.php | 2 +- 7 files changed, 58 insertions(+), 17 deletions(-) create mode 100644 tests/lib/SimpleSAML/Utils/HTTPTest.php diff --git a/lib/SimpleSAML/Metadata/MetaDataStorageHandler.php b/lib/SimpleSAML/Metadata/MetaDataStorageHandler.php index 3903e726e..50c3209af 100644 --- a/lib/SimpleSAML/Metadata/MetaDataStorageHandler.php +++ b/lib/SimpleSAML/Metadata/MetaDataStorageHandler.php @@ -207,11 +207,7 @@ class SimpleSAML_Metadata_MetaDataStorageHandler } // then we look for the hostname - $currenthost = \SimpleSAML\Utils\HTTP::getSelfHost(); // sp.example.org - if (strpos($currenthost, ":") !== false) { - $currenthostdecomposed = explode(":", $currenthost); - $currenthost = $currenthostdecomposed[0]; - } + $currenthost = \SimpleSAML\Utils\HTTP::getSelfHostWithoutPort(); // sp.example.org foreach ($this->sources as $source) { $index = $source->getEntityIdFromHostPath($currenthost, $set, $type); diff --git a/lib/SimpleSAML/Metadata/MetaDataStorageHandlerFlatFile.php b/lib/SimpleSAML/Metadata/MetaDataStorageHandlerFlatFile.php index 3f8632227..80bade1c9 100644 --- a/lib/SimpleSAML/Metadata/MetaDataStorageHandlerFlatFile.php +++ b/lib/SimpleSAML/Metadata/MetaDataStorageHandlerFlatFile.php @@ -134,9 +134,9 @@ class SimpleSAML_Metadata_MetaDataStorageHandlerFlatFile extends SimpleSAML_Meta } elseif ($set === 'shib13-idp-hosted') { return $baseurl.'shib13/idp/metadata.php'; } elseif ($set === 'wsfed-sp-hosted') { - return 'urn:federation:'.\SimpleSAML\Utils\HTTP::getSelfHost(); + return 'urn:federation:'.\SimpleSAML\Utils\HTTP::getSelfHostWithoutPort(); } elseif ($set === 'adfs-idp-hosted') { - return 'urn:federation:'.\SimpleSAML\Utils\HTTP::getSelfHost().':idp'; + return 'urn:federation:'.\SimpleSAML\Utils\HTTP::getSelfHostWithoutPort().':idp'; } else { throw new Exception('Can not generate dynamic EntityID for metadata of this type: ['.$set.']'); } diff --git a/lib/SimpleSAML/Metadata/MetaDataStorageHandlerPdo.php b/lib/SimpleSAML/Metadata/MetaDataStorageHandlerPdo.php index e8981169d..7202a73f9 100644 --- a/lib/SimpleSAML/Metadata/MetaDataStorageHandlerPdo.php +++ b/lib/SimpleSAML/Metadata/MetaDataStorageHandlerPdo.php @@ -151,9 +151,9 @@ class SimpleSAML_Metadata_MetaDataStorageHandlerPdo extends SimpleSAML_Metadata_ } elseif ($set === 'shib13-sp-hosted') { return $baseurl.'shib13/sp/metadata.php'; } elseif ($set === 'wsfed-sp-hosted') { - return 'urn:federation:'.\SimpleSAML\Utils\HTTP::getSelfHost(); + return 'urn:federation:'.\SimpleSAML\Utils\HTTP::getSelfHostWithoutPort(); } elseif ($set === 'adfs-idp-hosted') { - return 'urn:federation:'.\SimpleSAML\Utils\HTTP::getSelfHost().':idp'; + return 'urn:federation:'.\SimpleSAML\Utils\HTTP::getSelfHostWithoutPort().':idp'; } else { throw new Exception('Can not generate dynamic EntityID for metadata of this type: ['.$set.']'); } diff --git a/lib/SimpleSAML/Metadata/MetaDataStorageSource.php b/lib/SimpleSAML/Metadata/MetaDataStorageSource.php index bc7e896ea..d442bf782 100644 --- a/lib/SimpleSAML/Metadata/MetaDataStorageSource.php +++ b/lib/SimpleSAML/Metadata/MetaDataStorageSource.php @@ -198,11 +198,7 @@ abstract class SimpleSAML_Metadata_MetaDataStorageSource $metadataSet = $this->getMetadataSet($set); // check for hostname - $currenthost = \SimpleSAML\Utils\HTTP::getSelfHost(); // sp.example.org - if (strpos($currenthost, ":") !== false) { - $currenthostdecomposed = explode(":", $currenthost); - $currenthost = $currenthostdecomposed[0]; - } + $currenthost = \SimpleSAML\Utils\HTTP::getSelfHostWithoutPort(); // sp.example.org foreach ($metadataSet as $index => $entry) { if ($index === $entityId) { diff --git a/lib/SimpleSAML/Utils/HTTP.php b/lib/SimpleSAML/Utils/HTTP.php index 5f791def7..c25bc6d57 100644 --- a/lib/SimpleSAML/Utils/HTTP.php +++ b/lib/SimpleSAML/Utils/HTTP.php @@ -336,7 +336,7 @@ class HTTP /** - * Helper function to retrieve a file or URL with proxy support, also + * Helper function to retrieve a file or URL with proxy support, also * supporting proxy basic authorization.. * * An exception will be thrown if we are unable to retrieve the data. @@ -605,11 +605,25 @@ class HTTP $url = self::getBaseURL(); $start = strpos($url, '://') + 3; - $length = strcspn($url, '/:', $start); + $length = strcspn($url, '/', $start); return substr($url, $start, $length); } + /** + * Retrieve our own host. + * + * @return string The current host without port specification. + */ + public static function getSelfHostWithoutPort() + { + $url = self::getBaseURL(); + + $start = strpos($url, '://') + 3; + $length = strcspn($url, '/:', $start); + + return substr($url, $start, $length); + } /** * Retrieve our own host together with the URL path. Please note this function will return the base URL for the diff --git a/tests/lib/SimpleSAML/Utils/HTTPTest.php b/tests/lib/SimpleSAML/Utils/HTTPTest.php new file mode 100644 index 000000000..b4e9845a7 --- /dev/null +++ b/tests/lib/SimpleSAML/Utils/HTTPTest.php @@ -0,0 +1,35 @@ +<?php +namespace SimpleSAML\Test\Utils; + +use SimpleSAML\Utils\HTTP; + +class HTTPTest extends \PHPUnit_Framework_TestCase +{ + /** + * Test HTTP::getSelfHost with and without custom port + */ + public function testGetSelfHost() + { + \SimpleSAML_Configuration::loadFromArray(array( + 'baseurlpath' => '', + ), '[ARRAY]', 'simplesaml'); + $_SERVER['SERVER_PORT'] = '80'; + $this->assertEquals('localhost', HTTP::getSelfHost()); + $_SERVER['SERVER_PORT'] = '3030'; + $this->assertEquals('localhost:3030', HTTP::getSelfHost()); + } + + /** + * Test HTTP::getSelfHostWithoutPort + */ + public function testGetSelfHostWithoutPort() + { + \SimpleSAML_Configuration::loadFromArray(array( + 'baseurlpath' => '', + ), '[ARRAY]', 'simplesaml'); + $_SERVER['SERVER_PORT'] = '80'; + $this->assertEquals('localhost', HTTP::getSelfHostWithoutPort()); + $_SERVER['SERVER_PORT'] = '3030'; + $this->assertEquals('localhost', HTTP::getSelfHostWithoutPort()); + } +} diff --git a/www/admin/hostnames.php b/www/admin/hostnames.php index 4baec753b..e77d99edc 100644 --- a/www/admin/hostnames.php +++ b/www/admin/hostnames.php @@ -17,7 +17,7 @@ $attributes['SERVER_PROTOCOL'] = array($_SERVER['SERVER_PROTOCOL']); $attributes['SERVER_PORT'] = array($_SERVER['SERVER_PORT']); $attributes['Utilities_getBaseURL()'] = array(\SimpleSAML\Utils\HTTP::getBaseURL()); -$attributes['Utilities_getSelfHost()'] = array(\SimpleSAML\Utils\HTTP::getSelfHost()); +$attributes['Utilities_getSelfHost()'] = array(\SimpleSAML\Utils\HTTP::getSelfHostWithoutPort()); $attributes['Utilities_selfURLhost()'] = array(\SimpleSAML\Utils\HTTP::getSelfURLHost()); $attributes['Utilities_selfURLNoQuery()'] = array(\SimpleSAML\Utils\HTTP::getSelfURLNoQuery()); $attributes['Utilities_getSelfHostWithPath()'] = array(\SimpleSAML\Utils\HTTP::getSelfHostWithPath()); -- GitLab