Repository navigation
Make middleware, callbacks and rescue_from reach the routes declared above them in their scope - #3105
Merged
Merged
Conversation
…above them in their scope An endpoint is built from a point-in-time copy of its scope's settings, which links to the enclosing scopes live. So a `use`, `http_basic`, callback or `rescue_from` declared below a route skipped that route, yet reached every route of the namespaces and mounted APIs declared above it. An `http_basic` below a top-level route left only that route open (#1331). Since #3061 the endpoint keeps the live settings of its scope, and reads its helpers from them when it compiles. It now reads its middleware, callbacks and rescue handlers from them too, so each reaches every route of its scope wherever it is declared, as helpers do. So does the warning about a bare Rack app mounted under authentication. Settings such as `format`, `prefix` and `version` are still read from the copy. For routes of the same scope this reverses the 4.0.0 behavior from #2841, and it breaks an API that kept routes open by declaring them above its `http_basic`. UPGRADING describes the change and how to keep such routes open: declare the registration in a namespace that holds only the routes it should cover. For most kinds of registration nothing else would tell, so until 5.0 the endpoint warns when it compiles. Its settings copy still holds what the route reached before, so it warns only about a route that a registration newly reaches, and names what was declared below it. It counts each store's entries rather than comparing them, so a route that gains nothing compiles without allocating for the check. The warning lives in Endpoint::RegistrationsBelowWarning, to be deleted with InheritableSetting#registered_after? in 5.0. The four specs that pinned the old behavior now pin the new one. Five specs declared a registration below a route that didn't matter to them; they now declare it in a namespace of their own, or on an app other than the subject. Fixes #1331 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ericproulx
force-pushed
the
fix/registrations-reach-whole-scope
branch
from
November 25, 2026 13:26
7abf52d to
ac6ba00
Compare
dblock
approved these changes
Nov 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1331.
The bug
An endpoint is built from a point-in-time copy of its scope's settings, and that copy links to the enclosing scopes live. So whether a registration declared below a route reached it depended on where the route was declared:
On master, every kind of registration splits this way:
http_basicusebeforerescue_fromhelpers(since #3061)A misplaced
http_basic,use MyAuthMiddlewareorbefore { authenticate! }left exactly the routes declared directly in the scope open, and nothing said so.Change
Since #3061 an endpoint keeps the live settings of its scope (
@scope_setting), and reads its helpers from them at compile. It now also reads these from them:use,insert*, and soauth/http_basic) and the rescue handlers, inEndpoint::RackStack.build(setting, scope:);build_callbacks;auth, for the existing warning about a bare Rack app mounted under authentication.So each of these reaches every route of its scope, wherever it is declared in it, as helpers do. A sibling namespace is still not reached.
format,prefix,versionand the other settings are still read from the route's copy.Contract break
For routes of the same scope this reverses the 4.0.0 behavior from #2841, documented in UPGRADING as "
use,helpers,rescue_fromand other registrations no longer reach routes defined above them". An API that kept routes open by declaring them above itshttp_basicnow gets 401s on them. UPGRADING has a new entry at the top of 4.2.0 with the fix: declare the registration in a namespace that holds only the routes it should cover. The README now says registrations reach every route of their scope.Warning until 5.0
Most of these changes are silent, so as with #3061, the endpoint warns when it compiles:
InheritableSetting#registered_after?(copy, keys)compares how many entries the scope's own stores and the copy's hold. A route that gains nothing compiles without allocating for the check.use Klass,auth :type,before,rescue_from, …) are only worked out when it warns.Endpoint::RegistrationsBelowWarningandregistered_after?are marked for removal in 5.0.Specs
http_basicprotects the routes above it in its scope, and only the top-level route warns;http_basicleaves a sibling namespace's routes alone;usebelow a route applies to it, also after the ones declared above it;rescue_frombelow a route applies to it, also alongside the ones above it;beforebelow a route runs after the ones above it.formatdeclared below a route still doesn't change it, and nothing warns.:grape_exceptionscontext) use an app other than the subject, whose barerescue_from :grape_exceptionsfrom the outerbeforewould otherwise replace the context's handler.bundle exec rake: no offenses, 3354 examples, 0 failures, and no warnings from the suite.🤖 Generated with Claude Code