Feature #21697 ยป 21697-secure-session-cookie.patch
| app/controllers/application_controller.rb | ||
|---|---|---|
|
end
|
||
|
end
|
||
|
before_action :api_session, :session_expiration, :user_setup, :check_if_login_required, :set_localization, :check_password_change, :check_twofa_activation
|
||
|
before_action :secure_session_cookie, :api_session, :session_expiration, :user_setup, :check_if_login_required, :set_localization, :check_password_change, :check_twofa_activation
|
||
|
after_action :record_project_usage
|
||
|
rescue_from ::Unauthorized, :with => :deny_access
|
||
| ... | ... | |
|
include Redmine::SudoMode::Controller
|
||
|
# Sets the secure flag of the session cookie for requests made over HTTPS,
|
||
|
# including requests forwarded by a reverse proxy with X-Forwarded-Proto
|
||
|
def secure_session_cookie
|
||
|
request.session_options[:secure] = true if request.ssl?
|
||
|
end
|
||
|
def api_session
|
||
|
return unless api_request?
|
||
| test/integration/sessions_test.rb | ||
|---|---|---|
|
Rails.application.config.redmine_verify_sessions = false
|
||
|
end
|
||
|
def test_session_cookie_should_be_secure_over_https
|
||
|
https!
|
||
|
post '/login', :params => {:username => 'jsmith', :password => 'jsmith'}
|
||
|
assert_redirected_to 'https://www.example.com/my/page'
|
||
|
assert_match /;\s*secure(;|\z)/i, session_cookie
|
||
|
end
|
||
|
def test_session_cookie_should_be_secure_behind_reverse_proxy_over_https
|
||
|
post '/login', :params => {:username => 'jsmith', :password => 'jsmith'},
|
||
|
:headers => {'X-Forwarded-Proto' => 'https'}
|
||
|
assert_redirected_to 'https://www.example.com/my/page'
|
||
|
assert_match /;\s*secure(;|\z)/i, session_cookie
|
||
|
end
|
||
|
def test_session_cookie_should_not_be_secure_over_http
|
||
|
post '/login', :params => {:username => 'jsmith', :password => 'jsmith'}
|
||
|
assert_redirected_to 'http://www.example.com/my/page'
|
||
|
assert_no_match /;\s*secure(;|\z)/i, session_cookie
|
||
|
get '/my/page'
|
||
|
assert_response :success
|
||
|
end
|
||
|
def test_change_password_kills_sessions
|
||
|
log_user('jsmith', 'jsmith')
|
||
| ... | ... | |
|
assert_nil response.headers['Set-Cookie']
|
||
|
end
|
||
|
end
|
||
|
private
|
||
|
def session_cookie
|
||
|
cookies = Array(response.headers['Set-Cookie']).flat_map {|cookie| cookie.split("\n")}
|
||
|
cookies.find {|cookie| cookie.start_with?('_redmine_session=')}.tap do |cookie|
|
||
|
assert_not_nil cookie, 'session cookie is not set'
|
||
|
end
|
||
|
end
|
||
|
end
|
||