-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
incorrect version accept header does not return a 406 response #1101
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,54 +22,78 @@ module Versioner | |
# X-Cascade header to alert Rack::Mount to attempt the next matched | ||
# route. | ||
class Header < Base | ||
VENDOR_VERSION_HEADER_REGEX = | ||
/\Avnd\.([a-z0-9*.]+)(?:-([a-z0-9*\-.]+))?(?:\+([a-z0-9*\-.+]+))?\z/ | ||
|
||
def before | ||
header = rack_accept_header | ||
strict_header_checks if strict? | ||
|
||
if strict? | ||
# If no Accept header: | ||
if header.qvalues.empty? | ||
fail Grape::Exceptions::InvalidAcceptHeader.new('Accept header must be set.', error_headers) | ||
end | ||
# Remove any acceptable content types with ranges. | ||
header.qvalues.reject! do |media_type, _| | ||
Rack::Accept::Header.parse_media_type(media_type).find { |s| s == '*' } | ||
end | ||
# If all Accept headers included a range: | ||
if header.qvalues.empty? | ||
fail Grape::Exceptions::InvalidAcceptHeader.new('Accept header must not contain ranges ("*").', | ||
error_headers) | ||
end | ||
if media_type | ||
media_type_header_handler | ||
elsif headers_contain_wrong_vendor_or_version? | ||
fail_with_invalid_accept_header!('API vendor or version not found.') | ||
end | ||
end | ||
|
||
media_type = header.best_of available_media_types | ||
private | ||
|
||
if media_type | ||
type, subtype = Rack::Accept::Header.parse_media_type media_type | ||
env['api.type'] = type | ||
env['api.subtype'] = subtype | ||
|
||
if /\Avnd\.([a-z0-9*.]+)(?:-([a-z0-9*\-.]+))?(?:\+([a-z0-9*\-.+]+))?\z/ =~ subtype | ||
env['api.vendor'] = Regexp.last_match[1] | ||
env['api.version'] = Regexp.last_match[2] | ||
env['api.format'] = Regexp.last_match[3] # weird that Grape::Middleware::Formatter also does this | ||
end | ||
# If none of the available content types are acceptable: | ||
elsif strict? | ||
fail Grape::Exceptions::InvalidAcceptHeader.new('406 Not Acceptable', error_headers) | ||
# If all acceptable content types specify a vendor or version that doesn't exist: | ||
elsif header.values.all? { |header_value| has_vendor?(header_value) || version?(header_value) } | ||
fail Grape::Exceptions::InvalidAcceptHeader.new('API vendor or version not found.', error_headers) | ||
def strict_header_checks | ||
strict_accept_header_presence_check | ||
strict_verion_vendor_accept_header_presence_check | ||
end | ||
|
||
def strict_accept_header_presence_check | ||
return unless header.qvalues.empty? | ||
fail_with_invalid_accept_header!('Accept header must be set.') | ||
end | ||
|
||
def strict_verion_vendor_accept_header_presence_check | ||
return unless versions.present? | ||
return if an_accept_header_with_version_and_vendor_is_present? | ||
fail_with_invalid_accept_header!('API vendor or version not found.') | ||
end | ||
|
||
def an_accept_header_with_version_and_vendor_is_present? | ||
header.qvalues.keys.any? do |h| | ||
VENDOR_VERSION_HEADER_REGEX =~ h.sub('application/', '') | ||
end | ||
end | ||
|
||
private | ||
def header | ||
@header ||= rack_accept_header | ||
end | ||
|
||
def media_type | ||
@media_type ||= header.best_of(available_media_types) | ||
end | ||
|
||
def media_type_header_handler | ||
type, subtype = Rack::Accept::Header.parse_media_type(media_type) | ||
env['api.type'] = type | ||
env['api.subtype'] = subtype | ||
|
||
if VENDOR_VERSION_HEADER_REGEX =~ subtype | ||
env['api.vendor'] = Regexp.last_match[1] | ||
env['api.version'] = Regexp.last_match[2] | ||
# weird that Grape::Middleware::Formatter also does this | ||
env['api.format'] = Regexp.last_match[3] | ||
end | ||
end | ||
|
||
def fail_with_invalid_accept_header!(message) | ||
fail Grape::Exceptions::InvalidAcceptHeader | ||
.new(message, error_headers) | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Super minor, here I would just |
||
|
||
def available_media_types | ||
available_media_types = [] | ||
|
||
content_types.each do |extension, _media_type| | ||
versions.reverse_each do |version| | ||
available_media_types += ["application/vnd.#{vendor}-#{version}+#{extension}", "application/vnd.#{vendor}-#{version}"] | ||
available_media_types += [ | ||
"application/vnd.#{vendor}-#{version}+#{extension}", | ||
"application/vnd.#{vendor}-#{version}" | ||
] | ||
end | ||
available_media_types << "application/vnd.#{vendor}+#{extension}" | ||
end | ||
|
@@ -83,30 +107,42 @@ def available_media_types | |
available_media_types.flatten | ||
end | ||
|
||
def headers_contain_wrong_vendor_or_version? | ||
header.values.all? do |header_value| | ||
has_vendor?(header_value) || version?(header_value) | ||
end | ||
end | ||
|
||
def rack_accept_header | ||
Rack::Accept::MediaType.new env[Grape::Http::Headers::HTTP_ACCEPT] | ||
rescue RuntimeError => e | ||
raise Grape::Exceptions::InvalidAcceptHeader.new(e.message, error_headers) | ||
fail_with_invalid_accept_header!(e.message) | ||
end | ||
|
||
def versions | ||
options[:versions] || [] | ||
end | ||
|
||
def vendor | ||
options[:version_options] && options[:version_options][:vendor] | ||
version_options && version_options[:vendor] | ||
end | ||
|
||
def strict? | ||
options[:version_options] && options[:version_options][:strict] | ||
version_options && version_options[:strict] | ||
end | ||
|
||
def version_options | ||
options[:version_options] | ||
end | ||
|
||
# By default those errors contain an `X-Cascade` header set to `pass`, which allows nesting and stacking | ||
# of routes (see [Rack::Mount](https://github.com/josh/rack-mount) for more information). To prevent | ||
# this behavior, and not add the `X-Cascade` header, one can set the `:cascade` option to `false`. | ||
# By default those errors contain an `X-Cascade` header set to `pass`, | ||
# which allows nesting and stacking of routes | ||
# (see [Rack::Mount](https://github.com/josh/rack-mount) for more | ||
# information). To prevent # this behavior, and not add the `X-Cascade` | ||
# header, one can set the `:cascade` option to `false`. | ||
def cascade? | ||
if options[:version_options] && options[:version_options].key?(:cascade) | ||
!!options[:version_options][:cascade] | ||
if version_options && version_options.key?(:cascade) | ||
!!version_options[:cascade] | ||
else | ||
true | ||
end | ||
|
@@ -119,14 +155,14 @@ def error_headers | |
# @param [String] media_type a content type | ||
# @return [Boolean] whether the content type sets a vendor | ||
def has_vendor?(media_type) | ||
_, subtype = Rack::Accept::Header.parse_media_type media_type | ||
_, subtype = Rack::Accept::Header.parse_media_type(media_type) | ||
subtype[/\Avnd\.[a-z0-9*.]+/] | ||
end | ||
|
||
# @param [String] media_type a content type | ||
# @return [Boolean] whether the content type sets an API version | ||
def version?(media_type) | ||
_, subtype = Rack::Accept::Header.parse_media_type media_type | ||
_, subtype = Rack::Accept::Header.parse_media_type(media_type) | ||
subtype[/\Avnd\.[a-z0-9*.]+-[a-z0-9*\-.]+/] | ||
end | ||
end | ||
|
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Open an issue with a link to this code to figure out why Grape::Middleware::Formatter does this instead.