mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #22591 from gregmolnar/ssl
add `constraint_to` option to SSL middleware
This commit is contained in:
commit
caa6fb3eb4
2 changed files with 13 additions and 2 deletions
|
@ -34,6 +34,10 @@ module ActionDispatch
|
|||
# original HSTS directive until it expires. Instead, use the header to tell browsers to
|
||||
# expire HSTS immediately. Setting `hsts: false` is a shortcut for
|
||||
# `hsts: { expires: 0 }`.
|
||||
#
|
||||
# Redirection can be constrained to only whitelisted requests with `constrain_to`:
|
||||
#
|
||||
# config.ssl_options = { redirect: { constrain_to: -> request { request.path !~ /healthcheck/ } } }
|
||||
class SSL
|
||||
# Default to 180 days, the low end for https://www.ssllabs.com/ssltest/
|
||||
# and greater than the 18-week requirement for browser preload lists.
|
||||
|
@ -55,7 +59,7 @@ module ActionDispatch
|
|||
else
|
||||
@redirect = redirect
|
||||
end
|
||||
|
||||
@constrain_to = @redirect && @redirect[:constrain_to] || proc { @redirect }
|
||||
@secure_cookies = secure_cookies
|
||||
|
||||
if hsts != true && hsts != false && hsts[:subdomains].nil?
|
||||
|
@ -80,7 +84,7 @@ module ActionDispatch
|
|||
flag_cookies_as_secure! headers if @secure_cookies
|
||||
end
|
||||
else
|
||||
return redirect_to_https request if @redirect
|
||||
return redirect_to_https request if @constrain_to.call(request)
|
||||
@app.call(env)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -39,6 +39,13 @@ class RedirectSSLTest < SSLTest
|
|||
assert_equal redirect[:body].join, @response.body
|
||||
end
|
||||
|
||||
test 'constrain to can avoid redirect' do
|
||||
constraining = { constrain_to: -> request { request.path !~ /healthcheck/ } }
|
||||
|
||||
assert_not_redirected 'http://example.org/healthcheck', redirect: constraining
|
||||
assert_redirected from: 'http://example.org/', redirect: constraining
|
||||
end
|
||||
|
||||
test 'https is not redirected' do
|
||||
assert_not_redirected 'https://example.org'
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue