rack-ensure_proper_host 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ .DS_Store
4
+ Gemfile.lock
5
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "http://rubygems.org"
2
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,27 @@
1
+ Copyright (c) 2011 - 2012 Spencer Steffen and Citrus Media Group.
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without modification,
5
+ are permitted provided that the following conditions are met:
6
+
7
+ * Redistributions of source code must retain the above copyright notice,
8
+ this list of conditions and the following disclaimer.
9
+
10
+ * Redistributions in binary form must reproduce the above copyright notice,
11
+ this list of conditions and the following disclaimer in the documentation
12
+ and/or other materials provided with the distribution.
13
+
14
+ * Neither the name of Citrus Media Group nor the names of its
15
+ contributors may be used to endorse or promote products derived from this
16
+ software without specific prior written permission.
17
+
18
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
22
+ ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25
+ ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,98 @@
1
+ # Rack::EnsureProperHost [![Build Status](https://secure.travis-ci.org/citrus/rack-ensure_proper_host.png)](http://travis-ci.org/citrus/rack-ensure_proper_host)
2
+
3
+ Inspired by [rack-no-www](https://github.com/logicaltext/rack-no-www).
4
+
5
+ ------------------------------------------------------------------------------
6
+ Usage
7
+ ------------------------------------------------------------------------------
8
+
9
+ All of these examples will redirect all requests from *.example.com to example.com. Requests to secure.example.com will remain untouched.
10
+
11
+ Rack::EnsureProperHost is middleware. Use it like this in your rack applications:
12
+
13
+
14
+ ### Rails
15
+
16
+ ```ruby
17
+ # config/application.rb
18
+ module MyApp
19
+ class Application < Rails::Application
20
+
21
+ config.middleware.insert_before Rack::Lock, Rack::EnsureProperHost, %w(example.com secure.example.com)
22
+
23
+ end
24
+ end
25
+ ```
26
+
27
+ ### Sinatra
28
+
29
+ ```ruby
30
+ require 'sinatra'
31
+ require 'rack/ensure_proper_host'
32
+
33
+ use Rack::EnsureProperHost, %w(example.com secure.example.com)
34
+
35
+ get '/hello' do
36
+ 'Hello World'
37
+ end
38
+ ```
39
+
40
+ ### Rack
41
+
42
+ Add the following to your `config.ru`
43
+
44
+ ```ruby
45
+ # config.ru
46
+ require 'your_app.rb'
47
+ require 'rack/ensure_proper_host'
48
+
49
+ use Rack::EnsureProperHost, %w(example.com secure.example.com)
50
+ run YourApp.new
51
+ ```
52
+
53
+
54
+
55
+ ------------------------------------------------------------------------------
56
+ Installation
57
+ ------------------------------------------------------------------------------
58
+
59
+ As usual, just use the `gem install` command:
60
+
61
+ ```bash
62
+ (sudo) gem install rack-ensure_proper_host
63
+ ```
64
+
65
+ Or add Rack::EnsureProperHost as a gem in your Gemfile:
66
+
67
+ ```bash
68
+ gem 'rack-ensure_proper_host', '~> 0.1.0'
69
+ ```
70
+
71
+ Then run `bundle install`
72
+
73
+
74
+ ------------------------------------------------------------------------------
75
+ Testing
76
+ ------------------------------------------------------------------------------
77
+
78
+ Testing is done with minitest. Run the tests with:
79
+
80
+ ```bash
81
+ rake
82
+ ```
83
+
84
+
85
+ ------------------------------------------------------------------------------
86
+ Changelog
87
+ ------------------------------------------------------------------------------
88
+
89
+ **2012/1/20 - v0.1.0**
90
+
91
+ - it exists!
92
+
93
+
94
+ ------------------------------------------------------------------------------
95
+ License
96
+ ------------------------------------------------------------------------------
97
+
98
+ Copyright (c) 2011 - 2012 Spencer Steffen & Citrus, released under the New BSD License All rights reserved.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.pattern = "**/*_test.rb"
6
+ end
7
+
8
+ task :default => [ :test ]
@@ -0,0 +1 @@
1
+ require "rack/ensure_proper_host"
@@ -0,0 +1,28 @@
1
+ require "rack/ensure_proper_host/version"
2
+
3
+ module Rack
4
+ class EnsureProperHost
5
+
6
+ def initialize(app, *hosts)
7
+ @app = app
8
+ @hosts = hosts.flatten
9
+ end
10
+
11
+ def call(env)
12
+ @host = env['HTTP_HOST']
13
+ if @hosts.include?(@host)
14
+ @app.call(env)
15
+ else
16
+ [ 301, request_with_proper_host(env), [ "Moved Permanently\n" ]]
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def request_with_proper_host(env)
23
+ req = Rack::Request.new(env)
24
+ { 'Location' => req.url.sub(req.host, @hosts.first), 'Content-Type' => 'text/html' }
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ class EnsureProperHost
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "rack/ensure_proper_host/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "rack-ensure_proper_host"
7
+ s.version = Rack::EnsureProperHost::VERSION
8
+ s.authors = ["Spencer Steffen"]
9
+ s.email = ["spencer@citrusme.com"]
10
+ s.homepage = "https://github.com/citrus/rack-ensure_proper_host"
11
+ s.summary = "Rack middleware for ensuring only proper hosts get passed to your application"
12
+ s.description = "Rack middleware for ensuring only proper hosts get passed to your application"
13
+
14
+ s.rubyforge_project = "rack-ensure_proper_host"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency "rake", "> 0"
22
+ s.add_development_dependency "bundler", "> 0"
23
+ s.add_development_dependency "minitest", "> 2"
24
+ s.add_development_dependency "minitest_should", "~> 0.3.0"
25
+ s.add_development_dependency "rack-test", "~> 0.6"
26
+
27
+ end
@@ -0,0 +1,98 @@
1
+ require "bundler/setup"
2
+ require "minitest/autorun"
3
+ require "minitest/should"
4
+
5
+ require "rack/test"
6
+ require "rack/ensure_proper_host"
7
+
8
+ module Rack
9
+ class EnsureProperHostTest < MiniTest::Should::TestCase
10
+
11
+ include Rack::Test::Methods
12
+
13
+ MockApp = lambda { |env| [200, {}, [ "Hello, world." ]] }
14
+
15
+ def request_with_host(host, path="/")
16
+ request path, { "HTTP_HOST" => host }
17
+ end
18
+
19
+ context "When used to ensure specific hosts" do
20
+
21
+ def allowed_hosts
22
+ %w(www.example.org secure.example.org)
23
+ end
24
+
25
+ def app
26
+ EnsureProperHost.new(MockApp, allowed_hosts)
27
+ end
28
+
29
+ should "redirect to default host" do
30
+ request_with_host "example.org"
31
+ follow_redirect!
32
+ assert_equal "http://www.example.org/", last_request.url
33
+ assert last_response.redirect?
34
+ end
35
+
36
+ should "redirect to default host for any non-allowed host" do
37
+ %w(something.example.org omg.wtf.bbq.example.com www2.exmple.com wwww.example.com).each do |host|
38
+ request_with_host host
39
+ follow_redirect!
40
+ assert_equal "http://www.example.org/", last_request.url
41
+ assert last_response.redirect?
42
+ end
43
+ end
44
+
45
+ should "redirect to default host and keep path" do
46
+ request_with_host "example.org", "/some/crazy/path?wtf=1"
47
+ follow_redirect!
48
+ assert_equal "http://www.example.org/some/crazy/path?wtf=1", last_request.url
49
+ assert last_response.redirect?
50
+ end
51
+
52
+ should "redirect and retain content type" do
53
+ request_with_host "example.org", "/some/crazy/path"
54
+ follow_redirect!
55
+ assert_equal "text/html", last_response.headers['Content-Type']
56
+ end
57
+
58
+ should "redirect and return 301 move permanently" do
59
+ request_with_host "example.org"
60
+ follow_redirect!
61
+ assert_equal 301, last_response.status
62
+ assert_equal "Moved Permanently\n", last_response.body
63
+ end
64
+
65
+ should "serve all allowed hosts without redirecting" do
66
+ allowed_hosts.each do |host|
67
+ request_with_host host
68
+ assert_equal "http://#{host}/", last_request.url
69
+ assert last_response.ok?
70
+ end
71
+ end
72
+
73
+ end
74
+
75
+ context "When used as no-www middleware" do
76
+
77
+ def app
78
+ EnsureProperHost.new(MockApp, "example.org")
79
+ end
80
+
81
+ should "redirect to non-www host" do
82
+ request_with_host "www.example.org"
83
+ follow_redirect!
84
+ assert_equal "http://example.org/", last_request.url
85
+ assert last_response.redirect?
86
+ end
87
+
88
+ should "redirect to non-www host and keep path" do
89
+ request_with_host "www.example.org", "/some/crazy/path?wtf=1"
90
+ follow_redirect!
91
+ assert_equal "http://example.org/some/crazy/path?wtf=1", last_request.url
92
+ assert last_response.redirect?
93
+ end
94
+
95
+ end
96
+
97
+ end
98
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-ensure_proper_host
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Spencer Steffen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-20 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: &70202462779080 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>'
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70202462779080
25
+ - !ruby/object:Gem::Dependency
26
+ name: bundler
27
+ requirement: &70202462778540 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>'
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70202462778540
36
+ - !ruby/object:Gem::Dependency
37
+ name: minitest
38
+ requirement: &70202462778060 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>'
42
+ - !ruby/object:Gem::Version
43
+ version: '2'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70202462778060
47
+ - !ruby/object:Gem::Dependency
48
+ name: minitest_should
49
+ requirement: &70202462777560 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 0.3.0
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70202462777560
58
+ - !ruby/object:Gem::Dependency
59
+ name: rack-test
60
+ requirement: &70202462777060 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: '0.6'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70202462777060
69
+ description: Rack middleware for ensuring only proper hosts get passed to your application
70
+ email:
71
+ - spencer@citrusme.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - LICENSE
79
+ - README.md
80
+ - Rakefile
81
+ - lib/rack-ensure_proper_host.rb
82
+ - lib/rack/ensure_proper_host.rb
83
+ - lib/rack/ensure_proper_host/version.rb
84
+ - rack-ensure_proper_host.gemspec
85
+ - test/rack/ensure_proper_host_test.rb
86
+ homepage: https://github.com/citrus/rack-ensure_proper_host
87
+ licenses: []
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project: rack-ensure_proper_host
106
+ rubygems_version: 1.8.10
107
+ signing_key:
108
+ specification_version: 3
109
+ summary: Rack middleware for ensuring only proper hosts get passed to your application
110
+ test_files:
111
+ - test/rack/ensure_proper_host_test.rb