From aecb85cf64fe060e3ba276fcfdf25ebccf989849 Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Wed, 31 May 2017 12:15:06 +0200 Subject: [PATCH 1/3] Treat PHP Errors on User session regenerate Signed-off-by: Arthur Schiwon --- lib/private/Session/Internal.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/private/Session/Internal.php b/lib/private/Session/Internal.php index 22878154c0..3aaaa43dca 100644 --- a/lib/private/Session/Internal.php +++ b/lib/private/Session/Internal.php @@ -28,6 +28,7 @@ namespace OC\Session; +use OC\ServiceUnavailableException; use OCP\Session\Exceptions\SessionNotAvailableException; /** @@ -110,9 +111,14 @@ class Internal extends Session { * * @param bool $deleteOldSession Whether to delete the old associated session file or not. * @return void + * @throws ServiceUnavailableException */ public function regenerateId($deleteOldSession = true) { - @session_regenerate_id($deleteOldSession); + try { + @session_regenerate_id($deleteOldSession); + } catch (\Error $e) { + $this->trapError($e->getCode(), $e->getMessage()); + } } /** From 05881fa4ed7c0745c39d5c3e2ce89bb2bc53cbad Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Wed, 31 May 2017 12:26:03 +0200 Subject: [PATCH 2/3] =?UTF-8?q?remove=20unnecessary=20lines=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Arthur Schiwon --- lib/private/Session/Internal.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/private/Session/Internal.php b/lib/private/Session/Internal.php index 3aaaa43dca..d8017280e7 100644 --- a/lib/private/Session/Internal.php +++ b/lib/private/Session/Internal.php @@ -28,7 +28,6 @@ namespace OC\Session; -use OC\ServiceUnavailableException; use OCP\Session\Exceptions\SessionNotAvailableException; /** @@ -111,7 +110,6 @@ class Internal extends Session { * * @param bool $deleteOldSession Whether to delete the old associated session file or not. * @return void - * @throws ServiceUnavailableException */ public function regenerateId($deleteOldSession = true) { try { From 6f1fd9ce087062d128e5ea61e7b69318e43a5dbc Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Wed, 14 Jun 2017 12:57:07 +0200 Subject: [PATCH 3/3] change PHP errors to ErrorException in the session (PHP >=7) Otherwise it might be that authentication apps are being disabled on during operation while in fact the session handler has hiccup. Signed-off-by: Arthur Schiwon --- lib/private/Session/Internal.php | 35 ++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/lib/private/Session/Internal.php b/lib/private/Session/Internal.php index d8017280e7..c6f9734c9e 100644 --- a/lib/private/Session/Internal.php +++ b/lib/private/Session/Internal.php @@ -43,12 +43,12 @@ class Internal extends Session { * @throws \Exception */ public function __construct($name) { - session_name($name); set_error_handler(array($this, 'trapError')); + $this->invoke('session_name', [$name]); try { - session_start(); + $this->invoke('session_start'); } catch (\Exception $e) { - setcookie(session_name(), null, -1, \OC::$WEBROOT ? : '/'); + setcookie($this->invoke('session_name'), null, -1, \OC::$WEBROOT ?: '/'); } restore_error_handler(); if (!isset($_SESSION)) { @@ -94,14 +94,14 @@ class Internal extends Session { } public function clear() { - session_unset(); + $this->invoke('session_unset'); $this->regenerateId(); - @session_start(); - $_SESSION = array(); + $this->invoke('session_start', [], true); + $_SESSION = []; } public function close() { - session_write_close(); + $this->invoke('session_write_close'); parent::close(); } @@ -127,7 +127,7 @@ class Internal extends Session { * @since 9.1.0 */ public function getId() { - $id = @session_id(); + $id = $this->invoke('session_id', [], true); if ($id === '') { throw new SessionNotAvailableException(); } @@ -158,4 +158,23 @@ class Internal extends Session { throw new \Exception('Session has been closed - no further changes to the session are allowed'); } } + + /** + * @param string $functionName the full session_* function name + * @param array $parameters + * @param bool $silence whether to suppress warnings + * @throws \ErrorException via trapError + * @return mixed + */ + private function invoke($functionName, array $parameters = [], $silence = false) { + try { + if($silence) { + return @call_user_func_array($functionName, $parameters); + } else { + return call_user_func_array($functionName, $parameters); + } + } catch(\Error $e) { + $this->trapError($e->getCode(), $e->getMessage()); + } + } }