Skip to content

Commit 1c1a6ab

Browse files
committed
fix: createFromString() returns URI with invalid hostname
1 parent 103ed86 commit 1c1a6ab

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

system/HTTP/SiteURIFactory.php

+18-8
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@ public function createFromString(string $uri): SiteURI
7171
}
7272

7373
$relativePath = $parts['path'] . $query . $fragment;
74+
$host = $this->getValidHost($parts['host']);
7475

75-
return new SiteURI($this->appConfig, $relativePath, $parts['host'], $parts['scheme']);
76+
return new SiteURI($this->appConfig, $relativePath, $host, $parts['scheme']);
7677
}
7778

7879
/**
@@ -231,21 +232,30 @@ private function createURIFromRoutePath(string $routePath): SiteURI
231232
}
232233

233234
/**
234-
* @return string|null The current hostname. Returns null if no host header.
235+
* @return string|null The current hostname. Returns null if no valid host.
235236
*/
236237
private function getHost(): ?string
237238
{
238-
$host = null;
239-
240239
$httpHostPort = $this->server['HTTP_HOST'] ?? null;
240+
241241
if ($httpHostPort !== null) {
242242
[$httpHost] = explode(':', $httpHostPort, 2);
243243

244-
if (in_array($httpHost, $this->appConfig->allowedHostnames, true)) {
245-
$host = $httpHost;
246-
}
244+
return $this->getValidHost($httpHost);
245+
}
246+
247+
return null;
248+
}
249+
250+
/**
251+
* @return string|null The valid hostname. Returns null if not valid.
252+
*/
253+
private function getValidHost(string $host): ?string
254+
{
255+
if (in_array($host, $this->appConfig->allowedHostnames, true)) {
256+
return $host;
247257
}
248258

249-
return $host;
259+
return null;
250260
}
251261
}

0 commit comments

Comments
 (0)