rack-ssl-enforcer 0.1.8 → 0.1.9

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -2,10 +2,12 @@
2
2
 
3
3
  Rack::SslEnforcer is a simple Rack middleware to enforce ssl connections
4
4
 
5
+
5
6
  == Installation
6
7
 
7
8
  gem install rack-ssl-enforcer
8
9
 
10
+
9
11
  == Usage
10
12
 
11
13
  require 'rack-ssl-enforcer'
@@ -17,7 +19,7 @@ You might need the :redirect_to option if the requested URL can't be determined
17
19
 
18
20
  use Rack::SslEnforcer, :redirect_to => 'https://example.org'
19
21
 
20
- You can also define specifics regex pattern or path to redirect.
22
+ You can also define specific regex patterns or paths to redirect.
21
23
 
22
24
  use Rack::SslEnforcer, :only => /^\/admin\//
23
25
  use Rack::SslEnforcer, :only => "/login"
@@ -27,6 +29,26 @@ And force http for non-https path
27
29
 
28
30
  use Rack::SslEnforcer, :only => ["/login", /\.xml$/], :strict => true
29
31
 
32
+
33
+ == TODO
34
+
35
+ * Add HSTS support, see http://en.wikipedia.org/wiki/Strict_Transport_Security
36
+ * Add configuration option to specify local http / https ports
37
+
38
+
39
+ == Contributors
40
+
41
+ * {Dan Mayer}[http://github.com/danmayer]
42
+ * {Rémy Coutable}[http://github.com/rymai]
43
+ * {Thibaud Guillaume-Gentil}[http://github.com/thibaudgg]
44
+ * {Paul Annesley}[https://github.com/pda]
45
+
46
+
47
+ == Credits
48
+
49
+ Flagging cookies as secure functionality is greatly inspired by {Joshua Peek's Rack::SSL}[https://github.com/josh/rack-ssl]
50
+
51
+
30
52
  == Note on Patches/Pull Requests
31
53
 
32
54
  * Fork the project.
@@ -37,11 +59,6 @@ And force http for non-https path
37
59
  (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
38
60
  * Send me a pull request. Bonus points for topic branches.
39
61
 
40
- == Contributors
41
-
42
- * {Dan Mayer}[http://github.com/danmayer]
43
- * {Rémy Coutable}[http://github.com/rymai]
44
- * {Thibaud Guillaume-Gentil}[http://github.com/thibaudgg]
45
62
 
46
63
  == Copyright
47
64
 
@@ -14,9 +14,13 @@ module Rack
14
14
  end
15
15
 
16
16
  if scheme
17
- location = @options[:redirect_to] || @req.url.gsub(/^https?/, scheme)
17
+ location = @options[:redirect_to] || replace_scheme(@req, scheme).url
18
18
  body = "<html><body>You are being <a href=\"#{location}\">redirected</a>.</body></html>"
19
19
  [301, { 'Content-Type' => 'text/html', 'Location' => location }, [body]]
20
+ elsif ssl_request?(env)
21
+ status, headers, body = @app.call(env)
22
+ flag_cookies_as_secure!(headers)
23
+ [status, headers, body]
20
24
  else
21
25
  @app.call(env)
22
26
  end
@@ -25,7 +29,18 @@ module Rack
25
29
  private
26
30
 
27
31
  def ssl_request?(env)
28
- (env['HTTP_X_FORWARDED_PROTO'] || @req.scheme) == 'https'
32
+ scheme(env) == 'https'
33
+ end
34
+
35
+ # Fixed in rack >= 1.3
36
+ def scheme(env)
37
+ if env['HTTPS'] == 'on'
38
+ 'https'
39
+ elsif env['HTTP_X_FORWARDED_PROTO']
40
+ env['HTTP_X_FORWARDED_PROTO'].split(',')[0]
41
+ else
42
+ env['rack.url_scheme']
43
+ end
29
44
  end
30
45
 
31
46
  def enforce_ssl?(env)
@@ -43,5 +58,28 @@ module Rack
43
58
  end
44
59
  end
45
60
 
61
+ def replace_scheme(req, scheme)
62
+ Rack::Request.new(req.env.merge(
63
+ 'rack.url_scheme' => scheme,
64
+ 'SERVER_PORT' => port_for(scheme).to_s
65
+ ))
66
+ end
67
+
68
+ def port_for(scheme)
69
+ scheme == 'https' ? 443 : 80
70
+ end
71
+
72
+ def flag_cookies_as_secure!(headers)
73
+ if cookies = headers['Set-Cookie']
74
+ headers['Set-Cookie'] = cookies.split("\n").map { |cookie|
75
+ if cookie !~ / secure;/
76
+ "#{cookie}; secure"
77
+ else
78
+ cookie
79
+ end
80
+ }.join("\n")
81
+ end
82
+ end
83
+
46
84
  end
47
85
  end
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  class SslEnforcer
3
- VERSION = "0.1.8"
3
+ VERSION = "0.1.9"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-ssl-enforcer
3
3
  version: !ruby/object:Gem::Version
4
- hash: 11
5
4
  prerelease: false
6
5
  segments:
7
6
  - 0
8
7
  - 1
9
- - 8
10
- version: 0.1.8
8
+ - 9
9
+ version: 0.1.9
11
10
  platform: ruby
12
11
  authors:
13
12
  - Tobias Matthies
@@ -16,73 +15,127 @@ autorequire:
16
15
  bindir: bin
17
16
  cert_chain: []
18
17
 
19
- date: 2010-09-10 00:00:00 +02:00
18
+ date: 2010-11-17 00:00:00 +01:00
20
19
  default_executable:
21
20
  dependencies:
22
21
  - !ruby/object:Gem::Dependency
23
- name: bundler
22
+ name: guard
24
23
  prerelease: false
25
24
  requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ segments:
30
+ - 0
31
+ - 2
32
+ - 1
33
+ version: 0.2.1
34
+ type: :development
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: guard-test
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
26
40
  none: false
27
41
  requirements:
28
42
  - - ~>
29
43
  - !ruby/object:Gem::Version
30
- hash: 23
31
44
  segments:
45
+ - 0
32
46
  - 1
47
+ - 3
48
+ version: 0.1.3
49
+ type: :development
50
+ version_requirements: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ name: guard-bundler
53
+ prerelease: false
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ segments:
33
60
  - 0
61
+ version: "0"
62
+ type: :development
63
+ version_requirements: *id003
64
+ - !ruby/object:Gem::Dependency
65
+ name: bundler
66
+ prerelease: false
67
+ requirement: &id004 !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ~>
71
+ - !ruby/object:Gem::Version
72
+ segments:
73
+ - 1
34
74
  - 0
35
- version: 1.0.0
75
+ - 5
76
+ version: 1.0.5
36
77
  type: :development
37
- version_requirements: *id001
78
+ version_requirements: *id004
79
+ - !ruby/object:Gem::Dependency
80
+ name: test-unit
81
+ prerelease: false
82
+ requirement: &id005 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ~>
86
+ - !ruby/object:Gem::Version
87
+ segments:
88
+ - 2
89
+ - 1
90
+ - 1
91
+ version: 2.1.1
92
+ type: :development
93
+ version_requirements: *id005
38
94
  - !ruby/object:Gem::Dependency
39
95
  name: shoulda
40
96
  prerelease: false
41
- requirement: &id002 !ruby/object:Gem::Requirement
97
+ requirement: &id006 !ruby/object:Gem::Requirement
42
98
  none: false
43
99
  requirements:
44
100
  - - ~>
45
101
  - !ruby/object:Gem::Version
46
- hash: 37
47
102
  segments:
48
103
  - 2
49
104
  - 11
50
105
  - 3
51
106
  version: 2.11.3
52
107
  type: :development
53
- version_requirements: *id002
108
+ version_requirements: *id006
54
109
  - !ruby/object:Gem::Dependency
55
110
  name: rack
56
111
  prerelease: false
57
- requirement: &id003 !ruby/object:Gem::Requirement
112
+ requirement: &id007 !ruby/object:Gem::Requirement
58
113
  none: false
59
114
  requirements:
60
115
  - - ~>
61
116
  - !ruby/object:Gem::Version
62
- hash: 31
63
117
  segments:
64
118
  - 1
65
119
  - 2
66
120
  - 0
67
121
  version: 1.2.0
68
122
  type: :development
69
- version_requirements: *id003
123
+ version_requirements: *id007
70
124
  - !ruby/object:Gem::Dependency
71
125
  name: rack-test
72
126
  prerelease: false
73
- requirement: &id004 !ruby/object:Gem::Requirement
127
+ requirement: &id008 !ruby/object:Gem::Requirement
74
128
  none: false
75
129
  requirements:
76
130
  - - ~>
77
131
  - !ruby/object:Gem::Version
78
- hash: 3
79
132
  segments:
80
133
  - 0
81
134
  - 5
82
135
  - 4
83
136
  version: 0.5.4
84
137
  type: :development
85
- version_requirements: *id004
138
+ version_requirements: *id008
86
139
  description: Rack::SslEnforcer is a simple Rack middleware to enforce ssl connections
87
140
  email:
88
141
  - tm@mit2m.de
@@ -94,9 +147,9 @@ extensions: []
94
147
  extra_rdoc_files: []
95
148
 
96
149
  files:
150
+ - lib/rack/rack-ssl-enforcer.rb
97
151
  - lib/rack/ssl-enforcer/version.rb
98
152
  - lib/rack/ssl-enforcer.rb
99
- - lib/rack-ssl-enforcer.rb
100
153
  - LICENSE
101
154
  - README.rdoc
102
155
  has_rdoc: true
@@ -113,7 +166,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
113
166
  requirements:
114
167
  - - ">="
115
168
  - !ruby/object:Gem::Version
116
- hash: 3
117
169
  segments:
118
170
  - 0
119
171
  version: "0"
@@ -122,7 +174,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
122
174
  requirements:
123
175
  - - ">="
124
176
  - !ruby/object:Gem::Version
125
- hash: 23
126
177
  segments:
127
178
  - 1
128
179
  - 3