- Patch #346285 by grendzy, Damien Tournoud, thekevinday et al: fixed problem when HTTP_HOST is not transmitted. From: dries --- includes/bootstrap.inc | 26 ++++++++++++++++++-------- 1 files changed, 18 insertions(+), 8 deletions(-) diff --git includes/bootstrap.inc includes/bootstrap.inc index d6ce896..7d6dc1b 100644 --- includes/bootstrap.inc +++ includes/bootstrap.inc @@ -267,7 +267,7 @@ function drupal_unset_globals() { } /** - * Validate that $_SERVER['HTTP_HOST'] is safe. + * Validate that a hostname (for example $_SERVER['HTTP_HOST']) is safe. * * As $_SERVER['HTTP_HOST'] is user input, ensure it only contains characters * allowed in hostnames. See RFC 952 (and RFC 2181). $_SERVER['HTTP_HOST'] is @@ -276,9 +276,8 @@ function drupal_unset_globals() { * @return * TRUE if only containing valid characters, or FALSE otherwise. */ -function drupal_valid_http_host() { - $_SERVER['HTTP_HOST'] = strtolower($_SERVER['HTTP_HOST']); - return preg_match('/^\[?(?:[a-z0-9-:\]_]+\.?)+$/', $_SERVER['HTTP_HOST']); +function drupal_valid_http_host($host) { + return preg_match('/^\[?(?:[a-z0-9-:\]_]+\.?)+$/', $host); } /** @@ -292,10 +291,21 @@ function conf_init() { global $db_url, $db_prefix, $cookie_domain, $conf, $installed_profile, $update_free_access; $conf = array(); - if (!drupal_valid_http_host()) { - // HTTP_HOST is invalid, e.g. if containing slashes it may be an attack. - header('HTTP/1.1 400 Bad Request'); - exit; + if (isset($_SERVER['HTTP_HOST'])) { + // As HTTP_HOST is user input, ensure it only contains characters allowed + // in hostnames. See RFC 952 (and RFC 2181). + // $_SERVER['HTTP_HOST'] is lowercased here per specifications. + $_SERVER['HTTP_HOST'] = strtolower($_SERVER['HTTP_HOST']); + if (!drupal_valid_http_host($_SERVER['HTTP_HOST'])) { + // HTTP_HOST is invalid, e.g. if containing slashes it may be an attack. + header('HTTP/1.1 400 Bad Request'); + exit; + } + } + else { + // Some pre-HTTP/1.1 clients will not send a Host header. Ensure the key is + // defined for E_ALL compliance. + $_SERVER['HTTP_HOST'] = ''; } if (file_exists('./'. conf_path() .'/settings.php')) {