Skip to content

Commit

Permalink
Don't use FORWARDED_HOST when getting referer values.
Browse files Browse the repository at this point in the history
HTTP_X_FORWARDED_HOST is supposed to be used by proxies to indicate the
original HTTP_HOST value. It has nothing to do with referer values.

Since the HTTP_X_FORWARDED_HOST is intended to replace the HOST header
in proxied setups, add a trustProxy parameter to host() and default it
to false. This maintains existing behavior and allows people to access
the proxied value.

Fixes #2537
  • Loading branch information
markstory committed Dec 23, 2013
1 parent 1aaa565 commit 7053013
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
10 changes: 5 additions & 5 deletions lib/Cake/Network/CakeRequest.php
Expand Up @@ -417,10 +417,6 @@ public function clientIp($safe = true) {
*/
public function referer($local = false) {
$ref = env('HTTP_REFERER');
$forwarded = env('HTTP_X_FORWARDED_HOST');
if ($forwarded) {
$ref = $forwarded;
}

$base = Configure::read('App.fullBaseUrl') . $this->webroot;
if (!empty($ref) && !empty($base)) {
Expand Down Expand Up @@ -667,9 +663,13 @@ public function method() {
/**
* Get the host that the request was handled on.
*
* @param boolean $trustProxy Whether or not to trust the proxy host.
* @return string
*/
public function host() {
public function host($trustProxy = false) {
if ($trustProxy) {
return env('HTTP_X_FORWARDED_HOST');
}
return env('HTTP_HOST');
}

Expand Down
6 changes: 2 additions & 4 deletions lib/Cake/Test/Case/Network/CakeRequestTest.php
Expand Up @@ -698,10 +698,6 @@ public function testReferer() {
$_SERVER['HTTP_REFERER'] = Configure::read('App.fullBaseUrl') . '/recipes/add';
$result = $request->referer(true);
$this->assertSame($result, '/recipes/add');

$_SERVER['HTTP_X_FORWARDED_HOST'] = 'cakephp.org';
$result = $request->referer();
$this->assertSame($result, 'cakephp.org');
}

/**
Expand Down Expand Up @@ -804,9 +800,11 @@ public function testMethod() {
*/
public function testHost() {
$_SERVER['HTTP_HOST'] = 'localhost';
$_SERVER['HTTP_X_FORWARDED_HOST'] = 'cakephp.org';
$request = new CakeRequest('some/path');

$this->assertEquals('localhost', $request->host());
$this->assertEquals('cakephp.org', $request->host(true));
}

/**
Expand Down

0 comments on commit 7053013

Please sign in to comment.