Skip to content

Commit

Permalink
Automatic base url detection behind reverse proxy (NatLibFi#905)
Browse files Browse the repository at this point in the history
Use proxy headers (X-Forwarded-Proto and X-Forwarded-Host) to determine
the base url if configured to trust these headers (new configuration
option).
  • Loading branch information
danmichaelo committed Dec 9, 2019
1 parent bba726b commit a351230
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 1 deletion.
2 changes: 2 additions & 0 deletions config.ttl.dist
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
skosmos:serviceName "Skosmos" ;
# customize the base element. Set this if the automatic base url detection doesn't work. For example setups behind a proxy.
# skosmos:baseHref "http://localhost/Skosmos/" ;
# set this to true to determine the base url from the X-Forwarded-Proto and X-Forwarded-Host headers when running behind a proxy.
# skosmos:trustProxyHeaders true ;
# interface languages available, and the corresponding system locales
skosmos:languages (
[ rdfs:label "fi" ; rdf:value "fi_FI.utf8" ]
Expand Down
7 changes: 6 additions & 1 deletion controller/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,14 @@ private function guessBaseHref()
$base_url = preg_replace("!^{$doc_root}!", '', $base_dir);
$base_url = str_replace('/controller', '/', $base_url);
$protocol = filter_input(INPUT_SERVER, 'HTTPS', FILTER_SANITIZE_STRING) === null ? 'http' : 'https';
$domain = filter_input(INPUT_SERVER, 'SERVER_NAME', FILTER_SANITIZE_STRING);
$port = filter_input(INPUT_SERVER, 'SERVER_PORT', FILTER_SANITIZE_STRING);
if ($this->model->getConfig()->getTrustProxyHeaders()) {
$domain = filter_input(INPUT_SERVER, 'HTTP_X_FORWARDED_HOST', FILTER_SANITIZE_STRING) ?: $domain ;
$protocol = filter_input(INPUT_SERVER, 'HTTP_X_FORWARDED_PROTO', FILTER_SANITIZE_STRING) ?: $protocol ;
$port = filter_input(INPUT_SERVER, 'HTTP_X_FORWARDED_PORT', FILTER_SANITIZE_STRING) ?: $port ;
}
$disp_port = ($port == 80 || $port == 443) ? '' : ":$port";
$domain = filter_input(INPUT_SERVER, 'SERVER_NAME', FILTER_SANITIZE_STRING);
$full_url = "$protocol://{$domain}{$disp_port}{$base_url}";
return $full_url;
}
Expand Down
8 changes: 8 additions & 0 deletions model/GlobalConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,4 +356,12 @@ public function getCollationEnabled()
{
return $this->getBoolean('skosmos:sparqlCollationEnabled', FALSE);
}

/**
* @return boolean
*/
public function getTrustProxyHeaders()
{
return $this->getBoolean('skosmos:trustProxyHeaders', FALSE);
}
}
5 changes: 5 additions & 0 deletions tests/GlobalConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public function testGetBaseHref()
$this->assertEquals("http://tests.localhost/Skosmos/", $this->config->getBaseHref());
}

public function testGetTrustProxyHeaders()
{
$this->assertEquals(true, $this->config->getTrustProxyHeaders());
}

public function testGetLanguages()
{
$this->assertEquals(array('en' => 'en_GB.utf8'), $this->config->getLanguages());
Expand Down
2 changes: 2 additions & 0 deletions tests/testconfig.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
skosmos:serviceName "Skosmos being tested" ;
# customize the base element. Set this if the automatic base url detection doesn't work. For example setups behind a proxy.
skosmos:baseHref "http://tests.localhost/Skosmos/" ;
# set this to true to determine the base url from the X-Forwarded-Proto and X-Forwarded-Host headers when running behind a proxy.
skosmos:trustProxyHeaders true ;
# interface languages available, and the corresponding system locales
skosmos:languages ( [ rdfs:label "en" ; rdf:value "en_GB.utf8" ] ) ;
# how many results (maximum) to load at a time on the search results page
Expand Down

0 comments on commit a351230

Please sign in to comment.