Skip to content
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

Cloudfront forwarded proto header #2089

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ All notable changes to this project will be documented in this file. For info on
- Add fallback lookup and deprecation warning for obsolete status symbols. ([#2137](https://github.com/rack/rack/pull/2137), [@wtn])
- In `Rack::Files`, ignore the `Range` header if served file is 0 bytes. ([#2159](https://github.com/rack/rack/pull/2159), [@zarqman])
- rack.early_hints is now officially supported as an optional feature (already implemented by Unicorn, Puma, and Falcon). ([#1831](https://github.com/rack/rack/pull/1831), [@casperisfine, @jeremyevans])
- - Add `Rack::SetXForwardedProtoHeader` middleware ([#2089](https://github.com/rack/rack/pull/2089), [@tomharvey])

## [3.0.11] - 2024-05-10

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ middleware:
the request.
* `Rack::Sendfile` for working with web servers that can use optimized file
serving for file system paths.
* `Rack::SetXForwardedProtoHeader` for using a vendor managed proxy which provides
X-Forwarded-Proto like headers.
* `Rack::ShowException` for catching unhandled exceptions and presenting them in
a nice and helpful way with clickable backtrace.
* `Rack::ShowStatus` for using nice error pages for empty client error
Expand Down
1 change: 1 addition & 0 deletions lib/rack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ module Rack
autoload :RewindableInput, "rack/rewindable_input"
autoload :Runtime, "rack/runtime"
autoload :Sendfile, "rack/sendfile"
autoload :SetXForwardedProtoHeader, "rack/set_x_forwarded_proto_header"
autoload :ShowExceptions, "rack/show_exceptions"
autoload :ShowStatus, "rack/show_status"
autoload :Static, "rack/static"
Expand Down
32 changes: 32 additions & 0 deletions lib/rack/set_x_forwarded_proto_header.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

module Rack

# Middleware to set the X-Forwarded-Proto header to the value
# of another header.
#
# This header can be used to ensure the scheme matches when comparing
# request.origin and request.base_url for CSRF checking, but Rack
# expects that value to be in the X_FORWARDED_PROTO header.
#
# Example Rails usage:
# If you use a vendor managed proxy or CDN which sends the proto in a header add
#`config.middleware.use Rack::SetXForwardedProtoHeader, 'Vendor-Forwarded-Proto-Header'`
# to your application.rb file

class SetXForwardedProtoHeader
def initialize(app, vendor_forwarded_header)
@app = app
# Rack expects to see UPPER_UNDERSCORED_HEADERS, never SnakeCased-Dashed-Headers
@vendor_forwarded_header = "HTTP_#{vendor_forwarded_header.upcase.gsub "-", "_"}"
end

def call(env)
if value = env[@vendor_forwarded_header]
env["HTTP_X_FORWARDED_PROTO"] = value
end
@app.call(env)
end

end
end
52 changes: 52 additions & 0 deletions test/spec_set_x_forwarded_proto_header.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# frozen_string_literal: true

require_relative 'helper'

separate_testing do
require_relative '../lib/rack/set_x_forwarded_proto_header'
require_relative '../lib/rack/lint'
require_relative '../lib/rack/mock_request'
end


describe Rack::SetXForwardedProtoHeader do
response = lambda {|e| [200, {}, []] }
jeremyevans marked this conversation as resolved.
Show resolved Hide resolved

it "leaves the value of X_FORWARDED_PROTO intact if there is no vendor header passed in the request" do
vendor_forwarded_header = "not passed in the request"
env = Rack::MockRequest.env_for("/", "HTTP_X_FORWARDED_PROTO" => "http")

Rack::Lint.new(Rack::SetXForwardedProtoHeader.new(response, vendor_forwarded_header)).call env

env["HTTP_X_FORWARDED_PROTO"].must_equal "http"
end

it "does not set X-Forwarded-Proto when there is no vendor header passed in the request" do
vendor_forwarded_header = "not passed in the request"
env = Rack::MockRequest.env_for("/", "FOO" => "bar")

Rack::Lint.new(Rack::SetXForwardedProtoHeader.new(response, vendor_forwarded_header)).call env

env["FOO"].must_equal "bar"
assert_nil(env["HTTP_X_FORWARDED_PROTO"])
end


it "copies the value of the header to X-Forwarded-Proto" do
env = Rack::MockRequest.env_for("/", "HTTP_VENDOR_FORWARDED_PROTO_HEADER" => "https")

Rack::Lint.new(Rack::SetXForwardedProtoHeader.new(response, "Vendor-Forwarded-Proto-Header")).call env

env["HTTP_X_FORWARDED_PROTO"].must_equal "https"
end

it "copies the value of the header to X-Forwarded-Proto overwriting an existing X-Forwarded-Proto" do
env = Rack::MockRequest.env_for("/", "HTTP_VENDOR_FORWARDED_PROTO_HEADER" => "https", "HTTP_X_FORWARDED_PROTO" => "http")

Rack::Lint.new(Rack::SetXForwardedProtoHeader.new(response, "Vendor-Forwarded-Proto-Header")).call env

env["HTTP_X_FORWARDED_PROTO"].must_equal "https"
end

tomharvey marked this conversation as resolved.
Show resolved Hide resolved

end