Skip to content

Commit

Permalink
Check exclude before flagging cookies as secure in ActionDispatch::SSL (
Browse files Browse the repository at this point in the history
#32262)

* Check exclude before flagging cookies as secure.

* Update comments in ActionDispatch::SSL.

[Catherine Khuu + Rafael Mendonça França]
  • Loading branch information
catkhuu authored and rafaelfranca committed Mar 15, 2018
1 parent fa5c7c3 commit d0fd5ae
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
5 changes: 5 additions & 0 deletions actionpack/CHANGELOG.md
@@ -1,3 +1,8 @@
* Check exclude before flagging cookies as secure.

*Catherine Khuu*


## Rails 5.1.5 (February 14, 2018) ##

* Fix optimized url helpers when using relative url root
Expand Down
4 changes: 3 additions & 1 deletion actionpack/lib/action_dispatch/middleware/ssl.rb
Expand Up @@ -9,6 +9,8 @@ module ActionDispatch
# (e.g. `redirect: { host: "secure.widgets.com", port: 8080 }`), or set
# `redirect: false` to disable this feature.
#
# Cookies will not be flagged as secure for excluded requests.
#
# 2. Secure cookies: Sets the `secure` flag on cookies to tell browsers they
# mustn't be sent along with http:// requests. Enabled by default. Set
# `config.ssl_options` with `secure_cookies: false` to disable this feature.
Expand Down Expand Up @@ -65,7 +67,7 @@ def call(env)
if request.ssl?
@app.call(env).tap do |status, headers, body|
set_hsts_header! headers
flag_cookies_as_secure! headers if @secure_cookies
flag_cookies_as_secure! headers if @secure_cookies && !@exclude.call(request)
end
else
return redirect_to_https request unless @exclude.call(request)
Expand Down
8 changes: 8 additions & 0 deletions actionpack/test/dispatch/ssl_test.rb
Expand Up @@ -206,6 +206,14 @@ def test_cookies_as_not_secure_with_secure_cookies_disabled
assert_cookies(*DEFAULT.split("\n"))
end

def test_cookies_as_not_secure_with_exclude
excluding = { exclude: -> request { request.domain =~ /example/ } }
get headers: { "Set-Cookie" => DEFAULT }, ssl_options: { redirect: excluding }

assert_cookies(*DEFAULT.split("\n"))
assert_response :ok
end

def test_no_cookies
get
assert_nil response.headers["Set-Cookie"]
Expand Down

0 comments on commit d0fd5ae

Please sign in to comment.