Skip to content

Commit

Permalink
Use X-Forwarded-Proto in base url protocol guessing (#905)
Browse files Browse the repository at this point in the history
Use the X-Forwarded-Proto header to determine the protocol when
behind a TLS termination proxy.
  • Loading branch information
danmichaelo authored and osma committed Jan 10, 2020
1 parent bba726b commit a895be5
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion controller/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ protected function negotiateFormat($choices, $accept, $format)
return $format;
}

private function isSecure()
{
if ($protocol = filter_input(INPUT_SERVER, 'HTTP_X_FORWARDED_PROTO', FILTER_SANITIZE_STRING)) {
return \in_array(strtolower($protocol), ['https', 'on', 'ssl', '1'], true);
}

return filter_input(INPUT_SERVER, 'HTTPS', FILTER_SANITIZE_STRING) !== null;
}

private function guessBaseHref()
{
$script_name = filter_input(INPUT_SERVER, 'SCRIPT_NAME', FILTER_SANITIZE_STRING);
Expand All @@ -93,7 +102,7 @@ private function guessBaseHref()
$doc_root = preg_replace("!{$script_name}$!", '', $script_filename);
$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';
$protocol = $this->isSecure() ? 'https' : 'http';
$port = filter_input(INPUT_SERVER, 'SERVER_PORT', FILTER_SANITIZE_STRING);
$disp_port = ($port == 80 || $port == 443) ? '' : ":$port";
$domain = filter_input(INPUT_SERVER, 'SERVER_NAME', FILTER_SANITIZE_STRING);
Expand Down

0 comments on commit a895be5

Please sign in to comment.