Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check exclude before flagging cookies as secure in ActionDispatch::SSL #32262

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions actionpack/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
* Check exclude before flagging cookies as secure.

*Catherine Khuu*

* Rails 6 requires Ruby 2.4.1 or newer.

*Jeremy Daer*
Expand Down
4 changes: 3 additions & 1 deletion actionpack/lib/action_dispatch/middleware/ssl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ module ActionDispatch
#
# config.ssl_options = { redirect: { exclude: -> request { request.path =~ /healthcheck/ } } }
#
# Cookies will not be flagged as secure for excluded requests.
#
# 2. <b>Secure cookies</b>: Sets the +secure+ flag on cookies to tell browsers they
# must not be sent along with +http://+ requests. Enabled by default. Set
# +config.ssl_options+ with <tt>secure_cookies: false</tt> to disable this feature.
Expand Down Expand Up @@ -71,7 +73,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
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,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