Skip to content

Make middleware, callbacks and rescue_from reach the routes declared above them in their scope - #3105

Merged
ericproulx merged 1 commit into
masterfrom
fix/registrations-reach-whole-scope
Nov 27, 2026
Merged

ericproulx merged 1 commit into
masterfrom
fix/registrations-reach-whole-scope

Conversation

@ericproulx

Copy link
Copy Markdown
Contributor

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:

class API < Grape::API
  get(:status) { 'ok' }                       # 200: skipped
  namespace(:admin) { get(:stats) { '...' } } # 401
  mount Users                                 # 401
  http_basic { |username, password| false }
end

On master, every kind of registration splits this way:

declared below the routes route in the same scope route in a namespace above it
http_basic 200, open 401
use skipped runs
before skipped runs
rescue_from raises rescued
helpers (since #3061) reached reached

A misplaced http_basic, use MyAuthMiddleware or before { 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:

  • the middleware (use, insert*, and so auth/http_basic) and the rescue handlers, in Endpoint::RackStack.build(setting, scope:);
  • the callbacks, in 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, version and 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_from and other registrations no longer reach routes defined above them". An API that kept routes open by declaring them above its http_basic now 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:

Grape: since 4.2.0, GET /status also goes through what is declared below it in the same scope: `auth :http_basic` (see UPGRADING.md). Declare those above the route, or in a namespace holding only the routes they should cover.
  • It warns only for a route that a registration newly reaches. A route in a namespace or a mounted API was already reached, so it doesn't warn.
  • 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.
  • The names (use Klass, auth :type, before, rescue_from, …) are only worked out when it warns.
  • Endpoint::RegistrationsBelowWarning and registered_after? are marked for removal in 5.0.

Specs

  • Seven new or flipped specs fail without the change:
    • http_basic protects the routes above it in its scope, and only the top-level route warns;
    • http_basic leaves a sibling namespace's routes alone;
    • use below a route applies to it, also after the ones declared above it;
    • rescue_from below a route applies to it, also alongside the ones above it;
    • a before below a route runs after the ones above it.
  • A guard: format declared below a route still doesn't change it, and nothing warns.
  • Five specs declared a registration below a route that didn't matter to them, and now warned. They now declare it in a namespace of their own, or (in one :grape_exceptions context) use an app other than the subject, whose bare rescue_from :grape_exceptions from the outer before would 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

…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
ericproulx merged commit bdb42e0 into master Nov 27, 2026
27 checks passed
@ericproulx
ericproulx deleted the fix/registrations-reach-whole-scope branch November 27, 2026 13:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Calling .auth after class is loaded does not apply auth to top-level endpoints

2 participants