Changeset 690 for trunk/lib


Ignore:
Timestamp:
May 30, 2019 5:28:57 AM (5 years ago)
Author:
anonymous
Message:

Remove App's 'ssl_domain' and 'ssl_enabled' parameters; determine SSL usage by detecting the presence of HTTPS env var (or HTTP_X_FORWARDED_PROTO). Update Session parameters for greater logevity and security. Add 'session_dir' to store site-specific sess_* files with a longer gc_maxlifetime duration.

Location:
trunk/lib
Files:
6 edited

Legend:

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

    r686 r690  
    8989        'redirect_home_url' => '/',
    9090
    91         // SSL URL used when redirecting with $app->sslOn().
    92         'ssl_domain' => null,
    93         'ssl_enabled' => false,
    94 
    9591        // Use CSRF tokens. See notes in the getCSRFToken() method.
    9692        'csrf_token_enabled' => true,
     
    211207        'tmp_dir' => '/tmp',
    212208
     209        // Session files directory. If not defined, the default value from php.ini will be used.
     210        'session_dir' => '',
     211
    213212        // A key for calculating simple cryptographic signatures. Set using as an environment variables in the httpd.conf with 'SetEnv SIGNING_KEY <key>'.
    214213        // Existing password hashes rely on the same key/salt being used to compare encryptions.
     
    383382        if ($this->getParam('php_timezone')) {
    384383            $this->setTimezone($this->getParam('php_timezone'));
     384        }
     385
     386        // If external request was HTTPS but internal request is HTTP, set $_SERVER['HTTPS']='on', which is used by the application to determine that TLS features should be enabled.
     387        if (strtolower(getenv('HTTP_X_FORWARDED_PROTO')) == 'https' && strtolower(getenv('REQUEST_SCHEME')) == 'http') {
     388            putenv('HTTPS=on'); // Available via getenv(
)
     389            isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] = 'on'; // Available via $_SERVER[
]
    385390        }
    386391
     
    448453
    449454            // Session parameters.
     455            // https://www.php.net/manual/en/session.security.ini.php
     456            ini_set('session.cookie_httponly', true);
     457            ini_set('session.cookie_secure', getenv('HTTPS') == 'on');
     458            ini_set('session.cookie_samesite', 'Strict'); // Only PHP >= 7.3
     459            // TODO: Reliance on gc_maxlifetime is not recommended. Developers should manage the lifetime of sessions with a timestamp by themselves.
     460            ini_set('session.cookie_lifetime', 604800); // 7 days.
     461            ini_set('session.gc_maxlifetime', 604800); // 7 days.
     462            ini_set('session.gc_divisor', 1000);
    450463            ini_set('session.gc_probability', 1);
    451             ini_set('session.gc_divisor', 1000);
    452             ini_set('session.gc_maxlifetime', 43200); // 12 hours
    453464            ini_set('session.use_cookies', $this->getParam('session_use_cookies'));
     465            ini_set('session.use_only_cookies', true);
    454466            ini_set('session.use_trans_sid', false);
     467            ini_set('session.use_strict_mode', true);
    455468            ini_set('session.entropy_file', '/dev/urandom');
    456469            ini_set('session.entropy_length', '512');
    457             ini_set('session.cookie_httponly', true);
     470            ini_set('session.sid_length', '48'); // Only PHP >= 7.1
     471            ini_set('session.cache_limiter', 'nocache');
     472            if ('' != $this->getParam('session_dir') && is_dir($this->getParam('session_dir'))) {
     473                ini_set('session.save_path', $this->getParam('session_dir'));
     474            }
    458475            session_name($this->getParam('session_name'));
    459476
     
    11461163        $url = $parts[0];
    11471164        $anchor = isset($parts[1]) ? $parts[1] : '';
    1148         // $anchor =
    11491165
    11501166        // Include the necessary SID if the following is true:
     
    14111427     * and the session is not already defined in the given $url, the SID is appended as a URI query.
    14121428     * As with all header generating functions, make sure this is called before any other output.
     1429     * Using relative URI with Location: header is valid as per https://tools.ietf.org/html/rfc7231#section-7.1.2
    14131430     *
    14141431     * @param   string  $url                    The URL the client will be redirected to.
     
    14311448            $url = $this->getParam('redirect_home_url');
    14321449        }
    1433 
    1434         // FIXME: actually, it seems better to continue using a relative URL since we can't guess the port at the reverse proxy (e.g., port 3000 for browser-sync).
    1435         // if (preg_match('!^/!', $url)) {
    1436         //     // If relative URL is given, prepend site_url, which contains: scheme://hostname[:port]
    1437         //     $url = sprintf('%s%s', $this->getParam('site_url'), $url);k
    1438         // }
    14391450
    14401451        $url = $this->url($url, $carry_args, $always_include_sid);
     
    16381649        }
    16391650
    1640         if ($url == absoluteMe()) {
     1651        if ($url == absoluteMe() || $url == getenv('REQUEST_URI')) {
    16411652            // The URL we are directing to is the current page.
    1642             $this->logMsg(sprintf('validBoomerangURL(%s) not valid, same as absoluteMe: %s', $id, $url), LOG_DEBUG, __FILE__, __LINE__);
     1653            $this->logMsg(sprintf('validBoomerangURL(%s) not valid, same as absoluteMe or REQUEST_URI: %s', $id, $url), LOG_DEBUG, __FILE__, __LINE__);
    16431654            return false;
    16441655        }
     
    17111722        // Defaults.
    17121723        $expire = (is_numeric($expire) ? $expire : (is_string($expire) ? strtotime($expire) : $expire));
    1713         $secure = $secure ?: (getenv('HTTPS') && $this->getParam('ssl_enabled'));
     1724        $secure = $secure ?: getenv('HTTPS') == 'on';
    17141725        $httponly = $httponly ?: true;
    17151726
  • trunk/lib/Auth_File.inc.php

    r611 r690  
    324324
    325325            // Login scripts must have the same 'login' tag for boomerangURL verification/manipulation.
    326             $app->setBoomerangURL(absoluteMe(), 'login');
     326            $app->setBoomerangURL(getenv('REQUEST_URI'), 'login');
    327327            $app->dieURL($this->_params['login_url']);
    328328        }
  • trunk/lib/Auth_SQL.inc.php

    r674 r690  
    610610     *  - remote address is the same as the login remote address
    611611     *
     612     * TODO: implement persisten sessions as per https://paragonie.com/blog/2015/04/secure-authentication-php-with-long-term-persistence
     613     *
    612614     * @access public
    613615     */
     
    771773
    772774            // Login scripts must have the same 'login' tag for boomerangURL verification/manipulation.
    773             $app->setBoomerangURL(absoluteMe(), 'login');
     775            $app->setBoomerangURL(getenv('REQUEST_URI'), 'login');
    774776            $app->dieURL($this->_params['login_url']);
    775777        }
  • trunk/lib/Auth_Simple.inc.php

    r685 r690  
    261261
    262262            // Login scripts must have the same 'login' tag for boomerangURL verification/manipulation.
    263             $app->setBoomerangURL(absoluteMe(), 'login');
     263            $app->setBoomerangURL(getenv('REQUEST_URI'), 'login');
    264264            $app->dieURL($this->_params['login_url']);
    265265        }
  • trunk/lib/Lock.inc.php

    r685 r690  
    389389        $app =& App::getInstance();
    390390
    391         $app->dieURL($this->getParam('error_url'), array('lock_id' => $this->data['lock_id'], 'boomerang' => urlencode(absoluteMe())));
     391        $app->dieURL($this->getParam('error_url'), array('lock_id' => $this->data['lock_id'], 'boomerang' => urlencode(getenv('REQUEST_URI'))));
    392392    }
    393393
  • trunk/lib/Utilities.inc.php

    r679 r690  
    13991399
    14001400/**
    1401  * Returns a fully qualified URL to the current script, including the query.
     1401 * Returns a fully qualified URL to the current script, including the query. If you don't need the scheme://, use REQUEST_URI instead.
    14021402 *
    14031403 * @return string    a full url to the current script
Note: See TracChangeset for help on using the changeset viewer.