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

Commit 72ddbe4

Browse files
committed
Merge pull request #574 from facebook/fosco.xforwarded
Updating logic for x_forwarded_host
2 parents a76ab00 + 40c74a2 commit 72ddbe4

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
vendor/
22
composer.lock
33
tests/FacebookTestCredentials.php
4-

src/Facebook/Url/FacebookUrlDetectionHandler.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ protected function protocolWithActiveSsl($protocol)
9595
protected function getHostName()
9696
{
9797
// Check for proxy first
98-
if ($host = $this->getHeader('X_FORWARDED_HOST')) {
99-
$elements = explode(',', $host);
98+
if ($header = $this->getHeader('X_FORWARDED_HOST') && $this->isValidForwardedHost($header)) {
99+
$elements = explode(',', $header);
100100
$host = $elements[count($elements) - 1];
101101
} elseif (!$host = $this->getHeader('HOST')) {
102102
if (!$host = $this->getServerVar('SERVER_NAME')) {
@@ -160,4 +160,22 @@ 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+
* @param string $header
169+
*
170+
* @return boolean
171+
*/
172+
protected function isValidForwardedHost($header)
173+
{
174+
$elements = explode(',', $header);
175+
$host = $elements[count($elements) - 1];
176+
177+
return preg_match("/^([a-z\d](-*[a-z\d])*)(\.([a-z\d](-*[a-z\d])*))*$/i", $host) //valid chars check
178+
&& 0 < strlen($host) && strlen($host) < 254 //overall length check
179+
&& preg_match("/^[^\.]{1,63}(\.[^\.]{1,63})*$/", $host); //length of each label
180+
}
163181
}

0 commit comments

Comments
 (0)