Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/SimpleSAML/Utils/HTTP.php
Original file line number Diff line number Diff line change
Expand Up @@ -611,15 +611,15 @@ public function guessBasePath(): string
return '/';
}
// get the name of the current script
$path = explode('/', $_SERVER['SCRIPT_FILENAME']);
$path = explode(DIRECTORY_SEPARATOR, $_SERVER['SCRIPT_FILENAME']);
$script = array_pop($path);

// get the portion of the URI up to the script, i.e.: /simplesaml/some/directory/script.php
if (!preg_match('#^/(?:[^/]+/)*' . $script . '#', $_SERVER['REQUEST_URI'], $matches)) {
return '/';
}
$uri_s = explode('/', $matches[0]);
$file_s = explode('/', $_SERVER['SCRIPT_FILENAME']);
$file_s = explode(DIRECTORY_SEPARATOR, $_SERVER['SCRIPT_FILENAME']);

// compare both arrays from the end, popping elements matching out of them
while ($uri_s[count($uri_s) - 1] === $file_s[count($file_s) - 1]) {
Expand Down
23 changes: 15 additions & 8 deletions tests/src/SimpleSAML/Utils/HTTPTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ public function testAddURLParameters(): void
$this->assertEquals($url . '&bar=foo', $httpUtils->addURLParameters($url, $params));
}

private function makeNativePath($s)
{
if (DIRECTORY_SEPARATOR == '\\') {
$s = str_replace("/", "\\", $s);
}
return $s;
}

/**
* Test SimpleSAML\Utils\HTTP::guessBasePath().
Expand All @@ -86,35 +93,35 @@ public function testGuessBasePath(): void
$httpUtils = new Utils\HTTP();

$_SERVER['REQUEST_URI'] = '/simplesaml/module.php';
$_SERVER['SCRIPT_FILENAME'] = '/some/path/simplesamlphp/public/module.php';
$_SERVER['SCRIPT_FILENAME'] = self::makeNativePath('/some/path/simplesamlphp/public/module.php');
$this->assertEquals('/simplesaml/', $httpUtils->guessBasePath());

$_SERVER['REQUEST_URI'] = '/simplesaml/module.php/some/path/to/other/script.php';
$_SERVER['SCRIPT_FILENAME'] = '/some/path/simplesamlphp/public/module.php';
$_SERVER['SCRIPT_FILENAME'] = self::makeNativePath('/some/path/simplesamlphp/public/module.php');
$this->assertEquals('/simplesaml/', $httpUtils->guessBasePath());

$_SERVER['REQUEST_URI'] = '/module.php';
$_SERVER['SCRIPT_FILENAME'] = '/some/path/simplesamlphp/public/module.php';
$_SERVER['SCRIPT_FILENAME'] = self::makeNativePath('/some/path/simplesamlphp/public/module.php');
$this->assertEquals('/', $httpUtils->guessBasePath());

$_SERVER['REQUEST_URI'] = '/module.php/some/path/to/other/script.php';
$_SERVER['SCRIPT_FILENAME'] = '/some/path/simplesamlphp/public/module.php';
$_SERVER['SCRIPT_FILENAME'] = self::makeNativePath('/some/path/simplesamlphp/public/module.php');
$this->assertEquals('/', $httpUtils->guessBasePath());

$_SERVER['REQUEST_URI'] = '/some/path/module.php';
$_SERVER['SCRIPT_FILENAME'] = '/some/path/simplesamlphp/public/module.php';
$_SERVER['SCRIPT_FILENAME'] = self::makeNativePath('/some/path/simplesamlphp/public/module.php');
$this->assertEquals('/some/path/', $httpUtils->guessBasePath());

$_SERVER['REQUEST_URI'] = '/some/path/module.php/some/path/to/other/script.php';
$_SERVER['SCRIPT_FILENAME'] = '/some/path/simplesamlphp/public/module.php';
$_SERVER['SCRIPT_FILENAME'] = self::makeNativePath('/some/path/simplesamlphp/public/module.php');
$this->assertEquals('/some/path/', $httpUtils->guessBasePath());

$_SERVER['REQUEST_URI'] = '/some/dir/in/www/script.php';
$_SERVER['SCRIPT_FILENAME'] = '/some/path/simplesamlphp/www/some/dir/in/www/script.php';
$_SERVER['SCRIPT_FILENAME'] = self::makeNativePath('/some/path/simplesamlphp/www/some/dir/in/www/script.php');
$this->assertEquals('/', $httpUtils->guessBasePath());

$_SERVER['REQUEST_URI'] = '/simplesaml/some/dir/in/www/script.php';
$_SERVER['SCRIPT_FILENAME'] = '/some/path/simplesamlphp/www/some/dir/in/www/script.php';
$_SERVER['SCRIPT_FILENAME'] = self::makeNativePath('/some/path/simplesamlphp/www/some/dir/in/www/script.php');
$this->assertEquals('/simplesaml/', $httpUtils->guessBasePath());

$_SERVER = $original;
Expand Down
Loading