Skip to content

fix: force_https() redirects to wrong URL when baseURL has subfolder #8191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 16, 2023
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
20 changes: 2 additions & 18 deletions system/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -502,27 +502,11 @@ function force_https(
Services::session()->regenerate(); // @codeCoverageIgnore
}

$baseURL = config(App::class)->baseURL;

if (strpos($baseURL, 'https://') === 0) {
$authority = substr($baseURL, strlen('https://'));
} elseif (strpos($baseURL, 'http://') === 0) {
$authority = substr($baseURL, strlen('http://'));
} else {
$authority = $baseURL;
}

$uri = URI::createURIString(
'https',
$authority,
$request->getUri()->getPath(), // Absolute URIs should use a "/" for an empty path
$request->getUri()->getQuery(),
$request->getUri()->getFragment()
);
$uri = $request->getUri()->withScheme('https');

// Set an HSTS header
$response->setHeader('Strict-Transport-Security', 'max-age=' . $duration)
->redirect($uri)
->redirect((string) $uri)
->setStatusCode(307)
->setBody('')
->getCookieStore()
Expand Down
20 changes: 20 additions & 0 deletions tests/system/CommonFunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,7 @@ public function testViewNotSaveData(): void
public function testForceHttpsNullRequestAndResponse(): void
{
$this->assertNull(Services::response()->header('Location'));

Services::response()->setCookie('force', 'cookie');
Services::response()->setHeader('Force', 'header');
Services::response()->setBody('default body');
Expand All @@ -634,6 +635,25 @@ public function testForceHttpsNullRequestAndResponse(): void
force_https();
}

public function testForceHttpsWithBaseUrlSubFolder(): void
{
$config = config(App::class);
$config->baseURL = 'https://example.jp/codeIgniter/';
$uri = new SiteURI($config, 'en/home?foo=bar');
$request = new IncomingRequest($config, $uri, '', new UserAgent());
Services::injectMock('request', $request);

try {
force_https();
} catch (Exception $e) {
$this->assertInstanceOf(RedirectException::class, $e);
$this->assertSame(
'https://example.jp/codeIgniter/index.php/en/home?foo=bar',
$e->getResponse()->header('Location')->getValue()
);
}
}

/**
* @dataProvider provideCleanPathActuallyCleaningThePaths
*
Expand Down