Repository navigation
Support RFC 7239 Forwarded header and configurable priorities for checking forwarding headers #1834
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,8 +16,33 @@ module Rack | |
| class Request | ||
| class << self | ||
| attr_accessor :ip_filter | ||
|
|
||
| # The priority when checking forwarded headers. The default | ||
| # is <tt>[:forwarded, :x_forwarded]</tt>, which means, check the | ||
| # +Forwarded+ header first, followed by the appropriate | ||
| # <tt>X-Forwarded-*</tt> header. You can revert the priority by | ||
| # reversing the priority, or remove checking of either | ||
| # or both headers by removing elements from the array. | ||
| # | ||
| # This should be set as appropriate in your environment | ||
| # based on what reverse proxies are in use. If you are not | ||
| # using reverse proxies, you should probably use an empty | ||
| # array. | ||
| attr_accessor :forwarded_priority | ||
|
|
||
| # The priority when checking either the <tt>X-Forwarded-Proto</tt> | ||
| # or <tt>X-Forwarded-Scheme</tt> header for the forwarded protocol. | ||
| # The default is <tt>[:proto, :scheme]</tt>, to try the | ||
| # <tt>X-Forwarded-Proto</tt> header before the | ||
| # <tt>X-Forwarded-Scheme</tt> header. Rack 2 had behavior | ||
| # similar to <tt>[:scheme, :proto]</tt>. You can remove either or | ||
| # both of the entries in array to ignore that respective header. | ||
| attr_accessor :x_forwarded_proto_priority | ||
| end | ||
|
|
||
| @forwarded_priority = [:forwarded, :x_forwarded] | ||
| @x_forwarded_proto_priority = [:proto, :scheme] | ||
|
|
||
| valid_ipv4_octet = /\.(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])/ | ||
|
|
||
| trusted_proxies = Regexp.union( | ||
|
|
@@ -152,6 +177,8 @@ module Helpers | |
| # The contents of the host/:authority header sent to the proxy. | ||
| HTTP_X_FORWARDED_HOST = 'HTTP_X_FORWARDED_HOST' | ||
|
|
||
| HTTP_FORWARDED = 'HTTP_FORWARDED' | ||
|
|
||
| # The value of the scheme sent to the proxy. | ||
| HTTP_X_FORWARDED_SCHEME = 'HTTP_X_FORWARDED_SCHEME' | ||
|
|
||
|
|
@@ -331,7 +358,7 @@ def port | |
| end | ||
|
|
||
| if forwarded_port = self.forwarded_port | ||
| return forwarded_port.first | ||
| return forwarded_port.last | ||
| end | ||
|
|
||
| if scheme = self.scheme | ||
|
|
@@ -344,23 +371,60 @@ def port | |
| end | ||
|
|
||
| def forwarded_for | ||
| if value = get_header(HTTP_X_FORWARDED_FOR) | ||
| split_header(value).map do |authority| | ||
| split_authority(wrap_ipv6(authority))[1] | ||
| forwarded_priority.each do |type| | ||
| case type | ||
| when :forwarded | ||
| if forwarded_for = get_http_forwarded(:for) | ||
| return(forwarded_for.map! do |authority| | ||
| split_authority(authority)[1] | ||
| end) | ||
| end | ||
| when :x_forwarded | ||
| if value = get_header(HTTP_X_FORWARDED_FOR) | ||
| return(split_header(value).map do |authority| | ||
| split_authority(wrap_ipv6(authority))[1] | ||
| end) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| nil | ||
| end | ||
|
|
||
| def forwarded_port | ||
| if value = get_header(HTTP_X_FORWARDED_PORT) | ||
| split_header(value).map(&:to_i) | ||
| forwarded_priority.each do |type| | ||
| case type | ||
| when :forwarded | ||
| if forwarded = get_http_forwarded(:for) | ||
| return(forwarded.map do |authority| | ||
| split_authority(authority)[2] | ||
| end.compact) | ||
| end | ||
| when :x_forwarded | ||
| if value = get_header(HTTP_X_FORWARDED_PORT) | ||
| return split_header(value).map(&:to_i) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| nil | ||
| end | ||
|
|
||
| def forwarded_authority | ||
| if value = get_header(HTTP_X_FORWARDED_HOST) | ||
| wrap_ipv6(split_header(value).first) | ||
| forwarded_priority.each do |type| | ||
| case type | ||
| when :forwarded | ||
| if forwarded = get_http_forwarded(:host) | ||
| return forwarded.last | ||
| end | ||
| when :x_forwarded | ||
| if value = get_header(HTTP_X_FORWARDED_HOST) | ||
| return wrap_ipv6(split_header(value).last) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| nil | ||
| end | ||
|
|
||
| def ssl? | ||
|
|
@@ -372,7 +436,7 @@ def ip | |
| external_addresses = reject_trusted_ip_addresses(remote_addresses) | ||
|
|
||
| unless external_addresses.empty? | ||
| return external_addresses.first | ||
| return external_addresses.last | ||
| end | ||
|
|
||
| if forwarded_for = self.forwarded_for | ||
|
|
@@ -593,6 +657,11 @@ def parse_http_accept_header(header) | |
| end | ||
| end | ||
|
|
||
| # Get an array of values set in the RFC 7239 `Forwarded` request header. | ||
| def get_http_forwarded(token) | ||
| Utils.forwarded_values(get_header(HTTP_FORWARDED))&.[](token) | ||
| end | ||
|
|
||
| def query_parser | ||
| Utils.default_query_parser | ||
| end | ||
|
|
@@ -638,9 +707,33 @@ def reject_trusted_ip_addresses(ip_addresses) | |
| ip_addresses.reject { |ip| trusted_proxy?(ip) } | ||
| end | ||
|
|
||
| FORWARDED_SCHEME_HEADERS = { | ||
| proto: HTTP_X_FORWARDED_PROTO, | ||
| scheme: HTTP_X_FORWARDED_SCHEME | ||
| }.freeze | ||
| private_constant :FORWARDED_SCHEME_HEADERS | ||
| def forwarded_scheme | ||
| allowed_scheme(get_header(HTTP_X_FORWARDED_SCHEME)) || | ||
| allowed_scheme(extract_proto_header(get_header(HTTP_X_FORWARDED_PROTO))) | ||
| forwarded_priority.each do |type| | ||
| case type | ||
|
Member
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. I feel using a case statement here is a little bit cumbersome. Like, we know in advance what code should be executed. Using an array is flexible and could even contain the same entry more than once even thought it doesn't make sense. Thinking longer term, does the symbol list in Does it matter? My main thought is performance. I guess probably not. If it was a big issue, maybe later we can construct a lambda based on the expected behaviour and just cache it? I guess the alternative would be
Contributor
Author
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. I'm not attached to the particular approach used. If we want to change to use
Member
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. Do you have the energy to make that change and do you prefer it? I'm happy to defer to your judgement here.
Contributor
Author
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. I'm fine keeping the |
||
| when :forwarded | ||
| if (forwarded_proto = get_http_forwarded(:proto)) && | ||
| (scheme = allowed_scheme(forwarded_proto.last)) | ||
| return scheme | ||
| end | ||
| when :x_forwarded | ||
| x_forwarded_proto_priority.each do |x_type| | ||
| if header = FORWARDED_SCHEME_HEADERS[x_type] | ||
| split_header(get_header(header)).reverse_each do |scheme| | ||
| if allowed_scheme(scheme) | ||
| return scheme | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| nil | ||
| end | ||
|
|
||
| def allowed_scheme(header) | ||
|
|
@@ -656,6 +749,14 @@ def extract_proto_header(header) | |
| end | ||
| end | ||
| end | ||
|
|
||
| def forwarded_priority | ||
| Request.forwarded_priority | ||
| end | ||
|
|
||
| def x_forwarded_proto_priority | ||
| Request.x_forwarded_proto_priority | ||
| end | ||
| end | ||
|
|
||
| include Env | ||
|
|
||
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.
Would it be possible to clearly mark this change as a breaking change in the changelog of the release where this different behavior was included?
Would it also be possible to backport the fix to the v2 branch?
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.
Do you mean backport this entire PR?