Merge pull request #5730 from kenjis/fix-forceGlobalSecureRequests-and-other-than-HTTP

fix: forceGlobalSecureRequests break URI schemes other than HTTP
This commit is contained in:
kenjis 2022-02-25 08:42:51 +09:00 committed by GitHub
commit 200a04eab0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 9 deletions

View File

@ -581,12 +581,34 @@ class URI
$path = $this->getPath();
$scheme = $this->getScheme();
// If the hosts matches then assume this should be relative to baseURL
[$scheme, $path] = $this->changeSchemeAndPath($scheme, $path);
return static::createURIString(
$scheme,
$this->getAuthority(),
$path, // Absolute URIs should use a "/" for an empty path
$this->getQuery(),
$this->getFragment()
);
}
/**
* Change the path (and scheme) assuming URIs with the same host as baseURL
* should be relative to the project's configuration.
*
* @deprecated This method will be deleted.
*/
private function changeSchemeAndPath(string $scheme, string $path): array
{
// Check if this is an internal URI
$config = config('App');
$baseUri = new self($config->baseURL);
// If the hosts matches then assume this should be relative to baseURL
if ($this->getHost() === $baseUri->getHost()) {
if (
substr($this->getScheme(), 0, 4) === 'http'
&& $this->getHost() === $baseUri->getHost()
) {
// Check for additional segments
$basePath = trim($baseUri->getPath(), '/') . '/';
$trimPath = ltrim($path, '/');
@ -601,13 +623,7 @@ class URI
}
}
return static::createURIString(
$scheme,
$this->getAuthority(),
$path, // Absolute URIs should use a "/" for an empty path
$this->getQuery(),
$this->getFragment()
);
return [$scheme, $path];
}
/**

View File

@ -985,4 +985,20 @@ final class URITest extends CIUnitTestCase
$this->assertSame($expected, $uri);
}
/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/5728
*/
public function testForceGlobalSecureRequestsAndNonHTTPProtocol()
{
$config = new App();
$config->forceGlobalSecureRequests = true;
$config->baseURL = 'https://localhost/';
Factories::injectMock('config', 'App', $config);
$expected = 'ftp://localhost/path/to/test.txt';
$uri = new URI($expected);
$this->assertSame($expected, (string) $uri);
}
}