CodeIgniter icon indicating copy to clipboard operation
CodeIgniter copied to clipboard

"Cannot call session save handler in a recursive manner"

Open wrabit opened this issue 8 years ago • 42 comments

I am getting a timeout in Session_files_driver.phpwith these in my logs:

Severity: Error --> Maximum execution time of 30 seconds exceeded /system/libraries/Session/drivers/Session_files_driver.php 212
Severity: Warning --> Unknown: Cannot call session save handler in a recursive manner Unknown 0
Severity: Warning --> Unknown: Failed to write session data using user defined save handler. (session.save_path: /tmp/php) Unknown 0

What I am doing:

  • A view contains a form that posts data to one controller
  • The same view performs 2 asynchronous ajax calls to separate and different controllers when dom ready.
  • No problem so far
  • Data is posted from the form to controller
  • This controller, when detects the post, sets some flashdata and redirects back to same method
  • When hitting the view again from the redirect, only one ajax call is fired, the other hangs and hits the errors above.

Additional info:

  • Removing one ajax call, the operation is fine and it doesn't matter which one is removed.
  • Removing the set flash data, things are fine
  • Removing the redirect, things are fine

I have separated out the barebones setup of this in a new 3.19 and zipped up here to see, just hit the form submit on the form field on the welcome view.

https://www.dropbox.com/s/2mbkdp1cdhftafj/ci-issue.zip?dl=0

wrabit avatar Jul 13 '18 15:07 wrabit

3.18 doesn't have the issue.

wrabit avatar Jul 14 '18 10:07 wrabit

I have the same problem

https://github.com/bcit-ci/CodeIgniter/issues/5530#issuecomment-404860026

it-can avatar Jul 14 '18 10:07 it-can

If you comment/remove the function validateId($id) in your session-driver, it should working again. Due the changelog it was added because of: https://github.com/bcit-ci/CodeIgniter/commit/b3f7aae1079e8e484437bc67f4c126f34e7903d8

Fixed a possible session fixation vulnerability where the Session Library enabled session.use_strict_mode but it didn’t actually do anything.

I didnt debugged it deeper, but i think the validate-function dont work correctly, when it get called async.

Edit: Rename the function validateId($id) -> validateSessionId($id) in Session_files_driver.php (Line: 405) and also change the function call in Session_driver.php (Line: 124) and it also works. There maybe them to be a error on calling the correct function.

Xcreen avatar Jul 18 '18 13:07 Xcreen

@Xcreen would this not break other session drivers? and that commit (https://github.com/bcit-ci/CodeIgniter/commit/b3f7aae1079e8e484437bc67f4c126f34e7903d8) would only work on php < 7 right...

it-can avatar Jul 22 '18 12:07 it-can

What PHP version is this on? Using which session driver?

narfbg avatar Jul 22 '18 14:07 narfbg

files driver My problem was with php 7.1.20

it-can avatar Jul 22 '18 15:07 it-can

@it-can Yes, you said that in #5530 ... I'm asking the OP here.

narfbg avatar Jul 22 '18 15:07 narfbg

Files 7.2.1

wrabit avatar Jul 23 '18 04:07 wrabit

@Xcreen and you?

narfbg avatar Jul 23 '18 14:07 narfbg

My solution probably resolve this problem. https://github.com/bcit-ci/CodeIgniter/pull/5550

dkowalskidev avatar Jul 25 '18 07:07 dkowalskidev

No, #5550 doesn't resolve anything.

Anyway, I assume @Xcreen has no intention of replying or is just unavailable, but he did stumble upon a clue ...

validateId() is supposed to be auto-called by PHP 7+ only if the handler implements SessionUpdateTimestampHandlerInterface, which it doesn't because that's fucking ridiculous, poorly documented and I'd told the guy who wrote that logic that it's a bad idea, but he listens to nobody but the voices in his head.
The security researchers who reported the issue that warranted b3f7aae1079e8e484437bc67f4c126f34e7903d8 were supposed to verify it, and they didn't catch that either.

Yet, validateId() is somehow being auto-called and triggering those errors. The messages are confusing because that's what tends to happen when the bug is in PHP itself and it somehow doesn't straight-up crash with a segmentation fault.

So, a logical solution should be to rename validateId() to something else and then drop the version checks so that CI always calls it:

diff --git a/system/libraries/Session/Session_driver.php b/system/libraries/Session/Session_driver.php
index 2fe30b8..a26b3a3 100644
--- a/system/libraries/Session/Session_driver.php
+++ b/system/libraries/Session/Session_driver.php
@@ -121,7 +121,7 @@ abstract class CI_Session_driver implements SessionHandlerInterface {
 	 */
 	public function php5_validate_id()
 	{
-		if (PHP_VERSION_ID < 70000 && isset($_COOKIE[$this->_config['cookie_name']]) && ! $this->validateId($_COOKIE[$this->_config['cookie_name']]))
+		if (isset($_COOKIE[$this->_config['cookie_name']]) && ! $this->validateSessionId($_COOKIE[$this->_config['cookie_name']]))
 		{
 			unset($_COOKIE[$this->_config['cookie_name']]);
 		}
diff --git a/system/libraries/Session/drivers/Session_database_driver.php b/system/libraries/Session/drivers/Session_database_driver.php
index 074accf..f342690 100644
--- a/system/libraries/Session/drivers/Session_database_driver.php
+++ b/system/libraries/Session/drivers/Session_database_driver.php
@@ -353,7 +353,7 @@ class CI_Session_database_driver extends CI_Session_driver implements SessionHan
 	 * @param	string	$id
 	 * @return	bool
 	 */
-	public function validateId($id)
+	public function validateSessionId($id)
 	{
 		// Prevent previous QB calls from messing with our queries
 		$this->_db->reset_query();
diff --git a/system/libraries/Session/drivers/Session_files_driver.php b/system/libraries/Session/drivers/Session_files_driver.php
index 654f300..8c7fac0 100644
--- a/system/libraries/Session/drivers/Session_files_driver.php
+++ b/system/libraries/Session/drivers/Session_files_driver.php
@@ -402,7 +402,7 @@ class CI_Session_files_driver extends CI_Session_driver implements SessionHandle
 	 * @param	string	$id
 	 * @return	bool
 	 */
-	public function validateId($id)
+	public function validateSessionId($id)
 	{
 		return is_file($this->_file_path.$id);
 	}
diff --git a/system/libraries/Session/drivers/Session_memcached_driver.php b/system/libraries/Session/drivers/Session_memcached_driver.php
index 9e81168..b464439 100644
--- a/system/libraries/Session/drivers/Session_memcached_driver.php
+++ b/system/libraries/Session/drivers/Session_memcached_driver.php
@@ -303,7 +303,7 @@ class CI_Session_memcached_driver extends CI_Session_driver implements SessionHa
 	 * @param	string	$id
 	 * @return	bool
 	 */
-	public function validateId($id)
+	public function validateSessionId($id)
 	{
 		$this->_memcached->get($this->_key_prefix.$id);
 		return ($this->_memcached->getResultCode() === Memcached::RES_SUCCESS);
diff --git a/system/libraries/Session/drivers/Session_redis_driver.php b/system/libraries/Session/drivers/Session_redis_driver.php
index d7a5755..f351aff 100644
--- a/system/libraries/Session/drivers/Session_redis_driver.php
+++ b/system/libraries/Session/drivers/Session_redis_driver.php
@@ -323,7 +323,7 @@ class CI_Session_redis_driver extends CI_Session_driver implements SessionHandle
 	 * @param	string	$id
 	 * @return	bool
 	 */
-	public function validateId($id)
+	public function validateSessionId($id)
 	{
 		return (bool) $this->_redis->exists($this->_key_prefix.$id);
 	}

Can you all confirm that this works for you?

narfbg avatar Jul 26 '18 18:07 narfbg

FYI, regardless of how it looks like, this has nothing to do with AJAX requests, flash data, (a)synchronicity or anything ... those things just happen to be a trigger in your cases (@digitaloutback, @it-can).

narfbg avatar Jul 26 '18 18:07 narfbg

Cool I’ll test it tomorrow!

it-can avatar Jul 26 '18 20:07 it-can

I checked the solution you proposed. It doesn't work in my case. I have PHP 5.6.34

dkowalskidev avatar Jul 27 '18 06:07 dkowalskidev

@narfbg the fix is also not working for me... But when i use this php5_validate_id() function it works (because it will skip on php7, but PHP < 7 will still have a problem I think)

public function php5_validate_id()
	{
		if (PHP_VERSION_ID < 70000 && isset($_COOKIE[$this->_config['cookie_name']]) && ! $this->validateSessionId($_COOKIE[$this->_config['cookie_name']]))
		{
			unset($_COOKIE[$this->_config['cookie_name']]);
		}
	}

Also I found that commenting out $this->php5_validate_id(); in the session_files_driver.php, makes it work again...

--EDIT--

When commenting out return is_file($this->_file_path.$id); with return true; it also works for me... So could is_file() be a problem? file_exists() also seems to work, but this also returns true is it is a dir......

it-can avatar Jul 27 '18 07:07 it-can

Ok, now this is getting really bizzare ...

@dkowalskidev Does it only now not work on 5.6 or did you have the original issue on 5.6 as well? Also, are you getting the same error messages as @it-can and @digitaloutback?

@it-can Still the same error messages? And can you check what is_file() returns before the error triggers?

narfbg avatar Jul 27 '18 10:07 narfbg

@narfbg Yes, I have the same message error. My issue was on 5.6. When I use file_exists() instead is_file() it works.

dkowalskidev avatar Jul 27 '18 10:07 dkowalskidev

@narfbg same errors still...

it-can avatar Jul 27 '18 12:07 it-can

Mmm found something else...

https://github.com/bcit-ci/CodeIgniter/blob/3.1-stable/system/libraries/Session/Session.php#L108

If i change this:

if (is_php('5.4'))
{
    session_set_save_handler($class, TRUE);
}
else
{
    session_set_save_handler(
        array($class, 'open'),
        array($class, 'close'),
        array($class, 'read'),
        array($class, 'write'),
        array($class, 'destroy'),
        array($class, 'gc')
    );
    register_shutdown_function('session_write_close');
}

to this:

session_set_save_handler(
    array($class, 'open'),
    array($class, 'close'),
    array($class, 'read'),
    array($class, 'write'),
    array($class, 'destroy'),
    array($class, 'gc')
);
register_shutdown_function('session_write_close');

it works again... weird huh...

it-can avatar Jul 27 '18 12:07 it-can

is_php('5.4') is return true.

dkowalskidev avatar Jul 27 '18 12:07 dkowalskidev