Skip to content

Commit

Permalink
Merge pull request rails#5515 from rafaelfranca/remove-exclude
Browse files Browse the repository at this point in the history
Remove exclude option from ActionDispatch::SSL and fix secure cookies
  • Loading branch information
josevalim committed Mar 19, 2012
2 parents 09d884c + 6e04a78 commit ae97715
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
9 changes: 1 addition & 8 deletions actionpack/lib/action_dispatch/middleware/ssl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,11 @@ def initialize(app, options = {})
@hsts = {} if @hsts == true
@hsts = self.class.default_hsts_options.merge(@hsts) if @hsts

@exclude = options[:exclude]
@host = options[:host]
@port = options[:port]
end

def call(env)
return @app.call(env) if exclude?(env)

request = Request.new(env)

if request.ssl?
Expand All @@ -34,10 +31,6 @@ def call(env)
end

private
def exclude?(env)
@exclude && @exclude.call(env)
end

def redirect_to_https(request)
url = URI(request.url)
url.scheme = "https"
Expand Down Expand Up @@ -65,7 +58,7 @@ def flag_cookies_as_secure!(headers)
cookies = cookies.split("\n")

headers['Set-Cookie'] = cookies.map { |cookie|
if cookie !~ /; secure(;|$)/
if cookie !~ /;\s+secure(;|$)/
"#{cookie}; secure"
else
cookie
Expand Down
34 changes: 28 additions & 6 deletions actionpack/test/dispatch/ssl_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@ def test_redirects_http_to_https
response.headers['Location']
end

def test_exclude_from_redirect
self.app = ActionDispatch::SSL.new(default_app, :exclude => lambda { |env| true })
get "http://example.org/"
assert_response :success
end

def test_hsts_header_by_default
get "https://example.org/"
assert_equal "max-age=31536000",
Expand Down Expand Up @@ -90,6 +84,34 @@ def test_flag_cookies_as_secure_at_end_of_line
response.headers['Set-Cookie'].split("\n")
end

def test_flag_cookies_as_secure_with_more_spaces_before
self.app = ActionDispatch::SSL.new(lambda { |env|
headers = {
'Content-Type' => "text/html",
'Set-Cookie' => "problem=def; path=/; HttpOnly; secure"
}
[200, headers, ["OK"]]
})

get "https://example.org/"
assert_equal ["problem=def; path=/; HttpOnly; secure"],
response.headers['Set-Cookie'].split("\n")
end

def test_flag_cookies_as_secure_with_more_spaces_after
self.app = ActionDispatch::SSL.new(lambda { |env|
headers = {
'Content-Type' => "text/html",
'Set-Cookie' => "problem=def; path=/; secure; HttpOnly"
}
[200, headers, ["OK"]]
})

get "https://example.org/"
assert_equal ["problem=def; path=/; secure; HttpOnly"],
response.headers['Set-Cookie'].split("\n")
end

def test_no_cookies
self.app = ActionDispatch::SSL.new(lambda { |env|
[200, {'Content-Type' => "text/html"}, ["OK"]]
Expand Down

0 comments on commit ae97715

Please sign in to comment.