Fix UnknownAuthStrategy raised when custom auth strategy inherits from Grape::Middleware::Auth::Base#2674
Merged
ericproulx merged 1 commit intoruby-grape:masterfrom Apr 6, 2026
Conversation
Danger ReportNo issues found. |
Member
Author
|
Needs #2670. |
…m Grape::Middleware::Auth::Base When a custom auth strategy class inherits from Grape::Middleware::Auth::Base and is registered via Grape::Middleware::Auth::Strategies.add, it would raise Grape::Exceptions::UnknownAuthStrategy with a blank strategy name upon being instantiated by StrategyInfo#create. The root cause: PR ruby-grape#2563 moved the strategy lookup from request-time (_call) to initialization-time (initialize). When StrategyInfo#create instantiates the custom class via auth_class.new(app) (without options), the inherited initialize tries to look up options[:type] which is nil, causing the error. The fix: guard the strategy lookup so it only runs when :type is present in options. This preserves compile-time validation for the outer middleware wrapper while allowing subclasses to be instantiated as actual strategies. Fixes ruby-grape#2669. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
b891b0f to
d510262
Compare
ericproulx
approved these changes
Apr 6, 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.
Summary
Fixes #2669.
Problem
PR #2563 moved the strategy lookup in
Grape::Middleware::Auth::Base#initializefrom request-time to initialization-time. However, when a custom auth strategy class inherits fromGrape::Middleware::Auth::Base(as documented in the README and explicitly supported viaGrape::Middleware::Auth::Strategies.add), callingStrategyInfo#createinstantiates that class viaauth_class.new(app)— without anyoptions. The inheritedinitializethen tries to look upoptions[:type]which isnil, causing:(Note the blank strategy name — because
options[:type]isnil.)Fix
Guard the strategy lookup so it only runs when
:typeis present in options. This preserves compile-time validation for the outerGrape::Middleware::Auth::Basemiddleware wrapper while allowing subclasses to be instantiated cleanly as the actual strategy implementations.Test
Added a spec that registers a custom auth middleware class inheriting from
Grape::Middleware::Auth::Baseand verifies it works correctly withauth :custom_tokenin an API.