-
-
Notifications
You must be signed in to change notification settings - Fork 89
Implemented RFC 7239 - "Forwarded HTTP Extension" #94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
ec85d5d
61e9275
a6e205c
44660bd
11461e6
17d3397
a2c7a79
2383979
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -194,30 +194,54 @@ public function createHttpRequest() | |
| $usingTrustedProxy = $remoteAddr && array_filter($this->proxies, function ($proxy) use ($remoteAddr) { | ||
| return Helpers::ipMatch($remoteAddr, $proxy); | ||
| }); | ||
|
|
||
| if ($usingTrustedProxy) { | ||
| if (!empty($_SERVER['HTTP_X_FORWARDED_PROTO'])) { | ||
| $url->setScheme(strcasecmp($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') === 0 ? 'https' : 'http'); | ||
| } | ||
| if(!empty($_SERVER['HTTP_FORWARDED'])) { | ||
| $forwardParams = preg_split('/[,]|[;]/', $_SERVER['HTTP_FORWARDED']); | ||
| foreach ($forwardParams as $forwardParam) { | ||
| $param = explode("=", $forwardParam); | ||
| $proxyParams[trim($param[0])][] = trim($param[1]); //e.g. array['for'][0] = 192.168.0.1 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Parameter names must be treated in case-insensitive manner.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also this doesn't properly handle quoted values.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Parameter names must be treated in case-insensitive manner.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Quoted values in commit 11461e6 |
||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be simplified to foreach ($forwardParams as $forwardParam) {
list($key, $value) = explode('=', $forwardParam, 2) + [1 => NULL];
$proxyParams[strtolower(trim($key))] = trim($value, " \t\"");
}
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Integrated in commit a2c7a79 |
||
|
|
||
| if (!empty($_SERVER['HTTP_X_FORWARDED_PORT'])) { | ||
| $url->setPort((int) $_SERVER['HTTP_X_FORWARDED_PORT']); | ||
| } | ||
| if (isset($proxyParams['for'])) { | ||
| $remoteAddr = explode(':', $proxyParams['for'][0])[0]; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will not work with IPv6.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IPv6 support in commit 11461e6 |
||
| } | ||
|
|
||
| if (isset($proxyParams['host']) && count($proxyParams['host']) === 1) { | ||
| $remoteHostArr = explode(':', $proxyParams['host'][0]); | ||
| if(count($remoteHostArr) === 2) { | ||
| $remoteHost = $remoteHostArr[0]; | ||
| $url->setPort((int) $remoteHostArr[1]); | ||
| } else { | ||
| $remoteHost = $proxyParams['host'][0]; | ||
| } | ||
| } | ||
|
|
||
| $scheme = (isset($proxyParams['scheme']) && count($proxyParams['scheme']) === 1) ? $proxyParams['scheme'] : 'http'; | ||
| $url->setScheme(strcasecmp($scheme, 'https') === 0 ? 'https' : 'http'); | ||
| } else { | ||
| if (!empty($_SERVER['HTTP_X_FORWARDED_PROTO'])) { | ||
| $url->setScheme(strcasecmp($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') === 0 ? 'https' : 'http'); | ||
| } | ||
|
|
||
| if (!empty($_SERVER['HTTP_X_FORWARDED_PORT'])) { | ||
| $url->setPort((int) $_SERVER['HTTP_X_FORWARDED_PORT']); | ||
| } | ||
|
|
||
| if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { | ||
| $xForwardedForWithoutProxies = array_filter(explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']), function ($ip) { | ||
| return !array_filter($this->proxies, function ($proxy) use ($ip) { | ||
| return Helpers::ipMatch(trim($ip), $proxy); | ||
| if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { | ||
| $xForwardedForWithoutProxies = array_filter(explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']), function ($ip) { | ||
| return !array_filter($this->proxies, function ($proxy) use ($ip) { | ||
| return Helpers::ipMatch(trim($ip), $proxy); | ||
| }); | ||
| }); | ||
| }); | ||
| $remoteAddr = trim(end($xForwardedForWithoutProxies)); | ||
| $xForwardedForRealIpKey = key($xForwardedForWithoutProxies); | ||
| } | ||
| $remoteAddr = trim(end($xForwardedForWithoutProxies)); | ||
| $xForwardedForRealIpKey = key($xForwardedForWithoutProxies); | ||
| } | ||
|
|
||
| if (isset($xForwardedForRealIpKey) && !empty($_SERVER['HTTP_X_FORWARDED_HOST'])) { | ||
| $xForwardedHost = explode(',', $_SERVER['HTTP_X_FORWARDED_HOST']); | ||
| if (isset($xForwardedHost[$xForwardedForRealIpKey])) { | ||
| $remoteHost = trim($xForwardedHost[$xForwardedForRealIpKey]); | ||
| if (isset($xForwardedForRealIpKey) && !empty($_SERVER['HTTP_X_FORWARDED_HOST'])) { | ||
| $xForwardedHost = explode(',', $_SERVER['HTTP_X_FORWARDED_HOST']); | ||
| if (isset($xForwardedHost[$xForwardedForRealIpKey])) { | ||
| $remoteHost = trim($xForwardedHost[$xForwardedForRealIpKey]); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Semicolin is used as a forward-pair delimiter whereas comma is used as a field separator. I'm afraid current implementation won't be able to distinguish it...
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not. However it should not be a problem, as I believe there is no need for the distinction.
These two headers would produce the same result array:
for=192.0.2.43, for=198.51.100.17
for=192.0.2.43; for=198.51.100.17
I just need to split the "key-value" pairs and put them in the proper sub-array.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be simplified to
preg_split('#[,;]#')There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Integrated in commit a2c7a79