Skip to content
This repository was archived by the owner on Jan 13, 2022. It is now read-only.

Commit 2cfda40

Browse files
committed
Updating logic for x_forwarded_host
1 parent f950a24 commit 2cfda40

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/Facebook/Url/FacebookUrlDetectionHandler.php

+21-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ protected function protocolWithActiveSsl($protocol)
9595
protected function getHostName()
9696
{
9797
// Check for proxy first
98-
if ($host = $this->getHeader('X_FORWARDED_HOST')) {
98+
if ($this->isValidForwardedHost() && $host = $this->getHeader('X_FORWARDED_HOST')) {
9999
$elements = explode(',', $host);
100100
$host = $elements[count($elements) - 1];
101101
} elseif (!$host = $this->getHeader('HOST')) {
@@ -160,4 +160,24 @@ protected function getHeader($key)
160160
{
161161
return $this->getServerVar('HTTP_' . $key);
162162
}
163+
164+
/**
165+
* Checks if the value in X_FORWARDED_HOST is a valid hostname
166+
* Could prevent unintended redirections
167+
*/
168+
protected function isValidForwardedHost()
169+
{
170+
$host = $this->getHeader('X_FORWARDED_HOST');
171+
if (!$host) {
172+
return false;
173+
}
174+
175+
$elements = explode(',', $host);
176+
$host = $elements[count($elements) - 1];
177+
178+
return return (preg_match("/^([a-z\d](-*[a-z\d])*)(\.([a-z\d](-*[a-z\d])*))*$/i", $domain_name) //valid chars check
179+
&& preg_match("/^.{1,253}$/", $domain_name) //overall length check
180+
&& preg_match("/^[^\.]{1,63}(\.[^\.]{1,63})*$/", $domain_name) ); //length of each label
181+
}
182+
163183
}

0 commit comments

Comments
 (0)