فهرست منبع

Fix infinite redirect loop due to `SCRIPT_NAME` in `PATH_INFO` (#9282)

* Fix infinite redirect loop due to `SCRIPT_NAME` in `PATH_INFO`

* Move to Minz_Request::pathInfo()

---------

Co-authored-by: Alexandre Alapetite <alexandre@alapetite.fr>
Inverle 1 روز پیش
والد
کامیت
2cabd29d1b
4فایلهای تغییر یافته به همراه22 افزوده شده و 4 حذف شده
  1. 1 2
      lib/Minz/FrontController.php
  2. 19 0
      lib/Minz/Request.php
  3. 1 1
      p/api/index.php
  4. 1 1
      p/api/query.php

+ 1 - 2
lib/Minz/FrontController.php

@@ -46,8 +46,7 @@ class Minz_FrontController {
 				empty($url['params']) || !is_array($url['params']) ? [] : $url['params'],
 				array_filter($_POST, 'is_string', ARRAY_FILTER_USE_KEY)
 			);
-			$pathInfo = $_SERVER['PATH_INFO'] ?? $_SERVER['ORIG_PATH_INFO'] ?? '';
-			Minz_Request::forward($url, redirect: $pathInfo !== '');
+			Minz_Request::forward($url, redirect: Minz_Request::pathInfo() !== '');
 		} catch (Minz_Exception $e) {
 			Minz_Log::error($e->getMessage());
 			self::killApp($e->getMessage());

+ 19 - 0
lib/Minz/Request.php

@@ -282,6 +282,25 @@ class Minz_Request {
 		return $remoteIp;
 	}
 
+	/**
+	 * Returns `PATH_INFO` with `SCRIPT_NAME` stripped from the beginning of it,
+	 * if it's there on some shared hosting configurations.
+	 */
+	public static function pathInfo(): string {
+		$pathInfo = $_SERVER['PATH_INFO'] ?? $_SERVER['ORIG_PATH_INFO'] ?? '';
+		if (!is_string($pathInfo)) {
+			$pathInfo = '';
+		}
+		$scriptName = $_SERVER['SCRIPT_NAME'] ?? '';
+		if (!is_string($scriptName)) {
+			$scriptName = '';
+		}
+		if ($pathInfo !== '' && $scriptName !== '' && str_starts_with($pathInfo, $scriptName)) {
+			$pathInfo = substr($pathInfo, strlen($scriptName));
+		}
+		return $pathInfo;
+	}
+
 	/**
 	 * Return true if the request is over HTTPS, false otherwise (HTTP)
 	 */

+ 1 - 1
p/api/index.php

@@ -12,7 +12,7 @@ $frameAncestors = FreshRSS_Context::systemConf()->attributeString('csp.frame-anc
 header("Content-Security-Policy: default-src 'self'; frame-ancestors $frameAncestors");
 header('X-Content-Type-Options: nosniff');
 
-if (($_SERVER['PATH_INFO'] ?? $_SERVER['ORIG_PATH_INFO'] ?? '') !== '') {
+if (Minz_Request::pathInfo() !== '') {
 	// Do not allow trailing slashes
 	header('HTTP/1.1 400 Bad Request');
 	die('Invalid path!');

+ 1 - 1
p/api/query.php

@@ -38,7 +38,7 @@ if (!FreshRSS_Context::hasSystemConf() || !FreshRSS_Context::systemConf()->api_e
 	die('Service Unavailable!');
 }
 
-if (($_SERVER['PATH_INFO'] ?? $_SERVER['ORIG_PATH_INFO'] ?? '') !== '') {
+if (Minz_Request::pathInfo() !== '') {
 	// Do not allow trailing slashes
 	header('HTTP/1.1 400 Bad Request');
 	die('Invalid path!');