Procházet zdrojové kódy

Delete the stale legacy session cookie when duplicated (#9685)

* Delete the stale legacy session cookie when duplicated

A browser profile from before 1.29.0 can send the legacy session cookie
(explicit path) next to the current one (default path). PHP keeps the
first value, so the stale cookie wins and the CSRF token never matches,
which returns 403 on every action. Expire the legacy cookie when the
request carries the session name twice.

See #9586

* Reduce comments

---------

Co-authored-by: Juupeee <jupeee@users.noreply.github.com>
Co-authored-by: Alexandre Alapetite <alexandre@alapetite.fr>
M Jupri Amin před 10 hodinami
rodič
revize
cc352133f5
2 změnil soubory, kde provedl 64 přidání a 2 odebrání
  1. 24 2
      lib/Minz/Session.php
  2. 40 0
      tests/lib/Minz/SessionTest.php

+ 24 - 2
lib/Minz/Session.php

@@ -54,6 +54,9 @@ class Minz_Session {
 
 		session_name($name);
 
+		// A browser session from before FreshRSS 1.29.0
+		$hasDuplicatedCookie = self::hasDuplicatedSessionCookie($name);
+
 		// Reject an uninitialized (e.g. attacker-supplied) session ID
 		ini_set('session.use_strict_mode', '1');
 
@@ -62,6 +65,11 @@ class Minz_Session {
 		session_write_close();
 		// Use cookie only the first time the session is started to avoid resending HTTP headers
 		ini_set('session.use_cookies', '0');
+
+		if ($hasDuplicatedCookie) {
+			// Must be done after session_start(), which replaces the pending Set-Cookie header
+			self::deleteLegacyCookie($name);
+		}
 	}
 
 	/**
@@ -199,14 +207,28 @@ class Minz_Session {
 		return $cookie_dir;
 	}
 
-	/** Delete legacy cookie (before 1.29.0) if it exists */
-	public static function deleteLegacyCookie(string $name): void {
+	/**
+	 * Delete legacy cookie (before 1.29.0) if it exists
+	 * @return bool true if the cookie has been expired
+	 */
+	public static function deleteLegacyCookie(string $name): bool {
 		if (isset($_COOKIE[$name])) {
 			$legacyDir = self::getLegacyCookieDir();
 			if ($legacyDir !== '' && $legacyDir !== '/') {
 				setcookie($name, '', ['expires' => 1, 'path' => $legacyDir]);
+				return true;
 			}
 		}
+		return false;
+	}
+
+	/**
+	 * Whether the request carries the session cookie twice, which happens when a browser profile
+	 * has the legacy cookie next to the current one with two different paths.
+	 */
+	protected static function hasDuplicatedSessionCookie(string $name): bool {
+		$header = $_SERVER['HTTP_COOKIE'] ?? null;
+		return is_string($header) && substr_count($header, $name . '=') > 1;
 	}
 
 	/**

+ 40 - 0
tests/lib/Minz/SessionTest.php

@@ -44,4 +44,44 @@ final class SessionTest extends \PHPUnit\Framework\TestCase {
 		$this->expectException(RuntimeException::class);
 		Minz_Session::regenerateID('FreshRSS');
 	}
+
+	public function testDeleteLegacyCookie(): void {
+		$requestUri = $_SERVER['REQUEST_URI'] ?? null;
+		$cookie = $_COOKIE['FreshRSS'] ?? null;
+
+		$_SERVER['REQUEST_URI'] = '/i/index.php';
+		unset($_COOKIE['FreshRSS']);
+		self::assertFalse(Minz_Session::deleteLegacyCookie('FreshRSS'));
+
+		$_COOKIE['FreshRSS'] = 'stale';
+		self::assertTrue(Minz_Session::deleteLegacyCookie('FreshRSS'));
+
+		if ($cookie === null) {
+			unset($_COOKIE['FreshRSS']);
+		} else {
+			$_COOKIE['FreshRSS'] = $cookie;
+		}
+		if ($requestUri === null) {
+			unset($_SERVER['REQUEST_URI']);
+		} else {
+			$_SERVER['REQUEST_URI'] = $requestUri;
+		}
+	}
+
+	public function testDetectDuplicatedSessionCookie(): void {
+		$hasDuplicatedSessionCookie = new ReflectionMethod(Minz_Session::class, 'hasDuplicatedSessionCookie');
+		$header = $_SERVER['HTTP_COOKIE'] ?? null;
+
+		$_SERVER['HTTP_COOKIE'] = 'FreshRSS_login=token; FreshRSS=only-one';
+		self::assertFalse($hasDuplicatedSessionCookie->invoke(null, 'FreshRSS'));
+
+		$_SERVER['HTTP_COOKIE'] = 'FreshRSS=stale; FreshRSS_login=token; FreshRSS=current';
+		self::assertTrue($hasDuplicatedSessionCookie->invoke(null, 'FreshRSS'));
+
+		if ($header === null) {
+			unset($_SERVER['HTTP_COOKIE']);
+		} else {
+			$_SERVER['HTTP_COOKIE'] = $header;
+		}
+	}
 }