Skip to content

Commit 200a04e

Browse files
authored
Merge pull request #5730 from kenjis/fix-forceGlobalSecureRequests-and-other-than-HTTP
fix: forceGlobalSecureRequests break URI schemes other than HTTP
2 parents a223756 + 7c74eae commit 200a04e

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

system/HTTP/URI.php

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -581,12 +581,34 @@ public function __toString(): string
581581
$path = $this->getPath();
582582
$scheme = $this->getScheme();
583583

584+
// If the hosts matches then assume this should be relative to baseURL
585+
[$scheme, $path] = $this->changeSchemeAndPath($scheme, $path);
586+
587+
return static::createURIString(
588+
$scheme,
589+
$this->getAuthority(),
590+
$path, // Absolute URIs should use a "/" for an empty path
591+
$this->getQuery(),
592+
$this->getFragment()
593+
);
594+
}
595+
596+
/**
597+
* Change the path (and scheme) assuming URIs with the same host as baseURL
598+
* should be relative to the project's configuration.
599+
*
600+
* @deprecated This method will be deleted.
601+
*/
602+
private function changeSchemeAndPath(string $scheme, string $path): array
603+
{
584604
// Check if this is an internal URI
585605
$config = config('App');
586606
$baseUri = new self($config->baseURL);
587607

588-
// If the hosts matches then assume this should be relative to baseURL
589-
if ($this->getHost() === $baseUri->getHost()) {
608+
if (
609+
substr($this->getScheme(), 0, 4) === 'http'
610+
&& $this->getHost() === $baseUri->getHost()
611+
) {
590612
// Check for additional segments
591613
$basePath = trim($baseUri->getPath(), '/') . '/';
592614
$trimPath = ltrim($path, '/');
@@ -601,13 +623,7 @@ public function __toString(): string
601623
}
602624
}
603625

604-
return static::createURIString(
605-
$scheme,
606-
$this->getAuthority(),
607-
$path, // Absolute URIs should use a "/" for an empty path
608-
$this->getQuery(),
609-
$this->getFragment()
610-
);
626+
return [$scheme, $path];
611627
}
612628

613629
/**

tests/system/HTTP/URITest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -985,4 +985,20 @@ public function testCreateURIString()
985985

986986
$this->assertSame($expected, $uri);
987987
}
988+
989+
/**
990+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/5728
991+
*/
992+
public function testForceGlobalSecureRequestsAndNonHTTPProtocol()
993+
{
994+
$config = new App();
995+
$config->forceGlobalSecureRequests = true;
996+
$config->baseURL = 'https://localhost/';
997+
Factories::injectMock('config', 'App', $config);
998+
999+
$expected = 'ftp://localhost/path/to/test.txt';
1000+
$uri = new URI($expected);
1001+
1002+
$this->assertSame($expected, (string) $uri);
1003+
}
9881004
}

0 commit comments

Comments
 (0)