Skip to content
Snippets Groups Projects
Commit 1bb91383 authored by Jaime Perez Crespo's avatar Jaime Perez Crespo
Browse files

Bugfix: when using PHP sessions, if there's already a session, session_id()...

Bugfix: when using PHP sessions, if there's already a session, session_id() will return the identifier of that session, not our session. In that case, we need to make sure it is our session so that we don't hijack the one of the application.
parent e7c1d8ba
No related branches found
No related tags found
No related merge requests found
...@@ -107,36 +107,37 @@ class SimpleSAML_SessionHandlerPHP extends SimpleSAML_SessionHandler ...@@ -107,36 +107,37 @@ class SimpleSAML_SessionHandlerPHP extends SimpleSAML_SessionHandler
*/ */
public function getCookieSessionId() public function getCookieSessionId()
{ {
if (session_id() === '') { if (!self::hasSessionCookie()) {
if (!self::hasSessionCookie()) { return null; // there's no session cookie, can't return ID
return null; }
}
$session_cookie_params = session_get_cookie_params(); // do not rely on session_id() as it can return the ID of a previous session. Get it from the cookie instead.
session_id($_COOKIE[$this->cookie_name]);
if ($session_cookie_params['secure'] && !\SimpleSAML\Utils\HTTP::isHTTPS()) { $session_cookie_params = session_get_cookie_params();
throw new SimpleSAML_Error_Exception('Session start with secure cookie not allowed on http.');
}
$cacheLimiter = session_cache_limiter(); if ($session_cookie_params['secure'] && !\SimpleSAML\Utils\HTTP::isHTTPS()) {
if (headers_sent()) { throw new SimpleSAML_Error_Exception('Session start with secure cookie not allowed on http.');
/* }
* session_start() tries to send HTTP headers depending on the configuration, according to the
* documentation: $cacheLimiter = session_cache_limiter();
* if (headers_sent()) {
* http://php.net/manual/en/function.session-start.php /*
* * session_start() tries to send HTTP headers depending on the configuration, according to the
* If headers have been already sent, it will then trigger an error since no more headers can be sent. * documentation:
* Being unable to send headers does not mean we cannot recover the session by calling session_start(), *
* so we still want to call it. In this case, though, we want to avoid session_start() to send any * http://php.net/manual/en/function.session-start.php
* headers at all so that no error is generated, so we clear the cache limiter temporarily (no headers *
* sent then) and restore it after successfully starting the session. * If headers have been already sent, it will then trigger an error since no more headers can be sent.
*/ * Being unable to send headers does not mean we cannot recover the session by calling session_start(),
session_cache_limiter(''); * so we still want to call it. In this case, though, we want to avoid session_start() to send any
} * headers at all so that no error is generated, so we clear the cache limiter temporarily (no headers
session_start(); * sent then) and restore it after successfully starting the session.
session_cache_limiter($cacheLimiter); */
session_cache_limiter('');
} }
session_start();
session_cache_limiter($cacheLimiter);
return session_id(); return session_id();
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment