Repository navigation
Return 406 Not Acceptable for malformed Accept headers - #181
Merged
Merged
Conversation
jrhoads
requested review from
codycooperross,
digitaldogsbody,
kaysiz,
richardhallett and
svogt0511
July 15, 2026 14:40
kaysiz
approved these changes
Jul 15, 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.
Purpose
Handle malformed or invalid
Acceptheaders so the API returns a clean406 Not Acceptableresponse instead of raising an unhandledActionDispatch::Http::MimeNegotiation::InvalidTypeerror.Approach
Add a custom Rack middleware that intercepts
ActionDispatch::Http::MimeNegotiation::InvalidTypebefore it propagates through the Rails instrumentation stack, and return a short plain-text error message.Key Modifications
config/initializers/catch_invalid_mime_type.rbdefiningCatchInvalidMimeTypeMiddleware.ActionDispatch::DebugExceptions.spec/api/api_spec.rbfor a malformedAcceptheader (application/json, *).Important Technical Details
ActionDispatch::Http::MimeNegotiation::InvalidTypeis raised insideActionController::Instrumentation#process_action, which occurs beforerescue_fromcallbacks run, so a middleware layer is the appropriate place to handle it.[406, { "Content-Type" => "text/plain" }, [RESPONSE_BODY]].DebugExceptionsprevents noisy multi-line backtraces from being logged.ActionDispatch::DebugExceptionsso the exception can be handled cleanly before reaching the debug error page.Types of changes
Reviewer, please remember our guidelines: