bug PR-5.0-dev ? Pending

User tests: Successful: Unsuccessful:

avatar Denitz
Denitz
23 Feb 2022

Summary of Changes

Once the session is closed (before write), Joomla\CMS\Session\Storage\JoomlaStorage::close() creates the base64-decoded serialized session registry within cloning the session registry object ($this->data):

	public function close(): void
	{
		// Before storing data to the session, we serialize and encode the Registry
		$_SESSION['joomla'] = base64_encode(serialize(clone $this->data));

		parent::close();
	}

Joomla\Registry\Registry object's magic clone unserializes serialized data:

	public function __clone()
	{
		$this->data = unserialize(serialize($this->data));
	}

This unserialize results in magic wakeup of all objects in the session registry.

Joomla\CMS\User instance in session has the magic wakeup which results in additional load of data.

Basically, we have a chain of User-related magic sleep/wakeup:

  1. User::__sleep() - on Joomla\Registry\Registry::__clone() due to serialize()
  2. User::__wakeup() - on Joomla\Registry\Registry::__clone() due to unserialize()
  3. User::__sleep() - on Joomla\CMS\Session\Storage\JoomlaStorage::close() due to serialize()

This 2nd step is actually an extra wakeup which results in two extra DB queries which don't have any logic because the saved session contains only user ID (see User::__sleep()) and the session read re-loads the data via User::__wakeup().

I don't see any purpose of cloning the session registry before serializing it, any thoughts?

Testing Instructions

  1. Log in Joomla, ensure that Database Type is MySQLi in global config.
  2. Add full logging of SQL queries: edit libraries/vendor/joomla/database/src/DatabaseDriver.php and add after line 639 ($this->executed = $this->statement->execute();):
            file_put_contents(
                JPATH_SITE.'/tmp/'.microtime(true).'.txt',
                $this->sql .
                "\r\n" .
                print_r($bounded, true) .
                "\r\n" .
                print_r(debug_backtrace(2), true)
            );
  1. Load Joomla page
  2. See bunch of files in /tmp folder
  3. Search for these two extra queries in the final files (order files by filename), note the :userid and :muserid bound params as your user ID:
SELECT *
FROM `#__users`
WHERE `id` = :userid
SELECT `g`.`id`,`g`.`title`
FROM `#__usergroups` AS `g`
INNER JOIN `#__user_usergroup_map` AS `m` ON `m`.`group_id` = `g`.`id`
WHERE `m`.`user_id` = :muserid
  1. See the trace in files, these queries are the result of clone/sleep/wakeup described above.
  2. Apply patch
  3. Delete debug files
  4. Re-load Joomla page
  5. See less files (-2) - means no extra queries

Actual result BEFORE applying this Pull Request

Joomla executes extra two database queries on each page load for logged user.

Expected result AFTER applying this Pull Request

No extra queries. Joomla works as before.

Documentation Changes Required

No.

8171176 23 Feb 2022 avatar Denitz fix
avatar Denitz Denitz - open - 23 Feb 2022
avatar Denitz Denitz - change - 23 Feb 2022
Status New Pending
avatar joomla-cms-bot joomla-cms-bot - change - 23 Feb 2022
Category Libraries
avatar prakhar3062
prakhar3062 - comment - 24 Feb 2022

@test

patch works


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/37125.

avatar Denitz Denitz - change - 26 Feb 2022
Labels Added: ?
avatar HLeithner
HLeithner - comment - 27 Jun 2022

This pull request has automatically rebased to 4.2-dev.

avatar joomla-bot
joomla-bot - comment - 27 Jun 2022

This pull requests has been automatically converted to the PSR-12 coding standard.

avatar Denitz Denitz - change - 2 Jul 2022
Labels Added: ?
avatar Denitz Denitz - change - 23 Jul 2022
Labels Added: ?
Removed: ?
avatar Denitz Denitz - change - 24 Mar 2023
Labels Added: ?
Removed: ?
avatar Denitz
Denitz - comment - 24 Mar 2023

Somebody, please restart drone.

avatar Denitz Denitz - change - 25 Mar 2023
Labels Added: PR-4.3-dev
Removed: ?
avatar obuisard obuisard - change - 2 Jun 2023
Title
Remove extra User::__wakeup() on session write
[4.4] Remove extra User::__wakeup() on session write
avatar obuisard obuisard - edited - 2 Jun 2023
avatar obuisard obuisard - change - 9 Jun 2023
Title
[4.4] Remove extra User::__wakeup() on session write
[5.0] Remove extra User::__wakeup() on session write
avatar obuisard obuisard - edited - 9 Jun 2023
avatar ceford
ceford - comment - 13 Sep 2023

Testing this - I must have done something wrong because I ran into a 500 error and reverting the patch manually did not fix it. After much messing about I now have to re-install my test site. In the logging code above $query does not exist in 5.0.0-beta2-dev. My current error starts with:

Exception:
Failed to start application

  at /Users/ceford/Sites/joomla-cms/libraries/src/Factory.php:158
  at Joomla\CMS\Factory::getApplication()
     (/Users/ceford/Sites/joomla-cms/libraries/src/Session/Storage/JoomlaStorage.php:240)

Test this at your peril!


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/37125.

avatar Denitz
Denitz - comment - 13 Sep 2023

@ceford Please advise where did you get 5.0.0-beta2-dev? Can't find this branch.

avatar ceford
ceford - comment - 13 Sep 2023

@ceford Please advise where did you get 5.0.0-beta2-dev? Can't find this branch.

It is what you see in the Admin Title bar when you clone the repo, checkout the 5 branch and follow the install procedure.

avatar Denitz Denitz - change - 13 Sep 2023
Labels Added: bug PR-5.0-dev
Removed: PR-4.3-dev
avatar Denitz
Denitz - comment - 13 Sep 2023

Sorry, can't find the issue, merged 5.0-dev into my branch which is X years old.

avatar Denitz Denitz - change - 15 Sep 2023
The description was changed
avatar Denitz Denitz - edited - 15 Sep 2023
avatar Denitz
Denitz - comment - 15 Sep 2023

@ceford Please sorry for inconvenience, I have found the issue and updated the testing instructions.

avatar ceford ceford - test_item - 16 Sep 2023 - Tested successfully
avatar ceford
ceford - comment - 16 Sep 2023

I have tested this item ✅ successfully on 7632618

I see two fewer queries in the debug bar but four fewer files in my tmp folder. I guess that is OK.


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/37125.

avatar Denitz
Denitz - comment - 16 Sep 2023

Yes, we have minus two useless SQL queries per each page load.
The files in tmp folder are just for our testing purposes to see the real number of queries because Joomla debug is not able to log all database queries.

avatar HLeithner
HLeithner - comment - 18 Sep 2023

@wilsonge @laoneo do you object or have any input on this? (Database queries are expensive so if we can save one it would be good) but I'm not sure about other implications.

avatar Denitz
Denitz - comment - 18 Sep 2023

@HLeithner We can save two queries.

avatar wilsonge
wilsonge - comment - 18 Sep 2023

Needs to be checked from the security implication. This comes from the 3.4.6 security patch when we were trying to protect against malicious session data being used through an old PHP vulnerability (given our minimum version being 7.x I would assume that's been patched out in PHP itself now. Ref: https://www.joomla.org/announcements/release-news/5642-important-security-announcement-pre-release-347.html).

Honestly given how long ago that was I don't remember whether the clone operation was super relevant for that fix or not maybe @SniperSister can remember.

avatar SniperSister
SniperSister - comment - 18 Sep 2023

Honestly given how long ago that was I don't remember whether the clone operation was super relevant for that fix or not

It's not, so the patch is fine from a security perspective

avatar HLeithner
HLeithner - comment - 18 Sep 2023

Then I merge this, thanks all

avatar HLeithner HLeithner - change - 18 Sep 2023
Status Pending Fixed in Code Base
Closed_Date 0000-00-00 00:00:00 2023-09-18 11:13:48
Closed_By HLeithner
avatar HLeithner HLeithner - close - 18 Sep 2023
avatar HLeithner HLeithner - merge - 18 Sep 2023

Add a Comment

Login with GitHub to post a comment