Harden mounted-app auth surface and error HTML escaping#2789
Open
ericproulx wants to merge 1 commit into
Open
Conversation
Two security-relevant fixes surfaced by a review of Grape::API: - Grape::Middleware::Error#rack_response only escaped the error message when the response content-type was exactly 'text/html', so a parameterized value such as 'text/html; charset=utf-8' skipped escaping and could reflect an unescaped message into an HTML response. Compare the media type only. - A bare Rack app mounted with `mount` is called directly and never goes through the endpoint middleware stack, so an API's authentication middleware does not wrap it and the mount is reachable unauthenticated. Mounted Grape APIs are unaffected. Warn at compile time when a bare Rack app is mounted under configured authentication so the bypass isn't silent. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
d8ca6bf to
bb8ad5e
Compare
Danger ReportNo issues found. |
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.
Two security-relevant fixes surfaced by a review of
Grape::API.1. Error HTML escaping bypassable via a
charsetparameterGrape::Middleware::Error#rack_responseescaped the error message only when the response content-type was exactlytext/html:A parameterized value such as
text/html; charset=utf-8fails the==check and skips escaping, so a reflected message (e.g. viaerror!,rescue_from, or a validation message) can be returned unescaped into an HTML response. The framework's own default 406 uses a baretext/htmland stayed safe, but the check itself was wrong. It now compares the media type only (parameters stripped).2. Authentication does not wrap mounted bare Rack apps
A bare Rack app mounted with
mountis called directly and never goes through the endpoint middleware stack (@app = config.app || build_stack), so an API's authentication middleware does not wrap it and the mount is reachable unauthenticated:Mounted Grape APIs are unaffected because they rebuild their own stack from the inherited settings. Rather than change the call path (auto-wrapping mounted Rack apps would break apps that mount bare Rack apps expecting passthrough), this warns at compile time when a bare Rack app is mounted under configured authentication, so the bypass isn't silent.
Tests
api_spec+endpoint_spec+error_spec: 573 examples, 0 failures. RuboCop clean.🤖 Generated with Claude Code