Changeset 670 for trunk/lib/App.inc.php


Ignore:
Timestamp:
Mar 6, 2019 9:18:39 PM (5 years ago)
Author:
anonymous
Message:

Strip unsafe characters from HTTP_HOST

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/App.inc.php

    r668 r670  
    7878        'site_name' => null,
    7979        'site_email' => '', // Set to no-reply@HTTP_HOST if not set here.
     80        'site_hostname' => '', // The hostname of this application (if not set, use a cleaned HTTP_HOST environment variable).
    8081        'site_url' => '', // URL to the root of the site (created during App->start()).
    8182        'page_url' => '', // URL to the current page (created during App->start()).
     
    478479         */
    479480
     481        $safe_http_host = preg_replace('/[^a-z\d.-]/', '', getenv('HTTP_HOST'));
     482        if ('' != $safe_http_host && '' == $this->getParam('site_hostname')) {
     483            $this->setParam(array('site_hostname' => $safe_http_host));
     484        }
     485
    480486        // Site URL will become something like http://host.name.tld (no ending slash)
    481487        // and is used whenever a URL need be used to the current site.
    482488        // Not available on CLI scripts obviously.
    483         if (isset($_SERVER['HTTP_HOST']) && '' != $_SERVER['HTTP_HOST'] && '' == $this->getParam('site_url')) {
    484             $this->setParam(array('site_url' => sprintf('%s://%s', (getenv('HTTPS') ? 'https' : 'http'), getenv('HTTP_HOST'))));
     489        if ($safe_http_host && '' == $this->getParam('site_url')) {
     490            $this->setParam(array('site_url' => sprintf('%s://%s', (getenv('HTTPS') ? 'https' : 'http'), $safe_http_host)));
    485491        }
    486492
    487493        // Page URL will become a permalink to the current page.
    488494        // Also not available on CLI scripts obviously.
    489         if (isset($_SERVER['HTTP_HOST']) && '' != $_SERVER['HTTP_HOST']) {
    490             $this->setParam(array('page_url' => sprintf('%s://%s%s', (getenv('HTTPS') ? 'https' : 'http'), getenv('HTTP_HOST'), getenv('REQUEST_URI'))));
     495        if ('' != $safe_http_host) {
     496            $this->setParam(array('page_url' => sprintf('%s://%s%s', (getenv('HTTPS') ? 'https' : 'http'), $safe_http_host, getenv('REQUEST_URI'))));
    491497        }
    492498
    493499        // In case site_email isn't set, use something halfway presentable.
    494         if (isset($_SERVER['HTTP_HOST']) && '' != $_SERVER['HTTP_HOST'] && '' == $this->getParam('site_email')) {
    495             $this->setParam(array('site_email' => sprintf('no-reply@%s', getenv('HTTP_HOST'))));
     500        if ('' != $safe_http_host && '' == $this->getParam('site_email')) {
     501            $this->setParam(array('site_email' => sprintf('no-reply@%s', $safe_http_host)));
    496502        }
    497503
     
    909915        // EMAIL ACTION
    910916        if (false !== $this->getParam('log_email_priority') && $priority <= $this->getParam('log_email_priority') && $send_notifications) {
    911             $hostname = (isset($_SERVER['HTTP_HOST']) && '' != $_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : php_uname('n');
     917            $hostname = ('' != $this->getParam('site_hostname')) ? $this->getParam('site_hostname') : php_uname('n');
    912918            $subject = sprintf('[%s %s] %s', $hostname, $event['type'], mb_substr($event['message'], 0, 64));
    913919            $email_msg = sprintf("A log event of type '%s' occurred on %s\n\n", $event['type'], $hostname);
     
    923929        // SMS ACTION
    924930        if (false !== $this->getParam('log_sms_priority') && $priority <= $this->getParam('log_sms_priority') && $send_notifications) {
    925             $hostname = (isset($_SERVER['HTTP_HOST']) && '' != $_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : php_uname('n');
     931            $hostname = ('' != $this->getParam('site_hostname')) ? $this->getParam('site_hostname') : php_uname('n');
    926932            $subject = sprintf('[%s %s]', $hostname, $priority);
    927933            $sms_msg = sprintf('%s [%s:%s]', mb_substr($event_short['message'], 0, 64), basename($file), $line);
     
    13911397            // If relative URL is given, prepend correct local hostname.
    13921398            $scheme = getenv('HTTPS') ? 'https' : 'http';
    1393             $host = getenv('HTTP_HOST');
     1399            $host = $this->getParam('site_hostname');
    13941400            $url = sprintf('%s://%s%s', $scheme, $host, $url);
    13951401        }
Note: See TracChangeset for help on using the changeset viewer.