Skip to content

Commit 6d507e7

Browse files
committed
Fix buildWithBaseURLPath, See #581
1 parent ab20c86 commit 6d507e7

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

lib/Saml2/Utils.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -709,8 +709,12 @@ protected static function buildWithBaseURLPath($info)
709709
if (!empty($baseURLPath)) {
710710
$result = $baseURLPath;
711711
if (!empty($info)) {
712-
// Remove base path from the path info.
713-
$extractedInfo = str_replace($baseURLPath, '', $info);
712+
$extractedInfo = $info;
713+
if ($baseURLPath != '/') {
714+
// Remove base path from the path info.
715+
$extractedInfo = str_replace($baseURLPath, '', $info);
716+
}
717+
714718
// Remove starting and ending slash.
715719
$extractedInfo = trim($extractedInfo, '/');
716720
if (!empty($extractedInfo)) {

tests/src/OneLogin/Saml2/UtilsTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,47 @@ public function testSetBaseURL()
547547
$this->assertEquals('/', OneLogin_Saml2_Utils::getBaseURLPath());
548548
}
549549

550+
/**
551+
* @covers OneLogin_Saml2_Utils::setBaseURL
552+
*/
553+
public function testSetBaseURL2()
554+
{
555+
$_SERVER['HTTP_HOST'] = 'sp.example.com';
556+
$_SERVER['HTTPS'] = 'https';
557+
$_SERVER['REQUEST_URI'] = null;
558+
$_SERVER['QUERY_STRING'] = null;
559+
$_SERVER['SCRIPT_NAME'] = '/';
560+
unset($_SERVER['PATH_INFO']);
561+
562+
OneLogin_Saml2_Utils::setBaseURL('https://sp.example.com');
563+
$this->assertEquals("https://sp.example.com/", OneLogin_Saml2_Utils::getSelfURLNoQuery());
564+
$this->assertEquals("https://sp.example.com/", OneLogin_Saml2_Utils::getSelfRoutedURLNoQuery());
565+
$this->assertEquals("https://sp.example.com/", OneLogin_Saml2_Utils::getSelfURL());
566+
$this->assertEquals('/', OneLogin_Saml2_Utils::getBaseURLPath());
567+
568+
$_SERVER['REQUEST_URI'] = '/example1/path/route.php?x=test';
569+
$_SERVER['QUERY_STRING'] = '?x=test';
570+
$_SERVER['SCRIPT_NAME'] = '/example1/path/route.php';
571+
$this->assertEquals("https://sp.example.com/example1/path/route.php", OneLogin_Saml2_Utils::getSelfURLNoQuery());
572+
$this->assertEquals("https://sp.example.com/example1/path/route.php", OneLogin_Saml2_Utils::getSelfRoutedURLNoQuery());
573+
$this->assertEquals("https://sp.example.com/example1/path/route.php?x=test", OneLogin_Saml2_Utils::getSelfURL());
574+
$this->assertEquals('/', OneLogin_Saml2_Utils::getBaseURLPath());
575+
576+
Utils::setBaseURLPath('/example1/path/');
577+
$this->assertEquals("https://sp.example.com/example1/path/route.php", OneLogin_Saml2_Utils::getSelfURLNoQuery());
578+
$this->assertEquals("https://sp.example.com/example1/path/route.php", OneLogin_Saml2_Utils::getSelfRoutedURLNoQuery());
579+
$this->assertEquals("https://sp.example.com/example1/path/route.php?x=test", OneLogin_Saml2_Utils::getSelfURL());
580+
$this->assertEquals('/example1/path/', OneLogin_Saml2_Utils::getBaseURLPath());
581+
582+
$_SERVER['REQUEST_URI'] = '/example1/path/route/?x=test';
583+
$_SERVER['QUERY_STRING'] = '?x=test';
584+
$_SERVER['SCRIPT_NAME'] = '/example1/path/route';
585+
$this->assertEquals("https://sp.example.com/example1/path/route", OneLogin_Saml2_Utils::getSelfURLNoQuery());
586+
$this->assertEquals("https://sp.example.com/example1/path/route", OneLogin_Saml2_Utils::getSelfRoutedURLNoQuery());
587+
$this->assertEquals("https://sp.example.com/example1/path/route/?x=test", OneLogin_Saml2_Utils::getSelfURL());
588+
$this->assertEquals('/example1/path/', OneLogin_Saml2_Utils::getBaseURLPath());
589+
}
590+
550591
/**
551592
* Tests the getSelfURLhost method of the OneLogin_Saml2_Utils
552593
*

0 commit comments

Comments
 (0)