Skip to content

Commit

Permalink
API Fix HTTPS proxy header detection (Same as #3152)
Browse files Browse the repository at this point in the history
Didn't use the de facto standard HTTP_X_FORWARDED_PROTO or the less standard HTTP_FRONT_END_HTTPS.
Removed the 'X-Forwarded-Proto', since PHP should prefix/underscore all HTTP headers before it hits $_SERVER.

References:
- https://docs.djangoproject.com/en/1.4/ref/settings/#secure-proxy-ssl-header
- https://drupal.org/node/1859252
- https://drupal.org/node/313145
- http://scottwb.com/blog/2013/02/06/always-on-https-with-rails-behind-an-elb/
  • Loading branch information
Stephen Shkardoon committed Nov 24, 2014
1 parent 1661213 commit b3407ab
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
19 changes: 15 additions & 4 deletions control/Director.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,11 +414,22 @@ public static function protocolAndHost() {
* @return String
*/
public static function protocol() {
if(isset($_SERVER['HTTP_X_FORWARDED_PROTOCOL'])&&strtolower($_SERVER['HTTP_X_FORWARDED_PROTOCOL'])=='https') {
return "https://";
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) == 'https') {
// Convention for (non-standard) proxy signaling a HTTPS forward,
// see https://en.wikipedia.org/wiki/List_of_HTTP_header_fields
return 'https://';
} else if (isset($_SERVER['HTTP_X_FORWARDED_PROTOCOL']) && strtolower($_SERVER['HTTP_X_FORWARDED_PROTOCOL']) == 'https') {
// Less conventional proxy header
return 'https://';
} else if (isset($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) == 'on') {
// Microsoft proxy convention: https://support.microsoft.com/?kbID=307347
return 'https://';
} else if((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off')) {
return 'https://';
} else if (isset($_SERVER['SSL'])) {
return 'https://';
}
return (isset($_SERVER['SSL']) || (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off'))
? 'https://' : 'http://';
return 'http://';
}

/**
Expand Down
2 changes: 1 addition & 1 deletion control/HTTP.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ public static function add_cache_headers($body = null) {
// By also using and etag that includes both the modification date and all the varies
// values which we also check against we can catch this and not return a 304
$etagParts = array(self::$modification_date, serialize($_COOKIE));
if (isset($_SERVER['HTTP_X_FORWARDED_PROTOCOL'])) $etagParts[] = $_SERVER['HTTP_X_FORWARDED_PROTOCOL'];
$etagParts[] = Director::protocol();
if (isset($_SERVER['HTTP_USER_AGENT'])) $etagParts[] = $_SERVER['HTTP_USER_AGENT'];
if (isset($_SERVER['HTTP_ACCEPT'])) $etagParts[] = $_SERVER['HTTP_ACCEPT'];

Expand Down
24 changes: 16 additions & 8 deletions core/startup/ParameterConfirmationToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,24 @@ public function params() {
protected function currentAbsoluteURL() {
global $url;

// Are we http or https?
$proto = 'http';

if(isset($_SERVER['HTTP_X_FORWARDED_PROTOCOL'])) {
if(strtolower($_SERVER['HTTP_X_FORWARDED_PROTOCOL']) == 'https') $proto = 'https';
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) == 'https') {
// Convention for (non-standard) proxy signaling a HTTPS forward,
// see https://en.wikipedia.org/wiki/List_of_HTTP_header_fields
$proto = 'https';
} else if (isset($_SERVER['HTTP_X_FORWARDED_PROTOCOL']) && strtolower($_SERVER['HTTP_X_FORWARDED_PROTOCOL']) == 'https') {
// Less conventional proxy header
$proto = 'https';
} else if (isset($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) == 'on') {
// Microsoft proxy convention: https://support.microsoft.com/?kbID=307347
$proto = 'https';
} else if((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off')) {
$proto = 'https';
} else if (isset($_SERVER['SSL'])) {
$proto = 'https';
} else {
$proto = 'http';
}

if((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off')) $proto = 'https';
if(isset($_SERVER['SSL'])) $proto = 'https';

$parts = array_filter(array(
// What's our host
$_SERVER['HTTP_HOST'],
Expand Down

0 comments on commit b3407ab

Please sign in to comment.