Skip to content

fix: support for multibyte folder names when the app is served from a subfolder #9615

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

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
12 changes: 11 additions & 1 deletion system/HTTP/SiteURIFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ private function parseRequestURI(): string
&& pathinfo($this->superglobals->server('SCRIPT_NAME'), PATHINFO_EXTENSION) === 'php'
) {
// Compare each segment, dropping them until there is no match
$segments = $keep = explode('/', $path);
$segments = explode('/', rawurldecode($path));
$keep = explode('/', $path);

foreach (explode('/', $this->superglobals->server('SCRIPT_NAME')) as $i => $segment) {
// If these segments are not the same then we're done
Expand All @@ -146,6 +147,15 @@ private function parseRequestURI(): string
$path = implode('/', $keep);
}

// Cleanup: if indexPage is still visible in the path, remove it
if ($this->appConfig->indexPage !== '' && str_starts_with($path, $this->appConfig->indexPage)) {
$remainingPath = substr($path, strlen($this->appConfig->indexPage));
// Only remove if followed by '/' (route) or nothing (root)
if ($remainingPath === '' || str_starts_with($remainingPath, '/')) {
$path = ltrim($remainingPath, '/');
}
}

// This section ensures that even on servers that require the URI to
// contain the query string (Nginx) a correct URI is found, and also
// fixes the QUERY_STRING Server var and $_GET array.
Expand Down
105 changes: 105 additions & 0 deletions tests/system/HTTP/SiteURIFactoryDetectRoutePathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,4 +311,109 @@ public static function provideExtensionPHP(): iterable
],
];
}

#[DataProvider('provideRequestURIRewrite')]
public function testRequestURIRewrite(
string $requestUri,
string $scriptName,
string $indexPage,
string $expected,
): void {
$server = [];
$server['REQUEST_URI'] = $requestUri;
$server['SCRIPT_NAME'] = $scriptName;

$appConfig = new App();
$appConfig->indexPage = $indexPage;

$factory = $this->createSiteURIFactory($server, $appConfig);

$this->assertSame($expected, $factory->detectRoutePath('REQUEST_URI'));
}

/**
* @return iterable<string, array{
* requestUri: string,
* scriptName: string,
* indexPage: string,
* expected: string
* }>
*/
public static function provideRequestURIRewrite(): iterable
{
return [
'rewrite_with_route' => [
'requestUri' => '/ci/index.php/sample/method',
'scriptName' => '/ci/public/index.php',
'indexPage' => 'index.php',
'expected' => 'sample/method',
],
'rewrite_root' => [
'requestUri' => '/ci/index.php',
'scriptName' => '/ci/public/index.php',
'indexPage' => 'index.php',
'expected' => '/',
],
'rewrite_no_index_page' => [
'requestUri' => '/ci/sample/method',
'scriptName' => '/ci/public/index.php',
'indexPage' => '',
'expected' => 'sample/method',
],
'rewrite_nested_subfolder' => [
'requestUri' => '/projects/index.php/api/users/list',
'scriptName' => '/projects/myapp/public/index.php',
'indexPage' => 'index.php',
'expected' => 'api/users/list',
],
'rewrite_multiple_public_folders' => [
'requestUri' => '/public-sites/myapp/index.php/content/view',
'scriptName' => '/public-sites/myapp/public/index.php',
'indexPage' => 'index.php',
'expected' => 'content/view',
],
'rewrite_custom_app_folder' => [
'requestUri' => '/myapp/index.php/products/category/electronics',
'scriptName' => '/myapp/web/index.php',
'indexPage' => 'index.php',
'expected' => 'products/category/electronics',
],
'multiple_index_php_in_path' => [
'requestUri' => '/app/index.php/user/index.php/profile',
'scriptName' => '/app/public/index.php',
'indexPage' => 'index.php',
'expected' => 'user/index.php/profile',
],
'custom_index_page_name' => [
'requestUri' => '/ci/app.php/users/list',
'scriptName' => '/ci/public/app.php',
'indexPage' => 'app.php',
'expected' => 'users/list',
],
'custom_index_page_root' => [
'requestUri' => '/project/main.php',
'scriptName' => '/project/web/main.php',
'indexPage' => 'main.php',
'expected' => '/',
],
'partial_match_should_not_remove' => [
'requestUri' => '/app/myindex.php/route',
'scriptName' => '/app/public/index.php',
'indexPage' => 'index.php',
'expected' => 'myindex.php/route',
],
'multibyte_characters' => [
'requestUri' => '/%ED%85%8C%EC%8A%A4%ED%8A%B81/index.php/route',
'scriptName' => '/테스트1/public/index.php',
'indexPage' => 'index.php',
'expected' => 'route',
],
'multibyte_characters_with_nested_subfolder' => [
'requestUri' => '/%D0%BF%D1%80%D0%BE%D0%B5%D0%BA%D1%82/%D1%82%D0%B5%D1%81%D1%821/index.php/route',
'scriptName' => '/проект/тест1/public/index.php',
'indexPage' => 'index.php',
'expected' => 'route',
],
];
}
}
2 changes: 2 additions & 0 deletions user_guide_src/source/changelogs/v4.6.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ Bugs Fixed
- **Email:** Fixed a bug where ``Email::getHostname()`` failed to use ``$_SERVER['SERVER_ADDR']`` when ``$_SERVER['SERVER_NAME']`` was not set.
- **Security:** Fixed a bug where the ``sanitize_filename()`` function from the Security helper would throw an error when used in CLI requests.
- **Session:** Fixed a bug where using the ``DatabaseHandler`` with an unsupported database driver (such as ``SQLSRV``, ``OCI8``, or ``SQLite3``) did not throw an appropriate error.
- **SiteURI:** Fixed a bug in ``SiteURIFactory::parseRequestURI()`` where serving the app from a subfolder using ``mod_rewrite`` while preserving the ``index.php`` file would cause incorrect route path detection.
- **SiteURI:** Fixed a bug in ``SiteURIFactory::parseRequestURI()`` where folder names containing multibyte (non-ASCII) characters were not correctly resolved when the application was served from a subfolder.
- **URI:** Fixed a bug in ``URI::getAuthority()`` where schemes without defined default ports (like ``rtsp://``) would cause issues due to missing array key handling.

See the repo's
Expand Down
Loading