From 9362c021ba7415f5019ce186fcfc68175f264de7 Mon Sep 17 00:00:00 2001 From: Ryuta Hamasaki Date: Sat, 18 Oct 2025 21:19:40 +0900 Subject: [PATCH] Filter out invalid middleware values --- src/Illuminate/Routing/RouteRegistrar.php | 2 ++ tests/Routing/RouteRegistrarTest.php | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/Illuminate/Routing/RouteRegistrar.php b/src/Illuminate/Routing/RouteRegistrar.php index 52e1ae4d2130..6da02f501fef 100644 --- a/src/Illuminate/Routing/RouteRegistrar.php +++ b/src/Illuminate/Routing/RouteRegistrar.php @@ -121,6 +121,8 @@ public function attribute($key, $value) } if ($key === 'middleware') { + $value = array_filter(Arr::wrap($value)); + foreach ($value as $index => $middleware) { $value[$index] = (string) $middleware; } diff --git a/tests/Routing/RouteRegistrarTest.php b/tests/Routing/RouteRegistrarTest.php index 3f6dd0961f81..34299d0bfe24 100644 --- a/tests/Routing/RouteRegistrarTest.php +++ b/tests/Routing/RouteRegistrarTest.php @@ -127,6 +127,23 @@ public function __toString() $this->assertSame(['one', 'two'], $this->getRoute()->middleware()); } + public function testMiddlewareAsNull() + { + $this->router->middleware(null)->get('users', function () { + return 'all-users'; + }); + + $this->seeResponse('all-users', Request::create('users', 'GET')); + $this->assertSame([], $this->getRoute()->middleware()); + + $this->router->get('users', function () { + return 'all-users'; + })->middleware(null); + + $this->seeResponse('all-users', Request::create('users', 'GET')); + $this->assertSame([], $this->getRoute()->middleware()); + } + public function testWithoutMiddlewareRegistration() { $this->router->middleware(['one', 'two'])->get('users', function () {