Repository navigation
Replies: 2 comments
|
I think the root cause is the ambiguity between a valid index ( A small fix could be to initialize For example, instead of using $index = $after ? null : count($this->middlewarePriority);
foreach ((array) $existing as $existingMiddleware) {
if (in_array($existingMiddleware, $this->middlewarePriority)) {
$middlewareIndex = array_search($existingMiddleware, $this->middlewarePriority);
if ($after && ($index === null || $middlewareIndex > $index)) {
$index = $middlewareIndex + 1;
} elseif ($after === false && $middlewareIndex < $index) {
$index = $middlewareIndex;
}
}
}Then the final branching could distinguish between "the middleware was not found" ( This should make I’d also add a regression test specifically for a match at index |
|
There is a relevant update in the current framework source: The Useful regression cases are This is a comparison with the current source, not a claim that your installed 13.31.0 already contains the fix. I haven't checked the first release containing it or run the full framework suite; check the installed |
Uh oh!
There was an error while loading. Please reload this page.
Laravel Version
13.31.0
PHP Version
8.4.25
Database Driver & Version
No response
Description
Adding middleware after another entry in the priority list puts it at the end of the list instead, but only when the referenced middleware is the first entry.
EnsureTenantIsSetends up last in the priority list rather than second, so it runs after authentication, throttling and session handling instead of right after the entry it was placed behind.Kernel::addToMiddlewarePriorityRelative()starts the insertion point at0and only moves it when the match sits further along:For a match at position
0the guard is0 > 0, which is false, so$indexstays at its initial value. The branch below then reads$after && $index === 0as "the referenced middleware was not found" and appends.0is serving both as the starting value and as a real position, and the two cases are indistinguishable by the time the branch runs.Starting from
[A, B, C, D]:addToMiddlewarePriorityAfter('A', 'X')A, X, B, C, DA, B, C, D, XaddToMiddlewarePriorityAfter('B', 'X')A, B, X, C, DA, B, X, C, DaddToMiddlewarePriorityAfter('D', 'X')A, B, C, D, XA, B, C, D, XaddToMiddlewarePriorityAfter(['A', 'Missing'], 'X')A, X, B, C, DA, B, C, D, XaddToMiddlewarePriorityBefore('A', 'X')X, A, B, C, DX, A, B, C, DaddToMiddlewarePriorityBefore('D', 'X')A, B, C, X, DA, B, C, X, DOnly "after the first entry" is affected. Every other position is placed correctly, and
addToMiddlewarePriorityBefore()is correct throughout, including for the first entry, because its starting value iscount($this->middlewarePriority)rather than a reachable position.There is no error or warning; the middleware is registered, just in a different place than asked for.
Steps To Reproduce
Expected:
['A', 'X', 'B', 'C']and['A', 'B', 'X', 'C'].Actual:
['A', 'B', 'C', 'X']and['A', 'B', 'X', 'C'].The subclass only exists to keep the reproduction short — it skips the constructor and the router sync, and does not touch the method under test. The same happens through
appendToPriorityList()inbootstrap/app.php, which reachesaddToMiddlewarePriorityAfter()viaApplicationBuilder.All reactions