rack-neverlocal 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/Rakefile +1 -0
- data/lib/rack-neverlocal.rb +1 -0
- data/lib/rack/neverlocal.rb +15 -0
- data/lib/rack/neverlocal/remove_local_host.rb +21 -0
- data/lib/rack/neverlocal/version.rb +5 -0
- data/rack-neverlocal.gemspec +20 -0
- data/scenarios/env_sets_http_x_real_ip_spec.rb +63 -0
- data/spec/rack/neverlocal/remove_local_host_spec.rb +59 -0
- data/spec/rack/neverlocal_spec.rb +35 -0
- data/spec/spec_helper.rb +3 -0
- metadata +91 -0
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--format documentation
|
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1 @@
|
|
1
|
+
require "rack/neverlocal"
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Rack
|
2
|
+
class NeverLocal
|
3
|
+
module RemoveLocalHost
|
4
|
+
module_function
|
5
|
+
|
6
|
+
def from(env)
|
7
|
+
detect_localhost(env) and (transform_from('HTTP_X_REAL_IP',env) or transform_from('HTTP_X_CLUSTER_CLIENT_IP',env))
|
8
|
+
env
|
9
|
+
end
|
10
|
+
|
11
|
+
def transform_from(key,env)
|
12
|
+
env[key] and env['REMOTE_ADDR'] = env[key]
|
13
|
+
end
|
14
|
+
|
15
|
+
def detect_localhost(env)
|
16
|
+
['::1','127.0.0.1'].include? env['REMOTE_ADDR']
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "rack/neverlocal/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "rack-neverlocal"
|
7
|
+
s.version = Rack::NeverLocal::VERSION
|
8
|
+
s.authors = ["Jon Rowe"]
|
9
|
+
s.email = ["hello@jonrowe.co.uk"]
|
10
|
+
s.homepage = "https://github.com/jonrowe/rack-neverlocal"
|
11
|
+
s.summary = %q{What's this? A local request? Never!}
|
12
|
+
s.description = %q{Basically this gems aim is to strip 127.0.0.1 from REMOTE_ADDR to make Rails think it's a remote request... always...}
|
13
|
+
|
14
|
+
s.files = `git ls-files`.split("\n")
|
15
|
+
s.test_files = `git ls-files -- {spec}/*`.split("\n")
|
16
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
|
19
|
+
s.add_development_dependency "rspec"
|
20
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class FakeApp
|
4
|
+
def call(env)
|
5
|
+
@env = env
|
6
|
+
end
|
7
|
+
attr_reader :env
|
8
|
+
end
|
9
|
+
|
10
|
+
|
11
|
+
describe "server sends client in" do
|
12
|
+
let(:app) { FakeApp.new }
|
13
|
+
let(:middleware) { Rack::NeverLocal.new(app) }
|
14
|
+
|
15
|
+
context "HTTP_X_REAL_IP" do
|
16
|
+
let(:env) do
|
17
|
+
{
|
18
|
+
'HTTP_X_REAL_IP' => "212.123.212.123",
|
19
|
+
'REMOTE_ADDR' => "127.0.0.1"
|
20
|
+
}
|
21
|
+
end
|
22
|
+
|
23
|
+
subject { middleware.call(env) }
|
24
|
+
|
25
|
+
it "sends the app the correct REMOTE_ADDR" do
|
26
|
+
subject
|
27
|
+
app.env['REMOTE_ADDR'].should == "212.123.212.123"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context "HTTP_X_CLUSTER_CLIENT_IP" do
|
32
|
+
let(:env) do
|
33
|
+
{
|
34
|
+
'HTTP_X_CLUSTER_CLIENT_IP' => "212.123.212.123",
|
35
|
+
'REMOTE_ADDR' => "127.0.0.1"
|
36
|
+
}
|
37
|
+
end
|
38
|
+
|
39
|
+
subject { middleware.call(env) }
|
40
|
+
|
41
|
+
it "sends the app the correct REMOTE_ADDR" do
|
42
|
+
subject
|
43
|
+
app.env['REMOTE_ADDR'].should == "212.123.212.123"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context "both" do
|
48
|
+
let(:env) do
|
49
|
+
{
|
50
|
+
'HTTP_X_REAL_IP' => "212.123.212.123",
|
51
|
+
'HTTP_X_CLUSTER_CLIENT_IP' => "212.123.111.111",
|
52
|
+
'REMOTE_ADDR' => "127.0.0.1"
|
53
|
+
}
|
54
|
+
end
|
55
|
+
|
56
|
+
subject { middleware.call(env) }
|
57
|
+
|
58
|
+
it "sends the app the correct REMOTE_ADDR" do
|
59
|
+
subject
|
60
|
+
app.env['REMOTE_ADDR'].should == "212.123.212.123"
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Rack
|
4
|
+
class NeverLocal
|
5
|
+
describe RemoveLocalHost do
|
6
|
+
describe '.from' do
|
7
|
+
|
8
|
+
subject { RemoveLocalHost.from env }
|
9
|
+
|
10
|
+
context "not a local ip" do
|
11
|
+
let(:env) { { 'REMOTE_ADDR' => '192.168.1.1' } }
|
12
|
+
it { should == env }
|
13
|
+
end
|
14
|
+
|
15
|
+
context "127.0.0.1" do
|
16
|
+
let(:env) { { 'REMOTE_ADDR' => '127.0.0.1', 'HTTP_X_REAL_IP' => '192.168.1.1' } }
|
17
|
+
its(['REMOTE_ADDR']) { should_not == '127.0.0.1' }
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
describe 'transform_from(key,env)' do
|
23
|
+
subject { RemoveLocalHost.transform_from 'KEY', env }
|
24
|
+
|
25
|
+
context "key present" do
|
26
|
+
let(:env) { { 'KEY' => 'VALUE' } }
|
27
|
+
|
28
|
+
it "replaces remote addr with value" do
|
29
|
+
subject
|
30
|
+
env['REMOTE_ADDR'].should == 'VALUE'
|
31
|
+
end
|
32
|
+
end
|
33
|
+
context "key missing" do
|
34
|
+
let(:env) { { 'NOT_KEY' => 'VALUE' } }
|
35
|
+
specify { should be_nil }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe 'detect_localhost(env)' do
|
40
|
+
subject { RemoveLocalHost.detect_localhost(env) }
|
41
|
+
|
42
|
+
context "not a local ip" do
|
43
|
+
let(:env) { { 'REMOTE_ADDR' => '192.168.1.1' } }
|
44
|
+
it { should be_false }
|
45
|
+
end
|
46
|
+
|
47
|
+
context "127.0.0.1" do
|
48
|
+
let(:env) { { 'REMOTE_ADDR' => '127.0.0.1' } }
|
49
|
+
it { should be_true }
|
50
|
+
end
|
51
|
+
|
52
|
+
context "127.0.0.1" do
|
53
|
+
let(:env) { { 'REMOTE_ADDR' => '::1' } }
|
54
|
+
it { should be_true }
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Rack
|
4
|
+
describe NeverLocal do
|
5
|
+
describe "is rack middleware" do
|
6
|
+
let(:app) { mock "app", :call => response }
|
7
|
+
|
8
|
+
let(:response) { mock "response" }
|
9
|
+
|
10
|
+
let(:env) { { 'REMOTE_ADDR' => 'LOCAL' } }
|
11
|
+
let(:transformed_env) { { 'REMOTE_ADDR' => 'notlocal' } }
|
12
|
+
|
13
|
+
before { NeverLocal::RemoveLocalHost.stub(:from).with(env).and_return(transformed_env) }
|
14
|
+
|
15
|
+
it "takes an app" do
|
16
|
+
NeverLocal.new app
|
17
|
+
end
|
18
|
+
|
19
|
+
subject { NeverLocal.new(app).call(env) }
|
20
|
+
|
21
|
+
it "transforms the environment to strip out local addresses" do
|
22
|
+
NeverLocal::RemoveLocalHost.should_receive(:from).with(env).and_return(transformed_env)
|
23
|
+
subject
|
24
|
+
end
|
25
|
+
|
26
|
+
it "chains call onto app" do
|
27
|
+
app.should_receive(:call).with(transformed_env)
|
28
|
+
subject
|
29
|
+
end
|
30
|
+
|
31
|
+
it("outputs response") { should == response }
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-neverlocal
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Jon Rowe
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-10-22 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
version: "0"
|
32
|
+
type: :development
|
33
|
+
version_requirements: *id001
|
34
|
+
description: Basically this gems aim is to strip 127.0.0.1 from REMOTE_ADDR to make Rails think it's a remote request... always...
|
35
|
+
email:
|
36
|
+
- hello@jonrowe.co.uk
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files: []
|
42
|
+
|
43
|
+
files:
|
44
|
+
- .gitignore
|
45
|
+
- .rspec
|
46
|
+
- Gemfile
|
47
|
+
- Rakefile
|
48
|
+
- lib/rack-neverlocal.rb
|
49
|
+
- lib/rack/neverlocal.rb
|
50
|
+
- lib/rack/neverlocal/remove_local_host.rb
|
51
|
+
- lib/rack/neverlocal/version.rb
|
52
|
+
- rack-neverlocal.gemspec
|
53
|
+
- scenarios/env_sets_http_x_real_ip_spec.rb
|
54
|
+
- spec/rack/neverlocal/remove_local_host_spec.rb
|
55
|
+
- spec/rack/neverlocal_spec.rb
|
56
|
+
- spec/spec_helper.rb
|
57
|
+
homepage: https://github.com/jonrowe/rack-neverlocal
|
58
|
+
licenses: []
|
59
|
+
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
hash: 3
|
71
|
+
segments:
|
72
|
+
- 0
|
73
|
+
version: "0"
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
hash: 3
|
80
|
+
segments:
|
81
|
+
- 0
|
82
|
+
version: "0"
|
83
|
+
requirements: []
|
84
|
+
|
85
|
+
rubyforge_project:
|
86
|
+
rubygems_version: 1.8.6
|
87
|
+
signing_key:
|
88
|
+
specification_version: 3
|
89
|
+
summary: What's this? A local request? Never!
|
90
|
+
test_files: []
|
91
|
+
|