Explorar el Código

Safer login (#9592)

Avoid using some functions changing state before full verification.
Related to https://github.com/FreshRSS/FreshRSS/pull/9598
Alexandre Alapetite hace 21 horas
padre
commit
13280a4dfc
Se han modificado 4 ficheros con 50 adiciones y 11 borrados
  1. 7 5
      app/Controllers/authController.php
  2. 4 5
      app/Models/Context.php
  3. 9 1
      lib/Minz/User.php
  4. 30 0
      tests/lib/Minz/UserTest.php

+ 7 - 5
app/Controllers/authController.php

@@ -154,20 +154,22 @@ class FreshRSS_auth_Controller extends FreshRSS_ActionController {
 
 			usleep(random_int(100, 10000));	// Primitive mitigation of timing attacks, in μs
 
-			FreshRSS_Context::initUser($username);
-			if (!FreshRSS_Context::hasUserConf()) {
+			// Do not use `FreshRSS_Context::initUser()` before the password is verified
+			$userConf = FreshRSS_user_Controller::checkUsername($username) && FreshRSS_user_Controller::userExists($username) ?
+				FreshRSS_UserConfiguration::getForUser($username) : null;
+			if ($userConf === null) {
 				self::loginFailed(403, _t('feedback.auth.login.invalid'));
 				return;
 			}
 
-			if (!FreshRSS_Context::userConf()->enabled || FreshRSS_Context::userConf()->passwordHash == '') {
+			if (!$userConf->enabled || $userConf->passwordHash == '') {
 				usleep(random_int(100, 5000));	// Primitive mitigation of timing attacks, in μs
 				self::loginFailed(403, _t('feedback.auth.login.invalid'));
 				return;
 			}
 
 			$ok = FreshRSS_FormAuth::checkCredentials(
-				$username, FreshRSS_Context::userConf()->passwordHash, $passwordPlain
+				$username, $userConf->passwordHash, $passwordPlain
 			);
 			if ($ok) {
 				try {
@@ -180,7 +182,7 @@ class FreshRSS_auth_Controller extends FreshRSS_ActionController {
 				// Set session parameters to give access to the user.
 				Minz_Session::_params([
 					Minz_User::CURRENT_USER => $username,
-					'passwordHash' => FreshRSS_Context::userConf()->passwordHash,
+					'passwordHash' => $userConf->passwordHash,
 					'csrf' => false,
 				]);
 				FreshRSS_Auth::giveAccess();

+ 4 - 5
app/Models/Context.php

@@ -109,7 +109,8 @@ final class FreshRSS_Context {
 
 		Minz_Session::lock();
 		if ($username == '') {
-			$username = Minz_User::name() ?? '';
+			// Do not `Minz_User::change()` until we have verified the user
+			$username = trim(Minz_Session::paramString(Minz_User::CURRENT_USER));
 		}
 		if (($username === Minz_User::INTERNAL_USER || FreshRSS_user_Controller::checkUsername($username)) &&
 			(!$userMustExist || FreshRSS_user_Controller::userExists($username))) {
@@ -125,10 +126,8 @@ final class FreshRSS_Context {
 			}
 		}
 		if (FreshRSS_Context::$user_conf == null) {
-			Minz_Session::_params([
-				'loginOk' => false,
-				Minz_User::CURRENT_USER => false,
-			]);
+			Minz_Session::_param('loginOk', false);
+			Minz_User::change();
 		}
 		Minz_Session::unlock();
 

+ 9 - 1
lib/Minz/User.php

@@ -10,11 +10,18 @@ final class Minz_User {
 
 	public const CURRENT_USER = 'currentUser';
 
+	/**
+	 * Name of the user of the current request as last set by `change()` (empty string if cleared), or null if not set yet.
+	 * Kept apart from the session, which is shared with concurrent requests and reloaded at each access:
+	 * another request changing the current user of the session (e.g. login, logout) must not change the user of a running request.
+	 */
+	private static ?string $name = null;
+
 	/**
 	 * @return string the name of the current user, or null if there is none
 	 */
 	public static function name(): ?string {
-		$currentUser = trim(Minz_Session::paramString(Minz_User::CURRENT_USER));
+		$currentUser = self::$name ?? trim(Minz_Session::paramString(Minz_User::CURRENT_USER));
 		return $currentUser === '' ? null : $currentUser;
 	}
 
@@ -23,6 +30,7 @@ final class Minz_User {
 	 */
 	public static function change(string $name = ''): void {
 		$name = trim($name);
+		self::$name = $name;
 		Minz_Session::_param(Minz_User::CURRENT_USER, $name === '' ? false : $name);
 	}
 }

+ 30 - 0
tests/lib/Minz/UserTest.php

@@ -0,0 +1,30 @@
+<?php
+declare(strict_types=1);
+
+use PHPUnit\Framework\Attributes\PreserveGlobalState;
+use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;
+
+#[RunTestsInSeparateProcesses]
+#[PreserveGlobalState(false)]
+final class UserTest extends \PHPUnit\Framework\TestCase {
+
+	public function testNameFollowsSessionUntilChanged(): void {
+		Minz_Session::init('FreshRSS', volatile: true);
+		self::assertNull(Minz_User::name());
+		$_SESSION[Minz_User::CURRENT_USER] = 'alice';
+		self::assertSame('alice', Minz_User::name());
+	}
+
+	public function testNameOfRunningRequestSurvivesSessionChange(): void {
+		Minz_Session::init('FreshRSS', volatile: true);
+		Minz_User::change('alice');
+		self::assertSame('alice', Minz_Session::paramString(Minz_User::CURRENT_USER));
+		// What reloading the session gives when a concurrent request has changed its current user meanwhile
+		$_SESSION[Minz_User::CURRENT_USER] = 'bob';
+		self::assertSame('alice', Minz_User::name());
+
+		Minz_User::change();
+		$_SESSION[Minz_User::CURRENT_USER] = 'bob';
+		self::assertNull(Minz_User::name());
+	}
+}