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

Allows me to override the REMOTE_ADDR too. #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions lib/rack/cloudflare_ip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@

module Rack
class CloudflareIp
def initialize(app)
def initialize(app, override_remote_address: false)
@app = app
@override_remote_address = override_remote_address
end

def call(env)
if env.has_key?("HTTP_CF_CONNECTING_IP")
env["HTTP_ORIGINAL_X_FORWARDED_FOR"] = env["HTTP_X_FORWARDED_FOR"]
env["HTTP_X_FORWARDED_FOR"] = env["HTTP_CF_CONNECTING_IP"]
if @override_remote_address
env["ORIGINAL_REMOTE_ADDR"] = env["REMOTE_ADDR"]
env["REMOTE_ADDR"] = env["HTTP_CF_CONNECTING_IP"]
env["HTTP_ORIGINAL_X_FORWARDED_FOR"] =
env.delete("HTTP_X_FORWARDED_FOR")
else
env["HTTP_ORIGINAL_X_FORWARDED_FOR"] = env["HTTP_X_FORWARDED_FOR"]
env["HTTP_X_FORWARDED_FOR"] = env["HTTP_CF_CONNECTING_IP"]
end
end
@app.call(env)
end
Expand Down
57 changes: 53 additions & 4 deletions spec/rack/cloudflare_ip_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def app

def base_app
lambda { |env|
headers = env.select { |k, _| k =~ /^HTTP_.*/ }
headers = env.select { |k, _| k =~ /^(HTTP_|ORIGINAL_|REMOTE_)/ }
[200, headers, []]
}
end
Expand All @@ -21,17 +21,23 @@ def base_app
context "with HTTP_CF_CONNECTING_IP header" do
let(:headers) { {
"HTTP_CF_CONNECTING_IP" => "123.123.123.123",
"HTTP_X_FORWARDED_FOR" => "234.234.234.234"
"HTTP_X_FORWARDED_FOR" => "234.234.234.234",
"REMOTE_ADDR" => "222.222.222.222",
} }

it "overwrites the HTTP_X_FORWARDED_FOR header" do
expect(last_response.headers["HTTP_X_FORWARDED_FOR"]).to eq("123.123.123.123")
expect(last_response.headers["HTTP_X_FORWARDED_FOR"])
.to eq("123.123.123.123")
end

it "saves the original header in HTTP_ORIGINAL_X_FORWARDED_FOR" do
expect(last_response.headers["HTTP_ORIGINAL_X_FORWARDED_FOR"])
.to eq("234.234.234.234")
end

it "leaves the remote address alone" do
expect(last_response.headers["REMOTE_ADDR"]).to eq("222.222.222.222")
end
end

context "without HTTP_CF_CONNECTING_IP header" do
Expand All @@ -40,7 +46,50 @@ def base_app
} }

it "doesn't modify the HTTP_X_FORWARDED_FOR header" do
expect(last_response.headers["HTTP_X_FORWARDED_FOR"]).to eq("234.234.234.234")
expect(last_response.headers["HTTP_X_FORWARDED_FOR"])
.to eq("234.234.234.234")
end
end

context "with override_remote_address set" do
def app
Rack::CloudflareIp.new(base_app, override_remote_address: true)
end

context "with HTTP_CF_CONNECTING_IP header" do
let(:headers) { {
"HTTP_CF_CONNECTING_IP" => "123.123.123.123",
"REMOTE_ADDR" => "234.234.234.234",
"HTTP_X_FORWARDED_FOR" => "234.234.234.234",
} }

it "overwrites the REMOTE_ADDR env var" do
expect(last_response.headers["REMOTE_ADDR"]).to eq("123.123.123.123")
end

it "saves the original header in ORIGINAL_REMOTE_ADDR" do
expect(last_response.headers["ORIGINAL_REMOTE_ADDR"])
.to eq("234.234.234.234")
end

it "purges the HTTP_X_FORWARDED_FOR header" do
expect(last_response.headers["HTTP_X_FORWARDED_FOR"]).to be_nil
end

it "stashes the old HTTP_X_FORWARDED_FOR in HTTP_ORIGINAL_X_FORWARDED_FOR" do
expect(last_response.headers["HTTP_ORIGINAL_X_FORWARDED_FOR"])
.to eq("234.234.234.234")
end
end

context "without HTTP_CF_CONNECTING_IP header" do
let(:headers) { {
"REMOTE_ADDR" => "234.234.234.234"
} }

it "doesn't modify the HTTP_X_FORWARDED_FOR header" do
expect(last_response.headers["REMOTE_ADDR"]).to eq("234.234.234.234")
end
end
end
end