diff --git a/CHANGELOG.md b/CHANGELOG.md index bfc190af4..0861588fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ See also [the FreshRSS releases](https://github.com/FreshRSS/FreshRSS/releases). ## 2026-1X-XX FreshRSS 1.31.1-dev +* Security + * Minz: Launch actions case-sensitively [#9620](https://github.com/FreshRSS/FreshRSS/pull/9620) ## 2026-11-23 FreshRSS 1.31.0 diff --git a/lib/Minz/Dispatcher.php b/lib/Minz/Dispatcher.php index c83780eb6..e6a9107b7 100644 --- a/lib/Minz/Dispatcher.php +++ b/lib/Minz/Dispatcher.php @@ -100,6 +100,12 @@ final class Minz_Dispatcher { Minz_Exception::ERROR ); } + // PHP class names are case-insensitive, whereas controller names are not + if (!self::isRegistered($base_name) && get_class($controller) !== $controller_name) { + throw new Minz_ControllerNotExistException( + Minz_Exception::ERROR + ); + } $this->controller = $controller; } @@ -111,7 +117,8 @@ final class Minz_Dispatcher { */ private function launchAction(string $action_name): void { $call = [$this->controller, $action_name]; - if (!is_callable($call)) { + // PHP method names are case-insensitive, whereas action names are not + if (!is_callable($call) || !in_array($action_name, get_class_methods($this->controller), true)) { throw new Minz_ActionException( get_class($this->controller), $action_name, diff --git a/lib/lib_rss.php b/lib/lib_rss.php index 1b429bba2..dcac55cdd 100644 --- a/lib/lib_rss.php +++ b/lib/lib_rss.php @@ -76,41 +76,45 @@ if (!defined('WITH_COMPOSER')) { // function classAutoloader(string $class): void { + $include = ''; if (str_starts_with($class, 'FreshRSS')) { $components = explode('_', $class); switch (count($components)) { case 1: - include APP_PATH . '/' . $components[0] . '.php'; - return; + $include = APP_PATH . '/' . $components[0] . '.php'; + break; case 2: - include APP_PATH . '/Models/' . $components[1] . '.php'; - return; + $include = APP_PATH . '/Models/' . $components[1] . '.php'; + break; case 3: //Controllers, Exceptions - include APP_PATH . '/' . $components[2] . 's/' . $components[1] . $components[2] . '.php'; - return; + $include = APP_PATH . '/' . $components[2] . 's/' . $components[1] . $components[2] . '.php'; + break; } } elseif (str_starts_with($class, 'Minz')) { - include LIB_PATH . '/' . str_replace('_', '/', $class) . '.php'; + $include = LIB_PATH . '/' . str_replace('_', '/', $class) . '.php'; } elseif (str_starts_with($class, 'SimplePie\\')) { $prefix = 'SimplePie\\'; $base_dir = LIB_PATH . '/simplepie/simplepie/src/'; $relative_class_name = substr($class, strlen($prefix)); - include $base_dir . str_replace('\\', '/', $relative_class_name) . '.php'; + $include = $base_dir . str_replace('\\', '/', $relative_class_name) . '.php'; } elseif (str_starts_with($class, 'GT\\CssXPath\\')) { $prefix = 'GT\\CssXPath\\'; $base_dir = LIB_PATH . '/phpgt/cssxpath/src/'; $relative_class_name = substr($class, strlen($prefix)); - include $base_dir . str_replace('\\', '/', $relative_class_name) . '.php'; + $include = $base_dir . str_replace('\\', '/', $relative_class_name) . '.php'; } elseif (str_starts_with($class, 'marienfressinaud\\LibOpml\\')) { $prefix = 'marienfressinaud\\LibOpml\\'; $base_dir = LIB_PATH . '/marienfressinaud/lib_opml/src/LibOpml/'; $relative_class_name = substr($class, strlen($prefix)); - include $base_dir . str_replace('\\', '/', $relative_class_name) . '.php'; + $include = $base_dir . str_replace('\\', '/', $relative_class_name) . '.php'; } elseif (str_starts_with($class, 'PHPMailer\\PHPMailer\\')) { $prefix = 'PHPMailer\\PHPMailer\\'; $base_dir = LIB_PATH . '/phpmailer/phpmailer/src/'; $relative_class_name = substr($class, strlen($prefix)); - include $base_dir . str_replace('\\', '/', $relative_class_name) . '.php'; + $include = $base_dir . str_replace('\\', '/', $relative_class_name) . '.php'; + } + if ($include !== '' && is_file($include)) { + include $include; } } diff --git a/tests/lib/Minz/DispatcherTest.php b/tests/lib/Minz/DispatcherTest.php new file mode 100644 index 000000000..7eb275971 --- /dev/null +++ b/tests/lib/Minz/DispatcherTest.php @@ -0,0 +1,82 @@ + */ + public static array $executed = []; + + public function fooAction(): void { + self::$executed[] = Minz_Request::controllerName() . '/' . Minz_Request::actionName(); + $this->view->_layout(null); + $this->view->_path('index/index.phtml'); // Empty view + } +} + +final class DispatcherTest extends \PHPUnit\Framework\TestCase { + + private string $controllerName = ''; + private string $actionName = ''; + + #[\Override] + protected function setUp(): void { + FreshRSS_Context::initSystem(); + $this->controllerName = Minz_Request::controllerName(); + $this->actionName = Minz_Request::actionName(); + FreshRSS_dispatcherTest_Controller::$executed = []; + } + + #[\Override] + protected function tearDown(): void { + Minz_Request::_controllerName($this->controllerName); + Minz_Request::_actionName($this->actionName); + } + + public function testRunExecutesExactNames(): void { + Minz_Request::_controllerName('dispatcherTest'); + Minz_Request::_actionName('foo'); + + ob_start(); + try { + Minz_Dispatcher::getInstance()->run(); + } finally { + ob_end_clean(); + } + + self::assertSame(['dispatcherTest/foo'], FreshRSS_dispatcherTest_Controller::$executed); + } + + /** @return list */ + public static function provideNamesInAnotherLetterCase(): array { + return [ + ['dispatcherTest', 'Foo'], + ['dispatcherTest', 'FOO'], + ['DispatcherTest', 'foo'], + ['DISPATCHERTEST', 'foo'], + ]; + } + + /** + * PHP resolves class and method names case-insensitively, but e.g. `Minz_Request::is()`, + * which extensions use in `Minz_HookType::ActionExecute` to decide about an action, does not. + */ + #[DataProvider('provideNamesInAnotherLetterCase')] + public function testRunRefusesNamesInAnotherLetterCase(string $controllerName, string $actionName): void { + Minz_Request::_controllerName($controllerName); + Minz_Request::_actionName($actionName); + + $exception = null; + ob_start(); + try { + Minz_Dispatcher::getInstance()->run(); + } catch (Minz_ControllerNotExistException | Minz_ActionException $e) { + $exception = $e; + } finally { + ob_end_clean(); + } + + self::assertNotNull($exception); + self::assertSame([], FreshRSS_dispatcherTest_Controller::$executed); + } +}