CVE-2023-38489 (PHP, published 2023-07-27T15:15Z)
Description: value: Kirby is a content management system. A vulnerability in versions prior to 3.5.8.3, 3.6.6.3, 3.7.5.2, 3.8.4.1, and 3.9.6 affects all Kirby sites with user accounts (unless Kirby's API and Panel are disabled in the config). It can only be abused if a Kirby user is logged in on a device or browser that is shared with potentially untrusted users or if an attacker already maliciously used a previous password to log in to a Kirby site as the affected user.

Insufficient Session Expiration is when a web site permits an attacker to reuse old session credentials or session IDs for authorization. In the variation described in this advisory, it allows attackers to stay logged in to a Kirby site on another device even if the logged in user has since changed their password. Kirby did not invalidate user sessions that were created with a password that was since changed by the user or by a site admin. If a user changed their password to lock out an attacker who was already in possession of the previous password or of a login session on another device or browser, the attacker would not be reliably prevented from accessing the Kirby site as the affected user.

The problem has been patched in Kirby 3.5.8.3, 3.6.6.3, 3.7.5.2, 3.8.4.1, and 3.9.6. In all of the mentioned releases, the maintainers have updated the authentication implementation to keep track of the hashed password in each active session. If the password changed since the login, the session is invalidated. To enforce this fix even if the vulnerability was previously abused, all users are logged out from the Kirby site after updating to one of the patched releases.
Severity: nan (CVSS v3 base score: 7.3)
Weakness: CWE-613 \u2014 Insufficient Session Expiration
According to WASC, "Insufficient Session Expiration is when a web site permits an attacker to reuse old session credentials or session IDs for authorization."
Repository: https://github.com/getkirby/kirby
Files changed:
src/Cms/Auth.php
src/Cms/User.php
src/Cms/UserActions.php
tests/Cms/Auth/AuthTest.php
tests/Cms/Users/UserActionsTest.php
tests/Cms/Users/UserAuthTest.php
tests/Cms/Users/UserTest.php
Commit message: Invalidate session on password change

https://github.com/getkirby/kirby/security/advisories/GHSA-5mvj-rvp8-rf45


Vulnerable code:
		if ($user = $this->kirby->users()->find($id)) {
			// in case the session needs to be updated, do it now
			// for better performance
			$session->commit();
			return $user;
		return null;
		return F::read($this->root() . '/.htpasswd');
		return F::write($this->root() . '/.htpasswd', $password);
	protected $fixtures;
				'index' => $this->fixtures = __DIR__ . '/fixtures/AuthTest'
					'password' => password_hash('springfield123', PASSWORD_DEFAULT)
		Dir::make($this->fixtures . '/site/accounts');
		Dir::remove($this->fixtures);
	public function testUserSession1()
	public function testUserSession2()
					$calls++;
				}
		$this->assertSame(2, $calls);

Fixed code:
		// if no user is logged in, return immediately
		// a user is logged in, ensure it exists
		$user = $this->kirby->users()->find($id);
		if ($user === null) {
			return null;
		if ($passwordTimestamp = $user->passwordTimestamp()) {
			$loginTimestamp = $session->data()->get('kirby.loginTimestamp');
			if (is_int($loginTimestamp) !== true) {
				// session that was created before Kirby
				// 3.5.8.3, 3.6.6.3, 3.7.5.2, 3.8.4.1 or 3.9.6
				// or when the user didn't have a password set
				$user->logout();
				return null;
			}

			// invalidate the session if the password
			// changed since the login
			if ($loginTimestamp < $passwordTimestamp) {
				$user->logout();
				return null;
			}
		}

		// in case the session needs to be updated, do it now
		// for better performance
		$session->commit();
		return $user;
		if ($this->passwordTimestamp() !== null) {
			$session->data()->set('kirby.loginTimestamp', time());
		}
		$session->data()->remove('kirby.loginTimestamp');
	/**
	 * Returns the timestamp when the password
	 * was last changed
	 */
	public function passwordTimestamp(): int|null
	{
		$file = $this->passwordFile();

		// ensure we have the latest information
		// to prevent cache attacks
		clearstatcache();

		// user does not have a password
		if (is_file($file) === false) {
			return null;
		}

		return filemtime($file);
	}


	/**
	 * Returns the path to the password file
	 */
	protected function passwordFile(): string
	{
		return $this->root() . '/.htpasswd';
	}
			// keep the user logged in to the current browser
			// if they changed their own password
			// (regenerate the session token, update the login timestamp)
			if ($user->isLoggedIn() === true) {
				$user->loginPasswordless();
			}

		return F::read($this->passwordFile());
		return F::write($this->passwordFile(), $password);
use Kirby\Filesystem\F;
	protected $tmp;
				'index' => $this->tmp = __DIR__ . '/tmp'
					'password' => $hash = password_hash('springfield123', PASSWORD_DEFAULT)
		Dir::make($this->tmp . '/site/accounts/homer');
		F::write($this->tmp . '/site/accounts/homer/.htpasswd', $hash);
		touch($this->tmp . '/site/accounts/homer/.htpasswd', 1337000000);
		Dir::remove($this->tmp);
	public function testUserSession()
	public function testUserSessionManualSession()
		$session->set('kirby.loginTimestamp', 1337000000);
	/**
	 * @covers ::status
	 * @covers ::user
	 */
	public function testUserSessionOldTimestamp()
	{
		$session = $this->app->session();
		$session->set('kirby.userId', 'homer');
		$session->set('kirby.loginTimestamp', 1000000000);

		$this->assertNull($this->auth->user());
		$this->assertSame([
			'challenge' => null,
			'email'     => null,
			'status'    => 'inactive'
		], $this->auth->status()->toArray());

		// user should be logged out completely
		$this->assertSame([], $session->data()->get());
	}

	/**
	 * @covers ::status
	 * @covers ::user
	 */
	public function testUserSessionNoTimestamp()
	{
		$session = $this->app->session();
		$session->set('kirby.userId', 'homer');

		$this->assertNull($this->auth->user());
		$this->assertSame([
			'challenge' => null,
			'email'     => null,
			'status'    => 'inactive'
		], $this->auth->status()->toArray());

		// user should be logged out completely
		$this->assertSame([], $session->data()->get());
	}

		$this->app->session()->destroy();
					$calls += 2;
				},
				'user.login:before' => function () use (&$calls) {
					$calls += 4;
				},
				'user.login:after' => function () use (&$calls) {
					$calls += 8;
				},
		$this->assertSame(3, $calls);
	}

	public function testChangePasswordHooksCurrentUser()
	{
		$calls = 0;
		$phpunit = $this;

		$this->app = $this->app->clone([
			'hooks' => [
				'user.changePassword:before' => function (User $user, $password) use ($phpunit, &$calls) {
					$phpunit->assertEmpty($user->password());
					$phpunit->assertSame('topsecret2018', $password);
					$calls++;
				},
				'user.changePassword:after' => function (User $newUser, User $oldUser) use ($phpunit, &$calls) {
					$phpunit->assertTrue($newUser->validatePassword('topsecret2018'));
					$phpunit->assertEmpty($oldUser->password());
					$calls += 2;
				},
				'user.login:before' => function () use (&$calls) {
					$calls += 4;
				},
				'user.login:after' => function () use (&$calls) {
					$calls += 8;
				},
			]
		]);

		$user = $this->app->user('admin@domain.com');
		$user->changePassword('topsecret2018');

		$this->assertSame(15, $calls);
use Kirby\Filesystem\F;

					'id'    => 'testuser',

	public function testSessionData()
	{
		$user    = $this->app->user('test@getkirby.com');
		$session = $this->app->session();

		$this->assertSame([], $session->data()->get());
		$user->loginPasswordless();
		$this->assertSame(['kirby.userId' => 'testuser'], $session->data()->get());
		$user->logout();
		$this->assertSame([], $session->data()->get());
	}

	public function testSessionDataWithPassword()
	{
		F::write($this->tmp . '/accounts/testuser/.htpasswd', 'a very secure hash');

		$user    = $this->app->user('test@getkirby.com');
		$session = $this->app->session();

		$this->assertSame([], $session->data()->get());
		$user->loginPasswordless();
		$this->assertSame(['kirby.userId' => 'testuser', 'kirby.loginTimestamp' => 1337000000], $session->data()->get());
		$user->logout();
		$this->assertSame([], $session->data()->get());
	}
	public function testPasswordTimestamp()
	{
		$app = new App([
			'roots' => [
				'index'    => $this->tmp,
				'accounts' => $this->tmp
			]
		]);

		// create a user file
		F::write($this->tmp . '/test/index.php', '<?php return [];');

		$user = $app->user('test');
		$this->assertNull($user->passwordTimestamp());

		// create a password file
		F::write($this->tmp . '/test/.htpasswd', 'a very secure hash');
		touch($this->tmp . '/test/.htpasswd', 1337000000);

		$this->assertSame(1337000000, $user->passwordTimestamp());

		// timestamp is not cached
		touch($this->tmp . '/test/.htpasswd', 1338000000);
		$this->assertSame(1338000000, $user->passwordTimestamp());
	}