Skip to content

Commit

Permalink
fix: createFromString() returns URI with invalid hostname
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Jul 9, 2023
1 parent 103ed86 commit 1c1a6ab
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions system/HTTP/SiteURIFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ public function createFromString(string $uri): SiteURI
}

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

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

/**
Expand Down Expand Up @@ -231,21 +232,30 @@ private function createURIFromRoutePath(string $routePath): SiteURI
}

/**
* @return string|null The current hostname. Returns null if no host header.
* @return string|null The current hostname. Returns null if no valid host.
*/
private function getHost(): ?string
{
$host = null;

$httpHostPort = $this->server['HTTP_HOST'] ?? null;

if ($httpHostPort !== null) {
[$httpHost] = explode(':', $httpHostPort, 2);

if (in_array($httpHost, $this->appConfig->allowedHostnames, true)) {
$host = $httpHost;
}
return $this->getValidHost($httpHost);
}

return null;
}

/**
* @return string|null The valid hostname. Returns null if not valid.
*/
private function getValidHost(string $host): ?string
{
if (in_array($host, $this->appConfig->allowedHostnames, true)) {
return $host;
}

return $host;
return null;
}
}

0 comments on commit 1c1a6ab

Please sign in to comment.