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
2 changes: 1 addition & 1 deletion src/Control/Director.php
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ public static function baseURL()
// Check if BASE_SCRIPT_URL is defined
// e.g. `index.php/`
if (defined('BASE_SCRIPT_URL')) {
return $baseURL . BASE_SCRIPT_URL;
return rtrim($baseURL . BASE_SCRIPT_URL, '/') . '/';
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is technically not related, but matches how the other two scenarios in this method work. This method should always return a value with a trailing slash.

}

return $baseURL;
Expand Down
2 changes: 1 addition & 1 deletion src/Security/Security.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ public static function permissionFailure($controller = null, $messageSet = null)
$message = $messageSet['default'];

$request = $controller->getRequest();
$requestUrl = '/' . ltrim($request->getURL(true), '/');
$requestUrl = Director::baseURL() . ltrim($request->getURL(true), '/');
if ($request->hasSession()) {
list($messageText, $messageCast) = $parseMessage($message);
static::singleton()->setSessionMessage($messageText, ValidationResult::TYPE_WARNING, $messageCast);
Expand Down
26 changes: 25 additions & 1 deletion tests/php/Security/SecurityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,28 @@ protected function tearDown(): void
parent::tearDown();
}

public function testAccessingAuthenticatedPageRedirectsToLoginForm()
public static function provideAccessingAuthenticatedPageRedirectsToLoginForm(): array
{
return [
[
'baseUrl' => null,
'expectedBackUrl' => '/SecurityTest_SecuredController',
],
[
'baseUrl' => 'https://www.example.com/subfolder',
'expectedBackUrl' => '/subfolder/SecurityTest_SecuredController',
],
];
}

#[DataProvider('provideAccessingAuthenticatedPageRedirectsToLoginForm')]
public function testAccessingAuthenticatedPageRedirectsToLoginForm(?string $baseUrl, string $expectedBackUrl): void
{
if ($baseUrl) {
// We can't set `SS_BASE_URL` because that gets consumed to define the `BASE_URL` constant way before this point.
// Instead set alternate_base_url which at least checks the director logic is being used correctly.
Director::config()->set('alternate_base_url', $baseUrl);
}
$this->autoFollowRedirection = false;

$response = $this->get('SecurityTest_SecuredController');
Expand All @@ -89,6 +109,10 @@ public function testAccessingAuthenticatedPageRedirectsToLoginForm()
$response->getHeader('Location')
);

parse_str(parse_url($response->getHeader('Location'), PHP_URL_QUERY), $query);
$this->assertArrayHasKey('BackURL', $query);
$this->assertSame($expectedBackUrl, $query['BackURL']);

$this->logInWithPermission('ADMIN');
$response = $this->get('SecurityTest_SecuredController');
$this->assertEquals(200, $response->getStatusCode());
Expand Down