forked from tobmatth/rack-ssl-enforcer
/
ssl-enforcer.rb
174 lines (158 loc) · 4.85 KB
/
ssl-enforcer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
module Rack
class SslEnforcer
# Warning: If you set the option force_secure_cookies to false, make sure that your cookies
# are encoded and that you understand the consequences (see documentation)
def initialize(app, options={})
default_options = {
:redirect_to => nil,
:only => nil,
:only_hosts => nil,
:except => nil,
:except_hosts => nil,
:strict => false,
:mixed => false,
:hsts => nil,
:http_port => nil,
:https_port => nil,
:force_secure_cookies => true
}
@app, @options = app, default_options.merge(options)
end
def call(env)
@req = Rack::Request.new(env)
if enforce_ssl?(@req)
scheme = 'https' unless ssl_request?(env)
elsif ssl_request?(env) && enforcement_non_ssl?(env)
scheme = 'http'
end
if scheme
location = replace_scheme(@req, scheme).url
body = "<html><body>You are being <a href=\"#{location}\">redirected</a>.</body></html>"
[301, { 'Content-Type' => 'text/html', 'Location' => location }, [body]]
elsif ssl_request?(env)
status, headers, body = @app.call(env)
flag_cookies_as_secure!(headers) if @options[:force_secure_cookies]
set_hsts_headers!(headers) if @options[:hsts] && !@options[:strict]
[status, headers, body]
else
@app.call(env)
end
end
private
def enforcement_non_ssl?(env)
true if @options[:strict] || @options[:mixed] && !(env['REQUEST_METHOD'] == 'PUT' || env['REQUEST_METHOD'] == 'POST')
end
def ssl_request?(env)
scheme(env) == 'https'
end
# Fixed in rack >= 1.3
def scheme(env)
if env['HTTPS'] == 'on'
'https'
elsif env['HTTP_X_FORWARDED_PROTO']
env['HTTP_X_FORWARDED_PROTO'].split(',')[0]
else
env['rack.url_scheme']
end
end
def matches?(key, pattern, req)
if pattern.is_a?(Regexp)
case key
when :only
req.path =~ pattern
when :except
req.path !~ pattern
when :only_hosts
req.host =~ pattern
when :except_hosts
req.host !~ pattern
end
else
case key
when :only
req.path[0,pattern.length] == pattern
when :except
req.path[0,pattern.length] != pattern
when :only_hosts
req.host == pattern
when :except_hosts
req.host != pattern
end
end
end
def enforce_ssl_for?(keys, req)
if keys.any? { |option| @options[option] }
keys.any? do |key|
rules = [@options[key]].flatten.compact
unless rules.empty?
rules.send(key == :except_hosts || key == :except ? "all?" : "any?") do |pattern|
matches?(key, pattern, req)
end
end
end
else
false
end
end
def enforce_ssl?(req)
path_keys = [:only, :except]
hosts_keys = [:only_hosts, :except_hosts]
if hosts_keys.any? { |option| @options[option] }
if enforce_ssl_for?(hosts_keys, req)
if path_keys.any? { |option| @options[option] }
enforce_ssl_for?(path_keys, req)
else
true
end
else
false
end
elsif path_keys.any? { |option| @options[option] }
enforce_ssl_for?(path_keys, req)
else
true
end
end
def replace_scheme(req, scheme)
if @options[:redirect_to]
uri = URI.split(@options[:redirect_to])
uri = uri[2] || uri[5]
else
uri = nil
end
Rack::Request.new(req.env.merge(
'rack.url_scheme' => scheme,
'HTTP_X_FORWARDED_PROTO' => scheme,
'HTTP_X_FORWARDED_PORT' => port_for(scheme).to_s,
'SERVER_PORT' => port_for(scheme).to_s
).merge(uri ? {'HTTP_HOST' => uri} : {}))
end
def port_for(scheme)
if scheme == 'https'
@options[:https_port] || 443
else
@options[:http_port] || 80
end
end
# see http://en.wikipedia.org/wiki/HTTP_cookie#Cookie_theft_and_session_hijacking
def flag_cookies_as_secure!(headers)
if cookies = headers['Set-Cookie']
# Support Rails 2.3 / Rack 1.1 arrays as headers
unless cookies.is_a?(Array)
cookies = cookies.split("\n")
end
headers['Set-Cookie'] = cookies.map do |cookie|
cookie !~ / secure;/ ? "#{cookie}; secure" : cookie
end.join("\n")
end
end
# see http://en.wikipedia.org/wiki/Strict_Transport_Security
def set_hsts_headers!(headers)
opts = { :expires => 31536000, :subdomains => true }
opts.merge!(@options[:hsts])
value = "max-age=#{opts[:expires]}"
value += "; includeSubDomains" if opts[:subdomains]
headers.merge!({ 'Strict-Transport-Security' => value })
end
end
end