Skip to content

Commit

Permalink
Item14544: Improve header code and add unit test
Browse files Browse the repository at this point in the history
Doesn't make sense to split every host / forwarded-host header unless
it's actually a forwarded-host.  Also add a unit test.
  • Loading branch information
gac410 committed Dec 10, 2017
1 parent b8511bb commit b248f35
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
35 changes: 35 additions & 0 deletions UnitTestContrib/test/unit/RequestTests.pm
Expand Up @@ -236,6 +236,41 @@ sub test_queryString {
); );
} }


sub test_forwarded_for {
my $this = shift;
my $req = new Foswiki::Request("");
$req->secure('1');
$req->header( Host => 'myhost.com' );
$req->header( 'X-Forwarded-Host' => 'hop1.com, hop2.com' );
$req->action('view');
$req->path_info('/Main/WebHome');
my $base = 'https://hop1.com';
$this->assert_str_equals(
$base,
$req->url( -base => 1 ),
'Wrong BASE url with Forwarded-Host header'
);
print STDERR $req->url() . "\n";

$req->header( 'X-Forwarded-Host' => 'onehop.com:8080' );
$base = 'https://onehop.com:8080';
$this->assert_str_equals(
$base,
$req->url( -base => 1 ),
'Wrong BASE url with Forwarded-Host multiple header'
);

$base = 'http://your.domain.com';
$Foswiki::cfg{ForceDefaultUrlHost} = 1;
$this->assert_str_equals(
$base,
$req->url( -base => 1 ),
'Wrong BASE url with Forwarded-Host single header + forceDefaultUrlHost'
);
print STDERR $req->url() . "\n";

}

sub perform_url_test { sub perform_url_test {
my $this = shift; my $this = shift;
my $req = new Foswiki::Request(""); my $req = new Foswiki::Request("");
Expand Down
16 changes: 9 additions & 7 deletions core/lib/Foswiki/Request.pm
Expand Up @@ -298,14 +298,16 @@ sub url {
$url = $Foswiki::cfg{DefaultUrlHost}; $url = $Foswiki::cfg{DefaultUrlHost};
} }
else { else {
my $vh = my $host;
$this->header('X-Forwarded-Host') if ( $this->header('X-Forwarded-Host') ) {
|| $this->header('Host') $host = ( split /[, ]+/, $this->header('X-Forwarded-Host') )[0];
|| ''; }
$vh = ( split /[, ]+/, $vh )[0]; else {
$host = $this->header('Host');
}
$url = $url =
$vh $host
? $this->protocol . '://' . $vh ? $this->protocol . '://' . $host
: $Foswiki::cfg{DefaultUrlHost}; : $Foswiki::cfg{DefaultUrlHost};
} }
return $url if $base; return $url if $base;
Expand Down

0 comments on commit b248f35

Please sign in to comment.