Skip to content

Commit 1fde828

Browse files
committed
fix(route): restore prefix/middleware state if group() callback throws
1 parent d4d6c88 commit 1fde828

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

src/Route.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,16 @@ public static function group($options, callable $callback): void
210210
self::$prefix = self::$prefix === '' ? $prefix : self::$prefix . '/' . $prefix;
211211
}
212212

213-
call_user_func($callback);
214-
215-
self::$prefix = $oldPrefix;
216-
self::$middlewareIndex = $oldMiddlewareIndex;
213+
try {
214+
call_user_func($callback);
215+
} finally {
216+
// Always restore the previous routing state, even if the callback
217+
// throws. Otherwise an exception inside the group would leave the
218+
// accumulated prefix/middleware behind and leak it into every route
219+
// registered afterwards (state leak).
220+
self::$prefix = $oldPrefix;
221+
self::$middlewareIndex = $oldMiddlewareIndex;
222+
}
217223
}
218224

219225
/**

0 commit comments

Comments
 (0)