Explorar o código

More secure remove access (#9610)

Fix https://github.com/FreshRSS/FreshRSS/security/advisories/GHSA-r27w-hr3c-hjr3
Alexandre Alapetite hai 9 horas
pai
achega
6320ff8af7
Modificáronse 3 ficheiros con 46 adicións e 2 borrados
  1. 1 1
      app/Controllers/userController.php
  2. 2 1
      app/Models/Auth.php
  3. 43 0
      tests/app/Models/AuthTest.php

+ 1 - 1
app/Controllers/userController.php

@@ -709,7 +709,7 @@ class FreshRSS_user_Controller extends FreshRSS_ActionController {
 	 */
 	public function deleteAction(): void {
 		$username = Minz_Request::paramString('username');
-		$self_deletion = Minz_User::name() === $username;
+		$self_deletion = FreshRSS_Auth::hasAccess() && Minz_User::name() === $username;
 
 		if (!FreshRSS_Auth::hasAccess('admin') && !$self_deletion) {
 			Minz_Error::error(403);

+ 2 - 1
app/Models/Auth.php

@@ -186,7 +186,8 @@ class FreshRSS_Auth {
 		if (!Minz_Request::tokenIsOk()) {
 			$username = FreshRSS_Context::systemConf()->default_user;
 		}
-		Minz_User::change($username);
+		// Do not keep the configuration loaded by giveAccess() for the user who has just been refused
+		FreshRSS_Context::initUser($username);
 
 		switch (FreshRSS_Context::systemConf()->auth_type) {
 			case 'form':

+ 43 - 0
tests/app/Models/AuthTest.php

@@ -0,0 +1,43 @@
+<?php
+declare(strict_types=1);
+
+use PHPUnit\Framework\Attributes\PreserveGlobalState;
+use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;
+
+#[RunTestsInSeparateProcesses]
+#[PreserveGlobalState(false)]
+final class AuthTest extends \PHPUnit\Framework\TestCase {
+	private const USERS = ['phpunit-alice', 'phpunit-mallory'];
+
+	#[\Override]
+	protected function setUp(): void {
+		foreach (self::USERS as $user) {
+			mkdir(USERS_PATH . '/' . $user);
+			file_put_contents(USERS_PATH . '/' . $user . '/config.php',
+				'<?php return ' . var_export(['passwordHash' => "hash-$user", 'token' => "token-$user"], true) . ';');
+		}
+	}
+
+	#[\Override]
+	protected function tearDown(): void {
+		foreach (self::USERS as $user) {
+			recursive_unlink(USERS_PATH . '/' . $user);
+		}
+	}
+
+	public function testRefusedSessionWithTokenLoadsConfigurationOfTokenUser(): void {
+		FreshRSS_Context::initSystem();
+		FreshRSS_Context::systemConf()->auth_type = 'form';
+		Minz_Session::init('FreshRSS', volatile: true);
+		Minz_Session::_params([
+			Minz_User::CURRENT_USER => 'phpunit-mallory',
+			'loginOk' => true,
+			'passwordHash' => 'outdated',
+		]);
+		Minz_Request::_params(['user' => 'phpunit-alice', 'token' => 'token-phpunit-alice']);
+
+		self::assertFalse(FreshRSS_Auth::init());
+		self::assertSame('phpunit-alice', Minz_User::name());
+		self::assertSame('hash-phpunit-alice', FreshRSS_Context::userConf()->passwordHash);
+	}
+}